OSDN Git Service

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