OSDN Git Service

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