OSDN Git Service

gcc:
[pf3gnuchains/gcc-fork.git] / gcc / genrecog.c
1 /* Generate code from machine description to recognize rtl as insns.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003 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
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22
23 /* This program is used to produce insn-recog.c, which contains a
24    function called `recog' plus its subroutines.  These functions
25    contain a decision tree that recognizes whether an rtx, the
26    argument given to recog, is a valid instruction.
27
28    recog returns -1 if the rtx is not valid.  If the rtx is valid,
29    recog returns a nonnegative number which is the insn code number
30    for the pattern that matched.  This is the same as the order in the
31    machine description of the entry that matched.  This number can be
32    used as an index into various insn_* tables, such as insn_template,
33    insn_outfun, and insn_n_operands (found in insn-output.c).
34
35    The third argument to recog is an optional pointer to an int.  If
36    present, recog will accept a pattern if it matches except for
37    missing CLOBBER expressions at the end.  In that case, the value
38    pointed to by the optional pointer will be set to the number of
39    CLOBBERs that need to be added (it should be initialized to zero by
40    the caller).  If it is set nonzero, the caller should allocate a
41    PARALLEL of the appropriate size, copy the initial entries, and
42    call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
43
44    This program also generates the function `split_insns', which
45    returns 0 if the rtl could not be split, or it returns the split
46    rtl as an INSN list.
47
48    This program also generates the function `peephole2_insns', which
49    returns 0 if the rtl could not be matched.  If there was a match,
50    the new rtl is returned in an INSN list, and LAST_INSN will point
51    to the last recognized insn in the old sequence.  */
52
53 #include "bconfig.h"
54 #include "system.h"
55 #include "coretypes.h"
56 #include "tm.h"
57 #include "rtl.h"
58 #include "errors.h"
59 #include "gensupport.h"
60
61
62 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
63   printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
64
65 /* Holds an array of names indexed by insn_code_number.  */
66 static char **insn_name_ptr = 0;
67 static int insn_name_ptr_size = 0;
68
69 /* A listhead of decision trees.  The alternatives to a node are kept
70    in a doubly-linked list so we can easily add nodes to the proper
71    place when merging.  */
72
73 struct decision_head
74 {
75   struct decision *first;
76   struct decision *last;
77 };
78
79 /* A single test.  The two accept types aren't tests per-se, but
80    their equality (or lack thereof) does affect tree merging so
81    it is convenient to keep them here.  */
82
83 struct decision_test
84 {
85   /* A linked list through the tests attached to a node.  */
86   struct decision_test *next;
87
88   /* These types are roughly in the order in which we'd like to test them.  */
89   enum decision_type
90     {
91       DT_mode, DT_code, DT_veclen,
92       DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide, DT_elt_zero_wide_safe,
93       DT_veclen_ge, DT_dup, DT_pred, DT_c_test,
94       DT_accept_op, DT_accept_insn
95     } type;
96
97   union
98   {
99     enum machine_mode mode;     /* Machine mode of node.  */
100     RTX_CODE code;              /* Code to test.  */
101
102     struct
103     {
104       const char *name;         /* Predicate to call.  */
105       int index;                /* Index into `preds' or -1.  */
106       enum machine_mode mode;   /* Machine mode for node.  */
107     } pred;
108
109     const char *c_test;         /* Additional test to perform.  */
110     int veclen;                 /* Length of vector.  */
111     int dup;                    /* Number of operand to compare against.  */
112     HOST_WIDE_INT intval;       /* Value for XINT for XWINT.  */
113     int opno;                   /* Operand number matched.  */
114
115     struct {
116       int code_number;          /* Insn number matched.  */
117       int lineno;               /* Line number of the insn.  */
118       int num_clobbers_to_add;  /* Number of CLOBBERs to be added.  */
119     } insn;
120   } u;
121 };
122
123 /* Data structure for decision tree for recognizing legitimate insns.  */
124
125 struct decision
126 {
127   struct decision_head success; /* Nodes to test on success.  */
128   struct decision *next;        /* Node to test on failure.  */
129   struct decision *prev;        /* Node whose failure tests us.  */
130   struct decision *afterward;   /* Node to test on success,
131                                    but failure of successor nodes.  */
132
133   const char *position;         /* String denoting position in pattern.  */
134
135   struct decision_test *tests;  /* The tests for this node.  */
136
137   int number;                   /* Node number, used for labels */
138   int subroutine_number;        /* Number of subroutine this node starts */
139   int need_label;               /* Label needs to be output.  */
140 };
141
142 #define SUBROUTINE_THRESHOLD    100
143
144 static int next_subroutine_number;
145
146 /* We can write three types of subroutines: One for insn recognition,
147    one to split insns, and one for peephole-type optimizations.  This
148    defines which type is being written.  */
149
150 enum routine_type {
151   RECOG, SPLIT, PEEPHOLE2
152 };
153
154 #define IS_SPLIT(X) ((X) != RECOG)
155
156 /* Next available node number for tree nodes.  */
157
158 static int next_number;
159
160 /* Next number to use as an insn_code.  */
161
162 static int next_insn_code;
163
164 /* Similar, but counts all expressions in the MD file; used for
165    error messages.  */
166
167 static int next_index;
168
169 /* Record the highest depth we ever have so we know how many variables to
170    allocate in each subroutine we make.  */
171
172 static int max_depth;
173
174 /* The line number of the start of the pattern currently being processed.  */
175 static int pattern_lineno;
176
177 /* Count of errors.  */
178 static int error_count;
179 \f
180 /* This table contains a list of the rtl codes that can possibly match a
181    predicate defined in recog.c.  The function `maybe_both_true' uses it to
182    deduce that there are no expressions that can be matches by certain pairs
183    of tree nodes.  Also, if a predicate can match only one code, we can
184    hardwire that code into the node testing the predicate.  */
185
186 static const struct pred_table
187 {
188   const char *const name;
189   const RTX_CODE codes[NUM_RTX_CODE];
190 } preds[] = {
191   {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
192                        LABEL_REF, SUBREG, REG, MEM, ADDRESSOF}},
193 #ifdef PREDICATE_CODES
194   PREDICATE_CODES
195 #endif
196   {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
197                        LABEL_REF, SUBREG, REG, MEM, ADDRESSOF,
198                        PLUS, MINUS, MULT}},
199   {"register_operand", {SUBREG, REG, ADDRESSOF}},
200   {"pmode_register_operand", {SUBREG, REG, ADDRESSOF}},
201   {"scratch_operand", {SCRATCH, REG}},
202   {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
203                          LABEL_REF}},
204   {"const_int_operand", {CONST_INT}},
205   {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
206   {"nonimmediate_operand", {SUBREG, REG, MEM, ADDRESSOF}},
207   {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
208                          LABEL_REF, SUBREG, REG, ADDRESSOF}},
209   {"push_operand", {MEM}},
210   {"pop_operand", {MEM}},
211   {"memory_operand", {SUBREG, MEM}},
212   {"indirect_operand", {SUBREG, MEM}},
213   {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
214                            UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
215                            UNLT, LTGT}}
216 };
217
218 #define NUM_KNOWN_PREDS ARRAY_SIZE (preds)
219
220 static const char *const special_mode_pred_table[] = {
221 #ifdef SPECIAL_MODE_PREDICATES
222   SPECIAL_MODE_PREDICATES
223 #endif
224   "pmode_register_operand"
225 };
226
227 #define NUM_SPECIAL_MODE_PREDS ARRAY_SIZE (special_mode_pred_table)
228
229 static struct decision *new_decision
230   (const char *, struct decision_head *);
231 static struct decision_test *new_decision_test
232   (enum decision_type, struct decision_test ***);
233 static rtx find_operand
234   (rtx, int);
235 static rtx find_matching_operand
236   (rtx, int);
237 static void validate_pattern
238   (rtx, rtx, rtx, int);
239 static struct decision *add_to_sequence
240   (rtx, struct decision_head *, const char *, enum routine_type, int);
241
242 static int maybe_both_true_2
243   (struct decision_test *, struct decision_test *);
244 static int maybe_both_true_1
245   (struct decision_test *, struct decision_test *);
246 static int maybe_both_true
247   (struct decision *, struct decision *, int);
248
249 static int nodes_identical_1
250   (struct decision_test *, struct decision_test *);
251 static int nodes_identical
252   (struct decision *, struct decision *);
253 static void merge_accept_insn
254   (struct decision *, struct decision *);
255 static void merge_trees
256   (struct decision_head *, struct decision_head *);
257
258 static void factor_tests
259   (struct decision_head *);
260 static void simplify_tests
261   (struct decision_head *);
262 static int break_out_subroutines
263   (struct decision_head *, int);
264 static void find_afterward
265   (struct decision_head *, struct decision *);
266
267 static void change_state
268   (const char *, const char *, struct decision *, const char *);
269 static void print_code
270   (enum rtx_code);
271 static void write_afterward
272   (struct decision *, struct decision *, const char *);
273 static struct decision *write_switch
274   (struct decision *, int);
275 static void write_cond
276   (struct decision_test *, int, enum routine_type);
277 static void write_action
278   (struct decision *, struct decision_test *, int, int,
279    struct decision *, enum routine_type);
280 static int is_unconditional
281   (struct decision_test *, enum routine_type);
282 static int write_node
283   (struct decision *, int, enum routine_type);
284 static void write_tree_1
285   (struct decision_head *, int, enum routine_type);
286 static void write_tree
287   (struct decision_head *, const char *, enum routine_type, int);
288 static void write_subroutine
289   (struct decision_head *, enum routine_type);
290 static void write_subroutines
291   (struct decision_head *, enum routine_type);
292 static void write_header
293   (void);
294
295 static struct decision_head make_insn_sequence
296   (rtx, enum routine_type);
297 static void process_tree
298   (struct decision_head *, enum routine_type);
299
300 static void record_insn_name
301   (int, const char *);
302
303 static void debug_decision_0
304   (struct decision *, int, int);
305 static void debug_decision_1
306   (struct decision *, int);
307 static void debug_decision_2
308   (struct decision_test *);
309 extern void debug_decision
310   (struct decision *);
311 extern void debug_decision_list
312   (struct decision *);
313 \f
314 /* Create a new node in sequence after LAST.  */
315
316 static struct decision *
317 new_decision (const char *position, struct decision_head *last)
318 {
319   struct decision *new
320     = (struct decision *) xmalloc (sizeof (struct decision));
321
322   memset (new, 0, sizeof (*new));
323   new->success = *last;
324   new->position = xstrdup (position);
325   new->number = next_number++;
326
327   last->first = last->last = new;
328   return new;
329 }
330
331 /* Create a new test and link it in at PLACE.  */
332
333 static struct decision_test *
334 new_decision_test (enum decision_type type, struct decision_test ***pplace)
335 {
336   struct decision_test **place = *pplace;
337   struct decision_test *test;
338
339   test = (struct decision_test *) xmalloc (sizeof (*test));
340   test->next = *place;
341   test->type = type;
342   *place = test;
343
344   place = &test->next;
345   *pplace = place;
346
347   return test;
348 }
349
350 /* Search for and return operand N.  */
351
352 static rtx
353 find_operand (rtx pattern, int n)
354 {
355   const char *fmt;
356   RTX_CODE code;
357   int i, j, len;
358   rtx r;
359
360   code = GET_CODE (pattern);
361   if ((code == MATCH_SCRATCH
362        || code == MATCH_INSN
363        || code == MATCH_OPERAND
364        || code == MATCH_OPERATOR
365        || code == MATCH_PARALLEL)
366       && XINT (pattern, 0) == n)
367     return pattern;
368
369   fmt = GET_RTX_FORMAT (code);
370   len = GET_RTX_LENGTH (code);
371   for (i = 0; i < len; i++)
372     {
373       switch (fmt[i])
374         {
375         case 'e': case 'u':
376           if ((r = find_operand (XEXP (pattern, i), n)) != NULL_RTX)
377             return r;
378           break;
379
380         case 'V':
381           if (! XVEC (pattern, i))
382             break;
383           /* FALLTHRU */
384
385         case 'E':
386           for (j = 0; j < XVECLEN (pattern, i); j++)
387             if ((r = find_operand (XVECEXP (pattern, i, j), n)) != NULL_RTX)
388               return r;
389           break;
390
391         case 'i': case 'w': case '0': case 's':
392           break;
393
394         default:
395           abort ();
396         }
397     }
398
399   return NULL;
400 }
401
402 /* Search for and return operand M, such that it has a matching
403    constraint for operand N.  */
404
405 static rtx
406 find_matching_operand (rtx pattern, int n)
407 {
408   const char *fmt;
409   RTX_CODE code;
410   int i, j, len;
411   rtx r;
412
413   code = GET_CODE (pattern);
414   if (code == MATCH_OPERAND
415       && (XSTR (pattern, 2)[0] == '0' + n
416           || (XSTR (pattern, 2)[0] == '%'
417               && XSTR (pattern, 2)[1] == '0' + n)))
418     return pattern;
419
420   fmt = GET_RTX_FORMAT (code);
421   len = GET_RTX_LENGTH (code);
422   for (i = 0; i < len; i++)
423     {
424       switch (fmt[i])
425         {
426         case 'e': case 'u':
427           if ((r = find_matching_operand (XEXP (pattern, i), n)))
428             return r;
429           break;
430
431         case 'V':
432           if (! XVEC (pattern, i))
433             break;
434           /* FALLTHRU */
435
436         case 'E':
437           for (j = 0; j < XVECLEN (pattern, i); j++)
438             if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
439               return r;
440           break;
441
442         case 'i': case 'w': case '0': case 's':
443           break;
444
445         default:
446           abort ();
447         }
448     }
449
450   return NULL;
451 }
452
453
454 /* Check for various errors in patterns.  SET is nonnull for a destination,
455    and is the complete set pattern.  SET_CODE is '=' for normal sets, and
456    '+' within a context that requires in-out constraints.  */
457
458 static void
459 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
460 {
461   const char *fmt;
462   RTX_CODE code;
463   size_t i, len;
464   int j;
465
466   code = GET_CODE (pattern);
467   switch (code)
468     {
469     case MATCH_SCRATCH:
470       return;
471
472     case MATCH_INSN:
473     case MATCH_OPERAND:
474     case MATCH_OPERATOR:
475       {
476         const char *pred_name = XSTR (pattern, 1);
477         int allows_non_lvalue = 1, allows_non_const = 1;
478         int special_mode_pred = 0;
479         const char *c_test;
480
481         if (GET_CODE (insn) == DEFINE_INSN)
482           c_test = XSTR (insn, 2);
483         else
484           c_test = XSTR (insn, 1);
485
486         if (pred_name[0] != 0)
487           {
488             for (i = 0; i < NUM_KNOWN_PREDS; i++)
489               if (! strcmp (preds[i].name, pred_name))
490                 break;
491
492             if (i < NUM_KNOWN_PREDS)
493               {
494                 int j;
495
496                 allows_non_lvalue = allows_non_const = 0;
497                 for (j = 0; preds[i].codes[j] != 0; j++)
498                   {
499                     RTX_CODE c = preds[i].codes[j];
500                     if (c != LABEL_REF
501                         && c != SYMBOL_REF
502                         && c != CONST_INT
503                         && c != CONST_DOUBLE
504                         && c != CONST
505                         && c != HIGH
506                         && c != CONSTANT_P_RTX)
507                       allows_non_const = 1;
508
509                     if (c != REG
510                         && c != SUBREG
511                         && c != MEM
512                         && c != ADDRESSOF
513                         && c != CONCAT
514                         && c != PARALLEL
515                         && c != STRICT_LOW_PART)
516                       allows_non_lvalue = 1;
517                   }
518               }
519             else
520               {
521 #ifdef PREDICATE_CODES
522                 /* If the port has a list of the predicates it uses but
523                    omits one, warn.  */
524                 message_with_line (pattern_lineno,
525                                    "warning: `%s' not in PREDICATE_CODES",
526                                    pred_name);
527 #endif
528               }
529
530             for (i = 0; i < NUM_SPECIAL_MODE_PREDS; ++i)
531               if (strcmp (pred_name, special_mode_pred_table[i]) == 0)
532                 {
533                   special_mode_pred = 1;
534                   break;
535                 }
536           }
537
538         if (code == MATCH_OPERAND)
539           {
540             const char constraints0 = XSTR (pattern, 2)[0];
541
542             /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
543                don't use the MATCH_OPERAND constraint, only the predicate.
544                This is confusing to folks doing new ports, so help them
545                not make the mistake.  */
546             if (GET_CODE (insn) == DEFINE_EXPAND
547                 || GET_CODE (insn) == DEFINE_SPLIT
548                 || GET_CODE (insn) == DEFINE_PEEPHOLE2)
549               {
550                 if (constraints0)
551                   message_with_line (pattern_lineno,
552                                      "warning: constraints not supported in %s",
553                                      rtx_name[GET_CODE (insn)]);
554               }
555
556             /* A MATCH_OPERAND that is a SET should have an output reload.  */
557             else if (set && constraints0)
558               {
559                 if (set_code == '+')
560                   {
561                     if (constraints0 == '+')
562                       ;
563                     /* If we've only got an output reload for this operand,
564                        we'd better have a matching input operand.  */
565                     else if (constraints0 == '='
566                              && find_matching_operand (insn, XINT (pattern, 0)))
567                       ;
568                     else
569                       {
570                         message_with_line (pattern_lineno,
571                                            "operand %d missing in-out reload",
572                                            XINT (pattern, 0));
573                         error_count++;
574                       }
575                   }
576                 else if (constraints0 != '=' && constraints0 != '+')
577                   {
578                     message_with_line (pattern_lineno,
579                                        "operand %d missing output reload",
580                                        XINT (pattern, 0));
581                     error_count++;
582                   }
583               }
584           }
585
586         /* Allowing non-lvalues in destinations -- particularly CONST_INT --
587            while not likely to occur at runtime, results in less efficient
588            code from insn-recog.c.  */
589         if (set
590             && pred_name[0] != '\0'
591             && allows_non_lvalue)
592           {
593             message_with_line (pattern_lineno,
594                         "warning: destination operand %d allows non-lvalue",
595                         XINT (pattern, 0));
596           }
597
598         /* A modeless MATCH_OPERAND can be handy when we can
599            check for multiple modes in the c_test.  In most other cases,
600            it is a mistake.  Only DEFINE_INSN is eligible, since SPLIT
601            and PEEP2 can FAIL within the output pattern.  Exclude
602            address_operand, since its mode is related to the mode of
603            the memory not the operand.  Exclude the SET_DEST of a call
604            instruction, as that is a common idiom.  */
605
606         if (GET_MODE (pattern) == VOIDmode
607             && code == MATCH_OPERAND
608             && GET_CODE (insn) == DEFINE_INSN
609             && allows_non_const
610             && ! special_mode_pred
611             && pred_name[0] != '\0'
612             && strcmp (pred_name, "address_operand") != 0
613             && strstr (c_test, "operands") == NULL
614             && ! (set
615                   && GET_CODE (set) == SET
616                   && GET_CODE (SET_SRC (set)) == CALL))
617           {
618             message_with_line (pattern_lineno,
619                                "warning: operand %d missing mode?",
620                                XINT (pattern, 0));
621           }
622         return;
623       }
624
625     case SET:
626       {
627         enum machine_mode dmode, smode;
628         rtx dest, src;
629
630         dest = SET_DEST (pattern);
631         src = SET_SRC (pattern);
632
633         /* STRICT_LOW_PART is a wrapper.  Its argument is the real
634            destination, and it's mode should match the source.  */
635         if (GET_CODE (dest) == STRICT_LOW_PART)
636           dest = XEXP (dest, 0);
637
638         /* Find the referant for a DUP.  */
639
640         if (GET_CODE (dest) == MATCH_DUP
641             || GET_CODE (dest) == MATCH_OP_DUP
642             || GET_CODE (dest) == MATCH_PAR_DUP)
643           dest = find_operand (insn, XINT (dest, 0));
644
645         if (GET_CODE (src) == MATCH_DUP
646             || GET_CODE (src) == MATCH_OP_DUP
647             || GET_CODE (src) == MATCH_PAR_DUP)
648           src = find_operand (insn, XINT (src, 0));
649
650         dmode = GET_MODE (dest);
651         smode = GET_MODE (src);
652
653         /* The mode of an ADDRESS_OPERAND is the mode of the memory
654            reference, not the mode of the address.  */
655         if (GET_CODE (src) == MATCH_OPERAND
656             && ! strcmp (XSTR (src, 1), "address_operand"))
657           ;
658
659         /* The operands of a SET must have the same mode unless one
660            is VOIDmode.  */
661         else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
662           {
663             message_with_line (pattern_lineno,
664                                "mode mismatch in set: %smode vs %smode",
665                                GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
666             error_count++;
667           }
668
669         /* If only one of the operands is VOIDmode, and PC or CC0 is
670            not involved, it's probably a mistake.  */
671         else if (dmode != smode
672                  && GET_CODE (dest) != PC
673                  && GET_CODE (dest) != CC0
674                  && GET_CODE (src) != PC
675                  && GET_CODE (src) != CC0
676                  && GET_CODE (src) != CONST_INT)
677           {
678             const char *which;
679             which = (dmode == VOIDmode ? "destination" : "source");
680             message_with_line (pattern_lineno,
681                                "warning: %s missing a mode?", which);
682           }
683
684         if (dest != SET_DEST (pattern))
685           validate_pattern (dest, insn, pattern, '=');
686         validate_pattern (SET_DEST (pattern), insn, pattern, '=');
687         validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
688         return;
689       }
690
691     case CLOBBER:
692       validate_pattern (SET_DEST (pattern), insn, pattern, '=');
693       return;
694
695     case ZERO_EXTRACT:
696       validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
697       validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
698       validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
699       return;
700
701     case STRICT_LOW_PART:
702       validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
703       return;
704
705     case LABEL_REF:
706       if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
707         {
708           message_with_line (pattern_lineno,
709                              "operand to label_ref %smode not VOIDmode",
710                              GET_MODE_NAME (GET_MODE (XEXP (pattern, 0))));
711           error_count++;
712         }
713       break;
714
715     default:
716       break;
717     }
718
719   fmt = GET_RTX_FORMAT (code);
720   len = GET_RTX_LENGTH (code);
721   for (i = 0; i < len; i++)
722     {
723       switch (fmt[i])
724         {
725         case 'e': case 'u':
726           validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
727           break;
728
729         case 'E':
730           for (j = 0; j < XVECLEN (pattern, i); j++)
731             validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
732           break;
733
734         case 'i': case 'w': case '0': case 's':
735           break;
736
737         default:
738           abort ();
739         }
740     }
741 }
742
743 /* Create a chain of nodes to verify that an rtl expression matches
744    PATTERN.
745
746    LAST is a pointer to the listhead in the previous node in the chain (or
747    in the calling function, for the first node).
748
749    POSITION is the string representing the current position in the insn.
750
751    INSN_TYPE is the type of insn for which we are emitting code.
752
753    A pointer to the final node in the chain is returned.  */
754
755 static struct decision *
756 add_to_sequence (rtx pattern, struct decision_head *last, const char *position,
757                  enum routine_type insn_type, int top)
758 {
759   RTX_CODE code;
760   struct decision *this, *sub;
761   struct decision_test *test;
762   struct decision_test **place;
763   char *subpos;
764   size_t i;
765   const char *fmt;
766   int depth = strlen (position);
767   int len;
768   enum machine_mode mode;
769
770   if (depth > max_depth)
771     max_depth = depth;
772
773   subpos = (char *) xmalloc (depth + 2);
774   strcpy (subpos, position);
775   subpos[depth + 1] = 0;
776
777   sub = this = new_decision (position, last);
778   place = &this->tests;
779
780  restart:
781   mode = GET_MODE (pattern);
782   code = GET_CODE (pattern);
783
784   switch (code)
785     {
786     case PARALLEL:
787       /* Toplevel peephole pattern.  */
788       if (insn_type == PEEPHOLE2 && top)
789         {
790           /* We don't need the node we just created -- unlink it.  */
791           last->first = last->last = NULL;
792
793           for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
794             {
795               /* Which insn we're looking at is represented by A-Z. We don't
796                  ever use 'A', however; it is always implied.  */
797
798               subpos[depth] = (i > 0 ? 'A' + i : 0);
799               sub = add_to_sequence (XVECEXP (pattern, 0, i),
800                                      last, subpos, insn_type, 0);
801               last = &sub->success;
802             }
803           goto ret;
804         }
805
806       /* Else nothing special.  */
807       break;
808
809     case MATCH_PARALLEL:
810       /* The explicit patterns within a match_parallel enforce a minimum
811          length on the vector.  The match_parallel predicate may allow
812          for more elements.  We do need to check for this minimum here
813          or the code generated to match the internals may reference data
814          beyond the end of the vector.  */
815       test = new_decision_test (DT_veclen_ge, &place);
816       test->u.veclen = XVECLEN (pattern, 2);
817       /* FALLTHRU */
818
819     case MATCH_OPERAND:
820     case MATCH_SCRATCH:
821     case MATCH_OPERATOR:
822     case MATCH_INSN:
823       {
824         const char *pred_name;
825         RTX_CODE was_code = code;
826         int allows_const_int = 1;
827
828         if (code == MATCH_SCRATCH)
829           {
830             pred_name = "scratch_operand";
831             code = UNKNOWN;
832           }
833         else
834           {
835             pred_name = XSTR (pattern, 1);
836             if (code == MATCH_PARALLEL)
837               code = PARALLEL;
838             else
839               code = UNKNOWN;
840           }
841
842         if (pred_name[0] != 0)
843           {
844             test = new_decision_test (DT_pred, &place);
845             test->u.pred.name = pred_name;
846             test->u.pred.mode = mode;
847
848             /* See if we know about this predicate and save its number.
849                If we do, and it only accepts one code, note that fact.
850
851                If we know that the predicate does not allow CONST_INT,
852                we know that the only way the predicate can match is if
853                the modes match (here we use the kludge of relying on the
854                fact that "address_operand" accepts CONST_INT; otherwise,
855                it would have to be a special case), so we can test the
856                mode (but we need not).  This fact should considerably
857                simplify the generated code.  */
858
859             for (i = 0; i < NUM_KNOWN_PREDS; i++)
860               if (! strcmp (preds[i].name, pred_name))
861                 break;
862
863             if (i < NUM_KNOWN_PREDS)
864               {
865                 int j;
866
867                 test->u.pred.index = i;
868
869                 if (preds[i].codes[1] == 0 && code == UNKNOWN)
870                   code = preds[i].codes[0];
871
872                 allows_const_int = 0;
873                 for (j = 0; preds[i].codes[j] != 0; j++)
874                   if (preds[i].codes[j] == CONST_INT)
875                     {
876                       allows_const_int = 1;
877                       break;
878                     }
879               }
880             else
881               test->u.pred.index = -1;
882           }
883
884         /* Can't enforce a mode if we allow const_int.  */
885         if (allows_const_int)
886           mode = VOIDmode;
887
888         /* Accept the operand, ie. record it in `operands'.  */
889         test = new_decision_test (DT_accept_op, &place);
890         test->u.opno = XINT (pattern, 0);
891
892         if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
893           {
894             char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
895             for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
896               {
897                 subpos[depth] = i + base;
898                 sub = add_to_sequence (XVECEXP (pattern, 2, i),
899                                        &sub->success, subpos, insn_type, 0);
900               }
901           }
902         goto fini;
903       }
904
905     case MATCH_OP_DUP:
906       code = UNKNOWN;
907
908       test = new_decision_test (DT_dup, &place);
909       test->u.dup = XINT (pattern, 0);
910
911       test = new_decision_test (DT_accept_op, &place);
912       test->u.opno = XINT (pattern, 0);
913
914       for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
915         {
916           subpos[depth] = i + '0';
917           sub = add_to_sequence (XVECEXP (pattern, 1, i),
918                                  &sub->success, subpos, insn_type, 0);
919         }
920       goto fini;
921
922     case MATCH_DUP:
923     case MATCH_PAR_DUP:
924       code = UNKNOWN;
925
926       test = new_decision_test (DT_dup, &place);
927       test->u.dup = XINT (pattern, 0);
928       goto fini;
929
930     case ADDRESS:
931       pattern = XEXP (pattern, 0);
932       goto restart;
933
934     default:
935       break;
936     }
937
938   fmt = GET_RTX_FORMAT (code);
939   len = GET_RTX_LENGTH (code);
940
941   /* Do tests against the current node first.  */
942   for (i = 0; i < (size_t) len; i++)
943     {
944       if (fmt[i] == 'i')
945         {
946           if (i == 0)
947             {
948               test = new_decision_test (DT_elt_zero_int, &place);
949               test->u.intval = XINT (pattern, i);
950             }
951           else if (i == 1)
952             {
953               test = new_decision_test (DT_elt_one_int, &place);
954               test->u.intval = XINT (pattern, i);
955             }
956           else
957             abort ();
958         }
959       else if (fmt[i] == 'w')
960         {
961           /* If this value actually fits in an int, we can use a switch
962              statement here, so indicate that.  */
963           enum decision_type type
964             = ((int) XWINT (pattern, i) == XWINT (pattern, i))
965               ? DT_elt_zero_wide_safe : DT_elt_zero_wide;
966
967           if (i != 0)
968             abort ();
969
970           test = new_decision_test (type, &place);
971           test->u.intval = XWINT (pattern, i);
972         }
973       else if (fmt[i] == 'E')
974         {
975           if (i != 0)
976             abort ();
977
978           test = new_decision_test (DT_veclen, &place);
979           test->u.veclen = XVECLEN (pattern, i);
980         }
981     }
982
983   /* Now test our sub-patterns.  */
984   for (i = 0; i < (size_t) len; i++)
985     {
986       switch (fmt[i])
987         {
988         case 'e': case 'u':
989           subpos[depth] = '0' + i;
990           sub = add_to_sequence (XEXP (pattern, i), &sub->success,
991                                  subpos, insn_type, 0);
992           break;
993
994         case 'E':
995           {
996             int j;
997             for (j = 0; j < XVECLEN (pattern, i); j++)
998               {
999                 subpos[depth] = 'a' + j;
1000                 sub = add_to_sequence (XVECEXP (pattern, i, j),
1001                                        &sub->success, subpos, insn_type, 0);
1002               }
1003             break;
1004           }
1005
1006         case 'i': case 'w':
1007           /* Handled above.  */
1008           break;
1009         case '0':
1010           break;
1011
1012         default:
1013           abort ();
1014         }
1015     }
1016
1017  fini:
1018   /* Insert nodes testing mode and code, if they're still relevant,
1019      before any of the nodes we may have added above.  */
1020   if (code != UNKNOWN)
1021     {
1022       place = &this->tests;
1023       test = new_decision_test (DT_code, &place);
1024       test->u.code = code;
1025     }
1026
1027   if (mode != VOIDmode)
1028     {
1029       place = &this->tests;
1030       test = new_decision_test (DT_mode, &place);
1031       test->u.mode = mode;
1032     }
1033
1034   /* If we didn't insert any tests or accept nodes, hork.  */
1035   if (this->tests == NULL)
1036     abort ();
1037
1038  ret:
1039   free (subpos);
1040   return sub;
1041 }
1042 \f
1043 /* A subroutine of maybe_both_true; examines only one test.
1044    Returns > 0 for "definitely both true" and < 0 for "maybe both true".  */
1045
1046 static int
1047 maybe_both_true_2 (struct decision_test *d1, struct decision_test *d2)
1048 {
1049   if (d1->type == d2->type)
1050     {
1051       switch (d1->type)
1052         {
1053         case DT_mode:
1054           return d1->u.mode == d2->u.mode;
1055
1056         case DT_code:
1057           return d1->u.code == d2->u.code;
1058
1059         case DT_veclen:
1060           return d1->u.veclen == d2->u.veclen;
1061
1062         case DT_elt_zero_int:
1063         case DT_elt_one_int:
1064         case DT_elt_zero_wide:
1065         case DT_elt_zero_wide_safe:
1066           return d1->u.intval == d2->u.intval;
1067
1068         default:
1069           break;
1070         }
1071     }
1072
1073   /* If either has a predicate that we know something about, set
1074      things up so that D1 is the one that always has a known
1075      predicate.  Then see if they have any codes in common.  */
1076
1077   if (d1->type == DT_pred || d2->type == DT_pred)
1078     {
1079       if (d2->type == DT_pred)
1080         {
1081           struct decision_test *tmp;
1082           tmp = d1, d1 = d2, d2 = tmp;
1083         }
1084
1085       /* If D2 tests a mode, see if it matches D1.  */
1086       if (d1->u.pred.mode != VOIDmode)
1087         {
1088           if (d2->type == DT_mode)
1089             {
1090               if (d1->u.pred.mode != d2->u.mode
1091                   /* The mode of an address_operand predicate is the
1092                      mode of the memory, not the operand.  It can only
1093                      be used for testing the predicate, so we must
1094                      ignore it here.  */
1095                   && strcmp (d1->u.pred.name, "address_operand") != 0)
1096                 return 0;
1097             }
1098           /* Don't check two predicate modes here, because if both predicates
1099              accept CONST_INT, then both can still be true even if the modes
1100              are different.  If they don't accept CONST_INT, there will be a
1101              separate DT_mode that will make maybe_both_true_1 return 0.  */
1102         }
1103
1104       if (d1->u.pred.index >= 0)
1105         {
1106           /* If D2 tests a code, see if it is in the list of valid
1107              codes for D1's predicate.  */
1108           if (d2->type == DT_code)
1109             {
1110               const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
1111               while (*c != 0)
1112                 {
1113                   if (*c == d2->u.code)
1114                     break;
1115                   ++c;
1116                 }
1117               if (*c == 0)
1118                 return 0;
1119             }
1120
1121           /* Otherwise see if the predicates have any codes in common.  */
1122           else if (d2->type == DT_pred && d2->u.pred.index >= 0)
1123             {
1124               const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
1125               int common = 0;
1126
1127               while (*c1 != 0 && !common)
1128                 {
1129                   const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
1130                   while (*c2 != 0 && !common)
1131                     {
1132                       common = (*c1 == *c2);
1133                       ++c2;
1134                     }
1135                   ++c1;
1136                 }
1137
1138               if (!common)
1139                 return 0;
1140             }
1141         }
1142     }
1143
1144   /* Tests vs veclen may be known when strict equality is involved.  */
1145   if (d1->type == DT_veclen && d2->type == DT_veclen_ge)
1146     return d1->u.veclen >= d2->u.veclen;
1147   if (d1->type == DT_veclen_ge && d2->type == DT_veclen)
1148     return d2->u.veclen >= d1->u.veclen;
1149
1150   return -1;
1151 }
1152
1153 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1154    Returns > 0 for "definitely both true" and < 0 for "maybe both true".  */
1155
1156 static int
1157 maybe_both_true_1 (struct decision_test *d1, struct decision_test *d2)
1158 {
1159   struct decision_test *t1, *t2;
1160
1161   /* A match_operand with no predicate can match anything.  Recognize
1162      this by the existence of a lone DT_accept_op test.  */
1163   if (d1->type == DT_accept_op || d2->type == DT_accept_op)
1164     return 1;
1165
1166   /* Eliminate pairs of tests while they can exactly match.  */
1167   while (d1 && d2 && d1->type == d2->type)
1168     {
1169       if (maybe_both_true_2 (d1, d2) == 0)
1170         return 0;
1171       d1 = d1->next, d2 = d2->next;
1172     }
1173
1174   /* After that, consider all pairs.  */
1175   for (t1 = d1; t1 ; t1 = t1->next)
1176     for (t2 = d2; t2 ; t2 = t2->next)
1177       if (maybe_both_true_2 (t1, t2) == 0)
1178         return 0;
1179
1180   return -1;
1181 }
1182
1183 /* Return 0 if we can prove that there is no RTL that can match both
1184    D1 and D2.  Otherwise, return 1 (it may be that there is an RTL that
1185    can match both or just that we couldn't prove there wasn't such an RTL).
1186
1187    TOPLEVEL is nonzero if we are to only look at the top level and not
1188    recursively descend.  */
1189
1190 static int
1191 maybe_both_true (struct decision *d1, struct decision *d2,
1192                  int toplevel)
1193 {
1194   struct decision *p1, *p2;
1195   int cmp;
1196
1197   /* Don't compare strings on the different positions in insn.  Doing so
1198      is incorrect and results in false matches from constructs like
1199
1200         [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1201               (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1202      vs
1203         [(set (match_operand:HI "register_operand" "r")
1204               (match_operand:HI "register_operand" "r"))]
1205
1206      If we are presented with such, we are recursing through the remainder
1207      of a node's success nodes (from the loop at the end of this function).
1208      Skip forward until we come to a position that matches.
1209
1210      Due to the way position strings are constructed, we know that iterating
1211      forward from the lexically lower position (e.g. "00") will run into
1212      the lexically higher position (e.g. "1") and not the other way around.
1213      This saves a bit of effort.  */
1214
1215   cmp = strcmp (d1->position, d2->position);
1216   if (cmp != 0)
1217     {
1218       if (toplevel)
1219         abort ();
1220
1221       /* If the d2->position was lexically lower, swap.  */
1222       if (cmp > 0)
1223         p1 = d1, d1 = d2, d2 = p1;
1224
1225       if (d1->success.first == 0)
1226         return 1;
1227       for (p1 = d1->success.first; p1; p1 = p1->next)
1228         if (maybe_both_true (p1, d2, 0))
1229           return 1;
1230
1231       return 0;
1232     }
1233
1234   /* Test the current level.  */
1235   cmp = maybe_both_true_1 (d1->tests, d2->tests);
1236   if (cmp >= 0)
1237     return cmp;
1238
1239   /* We can't prove that D1 and D2 cannot both be true.  If we are only
1240      to check the top level, return 1.  Otherwise, see if we can prove
1241      that all choices in both successors are mutually exclusive.  If
1242      either does not have any successors, we can't prove they can't both
1243      be true.  */
1244
1245   if (toplevel || d1->success.first == 0 || d2->success.first == 0)
1246     return 1;
1247
1248   for (p1 = d1->success.first; p1; p1 = p1->next)
1249     for (p2 = d2->success.first; p2; p2 = p2->next)
1250       if (maybe_both_true (p1, p2, 0))
1251         return 1;
1252
1253   return 0;
1254 }
1255
1256 /* A subroutine of nodes_identical.  Examine two tests for equivalence.  */
1257
1258 static int
1259 nodes_identical_1 (struct decision_test *d1, struct decision_test *d2)
1260 {
1261   switch (d1->type)
1262     {
1263     case DT_mode:
1264       return d1->u.mode == d2->u.mode;
1265
1266     case DT_code:
1267       return d1->u.code == d2->u.code;
1268
1269     case DT_pred:
1270       return (d1->u.pred.mode == d2->u.pred.mode
1271               && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1272
1273     case DT_c_test:
1274       return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1275
1276     case DT_veclen:
1277     case DT_veclen_ge:
1278       return d1->u.veclen == d2->u.veclen;
1279
1280     case DT_dup:
1281       return d1->u.dup == d2->u.dup;
1282
1283     case DT_elt_zero_int:
1284     case DT_elt_one_int:
1285     case DT_elt_zero_wide:
1286     case DT_elt_zero_wide_safe:
1287       return d1->u.intval == d2->u.intval;
1288
1289     case DT_accept_op:
1290       return d1->u.opno == d2->u.opno;
1291
1292     case DT_accept_insn:
1293       /* Differences will be handled in merge_accept_insn.  */
1294       return 1;
1295
1296     default:
1297       abort ();
1298     }
1299 }
1300
1301 /* True iff the two nodes are identical (on one level only).  Due
1302    to the way these lists are constructed, we shouldn't have to
1303    consider different orderings on the tests.  */
1304
1305 static int
1306 nodes_identical (struct decision *d1, struct decision *d2)
1307 {
1308   struct decision_test *t1, *t2;
1309
1310   for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1311     {
1312       if (t1->type != t2->type)
1313         return 0;
1314       if (! nodes_identical_1 (t1, t2))
1315         return 0;
1316     }
1317
1318   /* For success, they should now both be null.  */
1319   if (t1 != t2)
1320     return 0;
1321
1322   /* Check that their subnodes are at the same position, as any one set
1323      of sibling decisions must be at the same position.  Allowing this
1324      requires complications to find_afterward and when change_state is
1325      invoked.  */
1326   if (d1->success.first
1327       && d2->success.first
1328       && strcmp (d1->success.first->position, d2->success.first->position))
1329     return 0;
1330
1331   return 1;
1332 }
1333
1334 /* A subroutine of merge_trees; given two nodes that have been declared
1335    identical, cope with two insn accept states.  If they differ in the
1336    number of clobbers, then the conflict was created by make_insn_sequence
1337    and we can drop the with-clobbers version on the floor.  If both
1338    nodes have no additional clobbers, we have found an ambiguity in the
1339    source machine description.  */
1340
1341 static void
1342 merge_accept_insn (struct decision *oldd, struct decision *addd)
1343 {
1344   struct decision_test *old, *add;
1345
1346   for (old = oldd->tests; old; old = old->next)
1347     if (old->type == DT_accept_insn)
1348       break;
1349   if (old == NULL)
1350     return;
1351
1352   for (add = addd->tests; add; add = add->next)
1353     if (add->type == DT_accept_insn)
1354       break;
1355   if (add == NULL)
1356     return;
1357
1358   /* If one node is for a normal insn and the second is for the base
1359      insn with clobbers stripped off, the second node should be ignored.  */
1360
1361   if (old->u.insn.num_clobbers_to_add == 0
1362       && add->u.insn.num_clobbers_to_add > 0)
1363     {
1364       /* Nothing to do here.  */
1365     }
1366   else if (old->u.insn.num_clobbers_to_add > 0
1367            && add->u.insn.num_clobbers_to_add == 0)
1368     {
1369       /* In this case, replace OLD with ADD.  */
1370       old->u.insn = add->u.insn;
1371     }
1372   else
1373     {
1374       message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1375                          get_insn_name (add->u.insn.code_number),
1376                          get_insn_name (old->u.insn.code_number));
1377       message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1378                          get_insn_name (old->u.insn.code_number));
1379       error_count++;
1380     }
1381 }
1382
1383 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively.  */
1384
1385 static void
1386 merge_trees (struct decision_head *oldh, struct decision_head *addh)
1387 {
1388   struct decision *next, *add;
1389
1390   if (addh->first == 0)
1391     return;
1392   if (oldh->first == 0)
1393     {
1394       *oldh = *addh;
1395       return;
1396     }
1397
1398   /* Trying to merge bits at different positions isn't possible.  */
1399   if (strcmp (oldh->first->position, addh->first->position))
1400     abort ();
1401
1402   for (add = addh->first; add ; add = next)
1403     {
1404       struct decision *old, *insert_before = NULL;
1405
1406       next = add->next;
1407
1408       /* The semantics of pattern matching state that the tests are
1409          done in the order given in the MD file so that if an insn
1410          matches two patterns, the first one will be used.  However,
1411          in practice, most, if not all, patterns are unambiguous so
1412          that their order is independent.  In that case, we can merge
1413          identical tests and group all similar modes and codes together.
1414
1415          Scan starting from the end of OLDH until we reach a point
1416          where we reach the head of the list or where we pass a
1417          pattern that could also be true if NEW is true.  If we find
1418          an identical pattern, we can merge them.  Also, record the
1419          last node that tests the same code and mode and the last one
1420          that tests just the same mode.
1421
1422          If we have no match, place NEW after the closest match we found.  */
1423
1424       for (old = oldh->last; old; old = old->prev)
1425         {
1426           if (nodes_identical (old, add))
1427             {
1428               merge_accept_insn (old, add);
1429               merge_trees (&old->success, &add->success);
1430               goto merged_nodes;
1431             }
1432
1433           if (maybe_both_true (old, add, 0))
1434             break;
1435
1436           /* Insert the nodes in DT test type order, which is roughly
1437              how expensive/important the test is.  Given that the tests
1438              are also ordered within the list, examining the first is
1439              sufficient.  */
1440           if ((int) add->tests->type < (int) old->tests->type)
1441             insert_before = old;
1442         }
1443
1444       if (insert_before == NULL)
1445         {
1446           add->next = NULL;
1447           add->prev = oldh->last;
1448           oldh->last->next = add;
1449           oldh->last = add;
1450         }
1451       else
1452         {
1453           if ((add->prev = insert_before->prev) != NULL)
1454             add->prev->next = add;
1455           else
1456             oldh->first = add;
1457           add->next = insert_before;
1458           insert_before->prev = add;
1459         }
1460
1461     merged_nodes:;
1462     }
1463 }
1464 \f
1465 /* Walk the tree looking for sub-nodes that perform common tests.
1466    Factor out the common test into a new node.  This enables us
1467    (depending on the test type) to emit switch statements later.  */
1468
1469 static void
1470 factor_tests (struct decision_head *head)
1471 {
1472   struct decision *first, *next;
1473
1474   for (first = head->first; first && first->next; first = next)
1475     {
1476       enum decision_type type;
1477       struct decision *new, *old_last;
1478
1479       type = first->tests->type;
1480       next = first->next;
1481
1482       /* Want at least two compatible sequential nodes.  */
1483       if (next->tests->type != type)
1484         continue;
1485
1486       /* Don't want all node types, just those we can turn into
1487          switch statements.  */
1488       if (type != DT_mode
1489           && type != DT_code
1490           && type != DT_veclen
1491           && type != DT_elt_zero_int
1492           && type != DT_elt_one_int
1493           && type != DT_elt_zero_wide_safe)
1494         continue;
1495
1496       /* If we'd been performing more than one test, create a new node
1497          below our first test.  */
1498       if (first->tests->next != NULL)
1499         {
1500           new = new_decision (first->position, &first->success);
1501           new->tests = first->tests->next;
1502           first->tests->next = NULL;
1503         }
1504
1505       /* Crop the node tree off after our first test.  */
1506       first->next = NULL;
1507       old_last = head->last;
1508       head->last = first;
1509
1510       /* For each compatible test, adjust to perform only one test in
1511          the top level node, then merge the node back into the tree.  */
1512       do
1513         {
1514           struct decision_head h;
1515
1516           if (next->tests->next != NULL)
1517             {
1518               new = new_decision (next->position, &next->success);
1519               new->tests = next->tests->next;
1520               next->tests->next = NULL;
1521             }
1522           new = next;
1523           next = next->next;
1524           new->next = NULL;
1525           h.first = h.last = new;
1526
1527           merge_trees (head, &h);
1528         }
1529       while (next && next->tests->type == type);
1530
1531       /* After we run out of compatible tests, graft the remaining nodes
1532          back onto the tree.  */
1533       if (next)
1534         {
1535           next->prev = head->last;
1536           head->last->next = next;
1537           head->last = old_last;
1538         }
1539     }
1540
1541   /* Recurse.  */
1542   for (first = head->first; first; first = first->next)
1543     factor_tests (&first->success);
1544 }
1545
1546 /* After factoring, try to simplify the tests on any one node.
1547    Tests that are useful for switch statements are recognizable
1548    by having only a single test on a node -- we'll be manipulating
1549    nodes with multiple tests:
1550
1551    If we have mode tests or code tests that are redundant with
1552    predicates, remove them.  */
1553
1554 static void
1555 simplify_tests (struct decision_head *head)
1556 {
1557   struct decision *tree;
1558
1559   for (tree = head->first; tree; tree = tree->next)
1560     {
1561       struct decision_test *a, *b;
1562
1563       a = tree->tests;
1564       b = a->next;
1565       if (b == NULL)
1566         continue;
1567
1568       /* Find a predicate node.  */
1569       while (b && b->type != DT_pred)
1570         b = b->next;
1571       if (b)
1572         {
1573           /* Due to how these tests are constructed, we don't even need
1574              to check that the mode and code are compatible -- they were
1575              generated from the predicate in the first place.  */
1576           while (a->type == DT_mode || a->type == DT_code)
1577             a = a->next;
1578           tree->tests = a;
1579         }
1580     }
1581
1582   /* Recurse.  */
1583   for (tree = head->first; tree; tree = tree->next)
1584     simplify_tests (&tree->success);
1585 }
1586
1587 /* Count the number of subnodes of HEAD.  If the number is high enough,
1588    make the first node in HEAD start a separate subroutine in the C code
1589    that is generated.  */
1590
1591 static int
1592 break_out_subroutines (struct decision_head *head, int initial)
1593 {
1594   int size = 0;
1595   struct decision *sub;
1596
1597   for (sub = head->first; sub; sub = sub->next)
1598     size += 1 + break_out_subroutines (&sub->success, 0);
1599
1600   if (size > SUBROUTINE_THRESHOLD && ! initial)
1601     {
1602       head->first->subroutine_number = ++next_subroutine_number;
1603       size = 1;
1604     }
1605   return size;
1606 }
1607
1608 /* For each node p, find the next alternative that might be true
1609    when p is true.  */
1610
1611 static void
1612 find_afterward (struct decision_head *head, struct decision *real_afterward)
1613 {
1614   struct decision *p, *q, *afterward;
1615
1616   /* We can't propagate alternatives across subroutine boundaries.
1617      This is not incorrect, merely a minor optimization loss.  */
1618
1619   p = head->first;
1620   afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1621
1622   for ( ; p ; p = p->next)
1623     {
1624       /* Find the next node that might be true if this one fails.  */
1625       for (q = p->next; q ; q = q->next)
1626         if (maybe_both_true (p, q, 1))
1627           break;
1628
1629       /* If we reached the end of the list without finding one,
1630          use the incoming afterward position.  */
1631       if (!q)
1632         q = afterward;
1633       p->afterward = q;
1634       if (q)
1635         q->need_label = 1;
1636     }
1637
1638   /* Recurse.  */
1639   for (p = head->first; p ; p = p->next)
1640     if (p->success.first)
1641       find_afterward (&p->success, p->afterward);
1642
1643   /* When we are generating a subroutine, record the real afterward
1644      position in the first node where write_tree can find it, and we
1645      can do the right thing at the subroutine call site.  */
1646   p = head->first;
1647   if (p->subroutine_number > 0)
1648     p->afterward = real_afterward;
1649 }
1650 \f
1651 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1652    actions are necessary to move to NEWPOS.  If we fail to move to the
1653    new state, branch to node AFTERWARD if nonzero, otherwise return.
1654
1655    Failure to move to the new state can only occur if we are trying to
1656    match multiple insns and we try to step past the end of the stream.  */
1657
1658 static void
1659 change_state (const char *oldpos, const char *newpos,
1660               struct decision *afterward, const char *indent)
1661 {
1662   int odepth = strlen (oldpos);
1663   int ndepth = strlen (newpos);
1664   int depth;
1665   int old_has_insn, new_has_insn;
1666
1667   /* Pop up as many levels as necessary.  */
1668   for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1669     continue;
1670
1671   /* Hunt for the last [A-Z] in both strings.  */
1672   for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1673     if (ISUPPER (oldpos[old_has_insn]))
1674       break;
1675   for (new_has_insn = ndepth - 1; new_has_insn >= 0; --new_has_insn)
1676     if (ISUPPER (newpos[new_has_insn]))
1677       break;
1678
1679   /* Go down to desired level.  */
1680   while (depth < ndepth)
1681     {
1682       /* It's a different insn from the first one.  */
1683       if (ISUPPER (newpos[depth]))
1684         {
1685           /* We can only fail if we're moving down the tree.  */
1686           if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1687             {
1688               printf ("%stem = peep2_next_insn (%d);\n",
1689                       indent, newpos[depth] - 'A');
1690             }
1691           else
1692             {
1693               printf ("%stem = peep2_next_insn (%d);\n",
1694                       indent, newpos[depth] - 'A');
1695               printf ("%sif (tem == NULL_RTX)\n", indent);
1696               if (afterward)
1697                 printf ("%s  goto L%d;\n", indent, afterward->number);
1698               else
1699                 printf ("%s  goto ret0;\n", indent);
1700             }
1701           printf ("%sx%d = PATTERN (tem);\n", indent, depth + 1);
1702         }
1703       else if (ISLOWER (newpos[depth]))
1704         printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1705                 indent, depth + 1, depth, newpos[depth] - 'a');
1706       else
1707         printf ("%sx%d = XEXP (x%d, %c);\n",
1708                 indent, depth + 1, depth, newpos[depth]);
1709       ++depth;
1710     }
1711 }
1712 \f
1713 /* Print the enumerator constant for CODE -- the upcase version of
1714    the name.  */
1715
1716 static void
1717 print_code (enum rtx_code code)
1718 {
1719   const char *p;
1720   for (p = GET_RTX_NAME (code); *p; p++)
1721     putchar (TOUPPER (*p));
1722 }
1723
1724 /* Emit code to cross an afterward link -- change state and branch.  */
1725
1726 static void
1727 write_afterward (struct decision *start, struct decision *afterward,
1728                  const char *indent)
1729 {
1730   if (!afterward || start->subroutine_number > 0)
1731     printf("%sgoto ret0;\n", indent);
1732   else
1733     {
1734       change_state (start->position, afterward->position, NULL, indent);
1735       printf ("%sgoto L%d;\n", indent, afterward->number);
1736     }
1737 }
1738
1739 /* Emit a switch statement, if possible, for an initial sequence of
1740    nodes at START.  Return the first node yet untested.  */
1741
1742 static struct decision *
1743 write_switch (struct decision *start, int depth)
1744 {
1745   struct decision *p = start;
1746   enum decision_type type = p->tests->type;
1747   struct decision *needs_label = NULL;
1748
1749   /* If we have two or more nodes in sequence that test the same one
1750      thing, we may be able to use a switch statement.  */
1751
1752   if (!p->next
1753       || p->tests->next
1754       || p->next->tests->type != type
1755       || p->next->tests->next
1756       || nodes_identical_1 (p->tests, p->next->tests))
1757     return p;
1758
1759   /* DT_code is special in that we can do interesting things with
1760      known predicates at the same time.  */
1761   if (type == DT_code)
1762     {
1763       char codemap[NUM_RTX_CODE];
1764       struct decision *ret;
1765       RTX_CODE code;
1766
1767       memset (codemap, 0, sizeof(codemap));
1768
1769       printf ("  switch (GET_CODE (x%d))\n    {\n", depth);
1770       code = p->tests->u.code;
1771       do
1772         {
1773           if (p != start && p->need_label && needs_label == NULL)
1774             needs_label = p;
1775
1776           printf ("    case ");
1777           print_code (code);
1778           printf (":\n      goto L%d;\n", p->success.first->number);
1779           p->success.first->need_label = 1;
1780
1781           codemap[code] = 1;
1782           p = p->next;
1783         }
1784       while (p
1785              && ! p->tests->next
1786              && p->tests->type == DT_code
1787              && ! codemap[code = p->tests->u.code]);
1788
1789       /* If P is testing a predicate that we know about and we haven't
1790          seen any of the codes that are valid for the predicate, we can
1791          write a series of "case" statement, one for each possible code.
1792          Since we are already in a switch, these redundant tests are very
1793          cheap and will reduce the number of predicates called.  */
1794
1795       /* Note that while we write out cases for these predicates here,
1796          we don't actually write the test here, as it gets kinda messy.
1797          It is trivial to leave this to later by telling our caller that
1798          we only processed the CODE tests.  */
1799       if (needs_label != NULL)
1800         ret = needs_label;
1801       else
1802         ret = p;
1803
1804       while (p && p->tests->type == DT_pred
1805              && p->tests->u.pred.index >= 0)
1806         {
1807           const RTX_CODE *c;
1808
1809           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1810             if (codemap[(int) *c] != 0)
1811               goto pred_done;
1812
1813           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1814             {
1815               printf ("    case ");
1816               print_code (*c);
1817               printf (":\n");
1818               codemap[(int) *c] = 1;
1819             }
1820
1821           printf ("      goto L%d;\n", p->number);
1822           p->need_label = 1;
1823           p = p->next;
1824         }
1825
1826     pred_done:
1827       /* Make the default case skip the predicates we managed to match.  */
1828
1829       printf ("    default:\n");
1830       if (p != ret)
1831         {
1832           if (p)
1833             {
1834               printf ("      goto L%d;\n", p->number);
1835               p->need_label = 1;
1836             }
1837           else
1838             write_afterward (start, start->afterward, "      ");
1839         }
1840       else
1841         printf ("     break;\n");
1842       printf ("   }\n");
1843
1844       return ret;
1845     }
1846   else if (type == DT_mode
1847            || type == DT_veclen
1848            || type == DT_elt_zero_int
1849            || type == DT_elt_one_int
1850            || type == DT_elt_zero_wide_safe)
1851     {
1852       const char *indent = "";
1853
1854       /* We cast switch parameter to integer, so we must ensure that the value
1855          fits.  */
1856       if (type == DT_elt_zero_wide_safe)
1857         {
1858           indent = "  ";
1859           printf("  if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n", depth, depth);
1860         }
1861       printf ("%s  switch (", indent);
1862       switch (type)
1863         {
1864         case DT_mode:
1865           printf ("GET_MODE (x%d)", depth);
1866           break;
1867         case DT_veclen:
1868           printf ("XVECLEN (x%d, 0)", depth);
1869           break;
1870         case DT_elt_zero_int:
1871           printf ("XINT (x%d, 0)", depth);
1872           break;
1873         case DT_elt_one_int:
1874           printf ("XINT (x%d, 1)", depth);
1875           break;
1876         case DT_elt_zero_wide_safe:
1877           /* Convert result of XWINT to int for portability since some C
1878              compilers won't do it and some will.  */
1879           printf ("(int) XWINT (x%d, 0)", depth);
1880           break;
1881         default:
1882           abort ();
1883         }
1884       printf (")\n%s    {\n", indent);
1885
1886       do
1887         {
1888           /* Merge trees will not unify identical nodes if their
1889              sub-nodes are at different levels.  Thus we must check
1890              for duplicate cases.  */
1891           struct decision *q;
1892           for (q = start; q != p; q = q->next)
1893             if (nodes_identical_1 (p->tests, q->tests))
1894               goto case_done;
1895
1896           if (p != start && p->need_label && needs_label == NULL)
1897             needs_label = p;
1898
1899           printf ("%s    case ", indent);
1900           switch (type)
1901             {
1902             case DT_mode:
1903               printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1904               break;
1905             case DT_veclen:
1906               printf ("%d", p->tests->u.veclen);
1907               break;
1908             case DT_elt_zero_int:
1909             case DT_elt_one_int:
1910             case DT_elt_zero_wide:
1911             case DT_elt_zero_wide_safe:
1912               printf (HOST_WIDE_INT_PRINT_DEC_C, p->tests->u.intval);
1913               break;
1914             default:
1915               abort ();
1916             }
1917           printf (":\n%s      goto L%d;\n", indent, p->success.first->number);
1918           p->success.first->need_label = 1;
1919
1920           p = p->next;
1921         }
1922       while (p && p->tests->type == type && !p->tests->next);
1923
1924     case_done:
1925       printf ("%s    default:\n%s      break;\n%s    }\n",
1926               indent, indent, indent);
1927
1928       return needs_label != NULL ? needs_label : p;
1929     }
1930   else
1931     {
1932       /* None of the other tests are amenable.  */
1933       return p;
1934     }
1935 }
1936
1937 /* Emit code for one test.  */
1938
1939 static void
1940 write_cond (struct decision_test *p, int depth,
1941             enum routine_type subroutine_type)
1942 {
1943   switch (p->type)
1944     {
1945     case DT_mode:
1946       printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1947       break;
1948
1949     case DT_code:
1950       printf ("GET_CODE (x%d) == ", depth);
1951       print_code (p->u.code);
1952       break;
1953
1954     case DT_veclen:
1955       printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1956       break;
1957
1958     case DT_elt_zero_int:
1959       printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1960       break;
1961
1962     case DT_elt_one_int:
1963       printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1964       break;
1965
1966     case DT_elt_zero_wide:
1967     case DT_elt_zero_wide_safe:
1968       printf ("XWINT (x%d, 0) == ", depth);
1969       printf (HOST_WIDE_INT_PRINT_DEC_C, p->u.intval);
1970       break;
1971
1972     case DT_veclen_ge:
1973       printf ("XVECLEN (x%d, 0) >= %d", depth, p->u.veclen);
1974       break;
1975
1976     case DT_dup:
1977       printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1978       break;
1979
1980     case DT_pred:
1981       printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1982               GET_MODE_NAME (p->u.pred.mode));
1983       break;
1984
1985     case DT_c_test:
1986       printf ("(%s)", p->u.c_test);
1987       break;
1988
1989     case DT_accept_insn:
1990       switch (subroutine_type)
1991         {
1992         case RECOG:
1993           if (p->u.insn.num_clobbers_to_add == 0)
1994             abort ();
1995           printf ("pnum_clobbers != NULL");
1996           break;
1997
1998         default:
1999           abort ();
2000         }
2001       break;
2002
2003     default:
2004       abort ();
2005     }
2006 }
2007
2008 /* Emit code for one action.  The previous tests have succeeded;
2009    TEST is the last of the chain.  In the normal case we simply
2010    perform a state change.  For the `accept' tests we must do more work.  */
2011
2012 static void
2013 write_action (struct decision *p, struct decision_test *test,
2014               int depth, int uncond, struct decision *success,
2015               enum routine_type subroutine_type)
2016 {
2017   const char *indent;
2018   int want_close = 0;
2019
2020   if (uncond)
2021     indent = "  ";
2022   else if (test->type == DT_accept_op || test->type == DT_accept_insn)
2023     {
2024       fputs ("    {\n", stdout);
2025       indent = "      ";
2026       want_close = 1;
2027     }
2028   else
2029     indent = "    ";
2030
2031   if (test->type == DT_accept_op)
2032     {
2033       printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
2034
2035       /* Only allow DT_accept_insn to follow.  */
2036       if (test->next)
2037         {
2038           test = test->next;
2039           if (test->type != DT_accept_insn)
2040             abort ();
2041         }
2042     }
2043
2044   /* Sanity check that we're now at the end of the list of tests.  */
2045   if (test->next)
2046     abort ();
2047
2048   if (test->type == DT_accept_insn)
2049     {
2050       switch (subroutine_type)
2051         {
2052         case RECOG:
2053           if (test->u.insn.num_clobbers_to_add != 0)
2054             printf ("%s*pnum_clobbers = %d;\n",
2055                     indent, test->u.insn.num_clobbers_to_add);
2056           printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
2057           break;
2058
2059         case SPLIT:
2060           printf ("%sreturn gen_split_%d (operands);\n",
2061                   indent, test->u.insn.code_number);
2062           break;
2063
2064         case PEEPHOLE2:
2065           {
2066             int match_len = 0, i;
2067
2068             for (i = strlen (p->position) - 1; i >= 0; --i)
2069               if (ISUPPER (p->position[i]))
2070                 {
2071                   match_len = p->position[i] - 'A';
2072                   break;
2073                 }
2074             printf ("%s*_pmatch_len = %d;\n", indent, match_len);
2075             printf ("%stem = gen_peephole2_%d (insn, operands);\n",
2076                     indent, test->u.insn.code_number);
2077             printf ("%sif (tem != 0)\n%s  return tem;\n", indent, indent);
2078           }
2079           break;
2080
2081         default:
2082           abort ();
2083         }
2084     }
2085   else
2086     {
2087       printf("%sgoto L%d;\n", indent, success->number);
2088       success->need_label = 1;
2089     }
2090
2091   if (want_close)
2092     fputs ("    }\n", stdout);
2093 }
2094
2095 /* Return 1 if the test is always true and has no fallthru path.  Return -1
2096    if the test does have a fallthru path, but requires that the condition be
2097    terminated.  Otherwise return 0 for a normal test.  */
2098 /* ??? is_unconditional is a stupid name for a tri-state function.  */
2099
2100 static int
2101 is_unconditional (struct decision_test *t, enum routine_type subroutine_type)
2102 {
2103   if (t->type == DT_accept_op)
2104     return 1;
2105
2106   if (t->type == DT_accept_insn)
2107     {
2108       switch (subroutine_type)
2109         {
2110         case RECOG:
2111           return (t->u.insn.num_clobbers_to_add == 0);
2112         case SPLIT:
2113           return 1;
2114         case PEEPHOLE2:
2115           return -1;
2116         default:
2117           abort ();
2118         }
2119     }
2120
2121   return 0;
2122 }
2123
2124 /* Emit code for one node -- the conditional and the accompanying action.
2125    Return true if there is no fallthru path.  */
2126
2127 static int
2128 write_node (struct decision *p, int depth,
2129             enum routine_type subroutine_type)
2130 {
2131   struct decision_test *test, *last_test;
2132   int uncond;
2133
2134   last_test = test = p->tests;
2135   uncond = is_unconditional (test, subroutine_type);
2136   if (uncond == 0)
2137     {
2138       printf ("  if (");
2139       write_cond (test, depth, subroutine_type);
2140
2141       while ((test = test->next) != NULL)
2142         {
2143           int uncond2;
2144
2145           last_test = test;
2146           uncond2 = is_unconditional (test, subroutine_type);
2147           if (uncond2 != 0)
2148             break;
2149
2150           printf ("\n      && ");
2151           write_cond (test, depth, subroutine_type);
2152         }
2153
2154       printf (")\n");
2155     }
2156
2157   write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
2158
2159   return uncond > 0;
2160 }
2161
2162 /* Emit code for all of the sibling nodes of HEAD.  */
2163
2164 static void
2165 write_tree_1 (struct decision_head *head, int depth,
2166               enum routine_type subroutine_type)
2167 {
2168   struct decision *p, *next;
2169   int uncond = 0;
2170
2171   for (p = head->first; p ; p = next)
2172     {
2173       /* The label for the first element was printed in write_tree.  */
2174       if (p != head->first && p->need_label)
2175         OUTPUT_LABEL (" ", p->number);
2176
2177       /* Attempt to write a switch statement for a whole sequence.  */
2178       next = write_switch (p, depth);
2179       if (p != next)
2180         uncond = 0;
2181       else
2182         {
2183           /* Failed -- fall back and write one node.  */
2184           uncond = write_node (p, depth, subroutine_type);
2185           next = p->next;
2186         }
2187     }
2188
2189   /* Finished with this chain.  Close a fallthru path by branching
2190      to the afterward node.  */
2191   if (! uncond)
2192     write_afterward (head->last, head->last->afterward, "  ");
2193 }
2194
2195 /* Write out the decision tree starting at HEAD.  PREVPOS is the
2196    position at the node that branched to this node.  */
2197
2198 static void
2199 write_tree (struct decision_head *head, const char *prevpos,
2200             enum routine_type type, int initial)
2201 {
2202   struct decision *p = head->first;
2203
2204   putchar ('\n');
2205   if (p->need_label)
2206     OUTPUT_LABEL (" ", p->number);
2207
2208   if (! initial && p->subroutine_number > 0)
2209     {
2210       static const char * const name_prefix[] = {
2211           "recog", "split", "peephole2"
2212       };
2213
2214       static const char * const call_suffix[] = {
2215           ", pnum_clobbers", "", ", _pmatch_len"
2216       };
2217
2218       /* This node has been broken out into a separate subroutine.
2219          Call it, test the result, and branch accordingly.  */
2220
2221       if (p->afterward)
2222         {
2223           printf ("  tem = %s_%d (x0, insn%s);\n",
2224                   name_prefix[type], p->subroutine_number, call_suffix[type]);
2225           if (IS_SPLIT (type))
2226             printf ("  if (tem != 0)\n    return tem;\n");
2227           else
2228             printf ("  if (tem >= 0)\n    return tem;\n");
2229
2230           change_state (p->position, p->afterward->position, NULL, "  ");
2231           printf ("  goto L%d;\n", p->afterward->number);
2232         }
2233       else
2234         {
2235           printf ("  return %s_%d (x0, insn%s);\n",
2236                   name_prefix[type], p->subroutine_number, call_suffix[type]);
2237         }
2238     }
2239   else
2240     {
2241       int depth = strlen (p->position);
2242
2243       change_state (prevpos, p->position, head->last->afterward, "  ");
2244       write_tree_1 (head, depth, type);
2245
2246       for (p = head->first; p; p = p->next)
2247         if (p->success.first)
2248           write_tree (&p->success, p->position, type, 0);
2249     }
2250 }
2251
2252 /* Write out a subroutine of type TYPE to do comparisons starting at
2253    node TREE.  */
2254
2255 static void
2256 write_subroutine (struct decision_head *head, enum routine_type type)
2257 {
2258   int subfunction = head->first ? head->first->subroutine_number : 0;
2259   const char *s_or_e;
2260   char extension[32];
2261   int i;
2262
2263   s_or_e = subfunction ? "static " : "";
2264
2265   if (subfunction)
2266     sprintf (extension, "_%d", subfunction);
2267   else if (type == RECOG)
2268     extension[0] = '\0';
2269   else
2270     strcpy (extension, "_insns");
2271
2272   switch (type)
2273     {
2274     case RECOG:
2275       printf ("%sint\n\
2276 recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", s_or_e, extension);
2277       break;
2278     case SPLIT:
2279       printf ("%srtx\n\
2280 split%s (rtx x0 ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED)\n",
2281               s_or_e, extension);
2282       break;
2283     case PEEPHOLE2:
2284       printf ("%srtx\n\
2285 peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n",
2286               s_or_e, extension);
2287       break;
2288     }
2289
2290   printf ("{\n  rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2291   for (i = 1; i <= max_depth; i++)
2292     printf ("  rtx x%d ATTRIBUTE_UNUSED;\n", i);
2293
2294   printf ("  %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2295
2296   if (!subfunction)
2297     printf ("  recog_data.insn = NULL_RTX;\n");
2298
2299   if (head->first)
2300     write_tree (head, "", type, 1);
2301   else
2302     printf ("  goto ret0;\n");
2303
2304   printf (" ret0:\n  return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2305 }
2306
2307 /* In break_out_subroutines, we discovered the boundaries for the
2308    subroutines, but did not write them out.  Do so now.  */
2309
2310 static void
2311 write_subroutines (struct decision_head *head, enum routine_type type)
2312 {
2313   struct decision *p;
2314
2315   for (p = head->first; p ; p = p->next)
2316     if (p->success.first)
2317       write_subroutines (&p->success, type);
2318
2319   if (head->first->subroutine_number > 0)
2320     write_subroutine (head, type);
2321 }
2322
2323 /* Begin the output file.  */
2324
2325 static void
2326 write_header (void)
2327 {
2328   puts ("\
2329 /* Generated automatically by the program `genrecog' from the target\n\
2330    machine description file.  */\n\
2331 \n\
2332 #include \"config.h\"\n\
2333 #include \"system.h\"\n\
2334 #include \"coretypes.h\"\n\
2335 #include \"tm.h\"\n\
2336 #include \"rtl.h\"\n\
2337 #include \"tm_p.h\"\n\
2338 #include \"function.h\"\n\
2339 #include \"insn-config.h\"\n\
2340 #include \"recog.h\"\n\
2341 #include \"real.h\"\n\
2342 #include \"output.h\"\n\
2343 #include \"flags.h\"\n\
2344 #include \"hard-reg-set.h\"\n\
2345 #include \"resource.h\"\n\
2346 #include \"toplev.h\"\n\
2347 #include \"reload.h\"\n\
2348 \n");
2349
2350   puts ("\n\
2351 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2352    X0 is a valid instruction.\n\
2353 \n\
2354    recog returns -1 if the rtx is not valid.  If the rtx is valid, recog\n\
2355    returns a nonnegative number which is the insn code number for the\n\
2356    pattern that matched.  This is the same as the order in the machine\n\
2357    description of the entry that matched.  This number can be used as an\n\
2358    index into `insn_data' and other tables.\n");
2359   puts ("\
2360    The third argument to recog is an optional pointer to an int.  If\n\
2361    present, recog will accept a pattern if it matches except for missing\n\
2362    CLOBBER expressions at the end.  In that case, the value pointed to by\n\
2363    the optional pointer will be set to the number of CLOBBERs that need\n\
2364    to be added (it should be initialized to zero by the caller).  If it");
2365   puts ("\
2366    is set nonzero, the caller should allocate a PARALLEL of the\n\
2367    appropriate size, copy the initial entries, and call add_clobbers\n\
2368    (found in insn-emit.c) to fill in the CLOBBERs.\n\
2369 ");
2370
2371   puts ("\n\
2372    The function split_insns returns 0 if the rtl could not\n\
2373    be split or the split rtl as an INSN list if it can be.\n\
2374 \n\
2375    The function peephole2_insns returns 0 if the rtl could not\n\
2376    be matched. If there was a match, the new rtl is returned in an INSN list,\n\
2377    and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2378 */\n\n");
2379 }
2380
2381 \f
2382 /* Construct and return a sequence of decisions
2383    that will recognize INSN.
2384
2385    TYPE says what type of routine we are recognizing (RECOG or SPLIT).  */
2386
2387 static struct decision_head
2388 make_insn_sequence (rtx insn, enum routine_type type)
2389 {
2390   rtx x;
2391   const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2392   int truth = maybe_eval_c_test (c_test);
2393   struct decision *last;
2394   struct decision_test *test, **place;
2395   struct decision_head head;
2396   char c_test_pos[2];
2397
2398   /* We should never see an insn whose C test is false at compile time.  */
2399   if (truth == 0)
2400     abort ();
2401
2402   record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2403
2404   c_test_pos[0] = '\0';
2405   if (type == PEEPHOLE2)
2406     {
2407       int i, j;
2408
2409       /* peephole2 gets special treatment:
2410          - X always gets an outer parallel even if it's only one entry
2411          - we remove all traces of outer-level match_scratch and match_dup
2412            expressions here.  */
2413       x = rtx_alloc (PARALLEL);
2414       PUT_MODE (x, VOIDmode);
2415       XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2416       for (i = j = 0; i < XVECLEN (insn, 0); i++)
2417         {
2418           rtx tmp = XVECEXP (insn, 0, i);
2419           if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2420             {
2421               XVECEXP (x, 0, j) = tmp;
2422               j++;
2423             }
2424         }
2425       XVECLEN (x, 0) = j;
2426
2427       c_test_pos[0] = 'A' + j - 1;
2428       c_test_pos[1] = '\0';
2429     }
2430   else if (XVECLEN (insn, type == RECOG) == 1)
2431     x = XVECEXP (insn, type == RECOG, 0);
2432   else
2433     {
2434       x = rtx_alloc (PARALLEL);
2435       XVEC (x, 0) = XVEC (insn, type == RECOG);
2436       PUT_MODE (x, VOIDmode);
2437     }
2438
2439   validate_pattern (x, insn, NULL_RTX, 0);
2440
2441   memset(&head, 0, sizeof(head));
2442   last = add_to_sequence (x, &head, "", type, 1);
2443
2444   /* Find the end of the test chain on the last node.  */
2445   for (test = last->tests; test->next; test = test->next)
2446     continue;
2447   place = &test->next;
2448
2449   /* Skip the C test if it's known to be true at compile time.  */
2450   if (truth == -1)
2451     {
2452       /* Need a new node if we have another test to add.  */
2453       if (test->type == DT_accept_op)
2454         {
2455           last = new_decision (c_test_pos, &last->success);
2456           place = &last->tests;
2457         }
2458       test = new_decision_test (DT_c_test, &place);
2459       test->u.c_test = c_test;
2460     }
2461
2462   test = new_decision_test (DT_accept_insn, &place);
2463   test->u.insn.code_number = next_insn_code;
2464   test->u.insn.lineno = pattern_lineno;
2465   test->u.insn.num_clobbers_to_add = 0;
2466
2467   switch (type)
2468     {
2469     case RECOG:
2470       /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends
2471          with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2472          If so, set up to recognize the pattern without these CLOBBERs.  */
2473
2474       if (GET_CODE (x) == PARALLEL)
2475         {
2476           int i;
2477
2478           /* Find the last non-clobber in the parallel.  */
2479           for (i = XVECLEN (x, 0); i > 0; i--)
2480             {
2481               rtx y = XVECEXP (x, 0, i - 1);
2482               if (GET_CODE (y) != CLOBBER
2483                   || (GET_CODE (XEXP (y, 0)) != REG
2484                       && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2485                 break;
2486             }
2487
2488           if (i != XVECLEN (x, 0))
2489             {
2490               rtx new;
2491               struct decision_head clobber_head;
2492
2493               /* Build a similar insn without the clobbers.  */
2494               if (i == 1)
2495                 new = XVECEXP (x, 0, 0);
2496               else
2497                 {
2498                   int j;
2499
2500                   new = rtx_alloc (PARALLEL);
2501                   XVEC (new, 0) = rtvec_alloc (i);
2502                   for (j = i - 1; j >= 0; j--)
2503                     XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2504                 }
2505
2506               /* Recognize it.  */
2507               memset (&clobber_head, 0, sizeof(clobber_head));
2508               last = add_to_sequence (new, &clobber_head, "", type, 1);
2509
2510               /* Find the end of the test chain on the last node.  */
2511               for (test = last->tests; test->next; test = test->next)
2512                 continue;
2513
2514               /* We definitely have a new test to add -- create a new
2515                  node if needed.  */
2516               place = &test->next;
2517               if (test->type == DT_accept_op)
2518                 {
2519                   last = new_decision ("", &last->success);
2520                   place = &last->tests;
2521                 }
2522
2523               /* Skip the C test if it's known to be true at compile
2524                  time.  */
2525               if (truth == -1)
2526                 {
2527                   test = new_decision_test (DT_c_test, &place);
2528                   test->u.c_test = c_test;
2529                 }
2530
2531               test = new_decision_test (DT_accept_insn, &place);
2532               test->u.insn.code_number = next_insn_code;
2533               test->u.insn.lineno = pattern_lineno;
2534               test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2535
2536               merge_trees (&head, &clobber_head);
2537             }
2538         }
2539       break;
2540
2541     case SPLIT:
2542       /* Define the subroutine we will call below and emit in genemit.  */
2543       printf ("extern rtx gen_split_%d (rtx *);\n", next_insn_code);
2544       break;
2545
2546     case PEEPHOLE2:
2547       /* Define the subroutine we will call below and emit in genemit.  */
2548       printf ("extern rtx gen_peephole2_%d (rtx, rtx *);\n",
2549               next_insn_code);
2550       break;
2551     }
2552
2553   return head;
2554 }
2555
2556 static void
2557 process_tree (struct decision_head *head, enum routine_type subroutine_type)
2558 {
2559   if (head->first == NULL)
2560     {
2561       /* We can elide peephole2_insns, but not recog or split_insns.  */
2562       if (subroutine_type == PEEPHOLE2)
2563         return;
2564     }
2565   else
2566     {
2567       factor_tests (head);
2568
2569       next_subroutine_number = 0;
2570       break_out_subroutines (head, 1);
2571       find_afterward (head, NULL);
2572
2573       /* We run this after find_afterward, because find_afterward needs
2574          the redundant DT_mode tests on predicates to determine whether
2575          two tests can both be true or not.  */
2576       simplify_tests(head);
2577
2578       write_subroutines (head, subroutine_type);
2579     }
2580
2581   write_subroutine (head, subroutine_type);
2582 }
2583 \f
2584 extern int main (int, char **);
2585
2586 int
2587 main (int argc, char **argv)
2588 {
2589   rtx desc;
2590   struct decision_head recog_tree, split_tree, peephole2_tree, h;
2591
2592   progname = "genrecog";
2593
2594   memset (&recog_tree, 0, sizeof recog_tree);
2595   memset (&split_tree, 0, sizeof split_tree);
2596   memset (&peephole2_tree, 0, sizeof peephole2_tree);
2597
2598   if (argc <= 1)
2599     fatal ("no input file name");
2600
2601   if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
2602     return (FATAL_EXIT_CODE);
2603
2604   next_insn_code = 0;
2605   next_index = 0;
2606
2607   write_header ();
2608
2609   /* Read the machine description.  */
2610
2611   while (1)
2612     {
2613       desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2614       if (desc == NULL)
2615         break;
2616
2617       if (GET_CODE (desc) == DEFINE_INSN)
2618         {
2619           h = make_insn_sequence (desc, RECOG);
2620           merge_trees (&recog_tree, &h);
2621         }
2622       else if (GET_CODE (desc) == DEFINE_SPLIT)
2623         {
2624           h = make_insn_sequence (desc, SPLIT);
2625           merge_trees (&split_tree, &h);
2626         }
2627       else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2628         {
2629           h = make_insn_sequence (desc, PEEPHOLE2);
2630           merge_trees (&peephole2_tree, &h);
2631         }
2632
2633       next_index++;
2634     }
2635
2636   if (error_count)
2637     return FATAL_EXIT_CODE;
2638
2639   puts ("\n\n");
2640
2641   process_tree (&recog_tree, RECOG);
2642   process_tree (&split_tree, SPLIT);
2643   process_tree (&peephole2_tree, PEEPHOLE2);
2644
2645   fflush (stdout);
2646   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2647 }
2648 \f
2649 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
2650 const char *
2651 get_insn_name (int code)
2652 {
2653   if (code < insn_name_ptr_size)
2654     return insn_name_ptr[code];
2655   else
2656     return NULL;
2657 }
2658
2659 static void
2660 record_insn_name (int code, const char *name)
2661 {
2662   static const char *last_real_name = "insn";
2663   static int last_real_code = 0;
2664   char *new;
2665
2666   if (insn_name_ptr_size <= code)
2667     {
2668       int new_size;
2669       new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2670       insn_name_ptr =
2671         (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2672       memset (insn_name_ptr + insn_name_ptr_size, 0,
2673               sizeof(char *) * (new_size - insn_name_ptr_size));
2674       insn_name_ptr_size = new_size;
2675     }
2676
2677   if (!name || name[0] == '\0')
2678     {
2679       new = xmalloc (strlen (last_real_name) + 10);
2680       sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2681     }
2682   else
2683     {
2684       last_real_name = new = xstrdup (name);
2685       last_real_code = code;
2686     }
2687
2688   insn_name_ptr[code] = new;
2689 }
2690 \f
2691 static void
2692 debug_decision_2 (struct decision_test *test)
2693 {
2694   switch (test->type)
2695     {
2696     case DT_mode:
2697       fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2698       break;
2699     case DT_code:
2700       fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2701       break;
2702     case DT_veclen:
2703       fprintf (stderr, "veclen=%d", test->u.veclen);
2704       break;
2705     case DT_elt_zero_int:
2706       fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2707       break;
2708     case DT_elt_one_int:
2709       fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2710       break;
2711     case DT_elt_zero_wide:
2712       fprintf (stderr, "elt0_w=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2713       break;
2714     case DT_elt_zero_wide_safe:
2715       fprintf (stderr, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2716       break;
2717     case DT_veclen_ge:
2718       fprintf (stderr, "veclen>=%d", test->u.veclen);
2719       break;
2720     case DT_dup:
2721       fprintf (stderr, "dup=%d", test->u.dup);
2722       break;
2723     case DT_pred:
2724       fprintf (stderr, "pred=(%s,%s)",
2725                test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2726       break;
2727     case DT_c_test:
2728       {
2729         char sub[16+4];
2730         strncpy (sub, test->u.c_test, sizeof(sub));
2731         memcpy (sub+16, "...", 4);
2732         fprintf (stderr, "c_test=\"%s\"", sub);
2733       }
2734       break;
2735     case DT_accept_op:
2736       fprintf (stderr, "A_op=%d", test->u.opno);
2737       break;
2738     case DT_accept_insn:
2739       fprintf (stderr, "A_insn=(%d,%d)",
2740                test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2741       break;
2742
2743     default:
2744       abort ();
2745     }
2746 }
2747
2748 static void
2749 debug_decision_1 (struct decision *d, int indent)
2750 {
2751   int i;
2752   struct decision_test *test;
2753
2754   if (d == NULL)
2755     {
2756       for (i = 0; i < indent; ++i)
2757         putc (' ', stderr);
2758       fputs ("(nil)\n", stderr);
2759       return;
2760     }
2761
2762   for (i = 0; i < indent; ++i)
2763     putc (' ', stderr);
2764
2765   putc ('{', stderr);
2766   test = d->tests;
2767   if (test)
2768     {
2769       debug_decision_2 (test);
2770       while ((test = test->next) != NULL)
2771         {
2772           fputs (" + ", stderr);
2773           debug_decision_2 (test);
2774         }
2775     }
2776   fprintf (stderr, "} %d n %d a %d\n", d->number,
2777            (d->next ? d->next->number : -1),
2778            (d->afterward ? d->afterward->number : -1));
2779 }
2780
2781 static void
2782 debug_decision_0 (struct decision *d, int indent, int maxdepth)
2783 {
2784   struct decision *n;
2785   int i;
2786
2787   if (maxdepth < 0)
2788     return;
2789   if (d == NULL)
2790     {
2791       for (i = 0; i < indent; ++i)
2792         putc (' ', stderr);
2793       fputs ("(nil)\n", stderr);
2794       return;
2795     }
2796
2797   debug_decision_1 (d, indent);
2798   for (n = d->success.first; n ; n = n->next)
2799     debug_decision_0 (n, indent + 2, maxdepth - 1);
2800 }
2801
2802 void
2803 debug_decision (struct decision *d)
2804 {
2805   debug_decision_0 (d, 0, 1000000);
2806 }
2807
2808 void
2809 debug_decision_list (struct decision *d)
2810 {
2811   while (d)
2812     {
2813       debug_decision_0 (d, 0, 0);
2814       d = d->next;
2815     }
2816 }