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