OSDN Git Service

* regmove.c (find_matches): Initialize matches->earlyclobber too.
[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 #ifdef __STDC__
28 #include <stdarg.h>
29 #else
30 #include <varargs.h>
31 #endif
32
33 /* Must precede rtl.h for FFS.  */
34 #include <stdio.h>
35
36 #include "rtl.h"
37 #include "insn-config.h"
38 #include "recog.h"
39 #include "output.h"
40 #include "reload.h"
41 #include "regs.h"
42 #include "hard-reg-set.h"
43 #include "flags.h"
44 #include "expr.h"
45 #include "insn-flags.h"
46
47 #ifndef REG_N_CALLS_CROSSED
48 #define REG_N_CALLS_CROSSED(x) (reg_n_calls_crossed[(x)])
49 #define REG_N_SETS(x) (reg_n_sets[(x)])
50 #define REG_N_REFS(x) (reg_n_refs[(x)])
51 #define REG_N_DEATHS(x) (reg_n_deaths[(x)])
52 #define REG_LIVE_LENGTH(x) (reg_live_length[(x)])
53 #endif
54
55 static void optimize_reg_copy_3 PROTO((rtx, rtx, rtx));
56
57 extern int flag_regmove;
58
59 struct match {
60   int with[MAX_RECOG_OPERANDS];
61   enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
62   int commutative[MAX_RECOG_OPERANDS];
63   int early_clobber[MAX_RECOG_OPERANDS];
64 };
65
66 static int find_matches PROTO((rtx, struct match *));
67 static int fixup_match_1 PROTO((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
68 ;
69 static int reg_is_remote_constant_p PROTO((rtx, rtx, rtx));
70 static int stable_but_for_p PROTO((rtx, rtx, rtx));
71 static int loop_depth;
72
73 #ifdef AUTO_INC_DEC
74
75 /* INC_INSN is an instruction that adds INCREMENT to REG.
76    Try to fold INC_INSN as a post/pre in/decrement into INSN.
77    Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
78    Return nonzero for success.  */
79 static int
80 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
81      rtx reg, insn, inc_insn ,inc_insn_set;
82      HOST_WIDE_INT increment;
83      int pre;
84 {
85   enum rtx_code inc_code;
86
87   rtx pset = single_set (insn);
88   if (pset)
89     {
90       /* Can't use the size of SET_SRC, we might have something like
91          (sign_extend:SI (mem:QI ...  */
92       rtx use = find_use_as_address (pset, reg, 0);
93       if (use != 0 && use != (rtx) 1)
94         {
95           int size = GET_MODE_SIZE (GET_MODE (use));
96           if (0
97 #ifdef HAVE_POST_INCREMENT
98               || (pre == 0 && (inc_code = POST_INC, increment == size))
99 #endif
100 #ifdef HAVE_PRE_INCREMENT
101               || (pre == 1 && (inc_code = PRE_INC, increment == size))
102 #endif
103 #ifdef HAVE_POST_DECREMENT
104               || (pre == 0 && (inc_code = POST_DEC, increment == -size))
105 #endif
106 #ifdef HAVE_PRE_DECREMENT
107               || (pre == 1 && (inc_code = PRE_DEC, increment == -size))
108 #endif
109           )
110             {
111               if (inc_insn_set)
112                 validate_change
113                   (inc_insn, 
114                    &SET_SRC (inc_insn_set),
115                    XEXP (SET_SRC (inc_insn_set), 0), 1);
116               validate_change (insn, &XEXP (use, 0),
117                                gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
118               if (apply_change_group ())
119                 {
120                   REG_NOTES (insn)
121                     = gen_rtx_EXPR_LIST (REG_INC,
122                                          reg, REG_NOTES (insn));
123                   if (! inc_insn_set)
124                     {
125                       PUT_CODE (inc_insn, NOTE);
126                       NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
127                       NOTE_SOURCE_FILE (inc_insn) = 0;
128                     }
129                   return 1;
130                 }
131             }
132         }
133     }
134   return 0;
135 }
136 #endif  /* AUTO_INC_DEC */
137
138 static int *regno_src_regno;
139
140 /* Indicate how good a choice REG (which appears as a source) is to replace
141    a destination register with.  The higher the returned value, the better
142    the choice.  The main objective is to avoid using a register that is
143    a candidate for tying to a hard register, since the output might in
144    turn be a candidate to be tied to a different hard register.  */
145 int
146 replacement_quality(reg)
147      rtx reg;
148 {
149   int src_regno;
150
151   /* Bad if this isn't a register at all.  */
152   if (GET_CODE (reg) != REG)
153     return 0;
154
155   /* If this register is not meant to get a hard register,
156      it is a poor choice.  */
157   if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
158     return 0;
159
160   src_regno = regno_src_regno[REGNO (reg)];
161
162   /* If it was not copied from another register, it is fine.  */
163   if (src_regno < 0)
164     return 3;
165
166   /* Copied from a hard register?  */
167   if (src_regno < FIRST_PSEUDO_REGISTER)
168     return 1;
169
170   /* Copied from a pseudo register - not as bad as from a hard register,
171      yet still cumbersome, since the register live length will be lengthened
172      when the registers get tied.  */
173   return 2;
174 }
175
176 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
177    Look if SRC dies there, and if it is only set once, by loading
178    it from memory.  If so, try to encorporate the zero/sign extension
179    into the memory read, change SRC to the mode of DEST, and alter
180    the remaining accesses to use the appropriate SUBREG.  This allows
181    SRC and DEST to be tied later.  */
182 static void
183 optimize_reg_copy_3 (insn, dest, src)
184      rtx insn;
185      rtx dest;
186      rtx src;
187 {
188   rtx src_reg = XEXP (src, 0);
189   int src_no = REGNO (src_reg);
190   int dst_no = REGNO (dest);
191   rtx p, set, subreg;
192   enum machine_mode old_mode;
193
194   if (src_no < FIRST_PSEUDO_REGISTER
195       || dst_no < FIRST_PSEUDO_REGISTER
196       || ! find_reg_note (insn, REG_DEAD, src_reg)
197       || REG_N_SETS (src_no) != 1)
198     return;
199   for (p = PREV_INSN (insn); ! reg_set_p (src_reg, p); p = PREV_INSN (p))
200     {
201       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
202         continue;
203       if (GET_CODE (p) == JUMP_INSN)
204         return;
205     }
206   if (! (set = single_set (p))
207       || GET_CODE (SET_SRC (set)) != MEM
208       || SET_DEST (set) != src_reg)
209     return;
210   old_mode = GET_MODE (src_reg);
211   PUT_MODE (src_reg, GET_MODE (src));
212   XEXP (src, 0) = SET_SRC (set);
213   if (! validate_change (p, &SET_SRC (set), src, 0))
214     {
215       PUT_MODE (src_reg, old_mode);
216       XEXP (src, 0) = src_reg;
217       return;
218     }
219   subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
220   while (p = NEXT_INSN (p), p != insn)
221     {
222       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
223         continue;
224       validate_replace_rtx (src_reg, subreg, p);
225     }
226   validate_replace_rtx (src, src_reg, insn);
227 }
228
229 /* Return whether REG is set in only one location, and is set to a
230    constant, but is set in a different basic block from INSN (an
231    instructions which uses REG).  In this case REG is equivalent to a
232    constant, and we don't want to break that equivalence, because that
233    may increase register pressure and make reload harder.  If REG is
234    set in the same basic block as INSN, we don't worry about it,
235    because we'll probably need a register anyhow (??? but what if REG
236    is used in a different basic block as well as this one?).  FIRST is
237    the first insn in the function.  */
238
239 static int
240 reg_is_remote_constant_p (reg, insn, first)
241      rtx reg;
242      rtx insn;
243      rtx first;
244 {
245   register rtx p;
246
247   if (REG_N_SETS (REGNO (reg)) != 1)
248     return 0;
249
250   /* Look for the set.  */
251   for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
252     {
253       rtx s;
254
255       if (REG_NOTE_KIND (p) != 0)
256         continue;
257       s = single_set (XEXP (p, 0));
258       if (s != 0
259           && GET_CODE (SET_DEST (s)) == REG
260           && REGNO (SET_DEST (s)) == REGNO (reg))
261         {
262           /* The register is set in the same basic block.  */
263           return 0;
264         }
265     }
266
267   for (p = first; p && p != insn; p = NEXT_INSN (p))
268     {
269       rtx s;
270
271       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
272         continue;
273       s = single_set (p);
274       if (s != 0
275           && GET_CODE (SET_DEST (s)) == REG
276           && REGNO (SET_DEST (s)) == REGNO (reg))
277         {
278           /* This is the instruction which sets REG.  If there is a
279              REG_EQUAL note, then REG is equivalent to a constant.  */
280           if (find_reg_note (p, REG_EQUAL, NULL_RTX))
281             return 1;
282           return 0;
283         }
284     }
285
286   return 0;
287 }
288
289 /* cse disrupts preincrement / postdecrement squences when it finds a
290    hard register as ultimate source, like the frame pointer.  */
291 int fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
292      rtx insn, dst, src, offset;
293      FILE *regmove_dump_file;
294 {
295   rtx p, dst_death = 0;
296   int length, num_calls = 0;
297
298   /* If SRC dies in INSN, we'd have to move the death note.  This is
299      considered to be very unlikely, so we just skip the optimization
300      in this case.  */
301   if (find_regno_note (insn, REG_DEAD, REGNO (src)))
302     return 0;
303
304   /* Scan backward to find the first instruction that sets DST.  */
305
306   for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
307     {
308       rtx pset;
309
310       if (GET_CODE (p) == CODE_LABEL
311           || GET_CODE (p) == JUMP_INSN
312           || (GET_CODE (p) == NOTE
313               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
314                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
315         break;
316
317       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
318         continue;
319
320   if (find_regno_note (p, REG_DEAD, REGNO (dst)))
321     dst_death = p;
322   if (! dst_death)
323     length++;
324
325       pset = single_set (p);
326       if (pset && SET_DEST (pset) == dst
327           && GET_CODE (SET_SRC (pset)) == PLUS
328           && XEXP (SET_SRC (pset), 0) == src
329           && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
330         {
331           HOST_WIDE_INT newconst
332             = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
333           if (validate_change (insn, &PATTERN (insn),
334                                gen_addsi3 (dst, dst, GEN_INT (newconst)), 0))
335             {
336               /* Remove the death note for DST from DST_DEATH.  */
337               if (dst_death)
338                 {
339                   remove_death (REGNO (dst), dst_death);
340                   REG_LIVE_LENGTH (REGNO (dst)) += length;
341                   REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
342                 }
343
344               REG_N_REFS (REGNO (dst)) += loop_depth;
345               REG_N_REFS (REGNO (src)) -= loop_depth;
346
347               if (regmove_dump_file)
348                 fprintf (regmove_dump_file,
349                          "Fixed operand of insn %d.\n",
350                           INSN_UID (insn));
351
352 #ifdef AUTO_INC_DEC
353               for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
354                 {
355                   if (GET_CODE (p) == CODE_LABEL
356                       || GET_CODE (p) == JUMP_INSN
357                       || (GET_CODE (p) == NOTE
358                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
359                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
360                     break;
361                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
362                     {
363                       if (try_auto_increment (p, insn, 0, dst, newconst, 0))
364                         return 1;
365                       break;
366                     }
367                 }
368               for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
369                 {
370                   if (GET_CODE (p) == CODE_LABEL
371                       || GET_CODE (p) == JUMP_INSN
372                       || (GET_CODE (p) == NOTE
373                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
374                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
375                     break;
376                   if (reg_overlap_mentioned_p (dst, PATTERN (p)))
377                     {
378                       try_auto_increment (p, insn, 0, dst, newconst, 1);
379                       break;
380                     }
381                 }
382 #endif
383               return 1;
384             }
385         }
386
387       if (reg_set_p (dst, PATTERN (p)))
388         break;
389
390       /* If we have passed a call instruction, and the
391          pseudo-reg SRC is not already live across a call,
392          then don't perform the optimization.  */
393       /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
394          hard regs are clobbered.  Thus, we only use it for src for
395          non-call insns.  */
396       if (GET_CODE (p) == CALL_INSN)
397         {
398           if (! dst_death)
399             num_calls++;
400
401           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
402             break;
403
404           if (call_used_regs [REGNO (dst)]
405               || find_reg_fusage (p, CLOBBER, dst))
406             break;
407         }
408       else if (reg_set_p (src, PATTERN (p)))
409         break;
410     }
411
412   return 0;
413 }
414
415 void
416 regmove_optimize (f, nregs, regmove_dump_file)
417      rtx f;
418      int nregs;
419      FILE *regmove_dump_file;
420 {
421   rtx insn;
422   struct match match;
423   int pass;
424   int maxregnum = max_reg_num (), i;
425
426   regno_src_regno = (int *)alloca (sizeof *regno_src_regno * maxregnum);
427   for (i = maxregnum; --i >= 0; ) regno_src_regno[i] = -1;
428
429   /* A forward/backward pass.  Replace output operands with input operands.  */
430
431   loop_depth = 1;
432
433   for (pass = 0; pass <= 2; pass++)
434     {
435       if (! flag_regmove && pass >= flag_expensive_optimizations)
436         return;
437
438       if (regmove_dump_file)
439         fprintf (regmove_dump_file, "Starting %s pass...\n",
440                  pass ? "backward" : "forward");
441
442       for (insn = pass ? get_last_insn () : f; insn;
443            insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
444         {
445           rtx set;
446           int insn_code_number;
447           int operand_number, match_number;
448
449           if (GET_CODE (insn) == NOTE)
450             {
451               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
452                 loop_depth++;
453               else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
454                 loop_depth--;
455             }
456
457           set = single_set (insn);
458           if (! set)
459             continue;
460
461           if (flag_expensive_optimizations && ! pass
462               && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
463                   || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
464               && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
465               && GET_CODE (SET_DEST(set)) == REG)
466             optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
467
468           if (flag_expensive_optimizations && ! pass
469               && GET_CODE (SET_SRC (set)) == REG
470               && GET_CODE (SET_DEST(set)) == REG)
471             {
472               /* If this is a register-register copy where SRC is not dead,
473                  see if we can optimize it.  If this optimization succeeds,
474                  it will become a copy where SRC is dead.  */
475               if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
476                    || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
477                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
478                 {
479                   /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
480                   if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
481                     optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
482                   if (regno_src_regno[REGNO (SET_DEST (set))] < 0
483                       && SET_SRC (set) != SET_DEST (set))
484                     {
485                       int srcregno = REGNO (SET_SRC(set));
486                       if (regno_src_regno[srcregno] >= 0)
487                         srcregno = regno_src_regno[srcregno];
488                       regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
489                     }
490                 }
491             }
492 #ifdef REGISTER_CONSTRAINTS
493           insn_code_number
494             = find_matches (insn, &match);
495
496           if (insn_code_number < 0)
497             continue;
498
499           /* Now scan through the operands looking for a source operand
500              which is supposed to match the destination operand.
501              Then scan forward for an instruction which uses the dest
502              operand.
503              If it dies there, then replace the dest in both operands with
504              the source operand.  */
505
506           for (operand_number = 0;
507                operand_number < insn_n_operands[insn_code_number];
508                operand_number++)
509             {
510               rtx p, src, dst, src_subreg;
511               enum reg_class src_class, dst_class;
512
513               match_number = match.with[operand_number];
514
515               /* Nothing to do if the two operands aren't supposed to match.  */
516               if (match_number < 0)
517                 continue;
518
519               src = recog_operand[operand_number];
520               dst = recog_operand[match_number];
521
522               if (GET_CODE (src) != REG)
523                 continue;
524
525               src_subreg = src;
526               if (GET_CODE (dst) == SUBREG
527                   && GET_MODE_SIZE (GET_MODE (dst))
528                      >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
529                 {
530                   src_subreg
531                     = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
532                                       src, SUBREG_WORD (dst));
533                   dst = SUBREG_REG (dst);
534                 }
535               if (GET_CODE (dst) != REG
536                   || REGNO (dst) < FIRST_PSEUDO_REGISTER)
537                 continue;
538
539               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
540                 {
541                   if (match.commutative[operand_number] < operand_number)
542                     regno_src_regno[REGNO (dst)] = REGNO (src);
543                   continue;
544                 }
545
546               if (REG_LIVE_LENGTH (REGNO (src)) < 0)
547                 continue;
548
549               /* operand_number/src must be a read-only operand, and
550                  match_operand/dst must be a write-only operand.  */
551               if (match.use[operand_number] != READ
552                   || match.use[match_number] != WRITE)
553                 continue;
554
555               if (match.early_clobber[match_number]
556                   && count_occurrences (PATTERN (insn), src) > 1)
557                 continue;
558
559               /* Make sure match_operand is the destination.  */
560               if (recog_operand[match_number] != SET_DEST (set))
561                 continue;
562
563               /* If the operands already match, then there is nothing to do.  */
564               /* But in the commutative case, we might find a better match.  */
565               if (operands_match_p (src, dst)
566                   || (match.commutative[operand_number] >= 0
567                       && operands_match_p (recog_operand[match.commutative
568                                                          [operand_number]], dst)
569                       && (replacement_quality (recog_operand[match.commutative
570                                                              [operand_number]])
571                           >= replacement_quality (src))))
572                 continue;
573
574               src_class = reg_preferred_class (REGNO (src));
575               dst_class = reg_preferred_class (REGNO (dst));
576               if (src_class != dst_class
577                   && (! reg_class_subset_p (src_class, dst_class)
578                       || CLASS_LIKELY_SPILLED_P (src_class))
579                   && (! reg_class_subset_p (dst_class, src_class)
580                       || CLASS_LIKELY_SPILLED_P (dst_class)))
581                 continue;
582           
583               if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
584                                  operand_number, match_number,
585                                  regmove_dump_file))
586                 break;
587             }
588         }
589     }
590
591   /* A backward pass.  Replace input operands with output operands.  */
592
593   if (regmove_dump_file)
594     fprintf (regmove_dump_file, "Starting backward pass...\n");
595
596   loop_depth = 1;
597
598   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
599     {
600       if (GET_CODE (insn) == NOTE)
601         {
602           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
603             loop_depth++;
604           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
605             loop_depth--;
606         }
607       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
608         {
609           int insn_code_number = find_matches (insn, &match);
610           int operand_number, match_number;
611           
612           if (insn_code_number < 0)
613             continue;
614
615           /* Now scan through the operands looking for a destination operand
616              which is supposed to match a source operand.
617              Then scan backward for an instruction which sets the source
618              operand.  If safe, then replace the source operand with the
619              dest operand in both instructions.  */
620
621           for (operand_number = 0;
622                operand_number < insn_n_operands[insn_code_number];
623                operand_number++)
624             {
625               rtx set, p, src, dst;
626               rtx src_note, dst_note;
627               int success = 0;
628               int num_calls = 0;
629               enum reg_class src_class, dst_class;
630               int length;
631
632               match_number = match.with[operand_number];
633
634               /* Nothing to do if the two operands aren't supposed to match.  */
635               if (match_number < 0)
636                 continue;
637
638               dst = recog_operand[match_number];
639               src = recog_operand[operand_number];
640
641               if (GET_CODE (src) != REG)
642                 continue;
643
644               if (GET_CODE (dst) != REG
645                   || REGNO (dst) < FIRST_PSEUDO_REGISTER
646                   || REG_LIVE_LENGTH (REGNO (dst)) < 0)
647                 continue;
648
649               /* If the operands already match, then there is nothing to do.  */
650               if (operands_match_p (src, dst)
651                   || (match.commutative[operand_number] >= 0
652                       && operands_match_p (recog_operand[match.commutative[operand_number]], dst)))
653                 continue;
654
655               set = single_set (insn);
656               if (! set)
657                 continue;
658
659               /* match_number/dst must be a write-only operand, and
660                  operand_operand/src must be a read-only operand.  */
661               if (match.use[operand_number] != READ
662                   || match.use[match_number] != WRITE)
663                 continue;
664
665               if (match.early_clobber[match_number]
666                   && count_occurrences (PATTERN (insn), src) > 1)
667                 continue;
668
669               /* Make sure match_number is the destination.  */
670               if (recog_operand[match_number] != SET_DEST (set))
671                 continue;
672
673               if (REGNO (src) < FIRST_PSEUDO_REGISTER)
674                 {
675                   if (GET_CODE (SET_SRC (set)) == PLUS
676                       && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
677                       && XEXP (SET_SRC (set), 0) == src
678                       && fixup_match_2 (insn, dst, src,
679                                         XEXP (SET_SRC (set), 1),
680                                         regmove_dump_file))
681                     break;
682                   continue;
683                 }
684               src_class = reg_preferred_class (REGNO (src));
685               dst_class = reg_preferred_class (REGNO (dst));
686               if (src_class != dst_class
687                   && (! reg_class_subset_p (src_class, dst_class)
688                       || CLASS_LIKELY_SPILLED_P (src_class))
689                   && (! reg_class_subset_p (dst_class, src_class)
690                       || CLASS_LIKELY_SPILLED_P (dst_class)))
691                 continue;
692           
693               if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
694                 continue;
695
696               /* Can not modify an earlier insn to set dst if this insn
697                  uses an old value in the source.  */
698               if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
699                 continue;
700
701               if (regmove_dump_file)
702                 fprintf (regmove_dump_file,
703                          "Could fix operand %d of insn %d matching operand %d.\n",
704                          operand_number, INSN_UID (insn), match_number);
705
706               /* If src is set once in a different basic block,
707                  and is set equal to a constant, then do not use
708                  it for this optimization, as this would make it
709                  no longer equivalent to a constant.  */
710               if (reg_is_remote_constant_p (src, insn, f))
711                 continue;
712
713               /* Scan backward to find the first instruction that uses
714                  the input operand.  If the operand is set here, then
715                  replace it in both instructions with match_number.  */
716
717               for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
718                 {
719                   rtx pset;
720
721                   if (GET_CODE (p) == CODE_LABEL
722                       || GET_CODE (p) == JUMP_INSN
723                       || (GET_CODE (p) == NOTE
724                           && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
725                               || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
726                     break;
727
728                   if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
729                     continue;
730
731                   length++;
732
733                   /* ??? See if all of SRC is set in P.  This test is much
734                      more conservative than it needs to be.  */
735                   pset = single_set (p);
736                   if (pset && SET_DEST (pset) == src)
737                     {
738                       /* We use validate_replace_rtx, in case there
739                          are multiple identical source operands.  All of
740                          them have to be changed at the same time.  */
741                       if (validate_replace_rtx (src, dst, insn))
742                         {
743                           if (validate_change (p, &SET_DEST (pset),
744                                                dst, 0))
745                             success = 1;
746                           else
747                             {
748                               /* Change all source operands back.
749                                  This modifies the dst as a side-effect.  */
750                               validate_replace_rtx (dst, src, insn);
751                               /* Now make sure the dst is right.  */
752                               validate_change (insn,
753                                                recog_operand_loc[match_number],
754                                                dst, 0);
755                             }
756                         }
757                       break;
758                     }
759
760                   if (reg_overlap_mentioned_p (src, PATTERN (p))
761                       || reg_overlap_mentioned_p (dst, PATTERN (p)))
762                     break;
763
764                   /* If we have passed a call instruction, and the
765                      pseudo-reg DST is not already live across a call,
766                      then don't perform the optimization.  */
767                   if (GET_CODE (p) == CALL_INSN)
768                     {
769                       num_calls++;
770
771                       if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
772                         break;
773                     }
774                 }
775
776               if (success)
777                 {
778                   int dstno, srcno;
779
780                   /* Remove the death note for SRC from INSN.  */
781                   remove_note (insn, src_note);
782                   /* Move the death note for SRC to P if it is used
783                      there.  */
784                   if (reg_overlap_mentioned_p (src, PATTERN (p)))
785                     {
786                       XEXP (src_note, 1) = REG_NOTES (p);
787                       REG_NOTES (p) = src_note;
788                     }
789                   /* If there is a REG_DEAD note for DST on P, then remove
790                      it, because DST is now set there.  */
791                   if (dst_note = find_reg_note (p, REG_DEAD, dst))
792                     remove_note (p, dst_note);
793
794                   dstno = REGNO (dst);
795                   srcno = REGNO (src);
796
797                   REG_N_SETS (dstno)++;
798                   REG_N_SETS (srcno)--;
799
800                   REG_N_CALLS_CROSSED (dstno) += num_calls;
801                   REG_N_CALLS_CROSSED (srcno) -= num_calls;
802
803                   REG_LIVE_LENGTH (dstno) += length;
804                   if (REG_LIVE_LENGTH (srcno) >= 0)
805                     {
806                       REG_LIVE_LENGTH (srcno) -= length;
807                       /* REG_LIVE_LENGTH is only an approximation after
808                          combine if sched is not run, so make sure that we
809                          still have a reasonable value.  */
810                       if (REG_LIVE_LENGTH (srcno) < 2)
811                         REG_LIVE_LENGTH (srcno) = 2;
812                     }
813
814                   /* We assume that a register is used exactly once per
815                      insn in the updates above.  If this is not correct,
816                      no great harm is done.  */
817
818                   REG_N_REFS (dstno) += 2 * loop_depth;
819                   REG_N_REFS (srcno) -= 2 * loop_depth;
820
821                   /* If that was the only time src was set,
822                      and src was not live at the start of the
823                      function, we know that we have no more
824                      references to src; clear REG_N_REFS so it
825                      won't make reload do any work.  */
826                   if (REG_N_SETS (REGNO (src)) == 0
827                       && ! regno_uninitialized (REGNO (src)))
828                     REG_N_REFS (REGNO (src)) = 0;
829
830                   if (regmove_dump_file)
831                     fprintf (regmove_dump_file,
832                              "Fixed operand %d of insn %d matching operand %d.\n",
833                              operand_number, INSN_UID (insn), match_number);
834
835                   break;
836                 }
837             }
838         }
839     }
840 #endif /* REGISTER_CONSTRAINTS */
841 }
842
843
844 static int
845 find_matches (insn, matchp)
846      rtx insn;
847      struct match *matchp;
848 {
849   int likely_spilled[MAX_RECOG_OPERANDS];
850   int operand_number;
851   int insn_code_number = recog_memoized (insn);
852   int any_matches = 0;
853
854   if (insn_code_number < 0)
855     return -1;
856
857   insn_extract (insn);
858   if (! constrain_operands (insn_code_number, 0))
859     return -1;
860
861   /* Must initialize this before main loop, because the code for
862      the commutative case may set matches for operands other than
863      the current one.  */
864   for (operand_number = insn_n_operands[insn_code_number];
865        --operand_number >= 0; )
866     matchp->with[operand_number] = matchp->commutative[operand_number] = -1;
867
868   for (operand_number = 0; operand_number < insn_n_operands[insn_code_number];
869        operand_number++)
870     {
871       int output_operand = 0;
872       int matching_operand = operand_number;
873       char *p, c;
874       int i = 0;
875
876       p = insn_operand_constraint[insn_code_number][operand_number];
877
878       likely_spilled[operand_number] = 0;
879       matchp->use[operand_number] = READ;
880       matchp->early_clobber[operand_number] = 0;
881       if (*p == '=')
882         matchp->use[operand_number] = WRITE;
883       else if (*p == '+')
884         matchp->use[operand_number] = READWRITE;
885
886       for (;*p && i < which_alternative; p++)
887         if (*p == ',')
888           i++;
889
890       while ((c = *p++) != '\0' && c != ',')
891         switch (c)
892           {
893           case '=':
894             break;
895           case '+':
896             break;
897           case '&':
898             matchp->early_clobber[operand_number] = 1;
899             break;
900           case '%':
901             matchp->commutative[operand_number] = operand_number + 1;
902             matchp->commutative[operand_number + 1] = operand_number;
903             break;
904           case '0': case '1': case '2': case '3': case '4':
905           case '5': case '6': case '7': case '8': case '9':
906             c -= '0';
907             if (c < operand_number && likely_spilled[c])
908               break;
909             matchp->with[operand_number] = c;
910             any_matches = 1;
911             if (matchp->commutative[operand_number] >= 0)
912               matchp->with[matchp->commutative[operand_number]] = c;
913             break;
914           case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
915           case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
916           case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
917           case 'C': case 'D': case 'W': case 'Y': case 'Z':
918             if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c)))
919               likely_spilled[operand_number] = 1;
920             break;
921           }
922     }
923   return any_matches ? insn_code_number : -1;
924 }
925
926 /* Try to replace output operand DST in SET, with input operand SRC.  SET is
927    the only set in INSN.  INSN has just been recgnized and constrained.
928    SRC is operand number OPERAND_NUMBER in INSN.
929    DST is operand number MATCH_NUMBER in INSN.
930    If BACKWARD is nonzero, we have been called in a backward pass.
931    Return nonzero for success.  */
932 static int
933 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
934                match_number, regmove_dump_file)
935      rtx insn, set, src, src_subreg, dst;
936      int backward, operand_number, match_number;
937      FILE *regmove_dump_file;
938 {
939   rtx p;
940   rtx post_inc = 0, post_inc_set = 0, search_end = 0;
941   int success = 0;
942   int num_calls = 0, s_num_calls = 0;
943   enum rtx_code code = NOTE;
944   HOST_WIDE_INT insn_const, newconst;
945   rtx overlap = 0; /* need to move insn ? */
946   rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
947   int length, s_length, true_loop_depth;
948
949   if (! src_note)
950     {
951       /* Look for (set (regX) (op regA constX))
952                   (set (regY) (op regA constY))
953          and change that to
954                   (set (regA) (op regA constX)).
955                   (set (regY) (op regA constY-constX)).
956          This works for add and shift operations, if
957          regA is dead after or set by the second insn.  */
958
959       code = GET_CODE (SET_SRC (set));
960       if ((code == PLUS || code == LSHIFTRT
961            || code == ASHIFT || code == ASHIFTRT)
962           && XEXP (SET_SRC (set), 0) == src
963           && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
964         insn_const = INTVAL (XEXP (SET_SRC (set), 1));
965       else if (! stable_but_for_p (SET_SRC (set), src, dst))
966         return 0;
967       else
968         /* We might find a src_note while scanning.  */
969         code = NOTE;
970     }
971
972   if (regmove_dump_file)
973     fprintf (regmove_dump_file,
974              "Could fix operand %d of insn %d matching operand %d.\n",
975              operand_number, INSN_UID (insn), match_number);
976
977   /* If SRC is equivalent to a constant set in a different basic block,
978      then do not use it for this optimization.  We want the equivalence
979      so that if we have to reload this register, we can reload the
980      constant, rather than extending the lifespan of the register.  */
981   if (reg_is_remote_constant_p (src, insn, get_insns ()))
982     return 0;
983
984   /* Scan forward to find the next instruction that
985      uses the output operand.  If the operand dies here,
986      then replace it in both instructions with
987      operand_number.  */
988
989   for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
990     {
991       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
992           || (GET_CODE (p) == NOTE
993               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
994                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
995         break;
996
997       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
998         continue;
999
1000       length++;
1001       if (src_note)
1002         s_length++;
1003
1004       if (reg_set_p (src, p) || reg_set_p (dst, p)
1005           || (GET_CODE (PATTERN (p)) == USE
1006               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1007         break;
1008
1009       /* See if all of DST dies in P.  This test is
1010          slightly more conservative than it needs to be.  */
1011       if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1012           && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1013         {
1014           if (! src_note)
1015             {
1016               rtx q;
1017               rtx set2;
1018
1019               /* If an optimization is done, the value of SRC while P
1020                  is executed will be changed.  Check that this is OK.  */
1021               if (reg_overlap_mentioned_p (src, PATTERN (p)))
1022                 break;
1023               for (q = p; q; q = NEXT_INSN (q))
1024                 {
1025                   if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1026                       || (GET_CODE (q) == NOTE
1027                           && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1028                               || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1029                     {
1030                       q = 0;
1031                       break;
1032                     }
1033                   if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1034                     continue;
1035                   if (reg_overlap_mentioned_p (src, PATTERN (q))
1036                       || reg_set_p (src, q))
1037                     break;
1038                 }
1039               if (q)
1040                 set2 = single_set (q);
1041               if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1042                   || XEXP (SET_SRC (set2), 0) != src
1043                   || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1044                   || (SET_DEST (set2) != src
1045                       && ! find_reg_note (q, REG_DEAD, src)))
1046                 {
1047                   /* If this is a PLUS, we can still save a register by doing
1048                      src += insn_const;
1049                      P;
1050                      src -= insn_const; .
1051                      This also gives opportunities for subsequent
1052                      optimizations in the backward pass, so do it there.  */
1053                   if (code == PLUS && backward
1054 #ifdef HAVE_cc0
1055                       /* We may not emit an insn directly
1056                          after P if the latter sets CC0.  */
1057                       && ! sets_cc0_p (PATTERN (p))
1058 #endif
1059                       )
1060
1061                     {
1062                       search_end = q;
1063                       q = insn;
1064                       set2 = set;
1065                       newconst = -insn_const;
1066                       code = MINUS;
1067                     }
1068                   else
1069                     break;
1070                 }
1071               else
1072                 {
1073                   newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1074                   /* Reject out of range shifts.  */
1075                   if (code != PLUS
1076                       && (newconst < 0
1077                           || (newconst
1078                               >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
1079                     break;
1080                   if (code == PLUS)
1081                     {
1082                       post_inc = q;
1083                       if (SET_DEST (set2) != src)
1084                         post_inc_set = set2;
1085                     }
1086                 }
1087               /* We use 1 as last argument to validate_change so that all
1088                  changes are accepted or rejected together by apply_change_group
1089                  when it is called by validate_replace_rtx .  */
1090               validate_change (q, &XEXP (SET_SRC (set2), 1),
1091                                GEN_INT (newconst), 1);
1092             }
1093           validate_change (insn, recog_operand_loc[match_number], src, 1);
1094           if (validate_replace_rtx (dst, src_subreg, p))
1095             success = 1;
1096           break;
1097         }
1098
1099       if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1100         break;
1101       if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1102         {
1103           /* INSN was already checked to be movable when
1104              we found no REG_DEAD note for src on it.  */
1105           overlap = p;
1106           src_note = find_reg_note (p, REG_DEAD, src);
1107         }
1108
1109       /* If we have passed a call instruction, and the pseudo-reg SRC is not
1110          already live across a call, then don't perform the optimization.  */
1111       if (GET_CODE (p) == CALL_INSN)
1112         {
1113           if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1114             break;
1115
1116           num_calls++;
1117
1118           if (src_note)
1119             s_num_calls++;
1120
1121         }
1122     }
1123
1124   if (! success)
1125     return 0;
1126
1127   true_loop_depth = backward ? 2 - loop_depth : loop_depth;
1128
1129   /* Remove the death note for DST from P.  */
1130   remove_note (p, dst_note);
1131   if (code == MINUS)
1132     {
1133       post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1134 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1135       if (search_end
1136           && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1137         post_inc = 0;
1138 #endif
1139       validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1140       REG_N_SETS (REGNO (src))++;
1141       REG_N_REFS (REGNO (src)) += true_loop_depth;
1142       REG_LIVE_LENGTH (REGNO (src))++;
1143     }
1144   if (overlap)
1145     {
1146       /* The lifetime of src and dest overlap,
1147          but we can change this by moving insn.  */
1148       rtx pat = PATTERN (insn);
1149       if (src_note)
1150         remove_note (overlap, src_note);
1151 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1152       if (code == PLUS
1153           && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1154         insn = overlap;
1155       else
1156 #endif
1157         {
1158           rtx notes = REG_NOTES (insn);
1159
1160           emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1161           PUT_CODE (insn, NOTE);
1162           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1163           NOTE_SOURCE_FILE (insn) = 0;
1164           /* emit_insn_after_with_line_notes has no
1165              return value, so search for the new insn.  */
1166           for (insn = p; PATTERN (insn) != pat; )
1167             insn = PREV_INSN (insn);
1168
1169           REG_NOTES (insn) = notes;
1170         }
1171     }
1172   /* Sometimes we'd generate src = const; src += n;
1173      if so, replace the instruction that set src
1174      in the first place.  */
1175
1176   if (! overlap && (code == PLUS || code == MINUS))
1177     {
1178       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1179       rtx q, set2;
1180       int num_calls2 = 0, s_length2 = 0;
1181
1182       if (note && CONSTANT_P (XEXP (note, 0)))
1183         {
1184           for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1185             {
1186               if (GET_CODE (q) == JUMP_INSN)
1187                 {
1188                   q = 0;
1189                   break;
1190                 }
1191               if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1192                 continue;
1193               s_length2++;
1194               if (reg_set_p (src, q))
1195                 {
1196                   set2 = single_set (q);
1197                   break;
1198                 }
1199               if (reg_overlap_mentioned_p (src, PATTERN (q)))
1200                 {
1201                   q = 0;
1202                   break;
1203                 }
1204               if (GET_CODE (p) == CALL_INSN)
1205                 num_calls2++;
1206             }
1207           if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1208               && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1209             {
1210               PUT_CODE (q, NOTE);
1211               NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1212               NOTE_SOURCE_FILE (q) = 0;
1213               REG_N_SETS (REGNO (src))--;
1214               REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1215               REG_N_REFS (REGNO (src)) -= true_loop_depth;
1216               REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1217               insn_const = 0;
1218             }
1219         }
1220     }
1221   if (0) ;
1222 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1223   else if ((code == PLUS || code == MINUS) && insn_const
1224            && try_auto_increment (p, insn, 0, src, insn_const, 1))
1225     insn = p;
1226 #endif
1227 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
1228   else if (post_inc
1229            && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1230     post_inc = 0;
1231 #endif
1232 #if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
1233   /* If post_inc still prevails, try to find an
1234      insn where it can be used as a pre-in/decrement.
1235      If code is MINUS, this was already tried.  */
1236   if (post_inc && code == PLUS
1237   /* Check that newconst is likely to be usable
1238      in a pre-in/decrement before starting the search.  */
1239       && (0
1240 #if defined (HAVE_PRE_INCREMENT)
1241           || (newconst > 0 && newconst <= MOVE_MAX)
1242 #endif
1243 #if defined (HAVE_PRE_DECREMENT)
1244           || (newconst < 0 && newconst >= -MOVE_MAX)
1245 #endif
1246          ) && exact_log2 (newconst))
1247     {
1248       rtx q, inc_dest;
1249
1250       inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1251       for (q = post_inc; q = NEXT_INSN (q); )
1252         {
1253           if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
1254               || (GET_CODE (q) == NOTE
1255                   && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
1256                       || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
1257             break;
1258           if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
1259             continue;
1260           if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
1261                                   || reg_set_p (src, q)))
1262             break;
1263           if (reg_set_p (inc_dest, q))
1264             break;
1265           if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1266             {
1267               try_auto_increment (q, post_inc,
1268                                   post_inc_set, inc_dest, newconst, 1);
1269               break;
1270             }
1271         }
1272     }
1273 #endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
1274   /* Move the death note for DST to INSN if it is used
1275      there.  */
1276   if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1277     {
1278       XEXP (dst_note, 1) = REG_NOTES (insn);
1279       REG_NOTES (insn) = dst_note;
1280     }
1281
1282   if (src_note)
1283     {
1284       /* Move the death note for SRC from INSN to P.  */
1285       if (! overlap)
1286         remove_note (insn, src_note);
1287       XEXP (src_note, 1) = REG_NOTES (p);
1288       REG_NOTES (p) = src_note;
1289
1290       REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1291     }
1292
1293   REG_N_SETS (REGNO (src))++;
1294   REG_N_SETS (REGNO (dst))--;
1295
1296   REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
1297
1298   REG_LIVE_LENGTH (REGNO (src)) += s_length;
1299   if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
1300     {
1301       REG_LIVE_LENGTH (REGNO (dst)) -= length;
1302       /* REG_LIVE_LENGTH is only an approximation after
1303          combine if sched is not run, so make sure that we
1304          still have a reasonable value.  */
1305       if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
1306         REG_LIVE_LENGTH (REGNO (dst)) = 2;
1307     }
1308
1309   /* We assume that a register is used exactly once per
1310       insn in the updates above.  If this is not correct,
1311       no great harm is done.  */
1312
1313   REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
1314   REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
1315
1316   /* If that was the only time dst was set,
1317      and dst was not live at the start of the
1318      function, we know that we have no more
1319      references to dst; clear REG_N_REFS so it
1320      won't make reload do any work.  */
1321   if (REG_N_SETS (REGNO (dst)) == 0
1322       && ! regno_uninitialized (REGNO (dst)))
1323     REG_N_REFS (REGNO (dst)) = 0;
1324
1325   if (regmove_dump_file)
1326     fprintf (regmove_dump_file,
1327              "Fixed operand %d of insn %d matching operand %d.\n",
1328              operand_number, INSN_UID (insn), match_number);
1329   return 1;
1330 }
1331
1332
1333 /* return nonzero if X is stable but for mentioning SRC or mentioning /
1334    changing DST .  If in doubt, presume it is unstable.  */
1335 static int
1336 stable_but_for_p (x, src, dst)
1337      rtx x, src, dst;
1338 {
1339   RTX_CODE code = GET_CODE (x);
1340   switch (GET_RTX_CLASS (code))
1341     {
1342     case '<': case '1': case 'c': case '2': case 'b': case '3':
1343       {
1344         int i;
1345         char *fmt = GET_RTX_FORMAT (code);
1346         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1347           if (fmt[i] == 'e' && ! stable_but_for_p (XEXP (x, i), src, dst))
1348               return 0;
1349         return 1;
1350       }
1351     case 'o':
1352       if (x == src || x == dst)
1353         return 1;
1354       /* fall through */
1355     default:
1356       return ! rtx_unstable_p (x);
1357     }
1358 }
1359
1360 /* Test if regmove seems profitable for this target.  */
1361 int
1362 regmove_profitable_p ()
1363 {
1364 #ifdef REGISTER_CONSTRAINTS
1365   struct match match;
1366   enum machine_mode mode;
1367   optab tstoptab = add_optab;
1368   do /* check add_optab and ashl_optab */
1369     for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1370            mode = GET_MODE_WIDER_MODE (mode))
1371         {
1372           int icode = (int) tstoptab->handlers[(int) mode].insn_code;
1373           rtx reg0, reg1, reg2, pat;
1374           int i;
1375     
1376           if (GET_MODE_BITSIZE (mode) < 32 || icode == CODE_FOR_nothing)
1377             continue;
1378           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1379             if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i))
1380               break;
1381           if (i + 2 >= FIRST_PSEUDO_REGISTER)
1382             break;
1383           reg0 = gen_rtx_REG (insn_operand_mode[icode][0], i);
1384           reg1 = gen_rtx_REG (insn_operand_mode[icode][1], i + 1);
1385           reg2 = gen_rtx_REG (insn_operand_mode[icode][2], i + 2);
1386           if (! (*insn_operand_predicate[icode][0]) (reg0, VOIDmode)
1387               || ! (*insn_operand_predicate[icode][1]) (reg1, VOIDmode)
1388               || ! (*insn_operand_predicate[icode][2]) (reg2, VOIDmode))
1389             break;
1390           pat = GEN_FCN (icode) (reg0, reg1, reg2);
1391           if (! pat)
1392             continue;
1393           if (GET_CODE (pat) == SEQUENCE)
1394             pat = XVECEXP (pat, 0,  XVECLEN (pat, 0) - 1);
1395           else
1396             pat = make_insn_raw (pat);
1397           if (! single_set (pat)
1398               || GET_CODE (SET_SRC (single_set (pat))) != tstoptab->code)
1399             /* Unexpected complexity;  don't need to handle this unless
1400                we find a machine where this occurs and regmove should
1401                be enabled.  */
1402             break;
1403           if (find_matches (pat, &match) >= 0)
1404             return 1;
1405           break;
1406         }
1407   while (tstoptab != ashl_optab && (tstoptab = ashl_optab, 1));
1408 #endif /* REGISTER_CONSTRAINTS */
1409   return 0;
1410 }