OSDN Git Service

* recog.c (validate_replace_src_1): New.
[pf3gnuchains/gcc-fork.git] / gcc / recog.c
1 /* Subroutines used by or related to instruction recognition.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3    1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "insn-attr.h"
29 #include "insn-flags.h"
30 #include "insn-codes.h"
31 #include "hard-reg-set.h"
32 #include "recog.h"
33 #include "regs.h"
34 #include "function.h"
35 #include "flags.h"
36 #include "real.h"
37 #include "toplev.h"
38 #include "basic-block.h"
39 #include "output.h"
40 #include "reload.h"
41
42 #ifndef STACK_PUSH_CODE
43 #ifdef STACK_GROWS_DOWNWARD
44 #define STACK_PUSH_CODE PRE_DEC
45 #else
46 #define STACK_PUSH_CODE PRE_INC
47 #endif
48 #endif
49
50 #ifndef STACK_POP_CODE
51 #ifdef STACK_GROWS_DOWNWARD
52 #define STACK_POP_CODE POST_INC
53 #else
54 #define STACK_POP_CODE POST_DEC
55 #endif
56 #endif
57
58 static void validate_replace_rtx_1      PARAMS ((rtx *, rtx, rtx, rtx));
59 static rtx *find_single_use_1           PARAMS ((rtx, rtx *));
60 static rtx *find_constant_term_loc      PARAMS ((rtx *));
61 static int insn_invalid_p               PARAMS ((rtx));
62 static void validate_replace_src_1      PARAMS ((rtx *, void *));
63
64 /* Nonzero means allow operands to be volatile.
65    This should be 0 if you are generating rtl, such as if you are calling
66    the functions in optabs.c and expmed.c (most of the time).
67    This should be 1 if all valid insns need to be recognized,
68    such as in regclass.c and final.c and reload.c.
69
70    init_recog and init_recog_no_volatile are responsible for setting this.  */
71
72 int volatile_ok;
73
74 struct recog_data recog_data;
75
76 /* Contains a vector of operand_alternative structures for every operand.
77    Set up by preprocess_constraints.  */
78 struct operand_alternative recog_op_alt[MAX_RECOG_OPERANDS][MAX_RECOG_ALTERNATIVES];
79
80 /* On return from `constrain_operands', indicate which alternative
81    was satisfied.  */
82
83 int which_alternative;
84
85 /* Nonzero after end of reload pass.
86    Set to 1 or 0 by toplev.c.
87    Controls the significance of (SUBREG (MEM)).  */
88
89 int reload_completed;
90
91 /* Initialize data used by the function `recog'.
92    This must be called once in the compilation of a function
93    before any insn recognition may be done in the function.  */
94
95 void
96 init_recog_no_volatile ()
97 {
98   volatile_ok = 0;
99 }
100
101 void
102 init_recog ()
103 {
104   volatile_ok = 1;
105 }
106
107 /* Try recognizing the instruction INSN,
108    and return the code number that results.
109    Remember the code so that repeated calls do not
110    need to spend the time for actual rerecognition.
111
112    This function is the normal interface to instruction recognition.
113    The automatically-generated function `recog' is normally called
114    through this one.  (The only exception is in combine.c.)  */
115
116 int
117 recog_memoized_1 (insn)
118      rtx insn;
119 {
120   if (INSN_CODE (insn) < 0)
121     INSN_CODE (insn) = recog (PATTERN (insn), insn, NULL_PTR);
122   return INSN_CODE (insn);
123 }
124 \f
125 /* Check that X is an insn-body for an `asm' with operands
126    and that the operands mentioned in it are legitimate.  */
127
128 int
129 check_asm_operands (x)
130      rtx x;
131 {
132   int noperands;
133   rtx *operands;
134   const char **constraints;
135   int i;
136
137   /* Post-reload, be more strict with things.  */
138   if (reload_completed)
139     {
140       /* ??? Doh!  We've not got the wrapping insn.  Cook one up.  */
141       extract_insn (make_insn_raw (x));
142       constrain_operands (1);
143       return which_alternative >= 0;
144     }
145
146   noperands = asm_noperands (x);
147   if (noperands < 0)
148     return 0;
149   if (noperands == 0)
150     return 1;
151
152   operands = (rtx *) alloca (noperands * sizeof (rtx));
153   constraints = (const char **) alloca (noperands * sizeof (char *));
154
155   decode_asm_operands (x, operands, NULL_PTR, constraints, NULL_PTR);
156
157   for (i = 0; i < noperands; i++)
158     {
159       const char *c = constraints[i];
160       if (c[0] == '%')
161         c++;
162       if (ISDIGIT ((unsigned char)c[0]) && c[1] == '\0')
163         c = constraints[c[0] - '0'];
164
165       if (! asm_operand_ok (operands[i], c))
166         return 0;
167     }
168
169   return 1;
170 }
171 \f
172 /* Static data for the next two routines.  */
173
174 typedef struct change_t
175 {
176   rtx object;
177   int old_code;
178   rtx *loc;
179   rtx old;
180 } change_t;
181
182 static change_t *changes;
183 static int changes_allocated;
184
185 static int num_changes = 0;
186
187 /* Validate a proposed change to OBJECT.  LOC is the location in the rtl for
188    at which NEW will be placed.  If OBJECT is zero, no validation is done,
189    the change is simply made.
190
191    Two types of objects are supported:  If OBJECT is a MEM, memory_address_p
192    will be called with the address and mode as parameters.  If OBJECT is
193    an INSN, CALL_INSN, or JUMP_INSN, the insn will be re-recognized with
194    the change in place.
195
196    IN_GROUP is non-zero if this is part of a group of changes that must be
197    performed as a group.  In that case, the changes will be stored.  The
198    function `apply_change_group' will validate and apply the changes.
199
200    If IN_GROUP is zero, this is a single change.  Try to recognize the insn
201    or validate the memory reference with the change applied.  If the result
202    is not valid for the machine, suppress the change and return zero.
203    Otherwise, perform the change and return 1.  */
204
205 int
206 validate_change (object, loc, new, in_group)
207     rtx object;
208     rtx *loc;
209     rtx new;
210     int in_group;
211 {
212   rtx old = *loc;
213
214   if (old == new || rtx_equal_p (old, new))
215     return 1;
216
217   if (in_group == 0 && num_changes != 0)
218     abort ();
219
220   *loc = new;
221
222   /* Save the information describing this change.  */
223   if (num_changes >= changes_allocated)
224     {
225       if (changes_allocated == 0)
226         /* This value allows for repeated substitutions inside complex
227            indexed addresses, or changes in up to 5 insns.  */
228         changes_allocated = MAX_RECOG_OPERANDS * 5;
229       else
230         changes_allocated *= 2;
231
232       changes = 
233         (change_t*) xrealloc (changes, 
234                               sizeof (change_t) * changes_allocated); 
235     }
236   
237   changes[num_changes].object = object;
238   changes[num_changes].loc = loc;
239   changes[num_changes].old = old;
240
241   if (object && GET_CODE (object) != MEM)
242     {
243       /* Set INSN_CODE to force rerecognition of insn.  Save old code in
244          case invalid.  */
245       changes[num_changes].old_code = INSN_CODE (object);
246       INSN_CODE (object) = -1;
247     }
248
249   num_changes++;
250
251   /* If we are making a group of changes, return 1.  Otherwise, validate the
252      change group we made.  */
253
254   if (in_group)
255     return 1;
256   else
257     return apply_change_group ();
258 }
259
260 /* This subroutine of apply_change_group verifies whether the changes to INSN
261    were valid; i.e. whether INSN can still be recognized.  */
262
263 static int
264 insn_invalid_p (insn)
265      rtx insn;
266 {
267   int icode = recog_memoized (insn);
268   int is_asm = icode < 0 && asm_noperands (PATTERN (insn)) >= 0;
269
270   if (is_asm && ! check_asm_operands (PATTERN (insn)))
271     return 1;
272   if (! is_asm && icode < 0)
273     return 1;
274
275   /* After reload, verify that all constraints are satisfied.  */
276   if (reload_completed)
277     {
278       extract_insn (insn);
279
280       if (! constrain_operands (1))
281         return 1;
282     }
283
284   return 0;
285 }
286
287 /* Apply a group of changes previously issued with `validate_change'.
288    Return 1 if all changes are valid, zero otherwise.  */
289
290 int
291 apply_change_group ()
292 {
293   int i;
294
295   /* The changes have been applied and all INSN_CODEs have been reset to force
296      rerecognition.
297
298      The changes are valid if we aren't given an object, or if we are
299      given a MEM and it still is a valid address, or if this is in insn
300      and it is recognized.  In the latter case, if reload has completed,
301      we also require that the operands meet the constraints for
302      the insn.  */
303
304   for (i = 0; i < num_changes; i++)
305     {
306       rtx object = changes[i].object;
307
308       if (object == 0)
309         continue;
310
311       if (GET_CODE (object) == MEM)
312         {
313           if (! memory_address_p (GET_MODE (object), XEXP (object, 0)))
314             break;
315         }
316       else if (insn_invalid_p (object))
317         {
318           rtx pat = PATTERN (object);
319
320           /* Perhaps we couldn't recognize the insn because there were
321              extra CLOBBERs at the end.  If so, try to re-recognize
322              without the last CLOBBER (later iterations will cause each of
323              them to be eliminated, in turn).  But don't do this if we
324              have an ASM_OPERAND.  */
325           if (GET_CODE (pat) == PARALLEL
326               && GET_CODE (XVECEXP (pat, 0, XVECLEN (pat, 0) - 1)) == CLOBBER
327               && asm_noperands (PATTERN (object)) < 0)
328             {
329                rtx newpat;
330
331                if (XVECLEN (pat, 0) == 2)
332                  newpat = XVECEXP (pat, 0, 0);
333                else
334                  {
335                    int j;
336
337                    newpat
338                      = gen_rtx_PARALLEL (VOIDmode, 
339                                          rtvec_alloc (XVECLEN (pat, 0) - 1));
340                    for (j = 0; j < XVECLEN (newpat, 0); j++)
341                      XVECEXP (newpat, 0, j) = XVECEXP (pat, 0, j);
342                  }
343
344                /* Add a new change to this group to replace the pattern
345                   with this new pattern.  Then consider this change
346                   as having succeeded.  The change we added will
347                   cause the entire call to fail if things remain invalid.
348
349                   Note that this can lose if a later change than the one
350                   we are processing specified &XVECEXP (PATTERN (object), 0, X)
351                   but this shouldn't occur.  */
352
353                validate_change (object, &PATTERN (object), newpat, 1);
354              }
355           else if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
356             /* If this insn is a CLOBBER or USE, it is always valid, but is
357                never recognized.  */
358             continue;
359           else
360             break;
361         }
362     }
363
364   if (i == num_changes)
365     {
366       num_changes = 0;
367       return 1;
368     }
369   else
370     {
371       cancel_changes (0);
372       return 0;
373     }
374 }
375
376 /* Return the number of changes so far in the current group.   */
377
378 int
379 num_validated_changes ()
380 {
381   return num_changes;
382 }
383
384 /* Retract the changes numbered NUM and up.  */
385
386 void
387 cancel_changes (num)
388      int num;
389 {
390   int i;
391
392   /* Back out all the changes.  Do this in the opposite order in which
393      they were made.  */
394   for (i = num_changes - 1; i >= num; i--)
395     {
396       *changes[i].loc = changes[i].old;
397       if (changes[i].object && GET_CODE (changes[i].object) != MEM)
398         INSN_CODE (changes[i].object) = changes[i].old_code;
399     }
400   num_changes = num;
401 }
402
403 /* Replace every occurrence of FROM in X with TO.  Mark each change with
404    validate_change passing OBJECT.  */
405
406 static void
407 validate_replace_rtx_1 (loc, from, to, object)
408      rtx *loc;
409      rtx from, to, object;
410 {
411   register int i, j;
412   register const char *fmt;
413   register rtx x = *loc;
414   enum rtx_code code;
415
416   if (!x)
417     return;
418   code = GET_CODE (x);
419   /* X matches FROM if it is the same rtx or they are both referring to the
420      same register in the same mode.  Avoid calling rtx_equal_p unless the
421      operands look similar.  */
422
423   if (x == from
424       || (GET_CODE (x) == REG && GET_CODE (from) == REG
425           && GET_MODE (x) == GET_MODE (from)
426           && REGNO (x) == REGNO (from))
427       || (GET_CODE (x) == GET_CODE (from) && GET_MODE (x) == GET_MODE (from)
428           && rtx_equal_p (x, from)))
429     {
430       validate_change (object, loc, to, 1);
431       return;
432     }
433
434   /* For commutative or comparison operations, try replacing each argument
435      separately and seeing if we made any changes.  If so, put a constant
436      argument last.*/
437   if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
438     {
439       int prev_changes = num_changes;
440
441       validate_replace_rtx_1 (&XEXP (x, 0), from, to, object);
442       validate_replace_rtx_1 (&XEXP (x, 1), from, to, object);
443       if (prev_changes != num_changes && CONSTANT_P (XEXP (x, 0)))
444         {
445           validate_change (object, loc,
446                            gen_rtx_fmt_ee (GET_RTX_CLASS (code) == 'c' ? code
447                                            : swap_condition (code),
448                                            GET_MODE (x), XEXP (x, 1),
449                                            XEXP (x, 0)),
450                            1);
451           x = *loc;
452           code = GET_CODE (x);
453         }
454     }
455
456   /* Note that if CODE's RTX_CLASS is "c" or "<" we will have already
457      done the substitution, otherwise we won't.  */
458
459   switch (code)
460     {
461     case PLUS:
462       /* If we have a PLUS whose second operand is now a CONST_INT, use
463          plus_constant to try to simplify it.  */
464       if (GET_CODE (XEXP (x, 1)) == CONST_INT && XEXP (x, 1) == to)
465         validate_change (object, loc, plus_constant (XEXP (x, 0), INTVAL (to)),
466                          1);
467       return;
468
469     case MINUS:
470       if (GET_CODE (to) == CONST_INT && XEXP (x, 1) == from)
471         {
472           validate_change (object, loc,
473                            plus_constant (XEXP (x, 0), - INTVAL (to)),
474                            1);
475           return;
476         }
477       break;
478       
479     case ZERO_EXTEND:
480     case SIGN_EXTEND:
481       /* In these cases, the operation to be performed depends on the mode
482          of the operand.  If we are replacing the operand with a VOIDmode
483          constant, we lose the information.  So try to simplify the operation
484          in that case.  */
485       if (GET_MODE (to) == VOIDmode
486           && (rtx_equal_p (XEXP (x, 0), from)
487               || (GET_CODE (XEXP (x, 0)) == SUBREG
488                   && rtx_equal_p (SUBREG_REG (XEXP (x, 0)), from))))
489         {
490           rtx new = NULL_RTX;
491
492           /* If there is a subreg involved, crop to the portion of the
493              constant that we are interested in.  */
494           if (GET_CODE (XEXP (x, 0)) == SUBREG)
495             {
496               if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) <= UNITS_PER_WORD)
497                 to = operand_subword (to, SUBREG_WORD (XEXP (x, 0)),
498                                       0, GET_MODE (from));
499               else if (GET_MODE_CLASS (GET_MODE (from)) == MODE_INT
500                        && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
501                            <= HOST_BITS_PER_WIDE_INT))
502                 {
503                   int i = SUBREG_WORD (XEXP (x, 0)) * BITS_PER_WORD;
504                   HOST_WIDE_INT valh;
505                   unsigned HOST_WIDE_INT vall;
506
507                   if (GET_CODE (to) == CONST_INT)
508                     {
509                       vall = INTVAL (to);
510                       valh = (HOST_WIDE_INT) vall < 0 ? ~0 : 0;
511                     }
512                   else
513                     {
514                       vall = CONST_DOUBLE_LOW (to);
515                       valh = CONST_DOUBLE_HIGH (to);
516                     }
517
518                   if (WORDS_BIG_ENDIAN)
519                     i = (GET_MODE_BITSIZE (GET_MODE (from))
520                          - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - i);
521                   if (i > 0 && i < HOST_BITS_PER_WIDE_INT)
522                     vall = vall >> i | valh << (HOST_BITS_PER_WIDE_INT - i);
523                   else if (i >= HOST_BITS_PER_WIDE_INT)
524                     vall = valh >> (i - HOST_BITS_PER_WIDE_INT);
525                   to = GEN_INT (trunc_int_for_mode (vall,
526                                                     GET_MODE (XEXP (x, 0))));
527                 }
528               else
529                 to = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
530             }
531
532           /* If the above didn't fail, perform the extension from the
533              mode of the operand (and not the mode of FROM).  */
534           if (to)
535             new = simplify_unary_operation (code, GET_MODE (x), to,
536                                             GET_MODE (XEXP (x, 0)));
537
538           /* If any of the above failed, substitute in something that
539              we know won't be recognized.  */
540           if (!new)
541             new = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
542
543           validate_change (object, loc, new, 1);
544           return;
545         }
546       break;
547         
548     case SUBREG:
549       /* In case we are replacing by constant, attempt to simplify it to non-SUBREG
550          expression.  We can't do this later, since the information about inner mode
551          may be lost.  */
552       if (CONSTANT_P (to) && rtx_equal_p (SUBREG_REG (x), from))
553         {
554           if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD
555               && GET_MODE_SIZE (GET_MODE (from)) > UNITS_PER_WORD
556               && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
557             {
558               rtx temp = operand_subword (to, SUBREG_WORD (x),
559                                           0, GET_MODE (from));
560               if (temp)
561                 {
562                   validate_change (object, loc, temp, 1);
563                   return;
564                 }
565             }
566           if (subreg_lowpart_p (x))
567             {
568               rtx new =  gen_lowpart_if_possible (GET_MODE (x), to);
569               if (new)
570                 {
571                   validate_change (object, loc, new, 1);
572                   return;
573                 }
574             }
575
576           /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
577              since we are saying that the high bits don't matter.  */
578           if (GET_MODE (to) == VOIDmode
579               && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (from)))
580             {
581               validate_change (object, loc, to, 1);
582               return;
583             }
584         }
585
586       /* Changing mode twice with SUBREG => just change it once,
587          or not at all if changing back to starting mode.  */
588       if (GET_CODE (to) == SUBREG
589           && rtx_equal_p (SUBREG_REG (x), from))
590         {
591           if (GET_MODE (x) == GET_MODE (SUBREG_REG (to))
592               && SUBREG_WORD (x) == 0 && SUBREG_WORD (to) == 0)
593             {
594               validate_change (object, loc, SUBREG_REG (to), 1);
595               return;
596             }
597
598           validate_change (object, loc,
599                            gen_rtx_SUBREG (GET_MODE (x), SUBREG_REG (to),
600                                            SUBREG_WORD (x) + SUBREG_WORD (to)), 1);
601           return;
602         }
603
604       /* If we have a SUBREG of a register that we are replacing and we are
605          replacing it with a MEM, make a new MEM and try replacing the
606          SUBREG with it.  Don't do this if the MEM has a mode-dependent address
607          or if we would be widening it.  */
608
609       if (GET_CODE (from) == REG
610           && GET_CODE (to) == MEM
611           && rtx_equal_p (SUBREG_REG (x), from)
612           && ! mode_dependent_address_p (XEXP (to, 0))
613           && ! MEM_VOLATILE_P (to)
614           && GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (GET_MODE (to)))
615         {
616           int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
617           enum machine_mode mode = GET_MODE (x);
618           rtx new;
619
620           if (BYTES_BIG_ENDIAN)
621             offset += (MIN (UNITS_PER_WORD,
622                             GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
623                        - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
624
625           new = gen_rtx_MEM (mode, plus_constant (XEXP (to, 0), offset));
626           MEM_COPY_ATTRIBUTES (new, to);
627           validate_change (object, loc, new, 1);
628           return;
629         }
630       break;
631
632     case ZERO_EXTRACT:
633     case SIGN_EXTRACT:
634       /* If we are replacing a register with memory, try to change the memory
635          to be the mode required for memory in extract operations (this isn't
636          likely to be an insertion operation; if it was, nothing bad will
637          happen, we might just fail in some cases).  */
638
639       if (GET_CODE (from) == REG && GET_CODE (to) == MEM
640           && rtx_equal_p (XEXP (x, 0), from)
641           && GET_CODE (XEXP (x, 1)) == CONST_INT
642           && GET_CODE (XEXP (x, 2)) == CONST_INT
643           && ! mode_dependent_address_p (XEXP (to, 0))
644           && ! MEM_VOLATILE_P (to))
645         {
646           enum machine_mode wanted_mode = VOIDmode;
647           enum machine_mode is_mode = GET_MODE (to);
648           int pos = INTVAL (XEXP (x, 2));
649
650 #ifdef HAVE_extzv
651           if (code == ZERO_EXTRACT)
652             {
653               wanted_mode = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
654               if (wanted_mode == VOIDmode)
655                 wanted_mode = word_mode;
656             }
657 #endif
658 #ifdef HAVE_extv
659           if (code == SIGN_EXTRACT)
660             {
661               wanted_mode = insn_data[(int) CODE_FOR_extv].operand[1].mode;
662               if (wanted_mode == VOIDmode)
663                 wanted_mode = word_mode;
664             }
665 #endif
666
667           /* If we have a narrower mode, we can do something.  */
668           if (wanted_mode != VOIDmode
669               && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
670             {
671               int offset = pos / BITS_PER_UNIT;
672               rtx newmem;
673
674                   /* If the bytes and bits are counted differently, we
675                      must adjust the offset.  */
676               if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
677                 offset = (GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (wanted_mode)
678                           - offset);
679
680               pos %= GET_MODE_BITSIZE (wanted_mode);
681
682               newmem = gen_rtx_MEM (wanted_mode,
683                                     plus_constant (XEXP (to, 0), offset));
684               MEM_COPY_ATTRIBUTES (newmem, to);
685
686               validate_change (object, &XEXP (x, 2), GEN_INT (pos), 1);
687               validate_change (object, &XEXP (x, 0), newmem, 1);
688             }
689         }
690
691       break;
692       
693     default:
694       break;
695     }
696       
697   /* For commutative or comparison operations we've already performed
698      replacements.  Don't try to perform them again.  */
699   if (GET_RTX_CLASS (code) != '<' && GET_RTX_CLASS (code) != 'c')
700     {
701       fmt = GET_RTX_FORMAT (code);
702       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
703         {
704           if (fmt[i] == 'e')
705             validate_replace_rtx_1 (&XEXP (x, i), from, to, object);
706           else if (fmt[i] == 'E')
707             for (j = XVECLEN (x, i) - 1; j >= 0; j--)
708               validate_replace_rtx_1 (&XVECEXP (x, i, j), from, to, object);
709         }
710     }
711 }
712
713 /* Try replacing every occurrence of FROM in subexpression LOC of INSN
714    with TO.  After all changes have been made, validate by seeing
715    if INSN is still valid.  */
716
717 int
718 validate_replace_rtx_subexp (from, to, insn, loc)
719      rtx from, to, insn, *loc;
720 {
721   validate_replace_rtx_1 (loc, from, to, insn);
722   return apply_change_group ();
723 }
724
725 /* Try replacing every occurrence of FROM in INSN with TO.  After all
726    changes have been made, validate by seeing if INSN is still valid.  */
727
728 int
729 validate_replace_rtx (from, to, insn)
730      rtx from, to, insn;
731 {
732   validate_replace_rtx_1 (&PATTERN (insn), from, to, insn);
733   return apply_change_group ();
734 }
735
736 /* Try replacing every occurrence of FROM in INSN with TO.  */
737
738 void
739 validate_replace_rtx_group (from, to, insn)
740      rtx from, to, insn;
741 {
742   validate_replace_rtx_1 (&PATTERN (insn), from, to, insn);
743 }
744
745 /* Function called by note_uses to replace used subexpressions.  */
746 struct validate_replace_src_data
747   {
748     rtx from, to, insn;
749   };
750
751 static void
752 validate_replace_src_1 (x, data)
753      rtx *x;
754      void *data;
755 {
756   struct validate_replace_src_data *d
757     = (struct validate_replace_src_data *) data;
758
759   validate_replace_rtx_1 (x, d->from, d->to, d->insn);
760 }
761
762 /* Try replacing every occurrence of FROM in INSN with TO, avoiding
763    SET_DESTs.  After all changes have been made, validate by seeing if
764    INSN is still valid.  */
765
766 int
767 validate_replace_src (from, to, insn)
768      rtx from, to, insn;
769 {
770   struct validate_replace_src_data d;
771
772   d.from = from;
773   d.to = to;
774   d.insn = insn;
775   note_uses (&PATTERN (insn), validate_replace_src_1, &d);
776   return apply_change_group ();
777 }
778 \f
779 #ifdef HAVE_cc0
780 /* Return 1 if the insn using CC0 set by INSN does not contain
781    any ordered tests applied to the condition codes.
782    EQ and NE tests do not count.  */
783
784 int
785 next_insn_tests_no_inequality (insn)
786      rtx insn;
787 {
788   register rtx next = next_cc0_user (insn);
789
790   /* If there is no next insn, we have to take the conservative choice.  */
791   if (next == 0)
792     return 0;
793
794   return ((GET_CODE (next) == JUMP_INSN
795            || GET_CODE (next) == INSN
796            || GET_CODE (next) == CALL_INSN)
797           && ! inequality_comparisons_p (PATTERN (next)));
798 }
799
800 #if 0  /* This is useless since the insn that sets the cc's
801           must be followed immediately by the use of them.  */
802 /* Return 1 if the CC value set up by INSN is not used.  */
803
804 int
805 next_insns_test_no_inequality (insn)
806      rtx insn;
807 {
808   register rtx next = NEXT_INSN (insn);
809
810   for (; next != 0; next = NEXT_INSN (next))
811     {
812       if (GET_CODE (next) == CODE_LABEL
813           || GET_CODE (next) == BARRIER)
814         return 1;
815       if (GET_CODE (next) == NOTE)
816         continue;
817       if (inequality_comparisons_p (PATTERN (next)))
818         return 0;
819       if (sets_cc0_p (PATTERN (next)) == 1)
820         return 1;
821       if (! reg_mentioned_p (cc0_rtx, PATTERN (next)))
822         return 1;
823     }
824   return 1;
825 }
826 #endif
827 #endif
828 \f
829 /* This is used by find_single_use to locate an rtx that contains exactly one
830    use of DEST, which is typically either a REG or CC0.  It returns a
831    pointer to the innermost rtx expression containing DEST.  Appearances of
832    DEST that are being used to totally replace it are not counted.  */
833
834 static rtx *
835 find_single_use_1 (dest, loc)
836      rtx dest;
837      rtx *loc;
838 {
839   rtx x = *loc;
840   enum rtx_code code = GET_CODE (x);
841   rtx *result = 0;
842   rtx *this_result;
843   int i;
844   const char *fmt;
845
846   switch (code)
847     {
848     case CONST_INT:
849     case CONST:
850     case LABEL_REF:
851     case SYMBOL_REF:
852     case CONST_DOUBLE:
853     case CLOBBER:
854       return 0;
855
856     case SET:
857       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
858          of a REG that occupies all of the REG, the insn uses DEST if
859          it is mentioned in the destination or the source.  Otherwise, we
860          need just check the source.  */
861       if (GET_CODE (SET_DEST (x)) != CC0
862           && GET_CODE (SET_DEST (x)) != PC
863           && GET_CODE (SET_DEST (x)) != REG
864           && ! (GET_CODE (SET_DEST (x)) == SUBREG
865                 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
866                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
867                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
868                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
869                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
870         break;
871
872       return find_single_use_1 (dest, &SET_SRC (x));
873
874     case MEM:
875     case SUBREG:
876       return find_single_use_1 (dest, &XEXP (x, 0));
877       
878     default:
879       break;
880     }
881
882   /* If it wasn't one of the common cases above, check each expression and
883      vector of this code.  Look for a unique usage of DEST.  */
884
885   fmt = GET_RTX_FORMAT (code);
886   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
887     {
888       if (fmt[i] == 'e')
889         {
890           if (dest == XEXP (x, i)
891               || (GET_CODE (dest) == REG && GET_CODE (XEXP (x, i)) == REG
892                   && REGNO (dest) == REGNO (XEXP (x, i))))
893             this_result = loc;
894           else
895             this_result = find_single_use_1 (dest, &XEXP (x, i));
896
897           if (result == 0)
898             result = this_result;
899           else if (this_result)
900             /* Duplicate usage.  */
901             return 0;
902         }
903       else if (fmt[i] == 'E')
904         {
905           int j;
906
907           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
908             {
909               if (XVECEXP (x, i, j) == dest
910                   || (GET_CODE (dest) == REG
911                       && GET_CODE (XVECEXP (x, i, j)) == REG
912                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
913                 this_result = loc;
914               else
915                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
916
917               if (result == 0)
918                 result = this_result;
919               else if (this_result)
920                 return 0;
921             }
922         }
923     }
924
925   return result;
926 }
927 \f
928 /* See if DEST, produced in INSN, is used only a single time in the
929    sequel.  If so, return a pointer to the innermost rtx expression in which
930    it is used.
931
932    If PLOC is non-zero, *PLOC is set to the insn containing the single use.
933
934    This routine will return usually zero either before flow is called (because
935    there will be no LOG_LINKS notes) or after reload (because the REG_DEAD
936    note can't be trusted).
937
938    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
939    care about REG_DEAD notes or LOG_LINKS.
940
941    Otherwise, we find the single use by finding an insn that has a
942    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
943    only referenced once in that insn, we know that it must be the first
944    and last insn referencing DEST.  */
945
946 rtx *
947 find_single_use (dest, insn, ploc)
948      rtx dest;
949      rtx insn;
950      rtx *ploc;
951 {
952   rtx next;
953   rtx *result;
954   rtx link;
955
956 #ifdef HAVE_cc0
957   if (dest == cc0_rtx)
958     {
959       next = NEXT_INSN (insn);
960       if (next == 0
961           || (GET_CODE (next) != INSN && GET_CODE (next) != JUMP_INSN))
962         return 0;
963
964       result = find_single_use_1 (dest, &PATTERN (next));
965       if (result && ploc)
966         *ploc = next;
967       return result;
968     }
969 #endif
970
971   if (reload_completed || reload_in_progress || GET_CODE (dest) != REG)
972     return 0;
973
974   for (next = next_nonnote_insn (insn);
975        next != 0 && GET_CODE (next) != CODE_LABEL;
976        next = next_nonnote_insn (next))
977     if (INSN_P (next) && dead_or_set_p (next, dest))
978       {
979         for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
980           if (XEXP (link, 0) == insn)
981             break;
982
983         if (link)
984           {
985             result = find_single_use_1 (dest, &PATTERN (next));
986             if (ploc)
987               *ploc = next;
988             return result;
989           }
990       }
991
992   return 0;
993 }
994 \f
995 /* Return 1 if OP is a valid general operand for machine mode MODE.
996    This is either a register reference, a memory reference,
997    or a constant.  In the case of a memory reference, the address
998    is checked for general validity for the target machine.
999
1000    Register and memory references must have mode MODE in order to be valid,
1001    but some constants have no machine mode and are valid for any mode.
1002
1003    If MODE is VOIDmode, OP is checked for validity for whatever mode
1004    it has.
1005
1006    The main use of this function is as a predicate in match_operand
1007    expressions in the machine description.
1008
1009    For an explanation of this function's behavior for registers of
1010    class NO_REGS, see the comment for `register_operand'.  */
1011
1012 int
1013 general_operand (op, mode)
1014      register rtx op;
1015      enum machine_mode mode;
1016 {
1017   register enum rtx_code code = GET_CODE (op);
1018   int mode_altering_drug = 0;
1019
1020   if (mode == VOIDmode)
1021     mode = GET_MODE (op);
1022
1023   /* Don't accept CONST_INT or anything similar
1024      if the caller wants something floating.  */
1025   if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1026       && GET_MODE_CLASS (mode) != MODE_INT
1027       && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1028     return 0;
1029
1030   if (CONSTANT_P (op))
1031     return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
1032              || mode == VOIDmode)
1033 #ifdef LEGITIMATE_PIC_OPERAND_P
1034             && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1035 #endif
1036             && LEGITIMATE_CONSTANT_P (op));
1037
1038   /* Except for certain constants with VOIDmode, already checked for,
1039      OP's mode must match MODE if MODE specifies a mode.  */
1040
1041   if (GET_MODE (op) != mode)
1042     return 0;
1043
1044   if (code == SUBREG)
1045     {
1046 #ifdef INSN_SCHEDULING
1047       /* On machines that have insn scheduling, we want all memory
1048          reference to be explicit, so outlaw paradoxical SUBREGs.  */
1049       if (GET_CODE (SUBREG_REG (op)) == MEM
1050           && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
1051         return 0;
1052 #endif
1053
1054       op = SUBREG_REG (op);
1055       code = GET_CODE (op);
1056 #if 0
1057       /* No longer needed, since (SUBREG (MEM...))
1058          will load the MEM into a reload reg in the MEM's own mode.  */
1059       mode_altering_drug = 1;
1060 #endif
1061     }
1062
1063   if (code == REG)
1064     /* A register whose class is NO_REGS is not a general operand.  */
1065     return (REGNO (op) >= FIRST_PSEUDO_REGISTER
1066             || REGNO_REG_CLASS (REGNO (op)) != NO_REGS);
1067
1068   if (code == MEM)
1069     {
1070       register rtx y = XEXP (op, 0);
1071
1072       if (! volatile_ok && MEM_VOLATILE_P (op))
1073         return 0;
1074
1075       if (GET_CODE (y) == ADDRESSOF)
1076         return 1;
1077
1078       /* Use the mem's mode, since it will be reloaded thus.  */
1079       mode = GET_MODE (op);
1080       GO_IF_LEGITIMATE_ADDRESS (mode, y, win);
1081     }
1082
1083   /* Pretend this is an operand for now; we'll run force_operand
1084      on its replacement in fixup_var_refs_1.  */
1085   if (code == ADDRESSOF)
1086     return 1;
1087
1088   return 0;
1089
1090  win:
1091   if (mode_altering_drug)
1092     return ! mode_dependent_address_p (XEXP (op, 0));
1093   return 1;
1094 }
1095 \f
1096 /* Return 1 if OP is a valid memory address for a memory reference
1097    of mode MODE.
1098
1099    The main use of this function is as a predicate in match_operand
1100    expressions in the machine description.  */
1101
1102 int
1103 address_operand (op, mode)
1104      register rtx op;
1105      enum machine_mode mode;
1106 {
1107   return memory_address_p (mode, op);
1108 }
1109
1110 /* Return 1 if OP is a register reference of mode MODE.
1111    If MODE is VOIDmode, accept a register in any mode.
1112
1113    The main use of this function is as a predicate in match_operand
1114    expressions in the machine description.
1115
1116    As a special exception, registers whose class is NO_REGS are
1117    not accepted by `register_operand'.  The reason for this change
1118    is to allow the representation of special architecture artifacts
1119    (such as a condition code register) without extending the rtl
1120    definitions.  Since registers of class NO_REGS cannot be used
1121    as registers in any case where register classes are examined,
1122    it is most consistent to keep this function from accepting them.  */
1123
1124 int
1125 register_operand (op, mode)
1126      register rtx op;
1127      enum machine_mode mode;
1128 {
1129   if (GET_MODE (op) != mode && mode != VOIDmode)
1130     return 0;
1131
1132   if (GET_CODE (op) == SUBREG)
1133     {
1134       /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1135          because it is guaranteed to be reloaded into one.
1136          Just make sure the MEM is valid in itself.
1137          (Ideally, (SUBREG (MEM)...) should not exist after reload,
1138          but currently it does result from (SUBREG (REG)...) where the
1139          reg went on the stack.)  */
1140       if (! reload_completed && GET_CODE (SUBREG_REG (op)) == MEM)
1141         return general_operand (op, mode);
1142
1143 #ifdef CLASS_CANNOT_CHANGE_MODE
1144       if (GET_CODE (SUBREG_REG (op)) == REG
1145           && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER
1146           && (TEST_HARD_REG_BIT
1147               (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE],
1148                REGNO (SUBREG_REG (op))))
1149           && CLASS_CANNOT_CHANGE_MODE_P (mode, GET_MODE (SUBREG_REG (op)))
1150           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op))) != MODE_COMPLEX_INT
1151           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op))) != MODE_COMPLEX_FLOAT)
1152         return 0;
1153 #endif
1154
1155       op = SUBREG_REG (op);
1156     }
1157
1158   /* If we have an ADDRESSOF, consider it valid since it will be
1159      converted into something that will not be a MEM. */
1160   if (GET_CODE (op) == ADDRESSOF)
1161     return 1;
1162
1163   /* We don't consider registers whose class is NO_REGS
1164      to be a register operand.  */
1165   return (GET_CODE (op) == REG
1166           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
1167               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
1168 }
1169
1170 /* Return 1 for a register in Pmode; ignore the tested mode.  */
1171
1172 int
1173 pmode_register_operand (op, mode)
1174      rtx op;
1175      enum machine_mode mode ATTRIBUTE_UNUSED;
1176 {
1177   return register_operand (op, Pmode);
1178 }
1179
1180 /* Return 1 if OP should match a MATCH_SCRATCH, i.e., if it is a SCRATCH
1181    or a hard register.  */
1182
1183 int
1184 scratch_operand (op, mode)
1185      register rtx op;
1186      enum machine_mode mode;
1187 {
1188   if (GET_MODE (op) != mode && mode != VOIDmode)
1189     return 0;
1190
1191   return (GET_CODE (op) == SCRATCH
1192           || (GET_CODE (op) == REG
1193               && REGNO (op) < FIRST_PSEUDO_REGISTER));
1194 }
1195
1196 /* Return 1 if OP is a valid immediate operand for mode MODE.
1197
1198    The main use of this function is as a predicate in match_operand
1199    expressions in the machine description.  */
1200
1201 int
1202 immediate_operand (op, mode)
1203      register rtx op;
1204      enum machine_mode mode;
1205 {
1206   /* Don't accept CONST_INT or anything similar
1207      if the caller wants something floating.  */
1208   if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1209       && GET_MODE_CLASS (mode) != MODE_INT
1210       && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1211     return 0;
1212
1213   /* Accept CONSTANT_P_RTX, since it will be gone by CSE1 and
1214      result in 0/1.  It seems a safe assumption that this is
1215      in range for everyone.  */
1216   if (GET_CODE (op) == CONSTANT_P_RTX)
1217     return 1;
1218
1219   return (CONSTANT_P (op)
1220           && (GET_MODE (op) == mode || mode == VOIDmode
1221               || GET_MODE (op) == VOIDmode)
1222 #ifdef LEGITIMATE_PIC_OPERAND_P
1223           && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1224 #endif
1225           && LEGITIMATE_CONSTANT_P (op));
1226 }
1227
1228 /* Returns 1 if OP is an operand that is a CONST_INT.  */
1229
1230 int
1231 const_int_operand (op, mode)
1232      register rtx op;
1233      enum machine_mode mode ATTRIBUTE_UNUSED;
1234 {
1235   return GET_CODE (op) == CONST_INT;
1236 }
1237
1238 /* Returns 1 if OP is an operand that is a constant integer or constant
1239    floating-point number.  */
1240
1241 int
1242 const_double_operand (op, mode)
1243      register rtx op;
1244      enum machine_mode mode;
1245 {
1246   /* Don't accept CONST_INT or anything similar
1247      if the caller wants something floating.  */
1248   if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1249       && GET_MODE_CLASS (mode) != MODE_INT
1250       && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1251     return 0;
1252
1253   return ((GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT)
1254           && (mode == VOIDmode || GET_MODE (op) == mode
1255               || GET_MODE (op) == VOIDmode));
1256 }
1257
1258 /* Return 1 if OP is a general operand that is not an immediate operand.  */
1259
1260 int
1261 nonimmediate_operand (op, mode)
1262      register rtx op;
1263      enum machine_mode mode;
1264 {
1265   return (general_operand (op, mode) && ! CONSTANT_P (op));
1266 }
1267
1268 /* Return 1 if OP is a register reference or immediate value of mode MODE.  */
1269
1270 int
1271 nonmemory_operand (op, mode)
1272      register rtx op;
1273      enum machine_mode mode;
1274 {
1275   if (CONSTANT_P (op))
1276     {
1277       /* Don't accept CONST_INT or anything similar
1278          if the caller wants something floating.  */
1279       if (GET_MODE (op) == VOIDmode && mode != VOIDmode
1280           && GET_MODE_CLASS (mode) != MODE_INT
1281           && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1282         return 0;
1283
1284       return ((GET_MODE (op) == VOIDmode || GET_MODE (op) == mode
1285               || mode == VOIDmode)
1286 #ifdef LEGITIMATE_PIC_OPERAND_P
1287               && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1288 #endif
1289               && LEGITIMATE_CONSTANT_P (op));
1290     }
1291
1292   if (GET_MODE (op) != mode && mode != VOIDmode)
1293     return 0;
1294
1295   if (GET_CODE (op) == SUBREG)
1296     {
1297       /* Before reload, we can allow (SUBREG (MEM...)) as a register operand
1298          because it is guaranteed to be reloaded into one.
1299          Just make sure the MEM is valid in itself.
1300          (Ideally, (SUBREG (MEM)...) should not exist after reload,
1301          but currently it does result from (SUBREG (REG)...) where the
1302          reg went on the stack.)  */
1303       if (! reload_completed && GET_CODE (SUBREG_REG (op)) == MEM)
1304         return general_operand (op, mode);
1305       op = SUBREG_REG (op);
1306     }
1307
1308   /* We don't consider registers whose class is NO_REGS
1309      to be a register operand.  */
1310   return (GET_CODE (op) == REG
1311           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
1312               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
1313 }
1314
1315 /* Return 1 if OP is a valid operand that stands for pushing a
1316    value of mode MODE onto the stack.
1317
1318    The main use of this function is as a predicate in match_operand
1319    expressions in the machine description.  */
1320
1321 int
1322 push_operand (op, mode)
1323      rtx op;
1324      enum machine_mode mode;
1325 {
1326   if (GET_CODE (op) != MEM)
1327     return 0;
1328
1329   if (mode != VOIDmode && GET_MODE (op) != mode)
1330     return 0;
1331
1332   op = XEXP (op, 0);
1333
1334   if (GET_CODE (op) != STACK_PUSH_CODE)
1335     return 0;
1336
1337   return XEXP (op, 0) == stack_pointer_rtx;
1338 }
1339
1340 /* Return 1 if OP is a valid operand that stands for popping a
1341    value of mode MODE off the stack.
1342
1343    The main use of this function is as a predicate in match_operand
1344    expressions in the machine description.  */
1345
1346 int
1347 pop_operand (op, mode)
1348      rtx op;
1349      enum machine_mode mode;
1350 {
1351   if (GET_CODE (op) != MEM)
1352     return 0;
1353
1354   if (mode != VOIDmode && GET_MODE (op) != mode)
1355     return 0;
1356
1357   op = XEXP (op, 0);
1358
1359   if (GET_CODE (op) != STACK_POP_CODE)
1360     return 0;
1361
1362   return XEXP (op, 0) == stack_pointer_rtx;
1363 }
1364
1365 /* Return 1 if ADDR is a valid memory address for mode MODE.  */
1366
1367 int
1368 memory_address_p (mode, addr)
1369      enum machine_mode mode ATTRIBUTE_UNUSED;
1370      register rtx addr;
1371 {
1372   if (GET_CODE (addr) == ADDRESSOF)
1373     return 1;
1374   
1375   GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
1376   return 0;
1377
1378  win:
1379   return 1;
1380 }
1381
1382 /* Return 1 if OP is a valid memory reference with mode MODE,
1383    including a valid address.
1384
1385    The main use of this function is as a predicate in match_operand
1386    expressions in the machine description.  */
1387
1388 int
1389 memory_operand (op, mode)
1390      register rtx op;
1391      enum machine_mode mode;
1392 {
1393   rtx inner;
1394
1395   if (! reload_completed)
1396     /* Note that no SUBREG is a memory operand before end of reload pass,
1397        because (SUBREG (MEM...)) forces reloading into a register.  */
1398     return GET_CODE (op) == MEM && general_operand (op, mode);
1399
1400   if (mode != VOIDmode && GET_MODE (op) != mode)
1401     return 0;
1402
1403   inner = op;
1404   if (GET_CODE (inner) == SUBREG)
1405     inner = SUBREG_REG (inner);
1406
1407   return (GET_CODE (inner) == MEM && general_operand (op, mode));
1408 }
1409
1410 /* Return 1 if OP is a valid indirect memory reference with mode MODE;
1411    that is, a memory reference whose address is a general_operand.  */
1412
1413 int
1414 indirect_operand (op, mode)
1415      register rtx op;
1416      enum machine_mode mode;
1417 {
1418   /* Before reload, a SUBREG isn't in memory (see memory_operand, above).  */
1419   if (! reload_completed
1420       && GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == MEM)
1421     {
1422       register int offset = SUBREG_WORD (op) * UNITS_PER_WORD;
1423       rtx inner = SUBREG_REG (op);
1424
1425       if (BYTES_BIG_ENDIAN)
1426         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (op)))
1427                    - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (inner))));
1428
1429       if (mode != VOIDmode && GET_MODE (op) != mode)
1430         return 0;
1431
1432       /* The only way that we can have a general_operand as the resulting
1433          address is if OFFSET is zero and the address already is an operand
1434          or if the address is (plus Y (const_int -OFFSET)) and Y is an
1435          operand.  */
1436
1437       return ((offset == 0 && general_operand (XEXP (inner, 0), Pmode))
1438               || (GET_CODE (XEXP (inner, 0)) == PLUS
1439                   && GET_CODE (XEXP (XEXP (inner, 0), 1)) == CONST_INT
1440                   && INTVAL (XEXP (XEXP (inner, 0), 1)) == -offset
1441                   && general_operand (XEXP (XEXP (inner, 0), 0), Pmode)));
1442     }
1443
1444   return (GET_CODE (op) == MEM
1445           && memory_operand (op, mode)
1446           && general_operand (XEXP (op, 0), Pmode));
1447 }
1448
1449 /* Return 1 if this is a comparison operator.  This allows the use of
1450    MATCH_OPERATOR to recognize all the branch insns.  */
1451
1452 int
1453 comparison_operator (op, mode)
1454     register rtx op;
1455     enum machine_mode mode;
1456 {
1457   return ((mode == VOIDmode || GET_MODE (op) == mode)
1458           && GET_RTX_CLASS (GET_CODE (op)) == '<');
1459 }
1460 \f
1461 /* If BODY is an insn body that uses ASM_OPERANDS,
1462    return the number of operands (both input and output) in the insn.
1463    Otherwise return -1.  */
1464
1465 int
1466 asm_noperands (body)
1467      rtx body;
1468 {
1469   switch (GET_CODE (body))
1470     {
1471     case ASM_OPERANDS:
1472       /* No output operands: return number of input operands.  */
1473       return ASM_OPERANDS_INPUT_LENGTH (body);
1474     case SET:
1475       if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1476         /* Single output operand: BODY is (set OUTPUT (asm_operands ...)).  */
1477         return ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body)) + 1;
1478       else
1479         return -1;
1480     case PARALLEL:
1481       if (GET_CODE (XVECEXP (body, 0, 0)) == SET
1482           && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
1483         {
1484           /* Multiple output operands, or 1 output plus some clobbers:
1485              body is [(set OUTPUT (asm_operands ...))... (clobber (reg ...))...].  */
1486           int i;
1487           int n_sets;
1488
1489           /* Count backwards through CLOBBERs to determine number of SETs.  */
1490           for (i = XVECLEN (body, 0); i > 0; i--)
1491             {
1492               if (GET_CODE (XVECEXP (body, 0, i - 1)) == SET)
1493                 break;
1494               if (GET_CODE (XVECEXP (body, 0, i - 1)) != CLOBBER)
1495                 return -1;
1496             }
1497
1498           /* N_SETS is now number of output operands.  */
1499           n_sets = i;
1500
1501           /* Verify that all the SETs we have
1502              came from a single original asm_operands insn
1503              (so that invalid combinations are blocked).  */
1504           for (i = 0; i < n_sets; i++)
1505             {
1506               rtx elt = XVECEXP (body, 0, i);
1507               if (GET_CODE (elt) != SET)
1508                 return -1;
1509               if (GET_CODE (SET_SRC (elt)) != ASM_OPERANDS)
1510                 return -1;
1511               /* If these ASM_OPERANDS rtx's came from different original insns
1512                  then they aren't allowed together.  */
1513               if (ASM_OPERANDS_INPUT_VEC (SET_SRC (elt))
1514                   != ASM_OPERANDS_INPUT_VEC (SET_SRC (XVECEXP (body, 0, 0))))
1515                 return -1;
1516             }
1517           return (ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)))
1518                   + n_sets);
1519         }
1520       else if (GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1521         {
1522           /* 0 outputs, but some clobbers:
1523              body is [(asm_operands ...) (clobber (reg ...))...].  */
1524           int i;
1525
1526           /* Make sure all the other parallel things really are clobbers.  */
1527           for (i = XVECLEN (body, 0) - 1; i > 0; i--)
1528             if (GET_CODE (XVECEXP (body, 0, i)) != CLOBBER)
1529               return -1;
1530
1531           return ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
1532         }
1533       else
1534         return -1;
1535     default:
1536       return -1;
1537     }
1538 }
1539
1540 /* Assuming BODY is an insn body that uses ASM_OPERANDS,
1541    copy its operands (both input and output) into the vector OPERANDS,
1542    the locations of the operands within the insn into the vector OPERAND_LOCS,
1543    and the constraints for the operands into CONSTRAINTS.
1544    Write the modes of the operands into MODES.
1545    Return the assembler-template.
1546
1547    If MODES, OPERAND_LOCS, CONSTRAINTS or OPERANDS is 0,
1548    we don't store that info.  */
1549
1550 const char *
1551 decode_asm_operands (body, operands, operand_locs, constraints, modes)
1552      rtx body;
1553      rtx *operands;
1554      rtx **operand_locs;
1555      const char **constraints;
1556      enum machine_mode *modes;
1557 {
1558   register int i;
1559   int noperands;
1560   const char *template = 0;
1561
1562   if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1563     {
1564       rtx asmop = SET_SRC (body);
1565       /* Single output operand: BODY is (set OUTPUT (asm_operands ....)).  */
1566
1567       noperands = ASM_OPERANDS_INPUT_LENGTH (asmop) + 1;
1568
1569       for (i = 1; i < noperands; i++)
1570         {
1571           if (operand_locs)
1572             operand_locs[i] = &ASM_OPERANDS_INPUT (asmop, i - 1);
1573           if (operands)
1574             operands[i] = ASM_OPERANDS_INPUT (asmop, i - 1);
1575           if (constraints)
1576             constraints[i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i - 1);
1577           if (modes)
1578             modes[i] = ASM_OPERANDS_INPUT_MODE (asmop, i - 1);
1579         }
1580
1581       /* The output is in the SET.
1582          Its constraint is in the ASM_OPERANDS itself.  */
1583       if (operands)
1584         operands[0] = SET_DEST (body);
1585       if (operand_locs)
1586         operand_locs[0] = &SET_DEST (body);
1587       if (constraints)
1588         constraints[0] = ASM_OPERANDS_OUTPUT_CONSTRAINT (asmop);
1589       if (modes)
1590         modes[0] = GET_MODE (SET_DEST (body));
1591       template = ASM_OPERANDS_TEMPLATE (asmop);
1592     }
1593   else if (GET_CODE (body) == ASM_OPERANDS)
1594     {
1595       rtx asmop = body;
1596       /* No output operands: BODY is (asm_operands ....).  */
1597
1598       noperands = ASM_OPERANDS_INPUT_LENGTH (asmop);
1599
1600       /* The input operands are found in the 1st element vector.  */
1601       /* Constraints for inputs are in the 2nd element vector.  */
1602       for (i = 0; i < noperands; i++)
1603         {
1604           if (operand_locs)
1605             operand_locs[i] = &ASM_OPERANDS_INPUT (asmop, i);
1606           if (operands)
1607             operands[i] = ASM_OPERANDS_INPUT (asmop, i);
1608           if (constraints)
1609             constraints[i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1610           if (modes)
1611             modes[i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1612         }
1613       template = ASM_OPERANDS_TEMPLATE (asmop);
1614     }
1615   else if (GET_CODE (body) == PARALLEL
1616            && GET_CODE (XVECEXP (body, 0, 0)) == SET)
1617     {
1618       rtx asmop = SET_SRC (XVECEXP (body, 0, 0));
1619       int nparallel = XVECLEN (body, 0); /* Includes CLOBBERs.  */
1620       int nin = ASM_OPERANDS_INPUT_LENGTH (asmop);
1621       int nout = 0;             /* Does not include CLOBBERs.  */
1622
1623       /* At least one output, plus some CLOBBERs.  */
1624
1625       /* The outputs are in the SETs.
1626          Their constraints are in the ASM_OPERANDS itself.  */
1627       for (i = 0; i < nparallel; i++)
1628         {
1629           if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
1630             break;              /* Past last SET */
1631           
1632           if (operands)
1633             operands[i] = SET_DEST (XVECEXP (body, 0, i));
1634           if (operand_locs)
1635             operand_locs[i] = &SET_DEST (XVECEXP (body, 0, i));
1636           if (constraints)
1637             constraints[i] = XSTR (SET_SRC (XVECEXP (body, 0, i)), 1);
1638           if (modes)
1639             modes[i] = GET_MODE (SET_DEST (XVECEXP (body, 0, i)));
1640           nout++;
1641         }
1642
1643       for (i = 0; i < nin; i++)
1644         {
1645           if (operand_locs)
1646             operand_locs[i + nout] = &ASM_OPERANDS_INPUT (asmop, i);
1647           if (operands)
1648             operands[i + nout] = ASM_OPERANDS_INPUT (asmop, i);
1649           if (constraints)
1650             constraints[i + nout] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1651           if (modes)
1652             modes[i + nout] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1653         }
1654
1655       template = ASM_OPERANDS_TEMPLATE (asmop);
1656     }
1657   else if (GET_CODE (body) == PARALLEL
1658            && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1659     {
1660       /* No outputs, but some CLOBBERs.  */
1661
1662       rtx asmop = XVECEXP (body, 0, 0);
1663       int nin = ASM_OPERANDS_INPUT_LENGTH (asmop);
1664
1665       for (i = 0; i < nin; i++)
1666         {
1667           if (operand_locs)
1668             operand_locs[i] = &ASM_OPERANDS_INPUT (asmop, i);
1669           if (operands)
1670             operands[i] = ASM_OPERANDS_INPUT (asmop, i);
1671           if (constraints)
1672             constraints[i] = ASM_OPERANDS_INPUT_CONSTRAINT (asmop, i);
1673           if (modes)
1674             modes[i] = ASM_OPERANDS_INPUT_MODE (asmop, i);
1675         }
1676
1677       template = ASM_OPERANDS_TEMPLATE (asmop);
1678     }
1679
1680   return template;
1681 }
1682
1683 /* Check if an asm_operand matches it's constraints. 
1684    Return > 0 if ok, = 0 if bad, < 0 if inconclusive.  */
1685
1686 int
1687 asm_operand_ok (op, constraint)
1688      rtx op;
1689      const char *constraint;
1690 {
1691   int result = 0;
1692
1693   /* Use constrain_operands after reload.  */
1694   if (reload_completed)
1695     abort ();
1696
1697   while (*constraint)
1698     {
1699       char c = *constraint++;
1700       switch (c)
1701         {
1702         case '=':
1703         case '+':
1704         case '*':
1705         case '%':
1706         case '?':
1707         case '!':
1708         case '#':
1709         case '&':
1710         case ',':
1711           break;
1712
1713         case '0': case '1': case '2': case '3': case '4':
1714         case '5': case '6': case '7': case '8': case '9':
1715           /* For best results, our caller should have given us the
1716              proper matching constraint, but we can't actually fail
1717              the check if they didn't.  Indicate that results are
1718              inconclusive.  */
1719           result = -1;
1720           break;
1721
1722         case 'p':
1723           if (address_operand (op, VOIDmode))
1724             return 1;
1725           break;
1726
1727         case 'm':
1728         case 'V': /* non-offsettable */
1729           if (memory_operand (op, VOIDmode))
1730             return 1;
1731           break;
1732
1733         case 'o': /* offsettable */
1734           if (offsettable_nonstrict_memref_p (op))
1735             return 1;
1736           break;
1737
1738         case '<':
1739           /* ??? Before flow, auto inc/dec insns are not supposed to exist,
1740              excepting those that expand_call created.  Further, on some
1741              machines which do not have generalized auto inc/dec, an inc/dec
1742              is not a memory_operand.
1743
1744              Match any memory and hope things are resolved after reload.  */
1745
1746           if (GET_CODE (op) == MEM
1747               && (1
1748                   || GET_CODE (XEXP (op, 0)) == PRE_DEC
1749                   || GET_CODE (XEXP (op, 0)) == POST_DEC))
1750             return 1;
1751           break;
1752
1753         case '>':
1754           if (GET_CODE (op) == MEM
1755               && (1
1756                   || GET_CODE (XEXP (op, 0)) == PRE_INC
1757                   || GET_CODE (XEXP (op, 0)) == POST_INC))
1758             return 1;
1759           break;
1760
1761         case 'E':
1762 #ifndef REAL_ARITHMETIC
1763           /* Match any floating double constant, but only if
1764              we can examine the bits of it reliably.  */
1765           if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
1766                || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
1767               && GET_MODE (op) != VOIDmode && ! flag_pretend_float)
1768             break;
1769 #endif
1770           /* FALLTHRU */
1771
1772         case 'F':
1773           if (GET_CODE (op) == CONST_DOUBLE)
1774             return 1;
1775           break;
1776
1777         case 'G':
1778           if (GET_CODE (op) == CONST_DOUBLE
1779               && CONST_DOUBLE_OK_FOR_LETTER_P (op, 'G'))
1780             return 1;
1781           break;
1782         case 'H':
1783           if (GET_CODE (op) == CONST_DOUBLE
1784               && CONST_DOUBLE_OK_FOR_LETTER_P (op, 'H'))
1785             return 1;
1786           break;
1787
1788         case 's':
1789           if (GET_CODE (op) == CONST_INT
1790               || (GET_CODE (op) == CONST_DOUBLE
1791                   && GET_MODE (op) == VOIDmode))
1792             break;
1793           /* FALLTHRU */
1794
1795         case 'i':
1796           if (CONSTANT_P (op)
1797 #ifdef LEGITIMATE_PIC_OPERAND_P
1798               && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1799 #endif
1800               )
1801             return 1;
1802           break;
1803
1804         case 'n':
1805           if (GET_CODE (op) == CONST_INT
1806               || (GET_CODE (op) == CONST_DOUBLE
1807                   && GET_MODE (op) == VOIDmode))
1808             return 1;
1809           break;
1810
1811         case 'I':
1812           if (GET_CODE (op) == CONST_INT
1813               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'))
1814             return 1;
1815           break;
1816         case 'J':
1817           if (GET_CODE (op) == CONST_INT
1818               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'J'))
1819             return 1;
1820           break;
1821         case 'K':
1822           if (GET_CODE (op) == CONST_INT
1823               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'K'))
1824             return 1;
1825           break;
1826         case 'L':
1827           if (GET_CODE (op) == CONST_INT
1828               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'))
1829             return 1;
1830           break;
1831         case 'M':
1832           if (GET_CODE (op) == CONST_INT
1833               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'M'))
1834             return 1;
1835           break;
1836         case 'N':
1837           if (GET_CODE (op) == CONST_INT
1838               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'N'))
1839             return 1;
1840           break;
1841         case 'O':
1842           if (GET_CODE (op) == CONST_INT
1843               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'O'))
1844             return 1;
1845           break;
1846         case 'P':
1847           if (GET_CODE (op) == CONST_INT
1848               && CONST_OK_FOR_LETTER_P (INTVAL (op), 'P'))
1849             return 1;
1850           break;
1851
1852         case 'X':
1853           return 1;
1854
1855         case 'g':
1856           if (general_operand (op, VOIDmode))
1857             return 1;
1858           break;
1859
1860         default:
1861           /* For all other letters, we first check for a register class,
1862              otherwise it is an EXTRA_CONSTRAINT.  */
1863           if (REG_CLASS_FROM_LETTER (c) != NO_REGS)
1864             {
1865             case 'r':
1866               if (GET_MODE (op) == BLKmode)
1867                 break;
1868               if (register_operand (op, VOIDmode))
1869                 return 1;
1870             }
1871 #ifdef EXTRA_CONSTRAINT
1872           if (EXTRA_CONSTRAINT (op, c))
1873             return 1;
1874 #endif
1875           break;
1876         }
1877     }
1878
1879   return result;
1880 }
1881 \f
1882 /* Given an rtx *P, if it is a sum containing an integer constant term,
1883    return the location (type rtx *) of the pointer to that constant term.
1884    Otherwise, return a null pointer.  */
1885
1886 static rtx *
1887 find_constant_term_loc (p)
1888      rtx *p;
1889 {
1890   register rtx *tem;
1891   register enum rtx_code code = GET_CODE (*p);
1892
1893   /* If *P IS such a constant term, P is its location.  */
1894
1895   if (code == CONST_INT || code == SYMBOL_REF || code == LABEL_REF
1896       || code == CONST)
1897     return p;
1898
1899   /* Otherwise, if not a sum, it has no constant term.  */
1900
1901   if (GET_CODE (*p) != PLUS)
1902     return 0;
1903
1904   /* If one of the summands is constant, return its location.  */
1905
1906   if (XEXP (*p, 0) && CONSTANT_P (XEXP (*p, 0))
1907       && XEXP (*p, 1) && CONSTANT_P (XEXP (*p, 1)))
1908     return p;
1909
1910   /* Otherwise, check each summand for containing a constant term.  */
1911
1912   if (XEXP (*p, 0) != 0)
1913     {
1914       tem = find_constant_term_loc (&XEXP (*p, 0));
1915       if (tem != 0)
1916         return tem;
1917     }
1918
1919   if (XEXP (*p, 1) != 0)
1920     {
1921       tem = find_constant_term_loc (&XEXP (*p, 1));
1922       if (tem != 0)
1923         return tem;
1924     }
1925
1926   return 0;
1927 }
1928 \f
1929 /* Return 1 if OP is a memory reference
1930    whose address contains no side effects
1931    and remains valid after the addition
1932    of a positive integer less than the
1933    size of the object being referenced.
1934
1935    We assume that the original address is valid and do not check it.
1936
1937    This uses strict_memory_address_p as a subroutine, so
1938    don't use it before reload.  */
1939
1940 int
1941 offsettable_memref_p (op)
1942      rtx op;
1943 {
1944   return ((GET_CODE (op) == MEM)
1945           && offsettable_address_p (1, GET_MODE (op), XEXP (op, 0)));
1946 }
1947
1948 /* Similar, but don't require a strictly valid mem ref:
1949    consider pseudo-regs valid as index or base regs.  */
1950
1951 int
1952 offsettable_nonstrict_memref_p (op)
1953      rtx op;
1954 {
1955   return ((GET_CODE (op) == MEM)
1956           && offsettable_address_p (0, GET_MODE (op), XEXP (op, 0)));
1957 }
1958
1959 /* Return 1 if Y is a memory address which contains no side effects
1960    and would remain valid after the addition of a positive integer
1961    less than the size of that mode.
1962
1963    We assume that the original address is valid and do not check it.
1964    We do check that it is valid for narrower modes.
1965
1966    If STRICTP is nonzero, we require a strictly valid address,
1967    for the sake of use in reload.c.  */
1968
1969 int
1970 offsettable_address_p (strictp, mode, y)
1971      int strictp;
1972      enum machine_mode mode;
1973      register rtx y;
1974 {
1975   register enum rtx_code ycode = GET_CODE (y);
1976   register rtx z;
1977   rtx y1 = y;
1978   rtx *y2;
1979   int (*addressp) PARAMS ((enum machine_mode, rtx)) =
1980     (strictp ? strict_memory_address_p : memory_address_p);
1981   unsigned int mode_sz = GET_MODE_SIZE (mode);
1982
1983   if (CONSTANT_ADDRESS_P (y))
1984     return 1;
1985
1986   /* Adjusting an offsettable address involves changing to a narrower mode.
1987      Make sure that's OK.  */
1988
1989   if (mode_dependent_address_p (y))
1990     return 0;
1991
1992   /* ??? How much offset does an offsettable BLKmode reference need?
1993      Clearly that depends on the situation in which it's being used.
1994      However, the current situation in which we test 0xffffffff is
1995      less than ideal.  Caveat user.  */
1996   if (mode_sz == 0)
1997     mode_sz = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
1998
1999   /* If the expression contains a constant term,
2000      see if it remains valid when max possible offset is added.  */
2001
2002   if ((ycode == PLUS) && (y2 = find_constant_term_loc (&y1)))
2003     {
2004       int good;
2005
2006       y1 = *y2;
2007       *y2 = plus_constant (*y2, mode_sz - 1);
2008       /* Use QImode because an odd displacement may be automatically invalid
2009          for any wider mode.  But it should be valid for a single byte.  */
2010       good = (*addressp) (QImode, y);
2011
2012       /* In any case, restore old contents of memory.  */
2013       *y2 = y1;
2014       return good;
2015     }
2016
2017   if (GET_RTX_CLASS (ycode) == 'a')
2018     return 0;
2019
2020   /* The offset added here is chosen as the maximum offset that
2021      any instruction could need to add when operating on something
2022      of the specified mode.  We assume that if Y and Y+c are
2023      valid addresses then so is Y+d for all 0<d<c.  */
2024
2025   z = plus_constant_for_output (y, mode_sz - 1);
2026
2027   /* Use QImode because an odd displacement may be automatically invalid
2028      for any wider mode.  But it should be valid for a single byte.  */
2029   return (*addressp) (QImode, z);
2030 }
2031
2032 /* Return 1 if ADDR is an address-expression whose effect depends
2033    on the mode of the memory reference it is used in.
2034
2035    Autoincrement addressing is a typical example of mode-dependence
2036    because the amount of the increment depends on the mode.  */
2037
2038 int
2039 mode_dependent_address_p (addr)
2040   rtx addr ATTRIBUTE_UNUSED; /* Maybe used in GO_IF_MODE_DEPENDENT_ADDRESS. */
2041 {
2042   GO_IF_MODE_DEPENDENT_ADDRESS (addr, win);
2043   return 0;
2044   /* Label `win' might (not) be used via GO_IF_MODE_DEPENDENT_ADDRESS. */
2045  win: ATTRIBUTE_UNUSED_LABEL
2046   return 1;
2047 }
2048
2049 /* Return 1 if OP is a general operand
2050    other than a memory ref with a mode dependent address.  */
2051
2052 int
2053 mode_independent_operand (op, mode)
2054      enum machine_mode mode;
2055      rtx op;
2056 {
2057   rtx addr;
2058
2059   if (! general_operand (op, mode))
2060     return 0;
2061
2062   if (GET_CODE (op) != MEM)
2063     return 1;
2064
2065   addr = XEXP (op, 0);
2066   GO_IF_MODE_DEPENDENT_ADDRESS (addr, lose);
2067   return 1;
2068   /* Label `lose' might (not) be used via GO_IF_MODE_DEPENDENT_ADDRESS. */
2069  lose: ATTRIBUTE_UNUSED_LABEL
2070   return 0;
2071 }
2072
2073 /* Given an operand OP that is a valid memory reference which
2074    satisfies offsettable_memref_p, return a new memory reference whose
2075    address has been adjusted by OFFSET.  OFFSET should be positive and
2076    less than the size of the object referenced.  */
2077
2078 rtx
2079 adj_offsettable_operand (op, offset)
2080      rtx op;
2081      int offset;
2082 {
2083   register enum rtx_code code = GET_CODE (op);
2084
2085   if (code == MEM) 
2086     {
2087       register rtx y = XEXP (op, 0);
2088       register rtx new;
2089
2090       if (CONSTANT_ADDRESS_P (y))
2091         {
2092           new = gen_rtx_MEM (GET_MODE (op),
2093                              plus_constant_for_output (y, offset));
2094           MEM_COPY_ATTRIBUTES (new, op);
2095           return new;
2096         }
2097
2098       if (GET_CODE (y) == PLUS)
2099         {
2100           rtx z = y;
2101           register rtx *const_loc;
2102
2103           op = copy_rtx (op);
2104           z = XEXP (op, 0);
2105           const_loc = find_constant_term_loc (&z);
2106           if (const_loc)
2107             {
2108               *const_loc = plus_constant_for_output (*const_loc, offset);
2109               return op;
2110             }
2111         }
2112
2113       new = gen_rtx_MEM (GET_MODE (op), plus_constant_for_output (y, offset));
2114       MEM_COPY_ATTRIBUTES (new, op);
2115       return new;
2116     }
2117   abort ();
2118 }
2119 \f
2120 /* Like extract_insn, but save insn extracted and don't extract again, when
2121    called again for the same insn expecting that recog_data still contain the
2122    valid information.  This is used primary by gen_attr infrastructure that
2123    often does extract insn again and again.  */
2124 void
2125 extract_insn_cached (insn)
2126      rtx insn;
2127 {
2128   if (recog_data.insn == insn && INSN_CODE (insn) >= 0)
2129     return;
2130   extract_insn (insn);
2131   recog_data.insn = insn;
2132 }
2133 /* Do cached extract_insn, constrain_operand and complain about failures.
2134    Used by insn_attrtab.  */
2135 void
2136 extract_constrain_insn_cached (insn)
2137      rtx insn;
2138 {
2139   extract_insn_cached (insn);
2140   if (which_alternative == -1
2141       && !constrain_operands (reload_completed))
2142     fatal_insn_not_found (insn);
2143 }
2144 /* Do cached constrain_operand and complain about failures.  */
2145 int
2146 constrain_operands_cached (strict)
2147         int strict;
2148 {
2149   if (which_alternative == -1)
2150     return constrain_operands (strict);
2151   else
2152     return 1;
2153 }
2154 \f
2155 /* Analyze INSN and fill in recog_data.  */
2156
2157 void
2158 extract_insn (insn)
2159      rtx insn;
2160 {
2161   int i;
2162   int icode;
2163   int noperands;
2164   rtx body = PATTERN (insn);
2165
2166   recog_data.insn = NULL;
2167   recog_data.n_operands = 0;
2168   recog_data.n_alternatives = 0;
2169   recog_data.n_dups = 0;
2170   which_alternative = -1;
2171
2172   switch (GET_CODE (body))
2173     {
2174     case USE:
2175     case CLOBBER:
2176     case ASM_INPUT:
2177     case ADDR_VEC:
2178     case ADDR_DIFF_VEC:
2179       return;
2180
2181     case SET:
2182       if (GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
2183         goto asm_insn;
2184       else
2185         goto normal_insn;
2186     case PARALLEL:
2187       if ((GET_CODE (XVECEXP (body, 0, 0)) == SET
2188            && GET_CODE (SET_SRC (XVECEXP (body, 0, 0))) == ASM_OPERANDS)
2189           || GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
2190         goto asm_insn;
2191       else
2192         goto normal_insn;
2193     case ASM_OPERANDS:
2194     asm_insn:
2195       recog_data.n_operands = noperands = asm_noperands (body);
2196       if (noperands >= 0)
2197         {
2198           /* This insn is an `asm' with operands.  */
2199
2200           /* expand_asm_operands makes sure there aren't too many operands.  */
2201           if (noperands > MAX_RECOG_OPERANDS)
2202             abort ();
2203
2204           /* Now get the operand values and constraints out of the insn.  */
2205           decode_asm_operands (body, recog_data.operand,
2206                                recog_data.operand_loc,
2207                                recog_data.constraints,
2208                                recog_data.operand_mode);
2209           if (noperands > 0)
2210             {
2211               const char *p =  recog_data.constraints[0];
2212               recog_data.n_alternatives = 1;
2213               while (*p)
2214                 recog_data.n_alternatives += (*p++ == ',');
2215             }
2216           break;
2217         }
2218       fatal_insn_not_found (insn);
2219
2220     default:
2221     normal_insn:
2222       /* Ordinary insn: recognize it, get the operands via insn_extract
2223          and get the constraints.  */
2224
2225       icode = recog_memoized (insn);
2226       if (icode < 0)
2227         fatal_insn_not_found (insn);
2228
2229       recog_data.n_operands = noperands = insn_data[icode].n_operands;
2230       recog_data.n_alternatives = insn_data[icode].n_alternatives;
2231       recog_data.n_dups = insn_data[icode].n_dups;
2232
2233       insn_extract (insn);
2234
2235       for (i = 0; i < noperands; i++)
2236         {
2237           recog_data.constraints[i] = insn_data[icode].operand[i].constraint;
2238           recog_data.operand_mode[i] = insn_data[icode].operand[i].mode;
2239           /* VOIDmode match_operands gets mode from their real operand.  */
2240           if (recog_data.operand_mode[i] == VOIDmode)
2241             recog_data.operand_mode[i] = GET_MODE (recog_data.operand[i]);
2242         }
2243     }
2244   for (i = 0; i < noperands; i++)
2245     recog_data.operand_type[i]
2246       = (recog_data.constraints[i][0] == '=' ? OP_OUT
2247          : recog_data.constraints[i][0] == '+' ? OP_INOUT
2248          : OP_IN);
2249
2250   if (recog_data.n_alternatives > MAX_RECOG_ALTERNATIVES)
2251     abort ();
2252 }
2253
2254 /* After calling extract_insn, you can use this function to extract some
2255    information from the constraint strings into a more usable form.
2256    The collected data is stored in recog_op_alt.  */
2257 void
2258 preprocess_constraints ()
2259 {
2260   int i;
2261
2262   memset (recog_op_alt, 0, sizeof recog_op_alt);
2263   for (i = 0; i < recog_data.n_operands; i++)
2264     {
2265       int j;
2266       struct operand_alternative *op_alt;
2267       const char *p = recog_data.constraints[i];
2268
2269       op_alt = recog_op_alt[i];
2270
2271       for (j = 0; j < recog_data.n_alternatives; j++)
2272         {
2273           op_alt[j].class = NO_REGS;
2274           op_alt[j].constraint = p;
2275           op_alt[j].matches = -1;
2276           op_alt[j].matched = -1;
2277
2278           if (*p == '\0' || *p == ',')
2279             {
2280               op_alt[j].anything_ok = 1;
2281               continue;
2282             }
2283
2284           for (;;)
2285             {
2286               char c = *p++;
2287               if (c == '#')
2288                 do
2289                   c = *p++;
2290                 while (c != ',' && c != '\0');
2291               if (c == ',' || c == '\0')
2292                 break;
2293
2294               switch (c)
2295                 {
2296                 case '=': case '+': case '*': case '%':
2297                 case 'E': case 'F': case 'G': case 'H':
2298                 case 's': case 'i': case 'n':
2299                 case 'I': case 'J': case 'K': case 'L':
2300                 case 'M': case 'N': case 'O': case 'P':
2301                   /* These don't say anything we care about.  */
2302                   break;
2303
2304                 case '?':
2305                   op_alt[j].reject += 6;
2306                   break;
2307                 case '!':
2308                   op_alt[j].reject += 600;
2309                   break;
2310                 case '&':
2311                   op_alt[j].earlyclobber = 1;
2312                   break;                  
2313
2314                 case '0': case '1': case '2': case '3': case '4':
2315                 case '5': case '6': case '7': case '8': case '9':
2316                   op_alt[j].matches = c - '0';
2317                   recog_op_alt[op_alt[j].matches][j].matched = i;
2318                   break;
2319
2320                 case 'm':
2321                   op_alt[j].memory_ok = 1;
2322                   break;
2323                 case '<':
2324                   op_alt[j].decmem_ok = 1;
2325                   break;
2326                 case '>':
2327                   op_alt[j].incmem_ok = 1;
2328                   break;
2329                 case 'V':
2330                   op_alt[j].nonoffmem_ok = 1;
2331                   break;
2332                 case 'o':
2333                   op_alt[j].offmem_ok = 1;
2334                   break;
2335                 case 'X':
2336                   op_alt[j].anything_ok = 1;
2337                   break;
2338
2339                 case 'p':
2340                   op_alt[j].is_address = 1;
2341                   op_alt[j].class = reg_class_subunion[(int) op_alt[j].class][(int) BASE_REG_CLASS];
2342                   break;
2343
2344                 case 'g': case 'r':
2345                   op_alt[j].class = reg_class_subunion[(int) op_alt[j].class][(int) GENERAL_REGS];
2346                   break;
2347
2348                 default:
2349                   op_alt[j].class = reg_class_subunion[(int) op_alt[j].class][(int) REG_CLASS_FROM_LETTER ((unsigned char)c)];
2350                   break;
2351                 }
2352             }
2353         }
2354     }
2355 }
2356  
2357 /* Check the operands of an insn against the insn's operand constraints
2358    and return 1 if they are valid.
2359    The information about the insn's operands, constraints, operand modes
2360    etc. is obtained from the global variables set up by extract_insn.
2361
2362    WHICH_ALTERNATIVE is set to a number which indicates which
2363    alternative of constraints was matched: 0 for the first alternative,
2364    1 for the next, etc.
2365
2366    In addition, when two operands are match
2367    and it happens that the output operand is (reg) while the
2368    input operand is --(reg) or ++(reg) (a pre-inc or pre-dec),
2369    make the output operand look like the input.
2370    This is because the output operand is the one the template will print.
2371
2372    This is used in final, just before printing the assembler code and by
2373    the routines that determine an insn's attribute.
2374
2375    If STRICT is a positive non-zero value, it means that we have been
2376    called after reload has been completed.  In that case, we must
2377    do all checks strictly.  If it is zero, it means that we have been called
2378    before reload has completed.  In that case, we first try to see if we can
2379    find an alternative that matches strictly.  If not, we try again, this
2380    time assuming that reload will fix up the insn.  This provides a "best
2381    guess" for the alternative and is used to compute attributes of insns prior
2382    to reload.  A negative value of STRICT is used for this internal call.  */
2383
2384 struct funny_match
2385 {
2386   int this, other;
2387 };
2388
2389 int
2390 constrain_operands (strict)
2391      int strict;
2392 {
2393   const char *constraints[MAX_RECOG_OPERANDS];
2394   int matching_operands[MAX_RECOG_OPERANDS];
2395   int earlyclobber[MAX_RECOG_OPERANDS];
2396   register int c;
2397
2398   struct funny_match funny_match[MAX_RECOG_OPERANDS];
2399   int funny_match_index;
2400
2401   which_alternative = 0;
2402   if (recog_data.n_operands == 0 || recog_data.n_alternatives == 0)
2403     return 1;
2404
2405   for (c = 0; c < recog_data.n_operands; c++)
2406     {
2407       constraints[c] = recog_data.constraints[c];
2408       matching_operands[c] = -1;
2409     }
2410
2411   do
2412     {
2413       register int opno;
2414       int lose = 0;
2415       funny_match_index = 0;
2416
2417       for (opno = 0; opno < recog_data.n_operands; opno++)
2418         {
2419           register rtx op = recog_data.operand[opno];
2420           enum machine_mode mode = GET_MODE (op);
2421           register const char *p = constraints[opno];
2422           int offset = 0;
2423           int win = 0;
2424           int val;
2425
2426           earlyclobber[opno] = 0;
2427
2428           /* A unary operator may be accepted by the predicate, but it
2429              is irrelevant for matching constraints.  */
2430           if (GET_RTX_CLASS (GET_CODE (op)) == '1')
2431             op = XEXP (op, 0);
2432
2433           if (GET_CODE (op) == SUBREG)
2434             {
2435               if (GET_CODE (SUBREG_REG (op)) == REG
2436                   && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
2437                 offset = SUBREG_WORD (op);
2438               op = SUBREG_REG (op);
2439             }
2440
2441           /* An empty constraint or empty alternative
2442              allows anything which matched the pattern.  */
2443           if (*p == 0 || *p == ',')
2444             win = 1;
2445
2446           while (*p && (c = *p++) != ',')
2447             switch (c)
2448               {
2449               case '?':  case '!': case '*':  case '%':
2450               case '=':  case '+':
2451                 break;
2452
2453               case '#':
2454                 /* Ignore rest of this alternative as far as
2455                    constraint checking is concerned.  */
2456                 while (*p && *p != ',')
2457                   p++;
2458                 break;
2459
2460               case '&':
2461                 earlyclobber[opno] = 1;
2462                 break;
2463
2464               case '0':  case '1':  case '2':  case '3':  case '4':
2465               case '5':  case '6':  case '7':  case '8':  case '9':
2466
2467                 /* This operand must be the same as a previous one.
2468                    This kind of constraint is used for instructions such
2469                    as add when they take only two operands.
2470
2471                    Note that the lower-numbered operand is passed first.
2472
2473                    If we are not testing strictly, assume that this constraint
2474                    will be satisfied.  */
2475                 if (strict < 0)
2476                   val = 1;
2477                 else
2478                   {
2479                     rtx op1 = recog_data.operand[c - '0'];
2480                     rtx op2 = recog_data.operand[opno];
2481
2482                     /* A unary operator may be accepted by the predicate,
2483                        but it is irrelevant for matching constraints.  */
2484                     if (GET_RTX_CLASS (GET_CODE (op1)) == '1')
2485                       op1 = XEXP (op1, 0);
2486                     if (GET_RTX_CLASS (GET_CODE (op2)) == '1')
2487                       op2 = XEXP (op2, 0);
2488
2489                     val = operands_match_p (op1, op2);
2490                   }
2491
2492                 matching_operands[opno] = c - '0';
2493                 matching_operands[c - '0'] = opno;
2494
2495                 if (val != 0)
2496                   win = 1;
2497                 /* If output is *x and input is *--x,
2498                    arrange later to change the output to *--x as well,
2499                    since the output op is the one that will be printed.  */
2500                 if (val == 2 && strict > 0)
2501                   {
2502                     funny_match[funny_match_index].this = opno;
2503                     funny_match[funny_match_index++].other = c - '0';
2504                   }
2505                 break;
2506
2507               case 'p':
2508                 /* p is used for address_operands.  When we are called by
2509                    gen_reload, no one will have checked that the address is
2510                    strictly valid, i.e., that all pseudos requiring hard regs
2511                    have gotten them.  */
2512                 if (strict <= 0
2513                     || (strict_memory_address_p (recog_data.operand_mode[opno],
2514                                                  op)))
2515                   win = 1;
2516                 break;
2517
2518                 /* No need to check general_operand again;
2519                    it was done in insn-recog.c.  */
2520               case 'g':
2521                 /* Anything goes unless it is a REG and really has a hard reg
2522                    but the hard reg is not in the class GENERAL_REGS.  */
2523                 if (strict < 0
2524                     || GENERAL_REGS == ALL_REGS
2525                     || GET_CODE (op) != REG
2526                     || (reload_in_progress
2527                         && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2528                     || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
2529                   win = 1;
2530                 break;
2531
2532               case 'X':
2533                 /* This is used for a MATCH_SCRATCH in the cases when
2534                    we don't actually need anything.  So anything goes
2535                    any time.  */
2536                 win = 1;
2537                 break;
2538
2539               case 'm':
2540                 if (GET_CODE (op) == MEM
2541                     /* Before reload, accept what reload can turn into mem.  */
2542                     || (strict < 0 && CONSTANT_P (op))
2543                     /* During reload, accept a pseudo  */
2544                     || (reload_in_progress && GET_CODE (op) == REG
2545                         && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2546                   win = 1;
2547                 break;
2548
2549               case '<':
2550                 if (GET_CODE (op) == MEM
2551                     && (GET_CODE (XEXP (op, 0)) == PRE_DEC
2552                         || GET_CODE (XEXP (op, 0)) == POST_DEC))
2553                   win = 1;
2554                 break;
2555
2556               case '>':
2557                 if (GET_CODE (op) == MEM
2558                     && (GET_CODE (XEXP (op, 0)) == PRE_INC
2559                         || GET_CODE (XEXP (op, 0)) == POST_INC))
2560                   win = 1;
2561                 break;
2562
2563               case 'E':
2564 #ifndef REAL_ARITHMETIC
2565                 /* Match any CONST_DOUBLE, but only if
2566                    we can examine the bits of it reliably.  */
2567                 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
2568                      || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
2569                     && GET_MODE (op) != VOIDmode && ! flag_pretend_float)
2570                   break;
2571 #endif
2572                 if (GET_CODE (op) == CONST_DOUBLE)
2573                   win = 1;
2574                 break;
2575
2576               case 'F':
2577                 if (GET_CODE (op) == CONST_DOUBLE)
2578                   win = 1;
2579                 break;
2580
2581               case 'G':
2582               case 'H':
2583                 if (GET_CODE (op) == CONST_DOUBLE
2584                     && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
2585                   win = 1;
2586                 break;
2587
2588               case 's':
2589                 if (GET_CODE (op) == CONST_INT
2590                     || (GET_CODE (op) == CONST_DOUBLE
2591                         && GET_MODE (op) == VOIDmode))
2592                   break;
2593               case 'i':
2594                 if (CONSTANT_P (op))
2595                   win = 1;
2596                 break;
2597
2598               case 'n':
2599                 if (GET_CODE (op) == CONST_INT
2600                     || (GET_CODE (op) == CONST_DOUBLE
2601                         && GET_MODE (op) == VOIDmode))
2602                   win = 1;
2603                 break;
2604
2605               case 'I':
2606               case 'J':
2607               case 'K':
2608               case 'L':
2609               case 'M':
2610               case 'N':
2611               case 'O':
2612               case 'P':
2613                 if (GET_CODE (op) == CONST_INT
2614                     && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
2615                   win = 1;
2616                 break;
2617
2618               case 'V':
2619                 if (GET_CODE (op) == MEM
2620                     && ((strict > 0 && ! offsettable_memref_p (op))
2621                         || (strict < 0
2622                             && !(CONSTANT_P (op) || GET_CODE (op) == MEM))
2623                         || (reload_in_progress
2624                             && !(GET_CODE (op) == REG
2625                                  && REGNO (op) >= FIRST_PSEUDO_REGISTER))))
2626                   win = 1;
2627                 break;
2628
2629               case 'o':
2630                 if ((strict > 0 && offsettable_memref_p (op))
2631                     || (strict == 0 && offsettable_nonstrict_memref_p (op))
2632                     /* Before reload, accept what reload can handle.  */
2633                     || (strict < 0
2634                         && (CONSTANT_P (op) || GET_CODE (op) == MEM))
2635                     /* During reload, accept a pseudo  */
2636                     || (reload_in_progress && GET_CODE (op) == REG
2637                         && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2638                   win = 1;
2639                 break;
2640
2641               default:
2642                 {
2643                   enum reg_class class;
2644
2645                   class = (c == 'r' ? GENERAL_REGS : REG_CLASS_FROM_LETTER (c));
2646                   if (class != NO_REGS)
2647                     {
2648                       if (strict < 0
2649                           || (strict == 0
2650                               && GET_CODE (op) == REG
2651                               && REGNO (op) >= FIRST_PSEUDO_REGISTER)
2652                           || (strict == 0 && GET_CODE (op) == SCRATCH)
2653                           || (GET_CODE (op) == REG
2654                               && reg_fits_class_p (op, class, offset, mode)))
2655                         win = 1;
2656                     }
2657 #ifdef EXTRA_CONSTRAINT
2658                   else if (EXTRA_CONSTRAINT (op, c))
2659                     win = 1;
2660 #endif
2661                   break;
2662                 }
2663               }
2664
2665           constraints[opno] = p;
2666           /* If this operand did not win somehow,
2667              this alternative loses.  */
2668           if (! win)
2669             lose = 1;
2670         }
2671       /* This alternative won; the operands are ok.
2672          Change whichever operands this alternative says to change.  */
2673       if (! lose)
2674         {
2675           int opno, eopno;
2676
2677           /* See if any earlyclobber operand conflicts with some other
2678              operand.  */
2679
2680           if (strict > 0)
2681             for (eopno = 0; eopno < recog_data.n_operands; eopno++)
2682               /* Ignore earlyclobber operands now in memory,
2683                  because we would often report failure when we have
2684                  two memory operands, one of which was formerly a REG.  */
2685               if (earlyclobber[eopno]
2686                   && GET_CODE (recog_data.operand[eopno]) == REG)
2687                 for (opno = 0; opno < recog_data.n_operands; opno++)
2688                   if ((GET_CODE (recog_data.operand[opno]) == MEM
2689                        || recog_data.operand_type[opno] != OP_OUT)
2690                       && opno != eopno
2691                       /* Ignore things like match_operator operands.  */
2692                       && *recog_data.constraints[opno] != 0
2693                       && ! (matching_operands[opno] == eopno
2694                             && operands_match_p (recog_data.operand[opno],
2695                                                  recog_data.operand[eopno]))
2696                       && ! safe_from_earlyclobber (recog_data.operand[opno],
2697                                                    recog_data.operand[eopno]))
2698                     lose = 1;
2699
2700           if (! lose)
2701             {
2702               while (--funny_match_index >= 0)
2703                 {
2704                   recog_data.operand[funny_match[funny_match_index].other]
2705                     = recog_data.operand[funny_match[funny_match_index].this];
2706                 }
2707
2708               return 1;
2709             }
2710         }
2711
2712       which_alternative++;
2713     }
2714   while (which_alternative < recog_data.n_alternatives);
2715
2716   which_alternative = -1;
2717   /* If we are about to reject this, but we are not to test strictly,
2718      try a very loose test.  Only return failure if it fails also.  */
2719   if (strict == 0)
2720     return constrain_operands (-1);
2721   else
2722     return 0;
2723 }
2724
2725 /* Return 1 iff OPERAND (assumed to be a REG rtx)
2726    is a hard reg in class CLASS when its regno is offset by OFFSET
2727    and changed to mode MODE.
2728    If REG occupies multiple hard regs, all of them must be in CLASS.  */
2729
2730 int
2731 reg_fits_class_p (operand, class, offset, mode)
2732      rtx operand;
2733      register enum reg_class class;
2734      int offset;
2735      enum machine_mode mode;
2736 {
2737   register int regno = REGNO (operand);
2738   if (regno < FIRST_PSEUDO_REGISTER
2739       && TEST_HARD_REG_BIT (reg_class_contents[(int) class],
2740                             regno + offset))
2741     {
2742       register int sr;
2743       regno += offset;
2744       for (sr = HARD_REGNO_NREGS (regno, mode) - 1;
2745            sr > 0; sr--)
2746         if (! TEST_HARD_REG_BIT (reg_class_contents[(int) class],
2747                                  regno + sr))
2748           break;
2749       return sr == 0;
2750     }
2751
2752   return 0;
2753 }
2754 \f
2755 /* Split all insns in the function.  If UPD_LIFE, update life info after.  */
2756
2757 void
2758 split_all_insns (upd_life)
2759      int upd_life;
2760 {
2761   sbitmap blocks;
2762   int changed;
2763   int i;
2764
2765   blocks = sbitmap_alloc (n_basic_blocks);
2766   sbitmap_zero (blocks);
2767   changed = 0;
2768
2769   for (i = n_basic_blocks - 1; i >= 0; --i)
2770     {
2771       basic_block bb = BASIC_BLOCK (i);
2772       rtx insn, next;
2773
2774       for (insn = bb->head; insn ; insn = next)
2775         {
2776           rtx set;
2777
2778           /* Can't use `next_real_insn' because that might go across
2779              CODE_LABELS and short-out basic blocks.  */
2780           next = NEXT_INSN (insn);
2781           if (! INSN_P (insn))
2782             ;
2783
2784           /* Don't split no-op move insns.  These should silently
2785              disappear later in final.  Splitting such insns would
2786              break the code that handles REG_NO_CONFLICT blocks.  */
2787
2788           else if ((set = single_set (insn)) != NULL
2789                    && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
2790             {
2791               /* Nops get in the way while scheduling, so delete them
2792                  now if register allocation has already been done.  It
2793                  is too risky to try to do this before register
2794                  allocation, and there are unlikely to be very many
2795                  nops then anyways.  */
2796               if (reload_completed)
2797                 {
2798                   PUT_CODE (insn, NOTE);
2799                   NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2800                   NOTE_SOURCE_FILE (insn) = 0;
2801                 }
2802             }
2803           else
2804             {
2805               /* Split insns here to get max fine-grain parallelism.  */
2806               rtx first = PREV_INSN (insn);
2807               rtx last = try_split (PATTERN (insn), insn, 1);
2808
2809               if (last != insn)
2810                 {
2811                   SET_BIT (blocks, i);
2812                   changed = 1;
2813
2814                   /* try_split returns the NOTE that INSN became.  */
2815                   PUT_CODE (insn, NOTE);
2816                   NOTE_SOURCE_FILE (insn) = 0;
2817                   NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2818
2819                   /* ??? Coddle to md files that generate subregs in post-
2820                      reload splitters instead of computing the proper 
2821                      hard register.  */
2822                   if (reload_completed && first != last)
2823                     {
2824                       first = NEXT_INSN (first);
2825                       while (1)
2826                         {
2827                           if (INSN_P (first))
2828                             cleanup_subreg_operands (first);
2829                           if (first == last)
2830                             break;
2831                           first = NEXT_INSN (first);
2832                         }
2833                     }
2834
2835                   if (insn == bb->end)
2836                     {
2837                       bb->end = last;
2838                       break;
2839                     }
2840                 }
2841             }
2842
2843           if (insn == bb->end)
2844             break;
2845         }
2846
2847       /* ??? When we're called from just after reload, the CFG is in bad
2848          shape, and we may have fallen off the end.  This could be fixed
2849          by having reload not try to delete unreachable code.  Otherwise
2850          assert we found the end insn.  */
2851       if (insn == NULL && upd_life)
2852         abort ();
2853     }
2854
2855   if (changed && upd_life)
2856     {
2857       compute_bb_for_insn (get_max_uid ());
2858       count_or_remove_death_notes (blocks, 1);
2859       update_life_info (blocks, UPDATE_LIFE_LOCAL, PROP_DEATH_NOTES);
2860     }
2861
2862   sbitmap_free (blocks);
2863 }
2864 \f
2865 #ifdef HAVE_peephole2
2866 struct peep2_insn_data
2867 {
2868   rtx insn;
2869   regset live_before;
2870 };
2871
2872 static struct peep2_insn_data peep2_insn_data[MAX_INSNS_PER_PEEP2 + 1];
2873 static int peep2_current;
2874
2875 /* A non-insn marker indicating the last insn of the block.
2876    The live_before regset for this element is correct, indicating
2877    global_live_at_end for the block.  */
2878 #define PEEP2_EOB       pc_rtx
2879
2880 /* Return the Nth non-note insn after `current', or return NULL_RTX if it
2881    does not exist.  Used by the recognizer to find the next insn to match
2882    in a multi-insn pattern.  */
2883
2884 rtx
2885 peep2_next_insn (n)
2886      int n;
2887 {
2888   if (n >= MAX_INSNS_PER_PEEP2 + 1)
2889     abort ();
2890
2891   n += peep2_current;
2892   if (n >= MAX_INSNS_PER_PEEP2 + 1)
2893     n -= MAX_INSNS_PER_PEEP2 + 1;
2894
2895   if (peep2_insn_data[n].insn == PEEP2_EOB)
2896     return NULL_RTX;
2897   return peep2_insn_data[n].insn;
2898 }
2899
2900 /* Return true if REGNO is dead before the Nth non-note insn
2901    after `current'.  */
2902
2903 int
2904 peep2_regno_dead_p (ofs, regno)
2905      int ofs;
2906      int regno;
2907 {
2908   if (ofs >= MAX_INSNS_PER_PEEP2 + 1)
2909     abort ();
2910
2911   ofs += peep2_current;
2912   if (ofs >= MAX_INSNS_PER_PEEP2 + 1)
2913     ofs -= MAX_INSNS_PER_PEEP2 + 1;
2914
2915   if (peep2_insn_data[ofs].insn == NULL_RTX)
2916     abort ();
2917
2918   return ! REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno);
2919 }
2920
2921 /* Similarly for a REG.  */
2922
2923 int
2924 peep2_reg_dead_p (ofs, reg)
2925      int ofs;
2926      rtx reg;
2927 {
2928   int regno, n;
2929
2930   if (ofs >= MAX_INSNS_PER_PEEP2 + 1)
2931     abort ();
2932
2933   ofs += peep2_current;
2934   if (ofs >= MAX_INSNS_PER_PEEP2 + 1)
2935     ofs -= MAX_INSNS_PER_PEEP2 + 1;
2936
2937   if (peep2_insn_data[ofs].insn == NULL_RTX)
2938     abort ();
2939
2940   regno = REGNO (reg);
2941   n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2942   while (--n >= 0)
2943     if (REGNO_REG_SET_P (peep2_insn_data[ofs].live_before, regno + n))
2944       return 0;
2945   return 1;
2946 }
2947
2948 /* Try to find a hard register of mode MODE, matching the register class in
2949    CLASS_STR, which is available at the beginning of insn CURRENT_INSN and
2950    remains available until the end of LAST_INSN.  LAST_INSN may be NULL_RTX,
2951    in which case the only condition is that the register must be available
2952    before CURRENT_INSN.
2953    Registers that already have bits set in REG_SET will not be considered.
2954
2955    If an appropriate register is available, it will be returned and the
2956    corresponding bit(s) in REG_SET will be set; otherwise, NULL_RTX is
2957    returned.  */
2958
2959 rtx
2960 peep2_find_free_register (from, to, class_str, mode, reg_set)
2961      int from, to;
2962      const char *class_str;
2963      enum machine_mode mode;
2964      HARD_REG_SET *reg_set;
2965 {
2966   static int search_ofs;
2967   enum reg_class class;
2968   HARD_REG_SET live;
2969   int i;
2970
2971   if (from >= MAX_INSNS_PER_PEEP2 + 1 || to >= MAX_INSNS_PER_PEEP2 + 1)
2972     abort ();
2973
2974   from += peep2_current;
2975   if (from >= MAX_INSNS_PER_PEEP2 + 1)
2976     from -= MAX_INSNS_PER_PEEP2 + 1;
2977   to += peep2_current;
2978   if (to >= MAX_INSNS_PER_PEEP2 + 1)
2979     to -= MAX_INSNS_PER_PEEP2 + 1;
2980
2981   if (peep2_insn_data[from].insn == NULL_RTX)
2982     abort ();
2983   REG_SET_TO_HARD_REG_SET (live, peep2_insn_data[from].live_before);
2984
2985   while (from != to)
2986     {
2987       HARD_REG_SET this_live;
2988
2989       if (++from >= MAX_INSNS_PER_PEEP2 + 1)
2990         from = 0;
2991       if (peep2_insn_data[from].insn == NULL_RTX)
2992         abort ();
2993       REG_SET_TO_HARD_REG_SET (this_live, peep2_insn_data[from].live_before);
2994       IOR_HARD_REG_SET (live, this_live);
2995     }
2996
2997   class = (class_str[0] == 'r' ? GENERAL_REGS
2998            : REG_CLASS_FROM_LETTER (class_str[0]));
2999
3000   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3001     {
3002       int raw_regno, regno, success, j;
3003
3004       /* Distribute the free registers as much as possible.  */
3005       raw_regno = search_ofs + i;
3006       if (raw_regno >= FIRST_PSEUDO_REGISTER)
3007         raw_regno -= FIRST_PSEUDO_REGISTER;
3008 #ifdef REG_ALLOC_ORDER
3009       regno = reg_alloc_order[raw_regno];
3010 #else
3011       regno = raw_regno;
3012 #endif
3013
3014       /* Don't allocate fixed registers.  */
3015       if (fixed_regs[regno])
3016         continue;
3017       /* Make sure the register is of the right class.  */
3018       if (! TEST_HARD_REG_BIT (reg_class_contents[class], regno))
3019         continue;
3020       /* And can support the mode we need.  */
3021       if (! HARD_REGNO_MODE_OK (regno, mode))
3022         continue;
3023       /* And that we don't create an extra save/restore.  */
3024       if (! call_used_regs[regno] && ! regs_ever_live[regno])
3025         continue;
3026       /* And we don't clobber traceback for noreturn functions.  */
3027       if ((regno == FRAME_POINTER_REGNUM || regno == HARD_FRAME_POINTER_REGNUM)
3028           && (! reload_completed || frame_pointer_needed))
3029         continue;
3030
3031       success = 1;
3032       for (j = HARD_REGNO_NREGS (regno, mode) - 1; j >= 0; j--)
3033         {
3034           if (TEST_HARD_REG_BIT (*reg_set, regno + j)
3035               || TEST_HARD_REG_BIT (live, regno + j))
3036             {
3037               success = 0;
3038               break;
3039             }
3040         }
3041       if (success)
3042         {
3043           for (j = HARD_REGNO_NREGS (regno, mode) - 1; j >= 0; j--)
3044             SET_HARD_REG_BIT (*reg_set, regno + j);
3045
3046           /* Start the next search with the next register.  */
3047           if (++raw_regno >= FIRST_PSEUDO_REGISTER)
3048             raw_regno = 0;
3049           search_ofs = raw_regno;
3050
3051           return gen_rtx_REG (mode, regno);
3052         }
3053     }
3054
3055   search_ofs = 0;
3056   return NULL_RTX;
3057 }
3058
3059 /* Perform the peephole2 optimization pass. */
3060
3061 void
3062 peephole2_optimize (dump_file)
3063      FILE *dump_file ATTRIBUTE_UNUSED;
3064 {
3065   regset_head rs_heads[MAX_INSNS_PER_PEEP2 + 2];
3066   rtx insn, prev;
3067   regset live;
3068   int i, b;
3069 #ifdef HAVE_conditional_execution
3070   sbitmap blocks;
3071   int changed;
3072 #endif
3073
3074   /* Initialize the regsets we're going to use.  */
3075   for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3076     peep2_insn_data[i].live_before = INITIALIZE_REG_SET (rs_heads[i]);
3077   live = INITIALIZE_REG_SET (rs_heads[i]);
3078
3079 #ifdef HAVE_conditional_execution
3080   blocks = sbitmap_alloc (n_basic_blocks);
3081   sbitmap_zero (blocks);
3082   changed = 0;
3083 #else
3084   count_or_remove_death_notes (NULL, 1);
3085 #endif
3086
3087   for (b = n_basic_blocks - 1; b >= 0; --b)
3088     {
3089       basic_block bb = BASIC_BLOCK (b);
3090       struct propagate_block_info *pbi;
3091
3092       /* Indicate that all slots except the last holds invalid data.  */
3093       for (i = 0; i < MAX_INSNS_PER_PEEP2; ++i)
3094         peep2_insn_data[i].insn = NULL_RTX;
3095
3096       /* Indicate that the last slot contains live_after data.  */
3097       peep2_insn_data[MAX_INSNS_PER_PEEP2].insn = PEEP2_EOB;
3098       peep2_current = MAX_INSNS_PER_PEEP2;
3099
3100       /* Start up propagation.  */
3101       COPY_REG_SET (live, bb->global_live_at_end);
3102       COPY_REG_SET (peep2_insn_data[MAX_INSNS_PER_PEEP2].live_before, live);
3103
3104 #ifdef HAVE_conditional_execution
3105       pbi = init_propagate_block_info (bb, live, NULL, NULL, 0);
3106 #else
3107       pbi = init_propagate_block_info (bb, live, NULL, NULL, PROP_DEATH_NOTES);
3108 #endif
3109
3110       for (insn = bb->end; ; insn = prev)
3111         {
3112           prev = PREV_INSN (insn);
3113           if (INSN_P (insn))
3114             {
3115               rtx try;
3116               int match_len;
3117
3118               /* Record this insn.  */
3119               if (--peep2_current < 0)
3120                 peep2_current = MAX_INSNS_PER_PEEP2;
3121               peep2_insn_data[peep2_current].insn = insn;
3122               propagate_one_insn (pbi, insn);
3123               COPY_REG_SET (peep2_insn_data[peep2_current].live_before, live);
3124
3125               /* Match the peephole.  */
3126               try = peephole2_insns (PATTERN (insn), insn, &match_len);
3127               if (try != NULL)
3128                 {
3129                   i = match_len + peep2_current;
3130                   if (i >= MAX_INSNS_PER_PEEP2 + 1)
3131                     i -= MAX_INSNS_PER_PEEP2 + 1;
3132
3133                   /* Replace the old sequence with the new.  */
3134                   flow_delete_insn_chain (insn, peep2_insn_data[i].insn);
3135                   try = emit_insn_after (try, prev);
3136
3137                   /* Adjust the basic block boundaries.  */
3138                   if (peep2_insn_data[i].insn == bb->end)
3139                     bb->end = try;
3140                   if (insn == bb->head)
3141                     bb->head = NEXT_INSN (prev);
3142
3143 #ifdef HAVE_conditional_execution
3144                   /* With conditional execution, we cannot back up the
3145                      live information so easily, since the conditional
3146                      death data structures are not so self-contained.
3147                      So record that we've made a modification to this
3148                      block and update life information at the end.  */
3149                   SET_BIT (blocks, b);
3150                   changed = 1;
3151
3152                   for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3153                     peep2_insn_data[i].insn = NULL_RTX;
3154                   peep2_insn_data[peep2_current].insn = PEEP2_EOB;
3155 #else
3156                   /* Back up lifetime information past the end of the
3157                      newly created sequence.  */
3158                   if (++i >= MAX_INSNS_PER_PEEP2 + 1)
3159                     i = 0;
3160                   COPY_REG_SET (live, peep2_insn_data[i].live_before);
3161
3162                   /* Update life information for the new sequence.  */
3163                   do
3164                     {
3165                       if (INSN_P (try))
3166                         {
3167                           if (--i < 0)
3168                             i = MAX_INSNS_PER_PEEP2;
3169                           peep2_insn_data[i].insn = try;
3170                           propagate_one_insn (pbi, try);
3171                           COPY_REG_SET (peep2_insn_data[i].live_before, live);
3172                         }
3173                       try = PREV_INSN (try);
3174                     }
3175                   while (try != prev);
3176
3177                   /* ??? Should verify that LIVE now matches what we
3178                      had before the new sequence.  */
3179
3180                   peep2_current = i;
3181 #endif
3182                 }
3183             }
3184
3185           if (insn == bb->head)
3186             break;
3187         }
3188
3189       free_propagate_block_info (pbi);
3190     }
3191
3192   for (i = 0; i < MAX_INSNS_PER_PEEP2 + 1; ++i)
3193     FREE_REG_SET (peep2_insn_data[i].live_before);
3194   FREE_REG_SET (live);
3195
3196 #ifdef HAVE_conditional_execution
3197   count_or_remove_death_notes (blocks, 1);
3198   update_life_info (blocks, UPDATE_LIFE_LOCAL, PROP_DEATH_NOTES);
3199   sbitmap_free (blocks);
3200 #endif
3201 }
3202 #endif /* HAVE_peephole2 */