OSDN Git Service

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