OSDN Git Service

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