OSDN Git Service

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