OSDN Git Service

Update Copyright years for files modified in 2008 and/or 2009.
[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, 2008, 2009
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 makes some simple RTL code transformations which
24    improve the subsequent register allocation.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h" /* stdio.h must precede rtl.h for FFS.  */
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "output.h"
35 #include "regs.h"
36 #include "hard-reg-set.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "basic-block.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "reload.h"
44 #include "timevar.h"
45 #include "tree-pass.h"
46 #include "df.h"
47
48 static int perhaps_ends_bb_p (rtx);
49 static int optimize_reg_copy_1 (rtx, rtx, rtx);
50 static void optimize_reg_copy_2 (rtx, rtx, rtx);
51 static void optimize_reg_copy_3 (rtx, rtx, rtx);
52 static void copy_src_to_dest (rtx, rtx, rtx);
53
54 struct match {
55   int with[MAX_RECOG_OPERANDS];
56   enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
57   int commutative[MAX_RECOG_OPERANDS];
58   int early_clobber[MAX_RECOG_OPERANDS];
59 };
60
61 static rtx discover_flags_reg (void);
62 static void mark_flags_life_zones (rtx);
63 static void flags_set_1 (rtx, const_rtx, void *);
64
65 static int find_matches (rtx, struct match *);
66 static int regclass_compatible_p (int, int);
67 static int fixup_match_2 (rtx, rtx, rtx, rtx);
68
69 /* Return nonzero if registers with CLASS1 and CLASS2 can be merged without
70    causing too much register allocation problems.  */
71 static int
72 regclass_compatible_p (int class0, int class1)
73 {
74   return (class0 == class1
75           || (reg_class_subset_p (class0, class1)
76               && ! CLASS_LIKELY_SPILLED_P (class0))
77           || (reg_class_subset_p (class1, class0)
78               && ! CLASS_LIKELY_SPILLED_P (class1)));
79 }
80
81 \f
82 /* Determine if the pattern generated by add_optab has a clobber,
83    such as might be issued for a flags hard register.  To make the
84    code elsewhere simpler, we handle cc0 in this same framework.
85
86    Return the register if one was discovered.  Return NULL_RTX if
87    if no flags were found.  Return pc_rtx if we got confused.  */
88
89 static rtx
90 discover_flags_reg (void)
91 {
92   rtx tmp;
93   tmp = gen_rtx_REG (word_mode, 10000);
94   tmp = gen_add3_insn (tmp, tmp, const2_rtx);
95
96   /* If we get something that isn't a simple set, or a
97      [(set ..) (clobber ..)], this whole function will go wrong.  */
98   if (GET_CODE (tmp) == SET)
99     return NULL_RTX;
100   else if (GET_CODE (tmp) == PARALLEL)
101     {
102       int found;
103
104       if (XVECLEN (tmp, 0) != 2)
105         return pc_rtx;
106       tmp = XVECEXP (tmp, 0, 1);
107       if (GET_CODE (tmp) != CLOBBER)
108         return pc_rtx;
109       tmp = XEXP (tmp, 0);
110
111       /* Don't do anything foolish if the md wanted to clobber a
112          scratch or something.  We only care about hard regs.
113          Moreover we don't like the notion of subregs of hard regs.  */
114       if (GET_CODE (tmp) == SUBREG
115           && REG_P (SUBREG_REG (tmp))
116           && REGNO (SUBREG_REG (tmp)) < FIRST_PSEUDO_REGISTER)
117         return pc_rtx;
118       found = (REG_P (tmp) && REGNO (tmp) < FIRST_PSEUDO_REGISTER);
119
120       return (found ? tmp : NULL_RTX);
121     }
122
123   return pc_rtx;
124 }
125
126 /* It is a tedious task identifying when the flags register is live and
127    when it is safe to optimize.  Since we process the instruction stream
128    multiple times, locate and record these live zones by marking the
129    mode of the instructions --
130
131    QImode is used on the instruction at which the flags becomes live.
132
133    HImode is used within the range (exclusive) that the flags are
134    live.  Thus the user of the flags is not marked.
135
136    All other instructions are cleared to VOIDmode.  */
137
138 /* Used to communicate with flags_set_1.  */
139 static rtx flags_set_1_rtx;
140 static int flags_set_1_set;
141
142 static void
143 mark_flags_life_zones (rtx flags)
144 {
145   int flags_regno;
146   int flags_nregs;
147   basic_block block;
148
149 #ifdef HAVE_cc0
150   /* If we found a flags register on a cc0 host, bail.  */
151   if (flags == NULL_RTX)
152     flags = cc0_rtx;
153   else if (flags != cc0_rtx)
154     flags = pc_rtx;
155 #endif
156
157   /* Simple cases first: if no flags, clear all modes.  If confusing,
158      mark the entire function as being in a flags shadow.  */
159   if (flags == NULL_RTX || flags == pc_rtx)
160     {
161       enum machine_mode mode = (flags ? HImode : VOIDmode);
162       rtx insn;
163       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
164         PUT_MODE (insn, mode);
165       return;
166     }
167
168 #ifdef HAVE_cc0
169   flags_regno = -1;
170   flags_nregs = 1;
171 #else
172   flags_regno = REGNO (flags);
173   flags_nregs = hard_regno_nregs[flags_regno][GET_MODE (flags)];
174 #endif
175   flags_set_1_rtx = flags;
176
177   /* Process each basic block.  */
178   FOR_EACH_BB_REVERSE (block)
179     {
180       rtx insn, end;
181       int live;
182
183       insn = BB_HEAD (block);
184       end = BB_END (block);
185
186       /* Look out for the (unlikely) case of flags being live across
187          basic block boundaries.  */
188       live = 0;
189 #ifndef HAVE_cc0
190       {
191         int i;
192         for (i = 0; i < flags_nregs; ++i)
193           live |= REGNO_REG_SET_P (df_get_live_in (block), flags_regno + i);
194       }
195 #endif
196
197       while (1)
198         {
199           /* Process liveness in reverse order of importance --
200              alive, death, birth.  This lets more important info
201              overwrite the mode of lesser info.  */
202
203           if (INSN_P (insn))
204             {
205 #ifdef HAVE_cc0
206               /* In the cc0 case, death is not marked in reg notes,
207                  but is instead the mere use of cc0 when it is alive.  */
208               if (live && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
209                 live = 0;
210 #else
211               /* In the hard reg case, we watch death notes.  */
212               if (live && find_regno_note (insn, REG_DEAD, flags_regno))
213                 live = 0;
214 #endif
215               PUT_MODE (insn, (live ? HImode : VOIDmode));
216
217               /* In either case, birth is denoted simply by its presence
218                  as the destination of a set.  */
219               flags_set_1_set = 0;
220               note_stores (PATTERN (insn), flags_set_1, NULL);
221               if (flags_set_1_set)
222                 {
223                   live = 1;
224                   PUT_MODE (insn, QImode);
225                 }
226             }
227           else
228             PUT_MODE (insn, (live ? HImode : VOIDmode));
229
230           if (insn == end)
231             break;
232           insn = NEXT_INSN (insn);
233         }
234     }
235 }
236
237 /* A subroutine of mark_flags_life_zones, called through note_stores.  */
238
239 static void
240 flags_set_1 (rtx x, const_rtx pat, void *data ATTRIBUTE_UNUSED)
241 {
242   if (GET_CODE (pat) == SET
243       && reg_overlap_mentioned_p (x, flags_set_1_rtx))
244     flags_set_1_set = 1;
245 }
246
247 #ifdef AUTO_INC_DEC
248
249 /* Find the place in the rtx X where REG is used as a memory address.
250    Return the MEM rtx that so uses it.
251    If PLUSCONST is nonzero, search instead for a memory address equivalent to
252    (plus REG (const_int PLUSCONST)).
253
254    If such an address does not appear, return 0.
255    If REG appears more than once, or is used other than in such an address,
256    return (rtx) 1.  */
257
258 static rtx
259 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
260 {
261   enum rtx_code code = GET_CODE (x);
262   const char * const fmt = GET_RTX_FORMAT (code);
263   int i;
264   rtx value = 0;
265   rtx tem;
266
267   if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
268     return x;
269
270   if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
271       && XEXP (XEXP (x, 0), 0) == reg
272       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
273       && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
274     return x;
275
276   if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
277     {
278       /* If REG occurs inside a MEM used in a bit-field reference,
279          that is unacceptable.  */
280       if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
281         return (rtx) (size_t) 1;
282     }
283
284   if (x == reg)
285     return (rtx) (size_t) 1;
286
287   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
288     {
289       if (fmt[i] == 'e')
290         {
291           tem = find_use_as_address (XEXP (x, i), reg, plusconst);
292           if (value == 0)
293             value = tem;
294           else if (tem != 0)
295             return (rtx) (size_t) 1;
296         }
297       else if (fmt[i] == 'E')
298         {
299           int j;
300           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
301             {
302               tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
303               if (value == 0)
304                 value = tem;
305               else if (tem != 0)
306                 return (rtx) (size_t) 1;
307             }
308         }
309     }
310
311   return value;
312 }
313
314
315 /* INC_INSN is an instruction that adds INCREMENT to REG.
316    Try to fold INC_INSN as a post/pre in/decrement into INSN.
317    Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
318    Return nonzero for success.  */
319 static int
320 try_auto_increment (rtx insn, rtx inc_insn, rtx inc_insn_set, rtx reg,
321                     HOST_WIDE_INT increment, int pre)
322 {
323   enum rtx_code inc_code;
324
325   rtx pset = single_set (insn);
326   if (pset)
327     {
328       /* Can't use the size of SET_SRC, we might have something like
329          (sign_extend:SI (mem:QI ...  */
330       rtx use = find_use_as_address (pset, reg, 0);
331       if (use != 0 && use != (rtx) (size_t) 1)
332         {
333           int size = GET_MODE_SIZE (GET_MODE (use));
334           if (0
335               || (HAVE_POST_INCREMENT
336                   && pre == 0 && (inc_code = POST_INC, increment == size))
337               || (HAVE_PRE_INCREMENT
338                   && pre == 1 && (inc_code = PRE_INC, increment == size))
339               || (HAVE_POST_DECREMENT
340                   && pre == 0 && (inc_code = POST_DEC, increment == -size))
341               || (HAVE_PRE_DECREMENT
342                   && pre == 1 && (inc_code = PRE_DEC, increment == -size))
343           )
344             {
345               if (inc_insn_set)
346                 validate_change
347                   (inc_insn,
348                    &SET_SRC (inc_insn_set),
349                    XEXP (SET_SRC (inc_insn_set), 0), 1);
350               validate_change (insn, &XEXP (use, 0),
351                                gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
352               if (apply_change_group ())
353                 {
354                   /* If there is a REG_DEAD note on this insn, we must
355                      change this not to REG_UNUSED meaning that the register
356                      is set, but the value is dead.  Failure to do so will
357                      result in sched1 dying -- when it recomputes lifetime
358                      information, the number of REG_DEAD notes will have
359                      changed.  */
360                   rtx note = find_reg_note (insn, REG_DEAD, reg);
361                   if (note)
362                     PUT_MODE (note, REG_UNUSED);
363
364                   add_reg_note (insn, REG_INC, reg);
365
366                   if (! inc_insn_set)
367                     delete_insn (inc_insn);
368                   return 1;
369                 }
370             }
371         }
372     }
373   return 0;
374 }
375 #endif
376
377 \f
378 static int *regno_src_regno;
379
380 \f
381 /* Return 1 if INSN might end a basic block.  */
382
383 static int perhaps_ends_bb_p (rtx insn)
384 {
385   switch (GET_CODE (insn))
386     {
387     case CODE_LABEL:
388     case JUMP_INSN:
389       /* These always end a basic block.  */
390       return 1;
391
392     case CALL_INSN:
393       /* A CALL_INSN might be the last insn of a basic block, if it is inside
394          an EH region or if there are nonlocal gotos.  Note that this test is
395          very conservative.  */
396       if (nonlocal_goto_handler_labels)
397         return 1;
398       /* Fall through.  */
399     default:
400       return can_throw_internal (insn);
401     }
402 }
403 \f
404 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
405    in INSN.
406
407    Search forward to see if SRC dies before either it or DEST is modified,
408    but don't scan past the end of a basic block.  If so, we can replace SRC
409    with DEST and let SRC die in INSN.
410
411    This will reduce the number of registers live in that range and may enable
412    DEST to be tied to SRC, thus often saving one register in addition to a
413    register-register copy.  */
414
415 static int
416 optimize_reg_copy_1 (rtx insn, rtx dest, rtx src)
417 {
418   rtx p, q;
419   rtx note;
420   rtx dest_death = 0;
421   int sregno = REGNO (src);
422   int dregno = REGNO (dest);
423
424   /* We don't want to mess with hard regs if register classes are small.  */
425   if (sregno == dregno
426       || (SMALL_REGISTER_CLASSES
427           && (sregno < FIRST_PSEUDO_REGISTER
428               || dregno < FIRST_PSEUDO_REGISTER))
429       /* We don't see all updates to SP if they are in an auto-inc memory
430          reference, so we must disallow this optimization on them.  */
431       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
432     return 0;
433
434   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
435     {
436       /* ??? We can't scan past the end of a basic block without updating
437          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
438       if (perhaps_ends_bb_p (p))
439         break;
440       else if (! INSN_P (p))
441         continue;
442
443       if (reg_set_p (src, p) || reg_set_p (dest, p)
444           /* If SRC is an asm-declared register, it must not be replaced
445              in any asm.  Unfortunately, the REG_EXPR tree for the asm
446              variable may be absent in the SRC rtx, so we can't check the
447              actual register declaration easily (the asm operand will have
448              it, though).  To avoid complicating the test for a rare case,
449              we just don't perform register replacement for a hard reg
450              mentioned in an asm.  */
451           || (sregno < FIRST_PSEUDO_REGISTER
452               && asm_noperands (PATTERN (p)) >= 0
453               && reg_overlap_mentioned_p (src, PATTERN (p)))
454           /* Don't change hard registers used by a call.  */
455           || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
456               && find_reg_fusage (p, USE, src))
457           /* Don't change a USE of a register.  */
458           || (GET_CODE (PATTERN (p)) == USE
459               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
460         break;
461
462       /* See if all of SRC dies in P.  This test is slightly more
463          conservative than it needs to be.  */
464       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
465           && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
466         {
467           int failed = 0;
468           int d_length = 0;
469           int s_length = 0;
470           int d_n_calls = 0;
471           int s_n_calls = 0;
472           int s_freq_calls = 0;
473           int d_freq_calls = 0;
474
475           /* We can do the optimization.  Scan forward from INSN again,
476              replacing regs as we go.  Set FAILED if a replacement can't
477              be done.  In that case, we can't move the death note for SRC.
478              This should be rare.  */
479
480           /* Set to stop at next insn.  */
481           for (q = next_real_insn (insn);
482                q != next_real_insn (p);
483                q = next_real_insn (q))
484             {
485               if (reg_overlap_mentioned_p (src, PATTERN (q)))
486                 {
487                   /* If SRC is a hard register, we might miss some
488                      overlapping registers with validate_replace_rtx,
489                      so we would have to undo it.  We can't if DEST is
490                      present in the insn, so fail in that combination
491                      of cases.  */
492                   if (sregno < FIRST_PSEUDO_REGISTER
493                       && reg_mentioned_p (dest, PATTERN (q)))
494                     failed = 1;
495                   
496                   /* Attempt to replace all uses.  */
497                   else if (!validate_replace_rtx (src, dest, q))
498                     failed = 1;
499
500                   /* If this succeeded, but some part of the register
501                      is still present, undo the replacement.  */
502                   else if (sregno < FIRST_PSEUDO_REGISTER
503                            && reg_overlap_mentioned_p (src, PATTERN (q)))
504                     {
505                       validate_replace_rtx (dest, src, q);
506                       failed = 1;
507                     }
508                 }
509
510               /* For SREGNO, count the total number of insns scanned.
511                  For DREGNO, count the total number of insns scanned after
512                  passing the death note for DREGNO.  */
513               s_length++;
514               if (dest_death)
515                 d_length++;
516
517               /* If the insn in which SRC dies is a CALL_INSN, don't count it
518                  as a call that has been crossed.  Otherwise, count it.  */
519               if (q != p && CALL_P (q))
520                 {
521                   /* Similarly, total calls for SREGNO, total calls beyond
522                      the death note for DREGNO.  */
523                   s_n_calls++;
524                   s_freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (q));
525                   if (dest_death)
526                     {
527                       d_n_calls++;
528                       d_freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (q));
529                     }
530                 }
531
532               /* If DEST dies here, remove the death note and save it for
533                  later.  Make sure ALL of DEST dies here; again, this is
534                  overly conservative.  */
535               if (dest_death == 0
536                   && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
537                 {
538                   if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
539                     failed = 1, dest_death = 0;
540                   else
541                     remove_note (q, dest_death);
542                 }
543             }
544
545           if (! failed)
546             {
547               /* These counters need to be updated if and only if we are
548                  going to move the REG_DEAD note.  */
549               if (sregno >= FIRST_PSEUDO_REGISTER)
550                 {
551                   if (REG_LIVE_LENGTH (sregno) >= 0)
552                     {
553                       REG_LIVE_LENGTH (sregno) -= s_length;
554                       /* REG_LIVE_LENGTH is only an approximation after
555                          combine if sched is not run, so make sure that we
556                          still have a reasonable value.  */
557                       if (REG_LIVE_LENGTH (sregno) < 2)
558                         REG_LIVE_LENGTH (sregno) = 2;
559                     }
560
561                   REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
562                   REG_FREQ_CALLS_CROSSED (sregno) -= s_freq_calls;
563                 }
564
565               /* Move death note of SRC from P to INSN.  */
566               remove_note (p, note);
567               XEXP (note, 1) = REG_NOTES (insn);
568               REG_NOTES (insn) = note;
569             }
570
571           /* DEST is also dead if INSN has a REG_UNUSED note for DEST.  */
572           if (! dest_death
573               && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
574             {
575               PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
576               remove_note (insn, dest_death);
577             }
578
579           /* Put death note of DEST on P if we saw it die.  */
580           if (dest_death)
581             {
582               XEXP (dest_death, 1) = REG_NOTES (p);
583               REG_NOTES (p) = dest_death;
584
585               if (dregno >= FIRST_PSEUDO_REGISTER)
586                 {
587                   /* If and only if we are moving the death note for DREGNO,
588                      then we need to update its counters.  */
589                   if (REG_LIVE_LENGTH (dregno) >= 0)
590                     REG_LIVE_LENGTH (dregno) += d_length;
591                   REG_N_CALLS_CROSSED (dregno) += d_n_calls;
592                   REG_FREQ_CALLS_CROSSED (dregno) += d_freq_calls;
593                 }
594             }
595
596           return ! failed;
597         }
598
599       /* If SRC is a hard register which is set or killed in some other
600          way, we can't do this optimization.  */
601       else if (sregno < FIRST_PSEUDO_REGISTER
602                && dead_or_set_p (p, src))
603         break;
604     }
605   return 0;
606 }
607 \f
608 /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
609    a sequence of insns that modify DEST followed by an insn that sets
610    SRC to DEST in which DEST dies, with no prior modification of DEST.
611    (There is no need to check if the insns in between actually modify
612    DEST.  We should not have cases where DEST is not modified, but
613    the optimization is safe if no such modification is detected.)
614    In that case, we can replace all uses of DEST, starting with INSN and
615    ending with the set of SRC to DEST, with SRC.  We do not do this
616    optimization if a CALL_INSN is crossed unless SRC already crosses a
617    call or if DEST dies before the copy back to SRC.
618
619    It is assumed that DEST and SRC are pseudos; it is too complicated to do
620    this for hard registers since the substitutions we may make might fail.  */
621
622 static void
623 optimize_reg_copy_2 (rtx insn, rtx dest, rtx src)
624 {
625   rtx p, q;
626   rtx set;
627   int sregno = REGNO (src);
628   int dregno = REGNO (dest);
629
630   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
631     {
632       /* ??? We can't scan past the end of a basic block without updating
633          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
634       if (perhaps_ends_bb_p (p))
635         break;
636       else if (! INSN_P (p))
637         continue;
638
639       set = single_set (p);
640       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
641           && find_reg_note (p, REG_DEAD, dest))
642         {
643           /* We can do the optimization.  Scan forward from INSN again,
644              replacing regs as we go.  */
645
646           /* Set to stop at next insn.  */
647           for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
648             if (INSN_P (q))
649               {
650                 if (reg_mentioned_p (dest, PATTERN (q)))
651                   {
652                     rtx note;
653
654                     PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
655                     note = FIND_REG_INC_NOTE (q, dest);
656                     if (note)
657                       {
658                         remove_note (q, note);
659                         add_reg_note (q, REG_INC, src);
660                       }
661                     df_insn_rescan (q);
662                   }
663
664                 if (CALL_P (q))
665                   {
666                     int freq = REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (q));
667                     REG_N_CALLS_CROSSED (dregno)--;
668                     REG_N_CALLS_CROSSED (sregno)++;
669                     REG_FREQ_CALLS_CROSSED (dregno) -= freq;
670                     REG_FREQ_CALLS_CROSSED (sregno) += freq;
671                   }
672               }
673
674           remove_note (p, find_reg_note (p, REG_DEAD, dest));
675           REG_N_DEATHS (dregno)--;
676           remove_note (insn, find_reg_note (insn, REG_DEAD, src));
677           REG_N_DEATHS (sregno)--;
678           return;
679         }
680
681       if (reg_set_p (src, p)
682           || find_reg_note (p, REG_DEAD, dest)
683           || (CALL_P (p) && REG_N_CALLS_CROSSED (sregno) == 0))
684         break;
685     }
686 }
687
688 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
689    Look if SRC dies there, and if it is only set once, by loading
690    it from memory.  If so, try to incorporate the zero/sign extension
691    into the memory read, change SRC to the mode of DEST, and alter
692    the remaining accesses to use the appropriate SUBREG.  This allows
693    SRC and DEST to be tied later.  */
694 static void
695 optimize_reg_copy_3 (rtx insn, rtx dest, rtx src)
696 {
697   rtx src_reg = XEXP (src, 0);
698   int src_no = REGNO (src_reg);
699   int dst_no = REGNO (dest);
700   rtx p, set;
701   enum machine_mode old_mode;
702
703   if (src_no < FIRST_PSEUDO_REGISTER
704       || dst_no < FIRST_PSEUDO_REGISTER
705       || ! find_reg_note (insn, REG_DEAD, src_reg)
706       || REG_N_DEATHS (src_no) != 1
707       || REG_N_SETS (src_no) != 1)
708     return;
709   for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
710     /* ??? We can't scan past the end of a basic block without updating
711        the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
712     if (perhaps_ends_bb_p (p))
713       break;
714
715   if (! p)
716     return;
717
718   if (! (set = single_set (p))
719       || !MEM_P (SET_SRC (set))
720       /* If there's a REG_EQUIV note, this must be an insn that loads an
721          argument.  Prefer keeping the note over doing this optimization.  */
722       || find_reg_note (p, REG_EQUIV, NULL_RTX)
723       || SET_DEST (set) != src_reg)
724     return;
725
726   /* Be conservative: although this optimization is also valid for
727      volatile memory references, that could cause trouble in later passes.  */
728   if (MEM_VOLATILE_P (SET_SRC (set)))
729     return;
730
731   /* Do not use a SUBREG to truncate from one mode to another if truncation
732      is not a nop.  */
733   if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
734       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
735                                  GET_MODE_BITSIZE (GET_MODE (src_reg))))
736     return;
737
738   old_mode = GET_MODE (src_reg);
739   PUT_MODE (src_reg, GET_MODE (src));
740   XEXP (src, 0) = SET_SRC (set);
741
742   /* Include this change in the group so that it's easily undone if
743      one of the changes in the group is invalid.  */
744   validate_change (p, &SET_SRC (set), src, 1);
745
746   /* Now walk forward making additional replacements.  We want to be able
747      to undo all the changes if a later substitution fails.  */
748   while (p = NEXT_INSN (p), p != insn)
749     {
750       if (! INSN_P (p))
751         continue;
752
753       /* Make a tentative change.  */
754       validate_replace_rtx_group (src_reg,
755                                   gen_lowpart_SUBREG (old_mode, src_reg),
756                                   p);
757     }
758
759   validate_replace_rtx_group (src, src_reg, insn);
760
761   /* Now see if all the changes are valid.  */
762   if (! apply_change_group ())
763     {
764       /* One or more changes were no good.  Back out everything.  */
765       PUT_MODE (src_reg, old_mode);
766       XEXP (src, 0) = src_reg;
767     }
768   else
769     {
770       rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
771       if (note)
772         remove_note (p, note);
773     }
774 }
775
776 \f
777 /* If we were not able to update the users of src to use dest directly, try
778    instead moving the value to dest directly before the operation.  */
779
780 static void
781 copy_src_to_dest (rtx insn, rtx src, rtx dest)
782 {
783   rtx seq;
784   rtx link;
785   rtx next;
786   rtx set;
787   rtx move_insn;
788   rtx *p_insn_notes;
789   rtx *p_move_notes;
790   int src_regno;
791   int dest_regno;
792   int insn_uid;
793   int move_uid;
794
795   /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
796      or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
797      parameter when there is no frame pointer that is not allocated a register.
798      For now, we just reject them, rather than incrementing the live length.  */
799
800   if (REG_P (src)
801       && REG_LIVE_LENGTH (REGNO (src)) > 0
802       && REG_P (dest)
803       && REG_LIVE_LENGTH (REGNO (dest)) > 0
804       && (set = single_set (insn)) != NULL_RTX
805       && !reg_mentioned_p (dest, SET_SRC (set))
806       && GET_MODE (src) == GET_MODE (dest))
807     {
808       int old_num_regs = reg_rtx_no;
809
810       /* Generate the src->dest move.  */
811       start_sequence ();
812       emit_move_insn (dest, src);
813       seq = get_insns ();
814       end_sequence ();
815       /* If this sequence uses new registers, we may not use it.  */
816       if (old_num_regs != reg_rtx_no
817           || ! validate_replace_rtx (src, dest, insn))
818         {
819           /* We have to restore reg_rtx_no to its old value, lest
820              recompute_reg_usage will try to compute the usage of the
821              new regs, yet reg_n_info is not valid for them.  */
822           reg_rtx_no = old_num_regs;
823           return;
824         }
825       emit_insn_before (seq, insn);
826       move_insn = PREV_INSN (insn);
827       p_move_notes = &REG_NOTES (move_insn);
828       p_insn_notes = &REG_NOTES (insn);
829
830       /* Move any notes mentioning src to the move instruction.  */
831       for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
832         {
833           next = XEXP (link, 1);
834           if (XEXP (link, 0) == src)
835             {
836               *p_move_notes = link;
837               p_move_notes = &XEXP (link, 1);
838             }
839           else
840             {
841               *p_insn_notes = link;
842               p_insn_notes = &XEXP (link, 1);
843             }
844         }
845
846       *p_move_notes = NULL_RTX;
847       *p_insn_notes = NULL_RTX;
848
849       insn_uid = INSN_UID (insn);
850       move_uid = INSN_UID (move_insn);
851
852       /* Update the various register tables.  */
853       dest_regno = REGNO (dest);
854       INC_REG_N_SETS (dest_regno, 1);
855       REG_LIVE_LENGTH (dest_regno)++;
856       src_regno = REGNO (src);
857       if (! find_reg_note (move_insn, REG_DEAD, src))
858         REG_LIVE_LENGTH (src_regno)++;
859     }
860 }
861
862 /* reg_set_in_bb[REGNO] points to basic block iff the register is set
863    only once in the given block and has REG_EQUAL note.  */
864
865 static basic_block *reg_set_in_bb;
866
867 /* Size of reg_set_in_bb array.  */
868 static unsigned int max_reg_computed;
869
870 \f
871 /* Return whether REG is set in only one location, and is set to a
872    constant, but is set in a different basic block from INSN (an
873    instructions which uses REG).  In this case REG is equivalent to a
874    constant, and we don't want to break that equivalence, because that
875    may increase register pressure and make reload harder.  If REG is
876    set in the same basic block as INSN, we don't worry about it,
877    because we'll probably need a register anyhow (??? but what if REG
878    is used in a different basic block as well as this one?).  */
879
880 static bool
881 reg_is_remote_constant_p (rtx reg, rtx insn)
882 {
883   basic_block bb;
884   rtx p;
885   int max;
886
887   if (!reg_set_in_bb)
888     {
889       max_reg_computed = max = max_reg_num ();
890       reg_set_in_bb = XCNEWVEC (basic_block, max);
891
892       FOR_EACH_BB (bb)
893         FOR_BB_INSNS (bb, p)
894           {
895             rtx s;
896
897             if (!INSN_P (p))
898               continue;
899             s = single_set (p);
900             /* This is the instruction which sets REG.  If there is a
901                REG_EQUAL note, then REG is equivalent to a constant.  */
902             if (s != 0
903                 && REG_P (SET_DEST (s))
904                 && REG_N_SETS (REGNO (SET_DEST (s))) == 1
905                 && find_reg_note (p, REG_EQUAL, NULL_RTX))
906               reg_set_in_bb[REGNO (SET_DEST (s))] = bb;
907           }
908     }
909
910   gcc_assert (REGNO (reg) < max_reg_computed);
911   if (reg_set_in_bb[REGNO (reg)] == NULL)
912     return false;
913   return (reg_set_in_bb[REGNO (reg)] != BLOCK_FOR_INSN (insn));
914 }
915
916 /* INSN is adding a CONST_INT to a REG.  We search backwards looking for
917    another add immediate instruction with the same source and dest registers,
918    and if we find one, we change INSN to an increment, and return 1.  If
919    no changes are made, we return 0.
920
921    This changes
922      (set (reg100) (plus reg1 offset1))
923      ...
924      (set (reg100) (plus reg1 offset2))
925    to
926      (set (reg100) (plus reg1 offset1))
927      ...
928      (set (reg100) (plus reg100 offset2-offset1))  */
929
930 /* ??? What does this comment mean?  */
931 /* cse disrupts preincrement / postdecrement sequences when it finds a
932    hard register as ultimate source, like the frame pointer.  */
933
934 static int
935 fixup_match_2 (rtx insn, rtx dst, rtx src, rtx offset)
936 {
937   rtx p, dst_death = 0;
938   int length, num_calls = 0, freq_calls = 0;
939
940   /* If SRC dies in INSN, we'd have to move the death note.  This is
941      considered to be very unlikely, so we just skip the optimization
942      in this case.  */
943   if (find_regno_note (insn, REG_DEAD, REGNO (src)))
944     return 0;
945
946   /* Scan backward to find the first instruction that sets DST.  */
947
948   for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
949     {
950       rtx pset;
951
952       /* ??? We can't scan past the end of a basic block without updating
953          the register lifetime info (REG_DEAD/basic_block_live_at_start).  */
954       if (perhaps_ends_bb_p (p))
955         break;
956       else if (! INSN_P (p))
957         continue;
958
959       if (find_regno_note (p, REG_DEAD, REGNO (dst)))
960         dst_death = p;
961       if (! dst_death)
962         length++;
963
964       pset = single_set (p);
965       if (pset && SET_DEST (pset) == dst
966           && GET_CODE (SET_SRC (pset)) == PLUS
967           && XEXP (SET_SRC (pset), 0) == src
968           && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
969         {
970           HOST_WIDE_INT newconst
971             = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
972           rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
973
974           if (add && validate_change (insn, &PATTERN (insn), add, 0))
975             {
976               /* Remove the death note for DST from DST_DEATH.  */
977               if (dst_death)
978                 {
979                   remove_death (REGNO (dst), dst_death);
980                   REG_LIVE_LENGTH (REGNO (dst)) += length;
981                   REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
982                   REG_FREQ_CALLS_CROSSED (REGNO (dst)) += freq_calls;
983                 }
984
985               if (dump_file)
986                 fprintf (dump_file,
987                          "Fixed operand of insn %d.\n",
988                           INSN_UID (insn));
989
990 #ifdef AUTO_INC_DEC
991               for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
992                 {
993                   if (LABEL_P (p)
994                       || JUMP_P (p))
995                     break;
996                   if (! INSN_P (p))
997                     continue;
998                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
999                     {
1000                       if (try_auto_increment (p, insn, 0, dst, newconst, 0))
1001                         return 1;
1002                       break;
1003                     }
1004                 }
1005               for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1006                 {
1007                   if (LABEL_P (p)
1008                       || JUMP_P (p))
1009                     break;
1010                   if (! INSN_P (p))
1011                     continue;
1012                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1013                     {
1014                       try_auto_increment (p, insn, 0, dst, newconst, 1);
1015                       break;
1016                     }
1017                 }
1018 #endif
1019               return 1;
1020             }
1021         }
1022
1023       if (reg_set_p (dst, PATTERN (p)))
1024         break;
1025
1026       /* If we have passed a call instruction, and the
1027          pseudo-reg SRC is not already live across a call,
1028          then don't perform the optimization.  */
1029       /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
1030          hard regs are clobbered.  Thus, we only use it for src for
1031          non-call insns.  */
1032       if (CALL_P (p))
1033         {
1034           if (! dst_death)
1035             {
1036               num_calls++;
1037               freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (p));
1038             }
1039
1040           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1041             break;
1042
1043           if (call_used_regs [REGNO (dst)]
1044               || find_reg_fusage (p, CLOBBER, dst))
1045             break;
1046         }
1047       else if (reg_set_p (src, PATTERN (p)))
1048         break;
1049     }
1050
1051   return 0;
1052 }
1053
1054 /* Main entry for the register move optimization.
1055    F is the first instruction.
1056    NREGS is one plus the highest pseudo-reg number used in the instruction.
1057    REGMOVE_DUMP_FILE is a stream for output of a trace of actions taken
1058    (or 0 if none should be output).  */
1059
1060 static void
1061 regmove_optimize (rtx f, int nregs)
1062 {
1063   rtx insn;
1064   struct match match;
1065   int pass;
1066   int i;
1067   rtx copy_src, copy_dst;
1068
1069   /* ??? Hack.  Regmove doesn't examine the CFG, and gets mightily
1070      confused by non-call exceptions ending blocks.  */
1071   if (flag_non_call_exceptions)
1072     return;
1073
1074   df_note_add_problem ();
1075   df_analyze ();
1076
1077   regstat_init_n_sets_and_refs ();
1078   regstat_compute_ri ();
1079
1080   /* Find out where a potential flags register is live, and so that we
1081      can suppress some optimizations in those zones.  */
1082   mark_flags_life_zones (discover_flags_reg ());
1083
1084   regno_src_regno = XNEWVEC (int, nregs);
1085   for (i = nregs; --i >= 0; )
1086     regno_src_regno[i] = -1;
1087
1088   /* A forward/backward pass.  Replace output operands with input operands.  */
1089
1090   for (pass = 0; pass <= 2; pass++)
1091     {
1092       if (! flag_regmove && pass >= flag_expensive_optimizations)
1093         goto done;
1094
1095       if (dump_file)
1096         fprintf (dump_file, "Starting %s pass...\n",
1097                  pass ? "backward" : "forward");
1098
1099       for (insn = pass ? get_last_insn () : f; insn;
1100            insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
1101         {
1102           rtx set;
1103
1104           set = single_set (insn);
1105           if (! set)
1106             continue;
1107
1108           if (flag_expensive_optimizations && ! pass
1109               && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
1110                   || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
1111               && REG_P (XEXP (SET_SRC (set), 0))
1112               && REG_P (SET_DEST (set)))
1113             optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
1114
1115           if (flag_expensive_optimizations && ! pass
1116               && REG_P (SET_SRC (set))
1117               && REG_P (SET_DEST (set)))
1118             {
1119               /* If this is a register-register copy where SRC is not dead,
1120                  see if we can optimize it.  If this optimization succeeds,
1121                  it will become a copy where SRC is dead.  */
1122               if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
1123                    || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
1124                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
1125                 {
1126                   /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
1127                   if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1128                     optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
1129                   if (regno_src_regno[REGNO (SET_DEST (set))] < 0
1130                       && SET_SRC (set) != SET_DEST (set))
1131                     {
1132                       int srcregno = REGNO (SET_SRC (set));
1133                       if (regno_src_regno[srcregno] >= 0)
1134                         srcregno = regno_src_regno[srcregno];
1135                       regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1136                     }
1137                 }
1138             }
1139         }
1140     }
1141
1142   /* A backward pass.  Replace input operands with output operands.  */
1143
1144   if (dump_file)
1145     fprintf (dump_file, "Starting backward pass...\n");
1146
1147   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1148     {
1149       if (INSN_P (insn))
1150         {
1151           int op_no, match_no;
1152           int success = 0;
1153
1154           if (! find_matches (insn, &match))
1155             continue;
1156
1157           /* Now scan through the operands looking for a destination operand
1158              which is supposed to match a source operand.
1159              Then scan backward for an instruction which sets the source
1160              operand.  If safe, then replace the source operand with the
1161              dest operand in both instructions.  */
1162
1163           copy_src = NULL_RTX;
1164           copy_dst = NULL_RTX;
1165           for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1166             {
1167               rtx set, p, src, dst;
1168               rtx src_note, dst_note;
1169               int num_calls = 0, freq_calls = 0;
1170               enum reg_class src_class, dst_class;
1171               int length;
1172
1173               match_no = match.with[op_no];
1174
1175               /* Nothing to do if the two operands aren't supposed to match.  */
1176               if (match_no < 0)
1177                 continue;
1178
1179               dst = recog_data.operand[match_no];
1180               src = recog_data.operand[op_no];
1181
1182               if (!REG_P (src))
1183                 continue;
1184
1185               if (!REG_P (dst)
1186                   || REGNO (dst) < FIRST_PSEUDO_REGISTER
1187                   || REG_LIVE_LENGTH (REGNO (dst)) < 0
1188                   || GET_MODE (src) != GET_MODE (dst))
1189                 continue;
1190
1191               /* If the operands already match, then there is nothing to do.  */
1192               if (operands_match_p (src, dst))
1193                 continue;
1194
1195               if (match.commutative[op_no] >= 0)
1196                 {
1197                   rtx comm = recog_data.operand[match.commutative[op_no]];
1198                   if (operands_match_p (comm, dst))
1199                     continue;
1200                 }
1201
1202               set = single_set (insn);
1203               if (! set)
1204                 continue;
1205
1206               /* Note that single_set ignores parts of a parallel set for
1207                  which one of the destinations is REG_UNUSED.  We can't
1208                  handle that here, since we can wind up rewriting things
1209                  such that a single register is set twice within a single
1210                  parallel.  */
1211               if (reg_set_p (src, insn))
1212                 continue;
1213
1214               /* match_no/dst must be a write-only operand, and
1215                  operand_operand/src must be a read-only operand.  */
1216               if (match.use[op_no] != READ
1217                   || match.use[match_no] != WRITE)
1218                 continue;
1219
1220               if (match.early_clobber[match_no]
1221                   && count_occurrences (PATTERN (insn), src, 0) > 1)
1222                 continue;
1223
1224               /* Make sure match_no is the destination.  */
1225               if (recog_data.operand[match_no] != SET_DEST (set))
1226                 continue;
1227
1228               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1229                 {
1230                   if (GET_CODE (SET_SRC (set)) == PLUS
1231                       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1232                       && XEXP (SET_SRC (set), 0) == src
1233                       && fixup_match_2 (insn, dst, src,
1234                                         XEXP (SET_SRC (set), 1)))
1235                     break;
1236                   continue;
1237                 }
1238               src_class = reg_preferred_class (REGNO (src));
1239               dst_class = reg_preferred_class (REGNO (dst));
1240
1241               if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1242                 {
1243                   /* We used to force the copy here like in other cases, but
1244                      it produces worse code, as it eliminates no copy
1245                      instructions and the copy emitted will be produced by
1246                      reload anyway.  On patterns with multiple alternatives,
1247                      there may be better solution available.
1248
1249                      In particular this change produced slower code for numeric
1250                      i387 programs.  */
1251
1252                   continue;
1253                 }
1254
1255               if (! regclass_compatible_p (src_class, dst_class))
1256                 {
1257                   if (!copy_src)
1258                     {
1259                       copy_src = src;
1260                       copy_dst = dst;
1261                     }
1262                   continue;
1263                 }
1264
1265               /* Can not modify an earlier insn to set dst if this insn
1266                  uses an old value in the source.  */
1267               if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1268                 {
1269                   if (!copy_src)
1270                     {
1271                       copy_src = src;
1272                       copy_dst = dst;
1273                     }
1274                   continue;
1275                 }
1276
1277               /* If src is set once in a different basic block,
1278                  and is set equal to a constant, then do not use
1279                  it for this optimization, as this would make it
1280                  no longer equivalent to a constant.  */
1281
1282               if (reg_is_remote_constant_p (src, insn))
1283                 {
1284                   if (!copy_src)
1285                     {
1286                       copy_src = src;
1287                       copy_dst = dst;
1288                     }
1289                   continue;
1290                 }
1291
1292
1293               if (dump_file)
1294                 fprintf (dump_file,
1295                          "Could fix operand %d of insn %d matching operand %d.\n",
1296                          op_no, INSN_UID (insn), match_no);
1297
1298               /* Scan backward to find the first instruction that uses
1299                  the input operand.  If the operand is set here, then
1300                  replace it in both instructions with match_no.  */
1301
1302               for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1303                 {
1304                   rtx pset;
1305
1306                   /* ??? We can't scan past the end of a basic block without
1307                      updating the register lifetime info
1308                      (REG_DEAD/basic_block_live_at_start).  */
1309                   if (perhaps_ends_bb_p (p))
1310                     break;
1311                   else if (! INSN_P (p))
1312                     continue;
1313
1314                   length++;
1315
1316                   /* ??? See if all of SRC is set in P.  This test is much
1317                      more conservative than it needs to be.  */
1318                   pset = single_set (p);
1319                   if (pset && SET_DEST (pset) == src)
1320                     {
1321                       /* We use validate_replace_rtx, in case there
1322                          are multiple identical source operands.  All of
1323                          them have to be changed at the same time.  */
1324                       if (validate_replace_rtx (src, dst, insn))
1325                         {
1326                           if (validate_change (p, &SET_DEST (pset),
1327                                                dst, 0))
1328                             success = 1;
1329                           else
1330                             {
1331                               /* Change all source operands back.
1332                                  This modifies the dst as a side-effect.  */
1333                               validate_replace_rtx (dst, src, insn);
1334                               /* Now make sure the dst is right.  */
1335                               validate_change (insn,
1336                                                recog_data.operand_loc[match_no],
1337                                                dst, 0);
1338                             }
1339                         }
1340                       break;
1341                     }
1342
1343                   /* We can't make this change if SRC is read or
1344                      partially written in P, since we are going to
1345                      eliminate SRC. We can't make this change 
1346                      if DST is mentioned at all in P,
1347                      since we are going to change its value.  */
1348                   if (reg_overlap_mentioned_p (src, PATTERN (p))
1349                       || reg_mentioned_p (dst, PATTERN (p)))
1350                     break;
1351
1352                   /* If we have passed a call instruction, and the
1353                      pseudo-reg DST is not already live across a call,
1354                      then don't perform the optimization.  */
1355                   if (CALL_P (p))
1356                     {
1357                       num_calls++;
1358                       freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (p));
1359
1360                       if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1361                         break;
1362                     }
1363                 }
1364
1365               if (success)
1366                 {
1367                   int dstno, srcno;
1368
1369                   /* Remove the death note for SRC from INSN.  */
1370                   remove_note (insn, src_note);
1371                   /* Move the death note for SRC to P if it is used
1372                      there.  */
1373                   if (reg_overlap_mentioned_p (src, PATTERN (p)))
1374                     {
1375                       XEXP (src_note, 1) = REG_NOTES (p);
1376                       REG_NOTES (p) = src_note;
1377                     }
1378                   /* If there is a REG_DEAD note for DST on P, then remove
1379                      it, because DST is now set there.  */
1380                   if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1381                     remove_note (p, dst_note);
1382
1383                   dstno = REGNO (dst);
1384                   srcno = REGNO (src);
1385
1386                   INC_REG_N_SETS (dstno, 1);
1387                   INC_REG_N_SETS (srcno, -1);
1388
1389                   REG_N_CALLS_CROSSED (dstno) += num_calls;
1390                   REG_N_CALLS_CROSSED (srcno) -= num_calls;
1391                   REG_FREQ_CALLS_CROSSED (dstno) += freq_calls;
1392                   REG_FREQ_CALLS_CROSSED (srcno) -= freq_calls;
1393
1394                   REG_LIVE_LENGTH (dstno) += length;
1395                   if (REG_LIVE_LENGTH (srcno) >= 0)
1396                     {
1397                       REG_LIVE_LENGTH (srcno) -= length;
1398                       /* REG_LIVE_LENGTH is only an approximation after
1399                          combine if sched is not run, so make sure that we
1400                          still have a reasonable value.  */
1401                       if (REG_LIVE_LENGTH (srcno) < 2)
1402                         REG_LIVE_LENGTH (srcno) = 2;
1403                     }
1404
1405                   if (dump_file)
1406                     fprintf (dump_file,
1407                              "Fixed operand %d of insn %d matching operand %d.\n",
1408                              op_no, INSN_UID (insn), match_no);
1409
1410                   break;
1411                 }
1412             }
1413
1414           /* If we weren't able to replace any of the alternatives, try an
1415              alternative approach of copying the source to the destination.  */
1416           if (!success && copy_src != NULL_RTX)
1417             copy_src_to_dest (insn, copy_src, copy_dst);
1418         }
1419     }
1420
1421  done:
1422   /* Clean up.  */
1423   free (regno_src_regno);
1424   if (reg_set_in_bb)
1425     {
1426       free (reg_set_in_bb);
1427       reg_set_in_bb = NULL;
1428     }
1429   regstat_free_n_sets_and_refs ();
1430   regstat_free_ri ();
1431 }
1432
1433 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1434    Returns 0 if INSN can't be recognized, or if the alternative can't be
1435    determined.
1436
1437    Initialize the info in MATCHP based on the constraints.  */
1438
1439 static int
1440 find_matches (rtx insn, struct match *matchp)
1441 {
1442   int likely_spilled[MAX_RECOG_OPERANDS];
1443   int op_no;
1444   int any_matches = 0;
1445
1446   extract_insn (insn);
1447   if (! constrain_operands (0))
1448     return 0;
1449
1450   /* Must initialize this before main loop, because the code for
1451      the commutative case may set matches for operands other than
1452      the current one.  */
1453   for (op_no = recog_data.n_operands; --op_no >= 0; )
1454     matchp->with[op_no] = matchp->commutative[op_no] = -1;
1455
1456   for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1457     {
1458       const char *p;
1459       char c;
1460       int i = 0;
1461
1462       p = recog_data.constraints[op_no];
1463
1464       likely_spilled[op_no] = 0;
1465       matchp->use[op_no] = READ;
1466       matchp->early_clobber[op_no] = 0;
1467       if (*p == '=')
1468         matchp->use[op_no] = WRITE;
1469       else if (*p == '+')
1470         matchp->use[op_no] = READWRITE;
1471
1472       for (;*p && i < which_alternative; p++)
1473         if (*p == ',')
1474           i++;
1475
1476       while ((c = *p) != '\0' && c != ',')
1477         {
1478           switch (c)
1479             {
1480             case '=':
1481               break;
1482             case '+':
1483               break;
1484             case '&':
1485               matchp->early_clobber[op_no] = 1;
1486               break;
1487             case '%':
1488               matchp->commutative[op_no] = op_no + 1;
1489               matchp->commutative[op_no + 1] = op_no;
1490               break;
1491
1492             case '0': case '1': case '2': case '3': case '4':
1493             case '5': case '6': case '7': case '8': case '9':
1494               {
1495                 char *end;
1496                 unsigned long match_ul = strtoul (p, &end, 10);
1497                 int match = match_ul;
1498
1499                 p = end;
1500
1501                 if (match < op_no && likely_spilled[match])
1502                   continue;
1503                 matchp->with[op_no] = match;
1504                 any_matches = 1;
1505                 if (matchp->commutative[op_no] >= 0)
1506                   matchp->with[matchp->commutative[op_no]] = match;
1507               }
1508             continue;
1509
1510           case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1511           case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1512           case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1513           case 'C': case 'D': case 'W': case 'Y': case 'Z':
1514             if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p) ))
1515               likely_spilled[op_no] = 1;
1516             break;
1517           }
1518           p += CONSTRAINT_LEN (c, p);
1519         }
1520     }
1521   return any_matches;
1522 }
1523
1524 \f
1525
1526 static bool
1527 gate_handle_regmove (void)
1528 {
1529   return (optimize > 0 && flag_regmove);
1530 }
1531
1532 /* Register allocation pre-pass, to reduce number of moves necessary
1533    for two-address machines.  */
1534 static unsigned int
1535 rest_of_handle_regmove (void)
1536 {
1537   regmove_optimize (get_insns (), max_reg_num ());
1538   return 0;
1539 }
1540
1541 struct rtl_opt_pass pass_regmove =
1542 {
1543  {
1544   RTL_PASS,
1545   "regmove",                            /* name */
1546   gate_handle_regmove,                  /* gate */
1547   rest_of_handle_regmove,               /* execute */
1548   NULL,                                 /* sub */
1549   NULL,                                 /* next */
1550   0,                                    /* static_pass_number */
1551   TV_REGMOVE,                           /* tv_id */
1552   0,                                    /* properties_required */
1553   0,                                    /* properties_provided */
1554   0,                                    /* properties_destroyed */
1555   0,                                    /* todo_flags_start */
1556   TODO_df_finish | TODO_verify_rtl_sharing |
1557   TODO_dump_func |
1558   TODO_ggc_collect                      /* todo_flags_finish */
1559  }
1560 };
1561