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