OSDN Git Service

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