aarch64 - Set the mode for the unspec in speculation_tracker insn.
[platform/upstream/linaro-gcc.git] / gcc / genrecog.c
1 /* Generate code from machine description to recognize rtl as insns.
2    Copyright (C) 1987-2016 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
6    GCC is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
11    GCC is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14    License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19
20
21 /* This program is used to produce insn-recog.c, which contains a
22    function called `recog' plus its subroutines.  These functions
23    contain a decision tree that recognizes whether an rtx, the
24    argument given to recog, is a valid instruction.
25
26    recog returns -1 if the rtx is not valid.  If the rtx is valid,
27    recog returns a nonnegative number which is the insn code number
28    for the pattern that matched.  This is the same as the order in the
29    machine description of the entry that matched.  This number can be
30    used as an index into various insn_* tables, such as insn_template,
31    insn_outfun, and insn_n_operands (found in insn-output.c).
32
33    The third argument to recog is an optional pointer to an int.  If
34    present, recog will accept a pattern if it matches except for
35    missing CLOBBER expressions at the end.  In that case, the value
36    pointed to by the optional pointer will be set to the number of
37    CLOBBERs that need to be added (it should be initialized to zero by
38    the caller).  If it is set nonzero, the caller should allocate a
39    PARALLEL of the appropriate size, copy the initial entries, and
40    call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
41
42    This program also generates the function `split_insns', which
43    returns 0 if the rtl could not be split, or it returns the split
44    rtl as an INSN list.
45
46    This program also generates the function `peephole2_insns', which
47    returns 0 if the rtl could not be matched.  If there was a match,
48    the new rtl is returned in an INSN list, and LAST_INSN will point
49    to the last recognized insn in the old sequence.
50
51
52    At a high level, the algorithm used in this file is as follows:
53
54    1. Build up a decision tree for each routine, using the following
55       approach to matching an rtx:
56
57       - First determine the "shape" of the rtx, based on GET_CODE,
58         XVECLEN and XINT.  This phase examines SET_SRCs before SET_DESTs
59         since SET_SRCs tend to be more distinctive.  It examines other
60         operands in numerical order, since the canonicalization rules
61         prefer putting complex operands of commutative operators first.
62
63       - Next check modes and predicates.  This phase examines all
64         operands in numerical order, even for SETs, since the mode of a
65         SET_DEST is exact while the mode of a SET_SRC can be VOIDmode
66         for constant integers.
67
68       - Next check match_dups.
69
70       - Finally check the C condition and (where appropriate) pnum_clobbers.
71
72    2. Try to optimize the tree by removing redundant tests, CSEing tests,
73       folding tests together, etc.
74
75    3. Look for common subtrees and split them out into "pattern" routines.
76       These common subtrees can be identical or they can differ in mode,
77       code, or integer (usually an UNSPEC or UNSPEC_VOLATILE code).
78       In the latter case the users of the pattern routine pass the
79       appropriate mode, etc., as argument.  For example, if two patterns
80       contain:
81
82          (plus:SI (match_operand:SI 1 "register_operand")
83                   (match_operand:SI 2 "register_operand"))
84
85       we can split the associated matching code out into a subroutine.
86       If a pattern contains:
87
88          (minus:DI (match_operand:DI 1 "register_operand")
89                    (match_operand:DI 2 "register_operand"))
90
91       then we can consider using the same matching routine for both
92       the plus and minus expressions, passing PLUS and SImode in the
93       former case and MINUS and DImode in the latter case.
94
95       The main aim of this phase is to reduce the compile time of the
96       insn-recog.c code and to reduce the amount of object code in
97       insn-recog.o.
98
99    4. Split the matching trees into functions, trying to limit the
100       size of each function to a sensible amount.
101
102       Again, the main aim of this phase is to reduce the compile time
103       of insn-recog.c.  (It doesn't help with the size of insn-recog.o.)
104
105    5. Write out C++ code for each function.  */
106
107 #include "bconfig.h"
108 #define INCLUDE_ALGORITHM
109 #include "system.h"
110 #include "coretypes.h"
111 #include "tm.h"
112 #include "rtl.h"
113 #include "errors.h"
114 #include "read-md.h"
115 #include "gensupport.h"
116
117 #undef GENERATOR_FILE
118 enum true_rtx_doe {
119 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) TRUE_##ENUM,
120 #include "rtl.def"
121 #undef DEF_RTL_EXPR
122   FIRST_GENERATOR_RTX_CODE
123 };
124 #define NUM_TRUE_RTX_CODE ((int) FIRST_GENERATOR_RTX_CODE)
125 #define GENERATOR_FILE 1
126
127 /* Debugging variables to control which optimizations are performed.
128    Note that disabling merge_states_p leads to very large output.  */
129 static const bool merge_states_p = true;
130 static const bool collapse_optional_decisions_p = true;
131 static const bool cse_tests_p = true;
132 static const bool simplify_tests_p = true;
133 static const bool use_operand_variables_p = true;
134 static const bool use_subroutines_p = true;
135 static const bool use_pattern_routines_p = true;
136
137 /* Whether to add comments for optional tests that we decided to keep.
138    Can be useful when debugging the generator itself but is noise when
139    debugging the generated code.  */
140 static const bool mark_optional_transitions_p = false;
141
142 /* Whether pattern routines should calculate positions relative to their
143    rtx parameter rather than use absolute positions.  This e.g. allows
144    a pattern routine to be shared between a plain SET and a PARALLEL
145    that includes a SET.
146
147    In principle it sounds like this should be useful, especially for
148    recog_for_combine, where the plain SET form is generated automatically
149    from a PARALLEL of a single SET and some CLOBBERs.  In practice it doesn't
150    seem to help much and leads to slightly bigger object files.  */
151 static const bool relative_patterns_p = false;
152
153 /* Whether pattern routines should be allowed to test whether pnum_clobbers
154    is null.  This requires passing pnum_clobbers around as a parameter.  */
155 static const bool pattern_have_num_clobbers_p = true;
156
157 /* Whether pattern routines should be allowed to test .md file C conditions.
158    This requires passing insn around as a parameter, in case the C
159    condition refers to it.  In practice this tends to lead to bigger
160    object files.  */
161 static const bool pattern_c_test_p = false;
162
163 /* Whether to require each parameter passed to a pattern routine to be
164    unique.  Disabling this check for example allows unary operators with
165    matching modes (like NEG) and unary operators with mismatched modes
166    (like ZERO_EXTEND) to be matched by a single pattern.  However, we then
167    often have cases where the same value is passed too many times.  */
168 static const bool force_unique_params_p = true;
169
170 /* The maximum (approximate) depth of block nesting that an individual
171    routine or subroutine should have.  This limit is about keeping the
172    output readable rather than reducing compile time.  */
173 static const unsigned int MAX_DEPTH = 6;
174
175 /* The minimum number of pseudo-statements that a state must have before
176    we split it out into a subroutine.  */
177 static const unsigned int MIN_NUM_STATEMENTS = 5;
178
179 /* The number of pseudo-statements a state can have before we consider
180    splitting out substates into subroutines.  This limit is about avoiding
181    compile-time problems with very big functions (and also about keeping
182    functions within --param optimization limits, etc.).  */
183 static const unsigned int MAX_NUM_STATEMENTS = 200;
184
185 /* The minimum number of pseudo-statements that can be used in a pattern
186    routine.  */
187 static const unsigned int MIN_COMBINE_COST = 4;
188
189 /* The maximum number of arguments that a pattern routine can have.
190    The idea is to prevent one pattern getting a ridiculous number of
191    arguments when it would be more beneficial to have a separate pattern
192    routine instead.  */
193 static const unsigned int MAX_PATTERN_PARAMS = 5;
194
195 /* The maximum operand number plus one.  */
196 int num_operands;
197
198 /* Ways of obtaining an rtx to be tested.  */
199 enum position_type {
200   /* PATTERN (peep2_next_insn (ARG)).  */
201   POS_PEEP2_INSN,
202
203   /* XEXP (BASE, ARG).  */
204   POS_XEXP,
205
206   /* XVECEXP (BASE, 0, ARG).  */
207   POS_XVECEXP0
208 };
209
210 /* The position of an rtx relative to X0.  Each useful position is
211    represented by exactly one instance of this structure.  */
212 struct position
213 {
214   /* The parent rtx.  This is the root position for POS_PEEP2_INSNs.  */
215   struct position *base;
216
217   /* A position with the same BASE and TYPE, but with the next value
218      of ARG.  */
219   struct position *next;
220
221   /* A list of all POS_XEXP positions that use this one as their base,
222      chained by NEXT fields.  The first entry represents XEXP (this, 0),
223      the second represents XEXP (this, 1), and so on.  */
224   struct position *xexps;
225
226   /* A list of POS_XVECEXP0 positions that use this one as their base,
227      chained by NEXT fields.  The first entry represents XVECEXP (this, 0, 0),
228      the second represents XVECEXP (this, 0, 1), and so on.  */
229   struct position *xvecexp0s;
230
231   /* The type of position.  */
232   enum position_type type;
233
234   /* The argument to TYPE (shown as ARG in the position_type comments).  */
235   int arg;
236
237   /* The instruction to which the position belongs.  */
238   unsigned int insn_id;
239
240   /* The depth of this position relative to the instruction pattern.
241      E.g. if the instruction pattern is a SET, the SET itself has a
242      depth of 0 while the SET_DEST and SET_SRC have depths of 1.  */
243   unsigned int depth;
244
245   /* A unique identifier for this position.  */
246   unsigned int id;
247 };
248
249 enum routine_type {
250   SUBPATTERN, RECOG, SPLIT, PEEPHOLE2
251 };
252
253 /* The root position (x0).  */
254 static struct position root_pos;
255
256 /* The number of positions created.  Also one higher than the maximum
257    position id.  */
258 static unsigned int num_positions = 1;
259
260 /* A list of all POS_PEEP2_INSNs.  The entry for insn 0 is the root position,
261    since we are given that instruction's pattern as x0.  */
262 static struct position *peep2_insn_pos_list = &root_pos;
263 \f
264 /* Return a position with the given BASE, TYPE and ARG.  NEXT_PTR
265    points to where the unique object that represents the position
266    should be stored.  Create the object if it doesn't already exist,
267    otherwise reuse the object that is already there.  */
268
269 static struct position *
270 next_position (struct position **next_ptr, struct position *base,
271                enum position_type type, int arg)
272 {
273   struct position *pos;
274
275   pos = *next_ptr;
276   if (!pos)
277     {
278       pos = XCNEW (struct position);
279       pos->type = type;
280       pos->arg = arg;
281       if (type == POS_PEEP2_INSN)
282         {
283           pos->base = 0;
284           pos->insn_id = arg;
285           pos->depth = base->depth;
286         }
287       else
288         {
289           pos->base = base;
290           pos->insn_id = base->insn_id;
291           pos->depth = base->depth + 1;
292         }
293       pos->id = num_positions++;
294       *next_ptr = pos;
295     }
296   return pos;
297 }
298
299 /* Compare positions POS1 and POS2 lexicographically.  */
300
301 static int
302 compare_positions (struct position *pos1, struct position *pos2)
303 {
304   int diff;
305
306   diff = pos1->depth - pos2->depth;
307   if (diff < 0)
308     do
309       pos2 = pos2->base;
310     while (pos1->depth != pos2->depth);
311   else if (diff > 0)
312     do
313       pos1 = pos1->base;
314     while (pos1->depth != pos2->depth);
315   while (pos1 != pos2)
316     {
317       diff = (int) pos1->type - (int) pos2->type;
318       if (diff == 0)
319         diff = pos1->arg - pos2->arg;
320       pos1 = pos1->base;
321       pos2 = pos2->base;
322     }
323   return diff;
324 }
325
326 /* Return the most deeply-nested position that is common to both
327    POS1 and POS2.  If the positions are from different instructions,
328    return the one with the lowest insn_id.  */
329
330 static struct position *
331 common_position (struct position *pos1, struct position *pos2)
332 {
333   if (pos1->insn_id != pos2->insn_id)
334     return pos1->insn_id < pos2->insn_id ? pos1 : pos2;
335   if (pos1->depth > pos2->depth)
336     std::swap (pos1, pos2);
337   while (pos1->depth != pos2->depth)
338     pos2 = pos2->base;
339   while (pos1 != pos2)
340     {
341       pos1 = pos1->base;
342       pos2 = pos2->base;
343     }
344   return pos1;
345 }
346 \f
347 /* Search for and return operand N, stop when reaching node STOP.  */
348
349 static rtx
350 find_operand (rtx pattern, int n, rtx stop)
351 {
352   const char *fmt;
353   RTX_CODE code;
354   int i, j, len;
355   rtx r;
356
357   if (pattern == stop)
358     return stop;
359
360   code = GET_CODE (pattern);
361   if ((code == MATCH_SCRATCH
362        || code == MATCH_OPERAND
363        || code == MATCH_OPERATOR
364        || code == MATCH_PARALLEL)
365       && XINT (pattern, 0) == n)
366     return pattern;
367
368   fmt = GET_RTX_FORMAT (code);
369   len = GET_RTX_LENGTH (code);
370   for (i = 0; i < len; i++)
371     {
372       switch (fmt[i])
373         {
374         case 'e': case 'u':
375           if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
376             return r;
377           break;
378
379         case 'V':
380           if (! XVEC (pattern, i))
381             break;
382           /* Fall through.  */
383
384         case 'E':
385           for (j = 0; j < XVECLEN (pattern, i); j++)
386             if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
387                 != NULL_RTX)
388               return r;
389           break;
390
391         case 'i': case 'r': case 'w': case '0': case 's':
392           break;
393
394         default:
395           gcc_unreachable ();
396         }
397     }
398
399   return NULL;
400 }
401
402 /* Search for and return operand M, such that it has a matching
403    constraint for operand N.  */
404
405 static rtx
406 find_matching_operand (rtx pattern, int n)
407 {
408   const char *fmt;
409   RTX_CODE code;
410   int i, j, len;
411   rtx r;
412
413   code = GET_CODE (pattern);
414   if (code == MATCH_OPERAND
415       && (XSTR (pattern, 2)[0] == '0' + n
416           || (XSTR (pattern, 2)[0] == '%'
417               && XSTR (pattern, 2)[1] == '0' + n)))
418     return pattern;
419
420   fmt = GET_RTX_FORMAT (code);
421   len = GET_RTX_LENGTH (code);
422   for (i = 0; i < len; i++)
423     {
424       switch (fmt[i])
425         {
426         case 'e': case 'u':
427           if ((r = find_matching_operand (XEXP (pattern, i), n)))
428             return r;
429           break;
430
431         case 'V':
432           if (! XVEC (pattern, i))
433             break;
434           /* Fall through.  */
435
436         case 'E':
437           for (j = 0; j < XVECLEN (pattern, i); j++)
438             if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
439               return r;
440           break;
441
442         case 'i': case 'r': case 'w': case '0': case 's':
443           break;
444
445         default:
446           gcc_unreachable ();
447         }
448     }
449
450   return NULL;
451 }
452
453 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
454    don't use the MATCH_OPERAND constraint, only the predicate.
455    This is confusing to folks doing new ports, so help them
456    not make the mistake.  */
457
458 static bool
459 constraints_supported_in_insn_p (rtx insn)
460 {
461   return !(GET_CODE (insn) == DEFINE_EXPAND
462            || GET_CODE (insn) == DEFINE_SPLIT
463            || GET_CODE (insn) == DEFINE_PEEPHOLE2);
464 }
465
466 /* Check for various errors in PATTERN, which is part of INFO.
467    SET is nonnull for a destination, and is the complete set pattern.
468    SET_CODE is '=' for normal sets, and '+' within a context that
469    requires in-out constraints.  */
470
471 static void
472 validate_pattern (rtx pattern, md_rtx_info *info, rtx set, int set_code)
473 {
474   const char *fmt;
475   RTX_CODE code;
476   size_t i, len;
477   int j;
478
479   code = GET_CODE (pattern);
480   switch (code)
481     {
482     case MATCH_SCRATCH:
483       {
484         const char constraints0 = XSTR (pattern, 1)[0];
485
486         if (!constraints_supported_in_insn_p (info->def))
487           {
488             if (constraints0)
489               {
490                 error_at (info->loc, "constraints not supported in %s",
491                           GET_RTX_NAME (GET_CODE (info->def)));
492               }
493             return;
494           }
495
496         /* If a MATCH_SCRATCH is used in a context requiring an write-only
497            or read/write register, validate that.  */
498         if (set_code == '='
499             && constraints0
500             && constraints0 != '='
501             && constraints0 != '+')
502           {
503             error_at (info->loc, "operand %d missing output reload",
504                       XINT (pattern, 0));
505           }
506         return;
507       }
508     case MATCH_DUP:
509     case MATCH_OP_DUP:
510     case MATCH_PAR_DUP:
511       if (find_operand (info->def, XINT (pattern, 0), pattern) == pattern)
512         error_at (info->loc, "operand %i duplicated before defined",
513                   XINT (pattern, 0));
514       break;
515     case MATCH_OPERAND:
516     case MATCH_OPERATOR:
517       {
518         const char *pred_name = XSTR (pattern, 1);
519         const struct pred_data *pred;
520         const char *c_test;
521
522         c_test = get_c_test (info->def);
523
524         if (pred_name[0] != 0)
525           {
526             pred = lookup_predicate (pred_name);
527             if (!pred)
528               error_at (info->loc, "unknown predicate '%s'", pred_name);
529           }
530         else
531           pred = 0;
532
533         if (code == MATCH_OPERAND)
534           {
535             const char *constraints = XSTR (pattern, 2);
536             const char constraints0 = constraints[0];
537
538             if (!constraints_supported_in_insn_p (info->def))
539               {
540                 if (constraints0)
541                   {
542                     error_at (info->loc, "constraints not supported in %s",
543                               GET_RTX_NAME (GET_CODE (info->def)));
544                   }
545               }
546
547             /* A MATCH_OPERAND that is a SET should have an output reload.  */
548             else if (set && constraints0)
549               {
550                 if (set_code == '+')
551                   {
552                     if (constraints0 == '+')
553                       ;
554                     /* If we've only got an output reload for this operand,
555                        we'd better have a matching input operand.  */
556                     else if (constraints0 == '='
557                              && find_matching_operand (info->def,
558                                                        XINT (pattern, 0)))
559                       ;
560                     else
561                       error_at (info->loc, "operand %d missing in-out reload",
562                                 XINT (pattern, 0));
563                   }
564                 else if (constraints0 != '=' && constraints0 != '+')
565                   error_at (info->loc, "operand %d missing output reload",
566                             XINT (pattern, 0));
567               }
568
569             /* For matching constraint in MATCH_OPERAND, the digit must be a
570                smaller number than the number of the operand that uses it in the
571                constraint.  */
572             while (1)
573               {
574                 while (constraints[0]
575                        && (constraints[0] == ' ' || constraints[0] == ','))
576                   constraints++;
577                 if (!constraints[0])
578                   break;
579
580                 if (constraints[0] >= '0' && constraints[0] <= '9')
581                   {
582                     int val;
583
584                     sscanf (constraints, "%d", &val);
585                     if (val >= XINT (pattern, 0))
586                       error_at (info->loc, "constraint digit %d is not"
587                                 " smaller than operand %d",
588                                 val, XINT (pattern, 0));
589                   }
590
591                 while (constraints[0] && constraints[0] != ',')
592                   constraints++;
593               }
594           }
595
596         /* Allowing non-lvalues in destinations -- particularly CONST_INT --
597            while not likely to occur at runtime, results in less efficient
598            code from insn-recog.c.  */
599         if (set && pred && pred->allows_non_lvalue)
600           error_at (info->loc, "destination operand %d allows non-lvalue",
601                     XINT (pattern, 0));
602
603         /* A modeless MATCH_OPERAND can be handy when we can check for
604            multiple modes in the c_test.  In most other cases, it is a
605            mistake.  Only DEFINE_INSN is eligible, since SPLIT and
606            PEEP2 can FAIL within the output pattern.  Exclude special
607            predicates, which check the mode themselves.  Also exclude
608            predicates that allow only constants.  Exclude the SET_DEST
609            of a call instruction, as that is a common idiom.  */
610
611         if (GET_MODE (pattern) == VOIDmode
612             && code == MATCH_OPERAND
613             && GET_CODE (info->def) == DEFINE_INSN
614             && pred
615             && !pred->special
616             && pred->allows_non_const
617             && strstr (c_test, "operands") == NULL
618             && ! (set
619                   && GET_CODE (set) == SET
620                   && GET_CODE (SET_SRC (set)) == CALL))
621           message_at (info->loc, "warning: operand %d missing mode?",
622                       XINT (pattern, 0));
623         return;
624       }
625
626     case SET:
627       {
628         machine_mode dmode, smode;
629         rtx dest, src;
630
631         dest = SET_DEST (pattern);
632         src = SET_SRC (pattern);
633
634         /* STRICT_LOW_PART is a wrapper.  Its argument is the real
635            destination, and it's mode should match the source.  */
636         if (GET_CODE (dest) == STRICT_LOW_PART)
637           dest = XEXP (dest, 0);
638
639         /* Find the referent for a DUP.  */
640
641         if (GET_CODE (dest) == MATCH_DUP
642             || GET_CODE (dest) == MATCH_OP_DUP
643             || GET_CODE (dest) == MATCH_PAR_DUP)
644           dest = find_operand (info->def, XINT (dest, 0), NULL);
645
646         if (GET_CODE (src) == MATCH_DUP
647             || GET_CODE (src) == MATCH_OP_DUP
648             || GET_CODE (src) == MATCH_PAR_DUP)
649           src = find_operand (info->def, XINT (src, 0), NULL);
650
651         dmode = GET_MODE (dest);
652         smode = GET_MODE (src);
653
654         /* The mode of an ADDRESS_OPERAND is the mode of the memory
655            reference, not the mode of the address.  */
656         if (GET_CODE (src) == MATCH_OPERAND
657             && ! strcmp (XSTR (src, 1), "address_operand"))
658           ;
659
660         /* The operands of a SET must have the same mode unless one
661            is VOIDmode.  */
662         else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
663           error_at (info->loc, "mode mismatch in set: %smode vs %smode",
664                     GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
665
666         /* If only one of the operands is VOIDmode, and PC or CC0 is
667            not involved, it's probably a mistake.  */
668         else if (dmode != smode
669                  && GET_CODE (dest) != PC
670                  && GET_CODE (dest) != CC0
671                  && GET_CODE (src) != PC
672                  && GET_CODE (src) != CC0
673                  && !CONST_INT_P (src)
674                  && !CONST_WIDE_INT_P (src)
675                  && GET_CODE (src) != CALL)
676           {
677             const char *which;
678             which = (dmode == VOIDmode ? "destination" : "source");
679             message_at (info->loc, "warning: %s missing a mode?", which);
680           }
681
682         if (dest != SET_DEST (pattern))
683           validate_pattern (dest, info, pattern, '=');
684         validate_pattern (SET_DEST (pattern), info, pattern, '=');
685         validate_pattern (SET_SRC (pattern), info, NULL_RTX, 0);
686         return;
687       }
688
689     case CLOBBER:
690       validate_pattern (SET_DEST (pattern), info, pattern, '=');
691       return;
692
693     case ZERO_EXTRACT:
694       validate_pattern (XEXP (pattern, 0), info, set, set ? '+' : 0);
695       validate_pattern (XEXP (pattern, 1), info, NULL_RTX, 0);
696       validate_pattern (XEXP (pattern, 2), info, NULL_RTX, 0);
697       return;
698
699     case STRICT_LOW_PART:
700       validate_pattern (XEXP (pattern, 0), info, set, set ? '+' : 0);
701       return;
702
703     case LABEL_REF:
704       if (GET_MODE (LABEL_REF_LABEL (pattern)) != VOIDmode)
705         error_at (info->loc, "operand to label_ref %smode not VOIDmode",
706                   GET_MODE_NAME (GET_MODE (LABEL_REF_LABEL (pattern))));
707       break;
708
709     default:
710       break;
711     }
712
713   fmt = GET_RTX_FORMAT (code);
714   len = GET_RTX_LENGTH (code);
715   for (i = 0; i < len; i++)
716     {
717       switch (fmt[i])
718         {
719         case 'e': case 'u':
720           validate_pattern (XEXP (pattern, i), info, NULL_RTX, 0);
721           break;
722
723         case 'E':
724           for (j = 0; j < XVECLEN (pattern, i); j++)
725             validate_pattern (XVECEXP (pattern, i, j), info, NULL_RTX, 0);
726           break;
727
728         case 'i': case 'r': case 'w': case '0': case 's':
729           break;
730
731         default:
732           gcc_unreachable ();
733         }
734     }
735 }
736 \f
737 /* Simple list structure for items of type T, for use when being part
738    of a list is an inherent property of T.  T must have members equivalent
739    to "T *prev, *next;" and a function "void set_parent (list_head <T> *)"
740    to set the parent list.  */
741 template <typename T>
742 struct list_head
743 {
744   /* A range of linked items.  */
745   struct range
746   {
747     range (T *);
748     range (T *, T *);
749
750     T *start, *end;
751     void set_parent (list_head *);
752   };
753
754   list_head ();
755   range release ();
756   void push_back (range);
757   range remove (range);
758   void replace (range, range);
759   T *singleton () const;
760
761   T *first, *last;
762 };
763
764 /* Create a range [START_IN, START_IN].  */
765
766 template <typename T>
767 list_head <T>::range::range (T *start_in) : start (start_in), end (start_in) {}
768
769 /* Create a range [START_IN, END_IN], linked by next and prev fields.  */
770
771 template <typename T>
772 list_head <T>::range::range (T *start_in, T *end_in)
773   : start (start_in), end (end_in) {}
774
775 template <typename T>
776 void
777 list_head <T>::range::set_parent (list_head <T> *owner)
778 {
779   for (T *item = start; item != end; item = item->next)
780     item->set_parent (owner);
781   end->set_parent (owner);
782 }
783
784 template <typename T>
785 list_head <T>::list_head () : first (0), last (0) {}
786
787 /* Add R to the end of the list.  */
788
789 template <typename T>
790 void
791 list_head <T>::push_back (range r)
792 {
793   if (last)
794     last->next = r.start;
795   else
796     first = r.start;
797   r.start->prev = last;
798   last = r.end;
799   r.set_parent (this);
800 }
801
802 /* Remove R from the list.  R remains valid and can be inserted into
803    other lists.  */
804
805 template <typename T>
806 typename list_head <T>::range
807 list_head <T>::remove (range r)
808 {
809   if (r.start->prev)
810     r.start->prev->next = r.end->next;
811   else
812     first = r.end->next;
813   if (r.end->next)
814     r.end->next->prev = r.start->prev;
815   else
816     last = r.start->prev;
817   r.start->prev = 0;
818   r.end->next = 0;
819   r.set_parent (0);
820   return r;
821 }
822
823 /* Replace OLDR with NEWR.  OLDR remains valid and can be inserted into
824    other lists.  */
825
826 template <typename T>
827 void
828 list_head <T>::replace (range oldr, range newr)
829 {
830   newr.start->prev = oldr.start->prev;
831   newr.end->next = oldr.end->next;
832
833   oldr.start->prev = 0;
834   oldr.end->next = 0;
835   oldr.set_parent (0);
836
837   if (newr.start->prev)
838     newr.start->prev->next = newr.start;
839   else
840     first = newr.start;
841   if (newr.end->next)
842     newr.end->next->prev = newr.end;
843   else
844     last = newr.end;
845   newr.set_parent (this);
846 }
847
848 /* Empty the list and return the previous contents as a range that can
849    be inserted into other lists.  */
850
851 template <typename T>
852 typename list_head <T>::range
853 list_head <T>::release ()
854 {
855   range r (first, last);
856   first = 0;
857   last = 0;
858   r.set_parent (0);
859   return r;
860 }
861
862 /* If the list contains a single item, return that item, otherwise return
863    null.  */
864
865 template <typename T>
866 T *
867 list_head <T>::singleton () const
868 {
869   return first == last ? first : 0;
870 }
871 \f
872 struct state;
873
874 /* Describes a possible successful return from a routine.  */
875 struct acceptance_type
876 {
877   /* The type of routine we're returning from.  */
878   routine_type type : 16;
879
880   /* True if this structure only really represents a partial match,
881      and if we must call a subroutine of type TYPE to complete the match.
882      In this case we'll call the subroutine and, if it succeeds, return
883      whatever the subroutine returned.
884
885      False if this structure presents a full match.  */
886   unsigned int partial_p : 1;
887
888   union
889   {
890     /* If PARTIAL_P, this is the number of the subroutine to call.  */
891     int subroutine_id;
892
893     /* Valid if !PARTIAL_P.  */
894     struct
895     {
896       /* The identifier of the matching pattern.  For SUBPATTERNs this
897          value belongs to an ad-hoc routine-specific enum.  For the
898          others it's the number of an .md file pattern.  */
899       int code;
900       union
901       {
902         /* For RECOG, the number of clobbers that must be added to the
903            pattern in order for it to match CODE.  */
904         int num_clobbers;
905
906         /* For PEEPHOLE2, the number of additional instructions that were
907            included in the optimization.  */
908         int match_len;
909       } u;
910     } full;
911   } u;
912 };
913
914 bool
915 operator == (const acceptance_type &a, const acceptance_type &b)
916 {
917   if (a.partial_p != b.partial_p)
918     return false;
919   if (a.partial_p)
920     return a.u.subroutine_id == b.u.subroutine_id;
921   else
922     return a.u.full.code == b.u.full.code;
923 }
924
925 bool
926 operator != (const acceptance_type &a, const acceptance_type &b)
927 {
928   return !operator == (a, b);
929 }
930
931 /* Represents a parameter to a pattern routine.  */
932 struct parameter
933 {
934   /* The C type of parameter.  */
935   enum type_enum {
936     /* Represents an invalid parameter.  */
937     UNSET,
938
939     /* A machine_mode parameter.  */
940     MODE,
941
942     /* An rtx_code parameter.  */
943     CODE,
944
945     /* An int parameter.  */
946     INT,
947
948     /* An unsigned int parameter.  */
949     UINT,
950
951     /* A HOST_WIDE_INT parameter.  */
952     WIDE_INT
953   };
954
955   parameter ();
956   parameter (type_enum, bool, uint64_t);
957
958   /* The type of the parameter.  */
959   type_enum type;
960
961   /* True if the value passed is variable, false if it is constant.  */
962   bool is_param;
963
964   /* If IS_PARAM, this is the number of the variable passed, for an "i%d"
965      format string.  If !IS_PARAM, this is the constant value passed.  */
966   uint64_t value;
967 };
968
969 parameter::parameter ()
970   : type (UNSET), is_param (false), value (0) {}
971
972 parameter::parameter (type_enum type_in, bool is_param_in, uint64_t value_in)
973   : type (type_in), is_param (is_param_in), value (value_in) {}
974
975 bool
976 operator == (const parameter &param1, const parameter &param2)
977 {
978   return (param1.type == param2.type
979           && param1.is_param == param2.is_param
980           && param1.value == param2.value);
981 }
982
983 bool
984 operator != (const parameter &param1, const parameter &param2)
985 {
986   return !operator == (param1, param2);
987 }
988
989 /* Represents a routine that matches a partial rtx pattern, returning
990    an ad-hoc enum value on success and -1 on failure.  The routine can
991    be used by any subroutine type.  The match can be parameterized by
992    things like mode, code and UNSPEC number.  */
993 struct pattern_routine
994 {
995   /* The state that implements the pattern.  */
996   state *s;
997
998   /* The deepest root position from which S can access all the rtxes it needs.
999      This is NULL if the pattern doesn't need an rtx input, usually because
1000      all matching is done on operands[] instead.  */
1001   position *pos;
1002
1003   /* A unique identifier for the routine.  */
1004   unsigned int pattern_id;
1005
1006   /* True if the routine takes pnum_clobbers as argument.  */
1007   bool pnum_clobbers_p;
1008
1009   /* True if the routine takes the enclosing instruction as argument.  */
1010   bool insn_p;
1011
1012   /* The types of the other parameters to the routine, if any.  */
1013   auto_vec <parameter::type_enum, MAX_PATTERN_PARAMS> param_types;
1014 };
1015
1016 /* All defined patterns.  */
1017 static vec <pattern_routine *> patterns;
1018
1019 /* Represents one use of a pattern routine.  */
1020 struct pattern_use
1021 {
1022   /* The pattern routine to use.  */
1023   pattern_routine *routine;
1024
1025   /* The values to pass as parameters.  This vector has the same length
1026      as ROUTINE->PARAM_TYPES.  */
1027   auto_vec <parameter, MAX_PATTERN_PARAMS> params;
1028 };
1029
1030 /* Represents a test performed by a decision.  */
1031 struct rtx_test
1032 {
1033   rtx_test ();
1034
1035   /* The types of test that can be performed.  Most of them take as input
1036      an rtx X.  Some also take as input a transition label LABEL; the others
1037      are booleans for which the transition label is always "true".
1038
1039      The order of the enum isn't important.  */
1040   enum kind_enum {
1041     /* Check GET_CODE (X) == LABEL.  */
1042     CODE,
1043
1044     /* Check GET_MODE (X) == LABEL.  */
1045     MODE,
1046
1047     /* Check REGNO (X) == LABEL.  */
1048     REGNO_FIELD,
1049
1050     /* Check XINT (X, u.opno) == LABEL.  */
1051     INT_FIELD,
1052
1053     /* Check XWINT (X, u.opno) == LABEL.  */
1054     WIDE_INT_FIELD,
1055
1056     /* Check XVECLEN (X, 0) == LABEL.  */
1057     VECLEN,
1058
1059     /* Check peep2_current_count >= u.min_len.  */
1060     PEEP2_COUNT,
1061
1062     /* Check XVECLEN (X, 0) >= u.min_len.  */
1063     VECLEN_GE,
1064
1065     /* Check whether X is a cached const_int with value u.integer.  */
1066     SAVED_CONST_INT,
1067
1068     /* Check u.predicate.data (X, u.predicate.mode).  */
1069     PREDICATE,
1070
1071     /* Check rtx_equal_p (X, operands[u.opno]).  */
1072     DUPLICATE,
1073
1074     /* Check whether X matches pattern u.pattern.  */
1075     PATTERN,
1076
1077     /* Check whether pnum_clobbers is nonnull (RECOG only).  */
1078     HAVE_NUM_CLOBBERS,
1079
1080     /* Check whether general C test u.string holds.  In general the condition
1081        needs access to "insn" and the full operand list.  */
1082     C_TEST,
1083
1084     /* Execute operands[u.opno] = X.  (Always succeeds.)  */
1085     SET_OP,
1086
1087     /* Accept u.acceptance.  Always succeeds for SUBPATTERN, RECOG and SPLIT.
1088        May fail for PEEPHOLE2 if the define_peephole2 C code executes FAIL.  */
1089     ACCEPT
1090   };
1091
1092   /* The position of rtx X in the above description, relative to the
1093      incoming instruction "insn".  The position is null if the test
1094      doesn't take an X as input.  */
1095   position *pos;
1096
1097   /* Which element of operands[] already contains POS, or -1 if no element
1098      is known to hold POS.  */
1099   int pos_operand;
1100
1101   /* The type of test and its parameters, as described above.  */
1102   kind_enum kind;
1103   union
1104   {
1105     int opno;
1106     int min_len;
1107     struct
1108     {
1109       bool is_param;
1110       int value;
1111     } integer;
1112     struct
1113     {
1114       const struct pred_data *data;
1115       /* True if the mode is taken from a machine_mode parameter
1116          to the routine rather than a constant machine_mode.  If true,
1117          MODE is the number of the parameter (for an "i%d" format string),
1118          otherwise it is the mode itself.  */
1119       bool mode_is_param;
1120       unsigned int mode;
1121     } predicate;
1122     pattern_use *pattern;
1123     const char *string;
1124     acceptance_type acceptance;
1125   } u;
1126
1127   static rtx_test code (position *);
1128   static rtx_test mode (position *);
1129   static rtx_test regno_field (position *);
1130   static rtx_test int_field (position *, int);
1131   static rtx_test wide_int_field (position *, int);
1132   static rtx_test veclen (position *);
1133   static rtx_test peep2_count (int);
1134   static rtx_test veclen_ge (position *, int);
1135   static rtx_test predicate (position *, const pred_data *, machine_mode);
1136   static rtx_test duplicate (position *, int);
1137   static rtx_test pattern (position *, pattern_use *);
1138   static rtx_test have_num_clobbers ();
1139   static rtx_test c_test (const char *);
1140   static rtx_test set_op (position *, int);
1141   static rtx_test accept (const acceptance_type &);
1142
1143   bool terminal_p () const;
1144   bool single_outcome_p () const;
1145
1146 private:
1147   rtx_test (position *, kind_enum);
1148 };
1149
1150 rtx_test::rtx_test () {}
1151
1152 rtx_test::rtx_test (position *pos_in, kind_enum kind_in)
1153   : pos (pos_in), pos_operand (-1), kind (kind_in) {}
1154
1155 rtx_test
1156 rtx_test::code (position *pos)
1157 {
1158   return rtx_test (pos, rtx_test::CODE);
1159 }
1160
1161 rtx_test
1162 rtx_test::mode (position *pos)
1163 {
1164   return rtx_test (pos, rtx_test::MODE);
1165 }
1166
1167 rtx_test
1168 rtx_test::regno_field (position *pos)
1169 {
1170   rtx_test res (pos, rtx_test::REGNO_FIELD);
1171   return res;
1172 }
1173
1174 rtx_test
1175 rtx_test::int_field (position *pos, int opno)
1176 {
1177   rtx_test res (pos, rtx_test::INT_FIELD);
1178   res.u.opno = opno;
1179   return res;
1180 }
1181
1182 rtx_test
1183 rtx_test::wide_int_field (position *pos, int opno)
1184 {
1185   rtx_test res (pos, rtx_test::WIDE_INT_FIELD);
1186   res.u.opno = opno;
1187   return res;
1188 }
1189
1190 rtx_test
1191 rtx_test::veclen (position *pos)
1192 {
1193   return rtx_test (pos, rtx_test::VECLEN);
1194 }
1195
1196 rtx_test
1197 rtx_test::peep2_count (int min_len)
1198 {
1199   rtx_test res (0, rtx_test::PEEP2_COUNT);
1200   res.u.min_len = min_len;
1201   return res;
1202 }
1203
1204 rtx_test
1205 rtx_test::veclen_ge (position *pos, int min_len)
1206 {
1207   rtx_test res (pos, rtx_test::VECLEN_GE);
1208   res.u.min_len = min_len;
1209   return res;
1210 }
1211
1212 rtx_test
1213 rtx_test::predicate (position *pos, const struct pred_data *data,
1214                      machine_mode mode)
1215 {
1216   rtx_test res (pos, rtx_test::PREDICATE);
1217   res.u.predicate.data = data;
1218   res.u.predicate.mode_is_param = false;
1219   res.u.predicate.mode = mode;
1220   return res;
1221 }
1222
1223 rtx_test
1224 rtx_test::duplicate (position *pos, int opno)
1225 {
1226   rtx_test res (pos, rtx_test::DUPLICATE);
1227   res.u.opno = opno;
1228   return res;
1229 }
1230
1231 rtx_test
1232 rtx_test::pattern (position *pos, pattern_use *pattern)
1233 {
1234   rtx_test res (pos, rtx_test::PATTERN);
1235   res.u.pattern = pattern;
1236   return res;
1237 }
1238
1239 rtx_test
1240 rtx_test::have_num_clobbers ()
1241 {
1242   return rtx_test (0, rtx_test::HAVE_NUM_CLOBBERS);
1243 }
1244
1245 rtx_test
1246 rtx_test::c_test (const char *string)
1247 {
1248   rtx_test res (0, rtx_test::C_TEST);
1249   res.u.string = string;
1250   return res;
1251 }
1252
1253 rtx_test
1254 rtx_test::set_op (position *pos, int opno)
1255 {
1256   rtx_test res (pos, rtx_test::SET_OP);
1257   res.u.opno = opno;
1258   return res;
1259 }
1260
1261 rtx_test
1262 rtx_test::accept (const acceptance_type &acceptance)
1263 {
1264   rtx_test res (0, rtx_test::ACCEPT);
1265   res.u.acceptance = acceptance;
1266   return res;
1267 }
1268
1269 /* Return true if the test represents an unconditionally successful match.  */
1270
1271 bool
1272 rtx_test::terminal_p () const
1273 {
1274   return kind == rtx_test::ACCEPT && u.acceptance.type != PEEPHOLE2;
1275 }
1276
1277 /* Return true if the test is a boolean that is always true.  */
1278
1279 bool
1280 rtx_test::single_outcome_p () const
1281 {
1282   return terminal_p () || kind == rtx_test::SET_OP;
1283 }
1284
1285 bool
1286 operator == (const rtx_test &a, const rtx_test &b)
1287 {
1288   if (a.pos != b.pos || a.kind != b.kind)
1289     return false;
1290   switch (a.kind)
1291     {
1292     case rtx_test::CODE:
1293     case rtx_test::MODE:
1294     case rtx_test::REGNO_FIELD:
1295     case rtx_test::VECLEN:
1296     case rtx_test::HAVE_NUM_CLOBBERS:
1297       return true;
1298
1299     case rtx_test::PEEP2_COUNT:
1300     case rtx_test::VECLEN_GE:
1301       return a.u.min_len == b.u.min_len;
1302
1303     case rtx_test::INT_FIELD:
1304     case rtx_test::WIDE_INT_FIELD:
1305     case rtx_test::DUPLICATE:
1306     case rtx_test::SET_OP:
1307       return a.u.opno == b.u.opno;
1308
1309     case rtx_test::SAVED_CONST_INT:
1310       return (a.u.integer.is_param == b.u.integer.is_param
1311               && a.u.integer.value == b.u.integer.value);
1312
1313     case rtx_test::PREDICATE:
1314       return (a.u.predicate.data == b.u.predicate.data
1315               && a.u.predicate.mode_is_param == b.u.predicate.mode_is_param
1316               && a.u.predicate.mode == b.u.predicate.mode);
1317
1318     case rtx_test::PATTERN:
1319       return (a.u.pattern->routine == b.u.pattern->routine
1320               && a.u.pattern->params == b.u.pattern->params);
1321
1322     case rtx_test::C_TEST:
1323       return strcmp (a.u.string, b.u.string) == 0;
1324
1325     case rtx_test::ACCEPT:
1326       return a.u.acceptance == b.u.acceptance;
1327     }
1328   gcc_unreachable ();
1329 }
1330
1331 bool
1332 operator != (const rtx_test &a, const rtx_test &b)
1333 {
1334   return !operator == (a, b);
1335 }
1336
1337 /* A simple set of transition labels.  Most transitions have a singleton
1338    label, so try to make that case as efficient as possible.  */
1339 struct int_set : public auto_vec <uint64_t, 1>
1340 {
1341   typedef uint64_t *iterator;
1342
1343   int_set ();
1344   int_set (uint64_t);
1345   int_set (const int_set &);
1346
1347   int_set &operator = (const int_set &);
1348
1349   iterator begin ();
1350   iterator end ();
1351 };
1352
1353 int_set::int_set () {}
1354
1355 int_set::int_set (uint64_t label)
1356 {
1357   safe_push (label);
1358 }
1359
1360 int_set::int_set (const int_set &other)
1361 {
1362   safe_splice (other);
1363 }
1364
1365 int_set &
1366 int_set::operator = (const int_set &other)
1367 {
1368   truncate (0);
1369   safe_splice (other);
1370   return *this;
1371 }
1372
1373 int_set::iterator
1374 int_set::begin ()
1375 {
1376   return address ();
1377 }
1378
1379 int_set::iterator
1380 int_set::end ()
1381 {
1382   return address () + length ();
1383 }
1384
1385 bool
1386 operator == (const int_set &a, const int_set &b)
1387 {
1388   if (a.length () != b.length ())
1389     return false;
1390   for (unsigned int i = 0; i < a.length (); ++i)
1391     if (a[i] != b[i])
1392       return false;
1393   return true;
1394 }
1395
1396 bool
1397 operator != (const int_set &a, const int_set &b)
1398 {
1399   return !operator == (a, b);
1400 }
1401
1402 struct decision;
1403
1404 /* Represents a transition between states, dependent on the result of
1405    a test T.  */
1406 struct transition
1407 {
1408   transition (const int_set &, state *, bool);
1409
1410   void set_parent (list_head <transition> *);
1411
1412   /* Links to other transitions for T.  Always null for boolean tests.  */
1413   transition *prev, *next;
1414
1415   /* The transition should be taken when T has one of these values.
1416      E.g. for rtx_test::CODE this is a set of codes, while for booleans like
1417      rtx_test::PREDICATE it is always a singleton "true".  The labels are
1418      sorted in ascending order.  */
1419   int_set labels;
1420
1421   /* The source decision.  */
1422   decision *from;
1423
1424   /* The target state.  */
1425   state *to;
1426
1427   /* True if TO would function correctly even if TEST wasn't performed.
1428      E.g. it isn't necessary to check whether GET_MODE (x1) is SImode
1429      before calling register_operand (x1, SImode), since register_operand
1430      performs its own mode check.  However, checking GET_MODE can be a cheap
1431      way of disambiguating SImode and DImode register operands.  */
1432   bool optional;
1433
1434   /* True if LABELS contains parameter numbers rather than constants.
1435      E.g. if this is true for a rtx_test::CODE, the label is the number
1436      of an rtx_code parameter rather than an rtx_code itself.
1437      LABELS is always a singleton when this variable is true.  */
1438   bool is_param;
1439 };
1440
1441 /* Represents a test and the action that should be taken on the result.
1442    If a transition exists for the test outcome, the machine switches
1443    to the transition's target state.  If no suitable transition exists,
1444    the machine either falls through to the next decision or, if there are no
1445    more decisions to try, fails the match.  */
1446 struct decision : list_head <transition>
1447 {
1448   decision (const rtx_test &);
1449
1450   void set_parent (list_head <decision> *s);
1451   bool if_statement_p (uint64_t * = 0) const;
1452
1453   /* The state to which this decision belongs.  */
1454   state *s;
1455
1456   /* Links to other decisions in the same state.  */
1457   decision *prev, *next;
1458
1459   /* The test to perform.  */
1460   rtx_test test;
1461 };
1462
1463 /* Represents one machine state.  For each state the machine tries a list
1464    of decisions, in order, and acts on the first match.  It fails without
1465    further backtracking if no decisions match.  */
1466 struct state : list_head <decision>
1467 {
1468   void set_parent (list_head <state> *) {}
1469 };
1470
1471 transition::transition (const int_set &labels_in, state *to_in,
1472                         bool optional_in)
1473   : prev (0), next (0), labels (labels_in), from (0), to (to_in),
1474     optional (optional_in), is_param (false) {}
1475
1476 /* Set the source decision of the transition.  */
1477
1478 void
1479 transition::set_parent (list_head <transition> *from_in)
1480 {
1481   from = static_cast <decision *> (from_in);
1482 }
1483
1484 decision::decision (const rtx_test &test_in)
1485   : prev (0), next (0), test (test_in) {}
1486
1487 /* Set the state to which this decision belongs.  */
1488
1489 void
1490 decision::set_parent (list_head <decision> *s_in)
1491 {
1492   s = static_cast <state *> (s_in);
1493 }
1494
1495 /* Return true if the decision has a single transition with a single label.
1496    If so, return the label in *LABEL if nonnull.  */
1497
1498 inline bool
1499 decision::if_statement_p (uint64_t *label) const
1500 {
1501   if (singleton () && first->labels.length () == 1)
1502     {
1503       if (label)
1504         *label = first->labels[0];
1505       return true;
1506     }
1507   return false;
1508 }
1509
1510 /* Add to FROM a decision that performs TEST and has a single transition
1511    TRANS.  */
1512
1513 static void
1514 add_decision (state *from, const rtx_test &test, transition *trans)
1515 {
1516   decision *d = new decision (test);
1517   from->push_back (d);
1518   d->push_back (trans);
1519 }
1520
1521 /* Add a transition from FROM to a new, empty state that is taken
1522    when TEST == LABELS.  OPTIONAL says whether the new transition
1523    should be optional.  Return the new state.  */
1524
1525 static state *
1526 add_decision (state *from, const rtx_test &test, int_set labels, bool optional)
1527 {
1528   state *to = new state;
1529   add_decision (from, test, new transition (labels, to, optional));
1530   return to;
1531 }
1532
1533 /* Insert a decision before decisions R to make them dependent on
1534    TEST == LABELS.  OPTIONAL says whether the new transition should be
1535    optional.  */
1536
1537 static decision *
1538 insert_decision_before (state::range r, const rtx_test &test,
1539                         const int_set &labels, bool optional)
1540 {
1541   decision *newd = new decision (test);
1542   state *news = new state;
1543   newd->push_back (new transition (labels, news, optional));
1544   r.start->s->replace (r, newd);
1545   news->push_back (r);
1546   return newd;
1547 }
1548
1549 /* Remove any optional transitions from S that turned out not to be useful.  */
1550
1551 static void
1552 collapse_optional_decisions (state *s)
1553 {
1554   decision *d = s->first;
1555   while (d)
1556     {
1557       decision *next = d->next;
1558       for (transition *trans = d->first; trans; trans = trans->next)
1559         collapse_optional_decisions (trans->to);
1560       /* A decision with a single optional transition doesn't help
1561          partition the potential matches and so is unlikely to be
1562          worthwhile.  In particular, if the decision that performs the
1563          test is the last in the state, the best it could do is reject
1564          an invalid pattern slightly earlier.  If instead the decision
1565          is not the last in the state, the condition it tests could hold
1566          even for the later decisions in the state.  The best it can do
1567          is save work in some cases where only the later decisions can
1568          succeed.
1569
1570          In both cases the optional transition would add extra work to
1571          successful matches when the tested condition holds.  */
1572       if (transition *trans = d->singleton ())
1573         if (trans->optional)
1574           s->replace (d, trans->to->release ());
1575       d = next;
1576     }
1577 }
1578
1579 /* Try to squash several separate tests into simpler ones.  */
1580
1581 static void
1582 simplify_tests (state *s)
1583 {
1584   for (decision *d = s->first; d; d = d->next)
1585     {
1586       uint64_t label;
1587       /* Convert checks for GET_CODE (x) == CONST_INT and XWINT (x, 0) == N
1588          into checks for const_int_rtx[N'], if N is suitably small.  */
1589       if (d->test.kind == rtx_test::CODE
1590           && d->if_statement_p (&label)
1591           && label == CONST_INT)
1592         if (decision *second = d->first->to->singleton ())
1593           if (d->test.pos == second->test.pos
1594               && second->test.kind == rtx_test::WIDE_INT_FIELD
1595               && second->test.u.opno == 0
1596               && second->if_statement_p (&label)
1597               && IN_RANGE (int64_t (label),
1598                            -MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT))
1599             {
1600               d->test.kind = rtx_test::SAVED_CONST_INT;
1601               d->test.u.integer.is_param = false;
1602               d->test.u.integer.value = label;
1603               d->replace (d->first, second->release ());
1604               d->first->labels[0] = true;
1605             }
1606       /* If we have a CODE test followed by a PREDICATE test, rely on
1607          the predicate to test the code.
1608
1609          This case exists for match_operators.  We initially treat the
1610          CODE test for a match_operator as non-optional so that we can
1611          safely move down to its operands.  It may turn out that all
1612          paths that reach that code test require the same predicate
1613          to be true.  cse_tests will then put the predicate test in
1614          series with the code test.  */
1615       if (d->test.kind == rtx_test::CODE)
1616         if (transition *trans = d->singleton ())
1617           {
1618             state *s = trans->to;
1619             while (decision *d2 = s->singleton ())
1620               {
1621                 if (d->test.pos != d2->test.pos)
1622                   break;
1623                 transition *trans2 = d2->singleton ();
1624                 if (!trans2)
1625                   break;
1626                 if (d2->test.kind == rtx_test::PREDICATE)
1627                   {
1628                     d->test = d2->test;
1629                     trans->labels = int_set (true);
1630                     s->replace (d2, trans2->to->release ());
1631                     break;
1632                   }
1633                 s = trans2->to;
1634               }
1635           }
1636       for (transition *trans = d->first; trans; trans = trans->next)
1637         simplify_tests (trans->to);
1638     }
1639 }
1640
1641 /* Return true if all successful returns passing through D require the
1642    condition tested by COMMON to be true.
1643
1644    When returning true, add all transitions like COMMON in D to WHERE.
1645    WHERE may contain a partial result on failure.  */
1646
1647 static bool
1648 common_test_p (decision *d, transition *common, vec <transition *> *where)
1649 {
1650   if (d->test.kind == rtx_test::ACCEPT)
1651     /* We found a successful return that didn't require COMMON.  */
1652     return false;
1653   if (d->test == common->from->test)
1654     {
1655       transition *trans = d->singleton ();
1656       if (!trans
1657           || trans->optional != common->optional
1658           || trans->labels != common->labels)
1659         return false;
1660       where->safe_push (trans);
1661       return true;
1662     }
1663   for (transition *trans = d->first; trans; trans = trans->next)
1664     for (decision *subd = trans->to->first; subd; subd = subd->next)
1665       if (!common_test_p (subd, common, where))
1666         return false;
1667   return true;
1668 }
1669
1670 /* Indicates that we have tested GET_CODE (X) for a particular rtx X.  */
1671 const unsigned char TESTED_CODE = 1;
1672
1673 /* Indicates that we have tested XVECLEN (X, 0) for a particular rtx X.  */
1674 const unsigned char TESTED_VECLEN = 2;
1675
1676 /* Represents a set of conditions that are known to hold.  */
1677 struct known_conditions
1678 {
1679   /* A mask of TESTED_ values for each position, indexed by the position's
1680      id field.  */
1681   auto_vec <unsigned char> position_tests;
1682
1683   /* Index N says whether operands[N] has been set.  */
1684   auto_vec <bool> set_operands;
1685
1686   /* A guranteed lower bound on the value of peep2_current_count.  */
1687   int peep2_count;
1688 };
1689
1690 /* Return true if TEST can safely be performed at D, where
1691    the conditions in KC hold.  TEST is known to occur along the
1692    first path from D (i.e. always following the first transition
1693    of the first decision).  Any intervening tests can be used as
1694    negative proof that hoisting isn't safe, but only KC can be used
1695    as positive proof.  */
1696
1697 static bool
1698 safe_to_hoist_p (decision *d, const rtx_test &test, known_conditions *kc)
1699 {
1700   switch (test.kind)
1701     {
1702     case rtx_test::C_TEST:
1703       /* In general, C tests require everything else to have been
1704          verified and all operands to have been set up.  */
1705       return false;
1706
1707     case rtx_test::ACCEPT:
1708       /* Don't accept something before all conditions have been tested.  */
1709       return false;
1710
1711     case rtx_test::PREDICATE:
1712       /* Don't move a predicate over a test for VECLEN_GE, since the
1713          predicate used in a match_parallel can legitimately expect the
1714          length to be checked first.  */
1715       for (decision *subd = d;
1716            subd->test != test;
1717            subd = subd->first->to->first)
1718         if (subd->test.pos == test.pos
1719             && subd->test.kind == rtx_test::VECLEN_GE)
1720           return false;
1721       goto any_rtx;
1722
1723     case rtx_test::DUPLICATE:
1724       /* Don't test for a match_dup until the associated operand has
1725          been set.  */
1726       if (!kc->set_operands[test.u.opno])
1727         return false;
1728       goto any_rtx;
1729
1730     case rtx_test::CODE:
1731     case rtx_test::MODE:
1732     case rtx_test::SAVED_CONST_INT:
1733     case rtx_test::SET_OP:
1734     any_rtx:
1735       /* Check whether it is safe to access the rtx under test.  */
1736       switch (test.pos->type)
1737         {
1738         case POS_PEEP2_INSN:
1739           return test.pos->arg < kc->peep2_count;
1740
1741         case POS_XEXP:
1742           return kc->position_tests[test.pos->base->id] & TESTED_CODE;
1743
1744         case POS_XVECEXP0:
1745           return kc->position_tests[test.pos->base->id] & TESTED_VECLEN;
1746         }
1747       gcc_unreachable ();
1748
1749     case rtx_test::REGNO_FIELD:
1750     case rtx_test::INT_FIELD:
1751     case rtx_test::WIDE_INT_FIELD:
1752     case rtx_test::VECLEN:
1753     case rtx_test::VECLEN_GE:
1754       /* These tests access a specific part of an rtx, so are only safe
1755          once we know what the rtx is.  */
1756       return kc->position_tests[test.pos->id] & TESTED_CODE;
1757
1758     case rtx_test::PEEP2_COUNT:
1759     case rtx_test::HAVE_NUM_CLOBBERS:
1760       /* These tests can be performed anywhere.  */
1761       return true;
1762
1763     case rtx_test::PATTERN:
1764       gcc_unreachable ();
1765     }
1766   gcc_unreachable ();
1767 }
1768
1769 /* Look for a transition that is taken by all successful returns from a range
1770    of decisions starting at OUTER and that would be better performed by
1771    OUTER's state instead.  On success, store all instances of that transition
1772    in WHERE and return the last decision in the range.  The range could
1773    just be OUTER, or it could include later decisions as well.
1774
1775    WITH_POSITION_P is true if only tests with position POS should be tried,
1776    false if any test should be tried.  WORTHWHILE_SINGLE_P is true if the
1777    result is useful even when the range contains just a single decision
1778    with a single transition.  KC are the conditions that are known to
1779    hold at OUTER.  */
1780
1781 static decision *
1782 find_common_test (decision *outer, bool with_position_p,
1783                   position *pos, bool worthwhile_single_p,
1784                   known_conditions *kc, vec <transition *> *where)
1785 {
1786   /* After this, WORTHWHILE_SINGLE_P indicates whether a range that contains
1787      just a single decision is useful, regardless of the number of
1788      transitions it has.  */
1789   if (!outer->singleton ())
1790     worthwhile_single_p = true;
1791   /* Quick exit if we don't have enough decisions to form a worthwhile
1792      range.  */
1793   if (!worthwhile_single_p && !outer->next)
1794     return 0;
1795   /* Follow the first chain down, as one example of a path that needs
1796      to contain the common test.  */
1797   for (decision *d = outer; d; d = d->first->to->first)
1798     {
1799       transition *trans = d->singleton ();
1800       if (trans
1801           && (!with_position_p || d->test.pos == pos)
1802           && safe_to_hoist_p (outer, d->test, kc))
1803         {
1804           if (common_test_p (outer, trans, where))
1805             {
1806               if (!outer->next)
1807                 /* We checked above whether the move is worthwhile.  */
1808                 return outer;
1809               /* See how many decisions in OUTER's chain could reuse
1810                  the same test.  */
1811               decision *outer_end = outer;
1812               do
1813                 {
1814                   unsigned int length = where->length ();
1815                   if (!common_test_p (outer_end->next, trans, where))
1816                     {
1817                       where->truncate (length);
1818                       break;
1819                     }
1820                   outer_end = outer_end->next;
1821                 }
1822               while (outer_end->next);
1823               /* It is worth moving TRANS if it can be shared by more than
1824                  one decision.  */
1825               if (outer_end != outer || worthwhile_single_p)
1826                 return outer_end;
1827             }
1828           where->truncate (0);
1829         }
1830     }
1831   return 0;
1832 }
1833
1834 /* Try to promote common subtests in S to a single, shared decision.
1835    Also try to bunch tests for the same position together.  POS is the
1836    position of the rtx tested before reaching S.  KC are the conditions
1837    that are known to hold on entry to S.  */
1838
1839 static void
1840 cse_tests (position *pos, state *s, known_conditions *kc)
1841 {
1842   for (decision *d = s->first; d; d = d->next)
1843     {
1844       auto_vec <transition *, 16> where;
1845       if (d->test.pos)
1846         {
1847           /* Try to find conditions that don't depend on a particular rtx,
1848              such as pnum_clobbers != NULL or peep2_current_count >= X.
1849              It's usually better to check these conditions as soon as
1850              possible, so the change is worthwhile even if there is
1851              only one copy of the test.  */
1852           decision *endd = find_common_test (d, true, 0, true, kc, &where);
1853           if (!endd && d->test.pos != pos)
1854             /* Try to find other conditions related to position POS
1855                before moving to the new position.  Again, this is
1856                worthwhile even if there is only one copy of the test,
1857                since it means that fewer position variables are live
1858                at a given time.  */
1859             endd = find_common_test (d, true, pos, true, kc, &where);
1860           if (!endd)
1861             /* Try to find any condition that is used more than once.  */
1862             endd = find_common_test (d, false, 0, false, kc, &where);
1863           if (endd)
1864             {
1865               transition *common = where[0];
1866               /* Replace [D, ENDD] with a test like COMMON.  We'll recurse
1867                  on the common test and see the original D again next time.  */
1868               d = insert_decision_before (state::range (d, endd),
1869                                           common->from->test,
1870                                           common->labels,
1871                                           common->optional);
1872               /* Remove the old tests.  */
1873               while (!where.is_empty ())
1874                 {
1875                   transition *trans = where.pop ();
1876                   trans->from->s->replace (trans->from, trans->to->release ());
1877                 }
1878             }
1879         }
1880
1881       /* Make sure that safe_to_hoist_p isn't being overly conservative.
1882          It should realize that D's test is safe in the current
1883          environment.  */
1884       gcc_assert (d->test.kind == rtx_test::C_TEST
1885                   || d->test.kind == rtx_test::ACCEPT
1886                   || safe_to_hoist_p (d, d->test, kc));
1887
1888       /* D won't be changed any further by the current optimization.
1889          Recurse with the state temporarily updated to include D.  */
1890       int prev = 0;
1891       switch (d->test.kind)
1892         {
1893         case rtx_test::CODE:
1894           prev = kc->position_tests[d->test.pos->id];
1895           kc->position_tests[d->test.pos->id] |= TESTED_CODE;
1896           break;
1897
1898         case rtx_test::VECLEN:
1899         case rtx_test::VECLEN_GE:
1900           prev = kc->position_tests[d->test.pos->id];
1901           kc->position_tests[d->test.pos->id] |= TESTED_VECLEN;
1902           break;
1903
1904         case rtx_test::SET_OP:
1905           prev = kc->set_operands[d->test.u.opno];
1906           gcc_assert (!prev);
1907           kc->set_operands[d->test.u.opno] = true;
1908           break;
1909
1910         case rtx_test::PEEP2_COUNT:
1911           prev = kc->peep2_count;
1912           kc->peep2_count = MAX (prev, d->test.u.min_len);
1913           break;
1914
1915         default:
1916           break;
1917         }
1918       for (transition *trans = d->first; trans; trans = trans->next)
1919         cse_tests (d->test.pos ? d->test.pos : pos, trans->to, kc);
1920       switch (d->test.kind)
1921         {
1922         case rtx_test::CODE:
1923         case rtx_test::VECLEN:
1924         case rtx_test::VECLEN_GE:
1925           kc->position_tests[d->test.pos->id] = prev;
1926           break;
1927
1928         case rtx_test::SET_OP:
1929           kc->set_operands[d->test.u.opno] = prev;
1930           break;
1931
1932         case rtx_test::PEEP2_COUNT:
1933           kc->peep2_count = prev;
1934           break;
1935
1936         default:
1937           break;
1938         }
1939     }
1940 }
1941
1942 /* Return the type of value that can be used to parameterize test KIND,
1943    or parameter::UNSET if none.  */
1944
1945 parameter::type_enum
1946 transition_parameter_type (rtx_test::kind_enum kind)
1947 {
1948   switch (kind)
1949     {
1950     case rtx_test::CODE:
1951       return parameter::CODE;
1952
1953     case rtx_test::MODE:
1954       return parameter::MODE;
1955
1956     case rtx_test::REGNO_FIELD:
1957       return parameter::UINT;
1958
1959     case rtx_test::INT_FIELD:
1960     case rtx_test::VECLEN:
1961     case rtx_test::PATTERN:
1962       return parameter::INT;
1963
1964     case rtx_test::WIDE_INT_FIELD:
1965       return parameter::WIDE_INT;
1966
1967     case rtx_test::PEEP2_COUNT:
1968     case rtx_test::VECLEN_GE:
1969     case rtx_test::SAVED_CONST_INT:
1970     case rtx_test::PREDICATE:
1971     case rtx_test::DUPLICATE:
1972     case rtx_test::HAVE_NUM_CLOBBERS:
1973     case rtx_test::C_TEST:
1974     case rtx_test::SET_OP:
1975     case rtx_test::ACCEPT:
1976       return parameter::UNSET;
1977     }
1978   gcc_unreachable ();
1979 }
1980
1981 /* Initialize the pos_operand fields of each state reachable from S.
1982    If OPERAND_POS[ID] >= 0, the position with id ID is stored in
1983    operands[OPERAND_POS[ID]] on entry to S.  */
1984
1985 static void
1986 find_operand_positions (state *s, vec <int> &operand_pos)
1987 {
1988   for (decision *d = s->first; d; d = d->next)
1989     {
1990       int this_operand = (d->test.pos ? operand_pos[d->test.pos->id] : -1);
1991       if (this_operand >= 0)
1992         d->test.pos_operand = this_operand;
1993       if (d->test.kind == rtx_test::SET_OP)
1994         operand_pos[d->test.pos->id] = d->test.u.opno;
1995       for (transition *trans = d->first; trans; trans = trans->next)
1996         find_operand_positions (trans->to, operand_pos);
1997       if (d->test.kind == rtx_test::SET_OP)
1998         operand_pos[d->test.pos->id] = this_operand;
1999     }
2000 }
2001
2002 /* Statistics about a matching routine.  */
2003 struct stats
2004 {
2005   stats ();
2006
2007   /* The total number of decisions in the routine, excluding trivial
2008      ones that never fail.  */
2009   unsigned int num_decisions;
2010
2011   /* The number of non-trivial decisions on the longest path through
2012      the routine, and the return value that contributes most to that
2013      long path.  */
2014   unsigned int longest_path;
2015   int longest_path_code;
2016
2017   /* The maximum number of times that a single call to the routine
2018      can backtrack, and the value returned at the end of that path.
2019      "Backtracking" here means failing one decision in state and
2020      going onto to the next.  */
2021   unsigned int longest_backtrack;
2022   int longest_backtrack_code;
2023 };
2024
2025 stats::stats ()
2026   : num_decisions (0), longest_path (0), longest_path_code (-1),
2027     longest_backtrack (0), longest_backtrack_code (-1) {}
2028
2029 /* Return statistics about S.  */
2030
2031 static stats
2032 get_stats (state *s)
2033 {
2034   stats for_s;
2035   unsigned int longest_path = 0;
2036   for (decision *d = s->first; d; d = d->next)
2037     {
2038       /* Work out the statistics for D.  */
2039       stats for_d;
2040       for (transition *trans = d->first; trans; trans = trans->next)
2041         {
2042           stats for_trans = get_stats (trans->to);
2043           for_d.num_decisions += for_trans.num_decisions;
2044           /* Each transition is mutually-exclusive, so just pick the
2045              longest of the individual paths.  */
2046           if (for_d.longest_path <= for_trans.longest_path)
2047             {
2048               for_d.longest_path = for_trans.longest_path;
2049               for_d.longest_path_code = for_trans.longest_path_code;
2050             }
2051           /* Likewise for backtracking.  */
2052           if (for_d.longest_backtrack <= for_trans.longest_backtrack)
2053             {
2054               for_d.longest_backtrack = for_trans.longest_backtrack;
2055               for_d.longest_backtrack_code = for_trans.longest_backtrack_code;
2056             }
2057         }
2058
2059       /* Account for D's test in its statistics.  */
2060       if (!d->test.single_outcome_p ())
2061         {
2062           for_d.num_decisions += 1;
2063           for_d.longest_path += 1;
2064         }
2065       if (d->test.kind == rtx_test::ACCEPT)
2066         {
2067           for_d.longest_path_code = d->test.u.acceptance.u.full.code;
2068           for_d.longest_backtrack_code = d->test.u.acceptance.u.full.code;
2069         }
2070
2071       /* Keep a running count of the number of backtracks.  */
2072       if (d->prev)
2073         for_s.longest_backtrack += 1;
2074
2075       /* Accumulate D's statistics into S's.  */
2076       for_s.num_decisions += for_d.num_decisions;
2077       for_s.longest_path += for_d.longest_path;
2078       for_s.longest_backtrack += for_d.longest_backtrack;
2079
2080       /* Use the code from the decision with the longest individual path,
2081          since that's more likely to be useful if trying to make the
2082          path shorter.  In the event of a tie, pick the later decision,
2083          since that's closer to the end of the path.  */
2084       if (longest_path <= for_d.longest_path)
2085         {
2086           longest_path = for_d.longest_path;
2087           for_s.longest_path_code = for_d.longest_path_code;
2088         }
2089
2090       /* Later decisions in a state are necessarily in a longer backtrack
2091          than earlier decisions.  */
2092       for_s.longest_backtrack_code = for_d.longest_backtrack_code;
2093     }
2094   return for_s;
2095 }
2096
2097 /* Optimize ROOT.  Use TYPE to describe ROOT in status messages.  */
2098
2099 static void
2100 optimize_subroutine_group (const char *type, state *root)
2101 {
2102   /* Remove optional transitions that turned out not to be worthwhile.  */
2103   if (collapse_optional_decisions_p)
2104     collapse_optional_decisions (root);
2105
2106   /* Try to remove duplicated tests and to rearrange tests into a more
2107      logical order.  */
2108   if (cse_tests_p)
2109     {
2110       known_conditions kc;
2111       kc.position_tests.safe_grow_cleared (num_positions);
2112       kc.set_operands.safe_grow_cleared (num_operands);
2113       kc.peep2_count = 1;
2114       cse_tests (&root_pos, root, &kc);
2115     }
2116
2117   /* Try to simplify two or more tests into one.  */
2118   if (simplify_tests_p)
2119     simplify_tests (root);
2120
2121   /* Try to use operands[] instead of xN variables.  */
2122   if (use_operand_variables_p)
2123     {
2124       auto_vec <int> operand_pos (num_positions);
2125       for (unsigned int i = 0; i < num_positions; ++i)
2126         operand_pos.quick_push (-1);
2127       find_operand_positions (root, operand_pos);
2128     }
2129
2130   /* Print a summary of the new state.  */
2131   stats st = get_stats (root);
2132   fprintf (stderr, "Statistics for %s:\n", type);
2133   fprintf (stderr, "  Number of decisions: %6d\n", st.num_decisions);
2134   fprintf (stderr, "  longest path:        %6d (code: %6d)\n",
2135            st.longest_path, st.longest_path_code);
2136   fprintf (stderr, "  longest backtrack:   %6d (code: %6d)\n",
2137            st.longest_backtrack, st.longest_backtrack_code);
2138 }
2139
2140 struct merge_pattern_info;
2141
2142 /* Represents a transition from one pattern to another.  */
2143 struct merge_pattern_transition
2144 {
2145   merge_pattern_transition (merge_pattern_info *);
2146
2147   /* The target pattern.  */
2148   merge_pattern_info *to;
2149
2150   /* The parameters that the source pattern passes to the target pattern.
2151      "parameter (TYPE, true, I)" represents parameter I of the source
2152      pattern.  */
2153   auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2154 };
2155
2156 merge_pattern_transition::merge_pattern_transition (merge_pattern_info *to_in)
2157   : to (to_in)
2158 {
2159 }
2160
2161 /* Represents a pattern that can might match several states.  The pattern
2162    may replace parts of the test with a parameter value.  It may also
2163    replace transition labels with parameters.  */
2164 struct merge_pattern_info
2165 {
2166   merge_pattern_info (unsigned int);
2167
2168   /* If PARAM_TEST_P, the state's singleton test should be generalized
2169      to use the runtime value of PARAMS[PARAM_TEST].  */
2170   unsigned int param_test : 8;
2171
2172   /* If PARAM_TRANSITION_P, the state's single transition label should
2173      be replaced by the runtime value of PARAMS[PARAM_TRANSITION].  */
2174   unsigned int param_transition : 8;
2175
2176   /* True if we have decided to generalize the root decision's test,
2177      as per PARAM_TEST.  */
2178   unsigned int param_test_p : 1;
2179
2180   /* Likewise for the root decision's transition, as per PARAM_TRANSITION.  */
2181   unsigned int param_transition_p : 1;
2182
2183   /* True if the contents of the structure are completely filled in.  */
2184   unsigned int complete_p : 1;
2185
2186   /* The number of pseudo-statements in the pattern.  Used to decide
2187      whether it's big enough to break out into a subroutine.  */
2188   unsigned int num_statements;
2189
2190   /* The number of states that use this pattern.  */
2191   unsigned int num_users;
2192
2193   /* The number of distinct success values that the pattern returns.  */
2194   unsigned int num_results;
2195
2196   /* This array has one element for each runtime parameter to the pattern.
2197      PARAMS[I] gives the default value of parameter I, which is always
2198      constant.
2199
2200      These default parameters are used in cases where we match the
2201      pattern against some state S1, then add more parameters while
2202      matching against some state S2.  S1 is then left passing fewer
2203      parameters than S2.  The array gives us enough informatino to
2204      construct a full parameter list for S1 (see update_parameters).
2205
2206      If we decide to create a subroutine for this pattern,
2207      PARAMS[I].type determines the C type of parameter I.  */
2208   auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2209
2210   /* All states that match this pattern must have the same number of
2211      transitions.  TRANSITIONS[I] describes the subpattern for transition
2212      number I; it is null if transition I represents a successful return
2213      from the pattern.  */
2214   auto_vec <merge_pattern_transition *, 1> transitions;
2215
2216   /* The routine associated with the pattern, or null if we haven't generated
2217      one yet.  */
2218   pattern_routine *routine;
2219 };
2220
2221 merge_pattern_info::merge_pattern_info (unsigned int num_transitions)
2222   : param_test (0),
2223     param_transition (0),
2224     param_test_p (false),
2225     param_transition_p (false),
2226     complete_p (false),
2227     num_statements (0),
2228     num_users (0),
2229     num_results (0),
2230     routine (0)
2231 {
2232   transitions.safe_grow_cleared (num_transitions);
2233 }
2234
2235 /* Describes one way of matching a particular state to a particular
2236    pattern.  */
2237 struct merge_state_result
2238 {
2239   merge_state_result (merge_pattern_info *, position *, merge_state_result *);
2240
2241   /* A pattern that matches the state.  */
2242   merge_pattern_info *pattern;
2243
2244   /* If we decide to use this match and create a subroutine for PATTERN,
2245      the state should pass the rtx at position ROOT to the pattern's
2246      rtx parameter.  A null root means that the pattern doesn't need
2247      an rtx parameter; all the rtxes it matches come from elsewhere.  */
2248   position *root;
2249
2250   /* The parameters that should be passed to PATTERN for this state.
2251      If the array is shorter than PATTERN->params, the missing entries
2252      should be taken from the corresponding element of PATTERN->params.  */
2253   auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2254
2255   /* An earlier match for the same state, or null if none.  Patterns
2256      matched by earlier entries are smaller than PATTERN.  */
2257   merge_state_result *prev;
2258 };
2259
2260 merge_state_result::merge_state_result (merge_pattern_info *pattern_in,
2261                                         position *root_in,
2262                                         merge_state_result *prev_in)
2263   : pattern (pattern_in), root (root_in), prev (prev_in)
2264 {}
2265
2266 /* Information about a state, used while trying to match it against
2267    a pattern.  */
2268 struct merge_state_info
2269 {
2270   merge_state_info (state *);
2271
2272   /* The state itself.  */
2273   state *s;
2274
2275   /* Index I gives information about the target of transition I.  */
2276   merge_state_info *to_states;
2277
2278   /* The number of transitions in S.  */
2279   unsigned int num_transitions;
2280
2281   /* True if the state has been deleted in favor of a call to a
2282      pattern routine.  */
2283   bool merged_p;
2284
2285   /* The previous state that might be a merge candidate for S, or null
2286      if no previous states could be merged with S.  */
2287   merge_state_info *prev_same_test;
2288
2289   /* A list of pattern matches for this state.  */
2290   merge_state_result *res;
2291 };
2292
2293 merge_state_info::merge_state_info (state *s_in)
2294   : s (s_in),
2295     to_states (0),
2296     num_transitions (0),
2297     merged_p (false),
2298     prev_same_test (0),
2299     res (0) {}
2300
2301 /* True if PAT would be useful as a subroutine.  */
2302
2303 static bool
2304 useful_pattern_p (merge_pattern_info *pat)
2305 {
2306   return pat->num_statements >= MIN_COMBINE_COST;
2307 }
2308
2309 /* PAT2 is a subpattern of PAT1.  Return true if PAT2 should be inlined
2310    into PAT1's C routine.  */
2311
2312 static bool
2313 same_pattern_p (merge_pattern_info *pat1, merge_pattern_info *pat2)
2314 {
2315   return pat1->num_users == pat2->num_users || !useful_pattern_p (pat2);
2316 }
2317
2318 /* PAT was previously matched against SINFO based on tentative matches
2319    for the target states of SINFO's state.  Return true if the match
2320    still holds; that is, if the target states of SINFO's state still
2321    match the corresponding transitions of PAT.  */
2322
2323 static bool
2324 valid_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2325 {
2326   for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2327     if (merge_pattern_transition *ptrans = pat->transitions[j])
2328       {
2329         merge_state_result *to_res = sinfo->to_states[j].res;
2330         if (!to_res || to_res->pattern != ptrans->to)
2331           return false;
2332       }
2333   return true;
2334 }
2335
2336 /* Remove any matches that are no longer valid from the head of SINFO's
2337    list of matches.  */
2338
2339 static void
2340 prune_invalid_results (merge_state_info *sinfo)
2341 {
2342   while (sinfo->res && !valid_result_p (sinfo->res->pattern, sinfo))
2343     {
2344       sinfo->res = sinfo->res->prev;
2345       gcc_assert (sinfo->res);
2346     }
2347 }
2348
2349 /* Return true if PAT represents the biggest posssible match for SINFO;
2350    that is, if the next action of SINFO's state on return from PAT will
2351    be something that cannot be merged with any other state.  */
2352
2353 static bool
2354 complete_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2355 {
2356   for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2357     if (sinfo->to_states[j].res && !pat->transitions[j])
2358       return false;
2359   return true;
2360 }
2361
2362 /* Update TO for any parameters that have been added to FROM since TO
2363    was last set.  The extra parameters in FROM will be constants or
2364    instructions to duplicate earlier parameters.  */
2365
2366 static void
2367 update_parameters (vec <parameter> &to, const vec <parameter> &from)
2368 {
2369   for (unsigned int i = to.length (); i < from.length (); ++i)
2370     to.quick_push (from[i]);
2371 }
2372
2373 /* Return true if A and B can be tested by a single test.  If the test
2374    can be parameterised, store the parameter value for A in *PARAMA and
2375    the parameter value for B in *PARAMB, otherwise leave PARAMA and
2376    PARAMB alone.  */
2377
2378 static bool
2379 compatible_tests_p (const rtx_test &a, const rtx_test &b,
2380                     parameter *parama, parameter *paramb)
2381 {
2382   if (a.kind != b.kind)
2383     return false;
2384   switch (a.kind)
2385     {
2386     case rtx_test::PREDICATE:
2387       if (a.u.predicate.data != b.u.predicate.data)
2388         return false;
2389       *parama = parameter (parameter::MODE, false, a.u.predicate.mode);
2390       *paramb = parameter (parameter::MODE, false, b.u.predicate.mode);
2391       return true;
2392
2393     case rtx_test::SAVED_CONST_INT:
2394       *parama = parameter (parameter::INT, false, a.u.integer.value);
2395       *paramb = parameter (parameter::INT, false, b.u.integer.value);
2396       return true;
2397
2398     default:
2399       return a == b;
2400     }
2401 }
2402
2403 /* PARAMS is an array of the parameters that a state is going to pass
2404    to a pattern routine.  It is still incomplete; index I has a kind of
2405    parameter::UNSET if we don't yet know what the state will pass
2406    as parameter I.  Try to make parameter ID equal VALUE, returning
2407    true on success.  */
2408
2409 static bool
2410 set_parameter (vec <parameter> &params, unsigned int id,
2411                const parameter &value)
2412 {
2413   if (params[id].type == parameter::UNSET)
2414     {
2415       if (force_unique_params_p)
2416         for (unsigned int i = 0; i < params.length (); ++i)
2417           if (params[i] == value)
2418             return false;
2419       params[id] = value;
2420       return true;
2421     }
2422   return params[id] == value;
2423 }
2424
2425 /* PARAMS2 is the "params" array for a pattern and PARAMS1 is the
2426    set of parameters that a particular state is going to pass to
2427    that pattern.
2428
2429    Try to extend PARAMS1 and PARAMS2 so that there is a parameter
2430    that is equal to PARAM1 for the state and has a default value of
2431    PARAM2.  Parameters beginning at START were added as part of the
2432    same match and so may be reused.  */
2433
2434 static bool
2435 add_parameter (vec <parameter> &params1, vec <parameter> &params2,
2436                const parameter &param1, const parameter &param2,
2437                unsigned int start, unsigned int *res)
2438 {
2439   gcc_assert (params1.length () == params2.length ());
2440   gcc_assert (!param1.is_param && !param2.is_param);
2441
2442   for (unsigned int i = start; i < params2.length (); ++i)
2443     if (params1[i] == param1 && params2[i] == param2)
2444       {
2445         *res = i;
2446         return true;
2447       }
2448
2449   if (force_unique_params_p)
2450     for (unsigned int i = 0; i < params2.length (); ++i)
2451       if (params1[i] == param1 || params2[i] == param2)
2452         return false;
2453
2454   if (params2.length () >= MAX_PATTERN_PARAMS)
2455     return false;
2456
2457   *res = params2.length ();
2458   params1.quick_push (param1);
2459   params2.quick_push (param2);
2460   return true;
2461 }
2462
2463 /* If *ROOTA is nonnull, return true if the same sequence of steps are
2464    required to reach A from *ROOTA as to reach B from ROOTB.  If *ROOTA
2465    is null, update it if necessary in order to make the condition hold.  */
2466
2467 static bool
2468 merge_relative_positions (position **roota, position *a,
2469                           position *rootb, position *b)
2470 {
2471   if (!relative_patterns_p)
2472     {
2473       if (a != b)
2474         return false;
2475       if (!*roota)
2476         {
2477           *roota = rootb;
2478           return true;
2479         }
2480       return *roota == rootb;
2481     }
2482   /* If B does not belong to the same instruction as ROOTB, we don't
2483      start with ROOTB but instead start with a call to peep2_next_insn.
2484      In that case the sequences for B and A are identical iff B and A
2485      are themselves identical.  */
2486   if (rootb->insn_id != b->insn_id)
2487     return a == b;
2488   while (rootb != b)
2489     {
2490       if (!a || b->type != a->type || b->arg != a->arg)
2491         return false;
2492       b = b->base;
2493       a = a->base;
2494     }
2495   if (!*roota)
2496     *roota = a;
2497   return *roota == a;
2498 }
2499
2500 /* A hasher of states that treats two states as "equal" if they might be
2501    merged (but trying to be more discriminating than "return true").  */
2502 struct test_pattern_hasher : nofree_ptr_hash <merge_state_info>
2503 {
2504   static inline hashval_t hash (const value_type &);
2505   static inline bool equal (const value_type &, const compare_type &);
2506 };
2507
2508 hashval_t
2509 test_pattern_hasher::hash (merge_state_info *const &sinfo)
2510 {
2511   inchash::hash h;
2512   decision *d = sinfo->s->singleton ();
2513   h.add_int (d->test.pos_operand + 1);
2514   if (!relative_patterns_p)
2515     h.add_int (d->test.pos ? d->test.pos->id + 1 : 0);
2516   h.add_int (d->test.kind);
2517   h.add_int (sinfo->num_transitions);
2518   return h.end ();
2519 }
2520
2521 bool
2522 test_pattern_hasher::equal (merge_state_info *const &sinfo1,
2523                             merge_state_info *const &sinfo2)
2524 {
2525   decision *d1 = sinfo1->s->singleton ();
2526   decision *d2 = sinfo2->s->singleton ();
2527   gcc_assert (d1 && d2);
2528
2529   parameter new_param1, new_param2;
2530   return (d1->test.pos_operand == d2->test.pos_operand
2531           && (relative_patterns_p || d1->test.pos == d2->test.pos)
2532           && compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2)
2533           && sinfo1->num_transitions == sinfo2->num_transitions);
2534 }
2535
2536 /* Try to make the state described by SINFO1 use the same pattern as the
2537    state described by SINFO2.  Return true on success.
2538
2539    SINFO1 and SINFO2 are known to have the same hash value.  */
2540
2541 static bool
2542 merge_patterns (merge_state_info *sinfo1, merge_state_info *sinfo2)
2543 {
2544   merge_state_result *res2 = sinfo2->res;
2545   merge_pattern_info *pat = res2->pattern;
2546
2547   /* Write to temporary arrays while matching, in case we have to abort
2548      half way through.  */
2549   auto_vec <parameter, MAX_PATTERN_PARAMS> params1;
2550   auto_vec <parameter, MAX_PATTERN_PARAMS> params2;
2551   params1.quick_grow_cleared (pat->params.length ());
2552   params2.splice (pat->params);
2553   unsigned int start_param = params2.length ();
2554
2555   /* An array for recording changes to PAT->transitions[?].params.
2556      All changes involve replacing a constant parameter with some
2557      PAT->params[N], where N is the second element of the pending_param.  */
2558   typedef std::pair <parameter *, unsigned int> pending_param;
2559   auto_vec <pending_param, 32> pending_params;
2560
2561   decision *d1 = sinfo1->s->singleton ();
2562   decision *d2 = sinfo2->s->singleton ();
2563   gcc_assert (d1 && d2);
2564
2565   /* If D2 tests a position, SINFO1's root relative to D1 is the same
2566      as SINFO2's root relative to D2.  */
2567   position *root1 = 0;
2568   position *root2 = res2->root;
2569   if (d2->test.pos_operand < 0
2570       && d1->test.pos
2571       && !merge_relative_positions (&root1, d1->test.pos,
2572                                     root2, d2->test.pos))
2573     return false;
2574
2575   /* Check whether the patterns have the same shape.  */
2576   unsigned int num_transitions = sinfo1->num_transitions;
2577   gcc_assert (num_transitions == sinfo2->num_transitions);
2578   for (unsigned int i = 0; i < num_transitions; ++i)
2579     if (merge_pattern_transition *ptrans = pat->transitions[i])
2580       {
2581         merge_state_result *to1_res = sinfo1->to_states[i].res;
2582         merge_state_result *to2_res = sinfo2->to_states[i].res;
2583         merge_pattern_info *to_pat = ptrans->to;
2584         gcc_assert (to2_res && to2_res->pattern == to_pat);
2585         if (!to1_res || to1_res->pattern != to_pat)
2586           return false;
2587         if (to2_res->root
2588             && !merge_relative_positions (&root1, to1_res->root,
2589                                           root2, to2_res->root))
2590           return false;
2591         /* Match the parameters that TO1_RES passes to TO_PAT with the
2592            parameters that PAT passes to TO_PAT.  */
2593         update_parameters (to1_res->params, to_pat->params);
2594         for (unsigned int j = 0; j < to1_res->params.length (); ++j)
2595           {
2596             const parameter &param1 = to1_res->params[j];
2597             const parameter &param2 = ptrans->params[j];
2598             gcc_assert (!param1.is_param);
2599             if (param2.is_param)
2600               {
2601                 if (!set_parameter (params1, param2.value, param1))
2602                   return false;
2603               }
2604             else if (param1 != param2)
2605               {
2606                 unsigned int id;
2607                 if (!add_parameter (params1, params2,
2608                                     param1, param2, start_param, &id))
2609                   return false;
2610                 /* Record that PAT should now pass parameter ID to TO_PAT,
2611                    instead of the current contents of *PARAM2.  We only
2612                    make the change if the rest of the match succeeds.  */
2613                 pending_params.safe_push
2614                   (pending_param (&ptrans->params[j], id));
2615               }
2616           }
2617       }
2618
2619   unsigned int param_test = pat->param_test;
2620   unsigned int param_transition = pat->param_transition;
2621   bool param_test_p = pat->param_test_p;
2622   bool param_transition_p = pat->param_transition_p;
2623
2624   /* If the tests don't match exactly, try to parameterize them.  */
2625   parameter new_param1, new_param2;
2626   if (!compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2))
2627     gcc_unreachable ();
2628   if (new_param1.type != parameter::UNSET)
2629     {
2630       /* If the test has not already been parameterized, all existing
2631          matches use constant NEW_PARAM2.  */
2632       if (param_test_p)
2633         {
2634           if (!set_parameter (params1, param_test, new_param1))
2635             return false;
2636         }
2637       else if (new_param1 != new_param2)
2638         {
2639           if (!add_parameter (params1, params2, new_param1, new_param2,
2640                               start_param, &param_test))
2641             return false;
2642           param_test_p = true;
2643         }
2644     }
2645
2646   /* Match the transitions.  */
2647   transition *trans1 = d1->first;
2648   transition *trans2 = d2->first;
2649   for (unsigned int i = 0; i < num_transitions; ++i)
2650     {
2651       if (param_transition_p || trans1->labels != trans2->labels)
2652         {
2653           /* We can only generalize a single transition with a single
2654              label.  */
2655           if (num_transitions != 1
2656               || trans1->labels.length () != 1
2657               || trans2->labels.length () != 1)
2658             return false;
2659
2660           /* Although we can match wide-int fields, in practice it leads
2661              to some odd results for const_vectors.  We end up
2662              parameterizing the first N const_ints of the vector
2663              and then (once we reach the maximum number of parameters)
2664              we go on to match the other elements exactly.  */
2665           if (d1->test.kind == rtx_test::WIDE_INT_FIELD)
2666             return false;
2667
2668           /* See whether the label has a generalizable type.  */
2669           parameter::type_enum param_type
2670             = transition_parameter_type (d1->test.kind);
2671           if (param_type == parameter::UNSET)
2672             return false;
2673
2674           /* Match the labels using parameters.  */
2675           new_param1 = parameter (param_type, false, trans1->labels[0]);
2676           if (param_transition_p)
2677             {
2678               if (!set_parameter (params1, param_transition, new_param1))
2679                 return false;
2680             }
2681           else
2682             {
2683               new_param2 = parameter (param_type, false, trans2->labels[0]);
2684               if (!add_parameter (params1, params2, new_param1, new_param2,
2685                                   start_param, &param_transition))
2686                 return false;
2687               param_transition_p = true;
2688             }
2689         }
2690       trans1 = trans1->next;
2691       trans2 = trans2->next;
2692     }
2693
2694   /* Set any unset parameters to their default values.  This occurs if some
2695      other state needed something to be parameterized in order to match SINFO2,
2696      but SINFO1 on its own does not.  */
2697   for (unsigned int i = 0; i < params1.length (); ++i)
2698     if (params1[i].type == parameter::UNSET)
2699       params1[i] = params2[i];
2700
2701   /* The match was successful.  Commit all pending changes to PAT.  */
2702   update_parameters (pat->params, params2);
2703   {
2704     pending_param *pp;
2705     unsigned int i;
2706     FOR_EACH_VEC_ELT (pending_params, i, pp)
2707       *pp->first = parameter (pp->first->type, true, pp->second);
2708   }
2709   pat->param_test = param_test;
2710   pat->param_transition = param_transition;
2711   pat->param_test_p = param_test_p;
2712   pat->param_transition_p = param_transition_p;
2713
2714   /* Record the match of SINFO1.  */
2715   merge_state_result *new_res1 = new merge_state_result (pat, root1,
2716                                                          sinfo1->res);
2717   new_res1->params.splice (params1);
2718   sinfo1->res = new_res1;
2719   return true;
2720 }
2721
2722 /* The number of states that were removed by calling pattern routines.  */
2723 static unsigned int pattern_use_states;
2724
2725 /* The number of states used while defining pattern routines.  */
2726 static unsigned int pattern_def_states;
2727
2728 /* Information used while constructing a use or definition of a pattern
2729    routine.  */
2730 struct create_pattern_info
2731 {
2732   /* The routine itself.  */
2733   pattern_routine *routine;
2734
2735   /* The first unclaimed return value for this particular use or definition.
2736      We walk the substates of uses and definitions in the same order
2737      so each return value always refers to the same position within
2738      the pattern.  */
2739   unsigned int next_result;
2740 };
2741
2742 static void populate_pattern_routine (create_pattern_info *,
2743                                       merge_state_info *, state *,
2744                                       const vec <parameter> &);
2745
2746 /* SINFO matches a pattern for which we've decided to create a C routine.
2747    Return a decision that performs a call to the pattern routine,
2748    but leave the caller to add the transitions to it.  Initialize CPI
2749    for this purpose.  Also create a definition for the pattern routine,
2750    if it doesn't already have one.
2751
2752    PARAMS are the parameters that SINFO passes to its pattern.  */
2753
2754 static decision *
2755 init_pattern_use (create_pattern_info *cpi, merge_state_info *sinfo,
2756                   const vec <parameter> &params)
2757 {
2758   state *s = sinfo->s;
2759   merge_state_result *res = sinfo->res;
2760   merge_pattern_info *pat = res->pattern;
2761   cpi->routine = pat->routine;
2762   if (!cpi->routine)
2763     {
2764       /* We haven't defined the pattern routine yet, so create
2765          a definition now.  */
2766       pattern_routine *routine = new pattern_routine;
2767       pat->routine = routine;
2768       cpi->routine = routine;
2769       routine->s = new state;
2770       routine->insn_p = false;
2771       routine->pnum_clobbers_p = false;
2772
2773       /* Create an "idempotent" mapping of parameter I to parameter I.
2774          Also record the C type of each parameter to the routine.  */
2775       auto_vec <parameter, MAX_PATTERN_PARAMS> def_params;
2776       for (unsigned int i = 0; i < pat->params.length (); ++i)
2777         {
2778           def_params.quick_push (parameter (pat->params[i].type, true, i));
2779           routine->param_types.quick_push (pat->params[i].type);
2780         }
2781
2782       /* Any of the states that match the pattern could be used to
2783          create the routine definition.  We might as well use SINFO
2784          since it's already to hand.  This means that all positions
2785          in the definition will be relative to RES->root.  */
2786       routine->pos = res->root;
2787       cpi->next_result = 0;
2788       populate_pattern_routine (cpi, sinfo, routine->s, def_params);
2789       gcc_assert (cpi->next_result == pat->num_results);
2790
2791       /* Add the routine to the global list, after the subroutines
2792          that it calls.  */
2793       routine->pattern_id = patterns.length ();
2794       patterns.safe_push (routine);
2795     }
2796
2797   /* Create a decision to call the routine, passing PARAMS to it.  */
2798   pattern_use *use = new pattern_use;
2799   use->routine = pat->routine;
2800   use->params.splice (params);
2801   decision *d = new decision (rtx_test::pattern (res->root, use));
2802
2803   /* If the original decision could use an element of operands[] instead
2804      of an rtx variable, try to transfer it to the new decision.  */
2805   if (s->first->test.pos && res->root == s->first->test.pos)
2806     d->test.pos_operand = s->first->test.pos_operand;
2807
2808   cpi->next_result = 0;
2809   return d;
2810 }
2811
2812 /* Make S return the next unclaimed pattern routine result for CPI.  */
2813
2814 static void
2815 add_pattern_acceptance (create_pattern_info *cpi, state *s)
2816 {
2817   acceptance_type acceptance;
2818   acceptance.type = SUBPATTERN;
2819   acceptance.partial_p = false;
2820   acceptance.u.full.code = cpi->next_result;
2821   add_decision (s, rtx_test::accept (acceptance), true, false);
2822   cpi->next_result += 1;
2823 }
2824
2825 /* Initialize new empty state NEWS so that it implements SINFO's pattern
2826    (here referred to as "P").  P may be the top level of a pattern routine
2827    or a subpattern that should be inlined into its parent pattern's routine
2828    (as per same_pattern_p).  The choice of SINFO for a top-level pattern is
2829    arbitrary; it could be any of the states that use P.  The choice for
2830    subpatterns follows the choice for the parent pattern.
2831
2832    PARAMS gives the value of each parameter to P in terms of the parameters
2833    to the top-level pattern.  If P itself is the top level pattern, PARAMS[I]
2834    is always "parameter (TYPE, true, I)".  */
2835
2836 static void
2837 populate_pattern_routine (create_pattern_info *cpi, merge_state_info *sinfo,
2838                           state *news, const vec <parameter> &params)
2839 {
2840   pattern_def_states += 1;
2841
2842   decision *d = sinfo->s->singleton ();
2843   merge_pattern_info *pat = sinfo->res->pattern;
2844   pattern_routine *routine = cpi->routine;
2845
2846   /* Create a copy of D's test for the pattern routine and generalize it
2847      as appropriate.  */
2848   decision *newd = new decision (d->test);
2849   gcc_assert (newd->test.pos_operand >= 0
2850               || !newd->test.pos
2851               || common_position (newd->test.pos,
2852                                   routine->pos) == routine->pos);
2853   if (pat->param_test_p)
2854     {
2855       const parameter &param = params[pat->param_test];
2856       switch (newd->test.kind)
2857         {
2858         case rtx_test::PREDICATE:
2859           newd->test.u.predicate.mode_is_param = param.is_param;
2860           newd->test.u.predicate.mode = param.value;
2861           break;
2862
2863         case rtx_test::SAVED_CONST_INT:
2864           newd->test.u.integer.is_param = param.is_param;
2865           newd->test.u.integer.value = param.value;
2866           break;
2867
2868         default:
2869           gcc_unreachable ();
2870           break;
2871         }
2872     }
2873   if (d->test.kind == rtx_test::C_TEST)
2874     routine->insn_p = true;
2875   else if (d->test.kind == rtx_test::HAVE_NUM_CLOBBERS)
2876     routine->pnum_clobbers_p = true;
2877   news->push_back (newd);
2878
2879   /* Fill in the transitions of NEWD.  */
2880   unsigned int i = 0;
2881   for (transition *trans = d->first; trans; trans = trans->next)
2882     {
2883       /* Create a new state to act as the target of the new transition.  */
2884       state *to_news = new state;
2885       if (merge_pattern_transition *ptrans = pat->transitions[i])
2886         {
2887           /* The pattern hasn't finished matching yet.  Get the target
2888              pattern and the corresponding target state of SINFO.  */
2889           merge_pattern_info *to_pat = ptrans->to;
2890           merge_state_info *to = sinfo->to_states + i;
2891           gcc_assert (to->res->pattern == to_pat);
2892           gcc_assert (ptrans->params.length () == to_pat->params.length ());
2893
2894           /* Express the parameters to TO_PAT in terms of the parameters
2895              to the top-level pattern.  */
2896           auto_vec <parameter, MAX_PATTERN_PARAMS> to_params;
2897           for (unsigned int j = 0; j < ptrans->params.length (); ++j)
2898             {
2899               const parameter &param = ptrans->params[j];
2900               to_params.quick_push (param.is_param
2901                                     ? params[param.value]
2902                                     : param);
2903             }
2904
2905           if (same_pattern_p (pat, to_pat))
2906             /* TO_PAT is part of the current routine, so just recurse.  */
2907             populate_pattern_routine (cpi, to, to_news, to_params);
2908           else
2909             {
2910               /* TO_PAT should be matched by calling a separate routine.  */
2911               create_pattern_info sub_cpi;
2912               decision *subd = init_pattern_use (&sub_cpi, to, to_params);
2913               routine->insn_p |= sub_cpi.routine->insn_p;
2914               routine->pnum_clobbers_p |= sub_cpi.routine->pnum_clobbers_p;
2915
2916               /* Add the pattern routine call to the new target state.  */
2917               to_news->push_back (subd);
2918
2919               /* Add a transition for each successful call result.  */
2920               for (unsigned int j = 0; j < to_pat->num_results; ++j)
2921                 {
2922                   state *res = new state;
2923                   add_pattern_acceptance (cpi, res);
2924                   subd->push_back (new transition (j, res, false));
2925                 }
2926             }
2927         }
2928       else
2929         /* This transition corresponds to a successful match.  */
2930         add_pattern_acceptance (cpi, to_news);
2931
2932       /* Create the transition itself, generalizing as necessary.  */
2933       transition *new_trans = new transition (trans->labels, to_news,
2934                                               trans->optional);
2935       if (pat->param_transition_p)
2936         {
2937           const parameter &param = params[pat->param_transition];
2938           new_trans->is_param = param.is_param;
2939           new_trans->labels[0] = param.value;
2940         }
2941       newd->push_back (new_trans);
2942       i += 1;
2943     }
2944 }
2945
2946 /* USE is a decision that calls a pattern routine and SINFO is part of the
2947    original state tree that the call is supposed to replace.  Add the
2948    transitions for SINFO and its substates to USE.  */
2949
2950 static void
2951 populate_pattern_use (create_pattern_info *cpi, decision *use,
2952                       merge_state_info *sinfo)
2953 {
2954   pattern_use_states += 1;
2955   gcc_assert (!sinfo->merged_p);
2956   sinfo->merged_p = true;
2957   merge_state_result *res = sinfo->res;
2958   merge_pattern_info *pat = res->pattern;
2959   decision *d = sinfo->s->singleton ();
2960   unsigned int i = 0;
2961   for (transition *trans = d->first; trans; trans = trans->next)
2962     {
2963       if (pat->transitions[i])
2964         /* The target state is also part of the pattern.  */
2965         populate_pattern_use (cpi, use, sinfo->to_states + i);
2966       else
2967         {
2968           /* The transition corresponds to a successful return from the
2969              pattern routine.  */
2970           use->push_back (new transition (cpi->next_result, trans->to, false));
2971           cpi->next_result += 1;
2972         }
2973       i += 1;
2974     }
2975 }
2976
2977 /* We have decided to replace SINFO's state with a call to a pattern
2978    routine.  Make the change, creating a definition of the pattern routine
2979    if it doesn't have one already.  */
2980
2981 static void
2982 use_pattern (merge_state_info *sinfo)
2983 {
2984   merge_state_result *res = sinfo->res;
2985   merge_pattern_info *pat = res->pattern;
2986   state *s = sinfo->s;
2987
2988   /* The pattern may have acquired new parameters after it was matched
2989      against SINFO.  Update the parameters that SINFO passes accordingly.  */
2990   update_parameters (res->params, pat->params);
2991
2992   create_pattern_info cpi;
2993   decision *d = init_pattern_use (&cpi, sinfo, res->params);
2994   populate_pattern_use (&cpi, d, sinfo);
2995   s->release ();
2996   s->push_back (d);
2997 }
2998
2999 /* Look through the state trees in STATES for common patterns and
3000    split them into subroutines.  */
3001
3002 static void
3003 split_out_patterns (vec <merge_state_info> &states)
3004 {
3005   unsigned int first_transition = states.length ();
3006   hash_table <test_pattern_hasher> hashtab (128);
3007   /* Stage 1: Create an order in which parent states come before their child
3008      states and in which sibling states are at consecutive locations.
3009      Having consecutive sibling states allows merge_state_info to have
3010      a single to_states pointer.  */
3011   for (unsigned int i = 0; i < states.length (); ++i)
3012     for (decision *d = states[i].s->first; d; d = d->next)
3013       for (transition *trans = d->first; trans; trans = trans->next)
3014         {
3015           states.safe_push (trans->to);
3016           states[i].num_transitions += 1;
3017         }
3018   /* Stage 2: Now that the addresses are stable, set up the to_states
3019      pointers.  Look for states that might be merged and enter them
3020      into the hash table.  */
3021   for (unsigned int i = 0; i < states.length (); ++i)
3022     {
3023       merge_state_info *sinfo = &states[i];
3024       if (sinfo->num_transitions)
3025         {
3026           sinfo->to_states = &states[first_transition];
3027           first_transition += sinfo->num_transitions;
3028         }
3029       /* For simplicity, we only try to merge states that have a single
3030          decision.  This is in any case the best we can do for peephole2,
3031          since whether a peephole2 ACCEPT succeeds or not depends on the
3032          specific peephole2 pattern (which is unique to each ACCEPT
3033          and so couldn't be shared between states).  */
3034       if (decision *d = sinfo->s->singleton ())
3035         /* ACCEPT states are unique, so don't even try to merge them.  */
3036         if (d->test.kind != rtx_test::ACCEPT
3037             && (pattern_have_num_clobbers_p
3038                 || d->test.kind != rtx_test::HAVE_NUM_CLOBBERS)
3039             && (pattern_c_test_p
3040                 || d->test.kind != rtx_test::C_TEST))
3041           {
3042             merge_state_info **slot = hashtab.find_slot (sinfo, INSERT);
3043             sinfo->prev_same_test = *slot;
3044             *slot = sinfo;
3045           }
3046     }
3047   /* Stage 3: Walk backwards through the list of states and try to merge
3048      them.  This is a greedy, bottom-up match; parent nodes can only start
3049      a new leaf pattern if they fail to match when combined with all child
3050      nodes that have matching patterns.
3051
3052      For each state we keep a list of potential matches, with each
3053      potential match being larger (and deeper) than the next match in
3054      the list.  The final element in the list is a leaf pattern that
3055      matches just a single state.
3056
3057      Each candidate pattern created in this loop is unique -- it won't
3058      have been seen by an earlier iteration.  We try to match each pattern
3059      with every state that appears earlier in STATES.
3060
3061      Because the patterns created in the loop are unique, any state
3062      that already has a match must have a final potential match that
3063      is different from any new leaf pattern.  Therefore, when matching
3064      leaf patterns, we need only consider states whose list of matches
3065      is empty.
3066
3067      The non-leaf patterns that we try are as deep as possible
3068      and are an extension of the state's previous best candidate match (PB).
3069      We need only consider states whose current potential match is also PB;
3070      any states that don't match as much as PB cannnot match the new pattern,
3071      while any states that already match more than PB must be different from
3072      the new pattern.  */
3073   for (unsigned int i2 = states.length (); i2-- > 0; )
3074     {
3075       merge_state_info *sinfo2 = &states[i2];
3076
3077       /* Enforce the bottom-upness of the match: remove matches with later
3078          states if SINFO2's child states ended up finding a better match.  */
3079       prune_invalid_results (sinfo2);
3080
3081       /* Do nothing if the state doesn't match a later one and if there are
3082          no earlier states it could match.  */
3083       if (!sinfo2->res && !sinfo2->prev_same_test)
3084         continue;
3085
3086       merge_state_result *res2 = sinfo2->res;
3087       decision *d2 = sinfo2->s->singleton ();
3088       position *root2 = (d2->test.pos_operand < 0 ? d2->test.pos : 0);
3089       unsigned int num_transitions = sinfo2->num_transitions;
3090
3091       /* If RES2 is null then SINFO2's test in isolation has not been seen
3092          before.  First try matching that on its own.  */
3093       if (!res2)
3094         {
3095           merge_pattern_info *new_pat
3096             = new merge_pattern_info (num_transitions);
3097           merge_state_result *new_res2
3098             = new merge_state_result (new_pat, root2, res2);
3099           sinfo2->res = new_res2;
3100
3101           new_pat->num_statements = !d2->test.single_outcome_p ();
3102           new_pat->num_results = num_transitions;
3103           bool matched_p = false;
3104           /* Look for states that don't currently match anything but
3105              can be made to match SINFO2 on its own.  */
3106           for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3107                sinfo1 = sinfo1->prev_same_test)
3108             if (!sinfo1->res && merge_patterns (sinfo1, sinfo2))
3109               matched_p = true;
3110           if (!matched_p)
3111             {
3112               /* No other states match.  */
3113               sinfo2->res = res2;
3114               delete new_pat;
3115               delete new_res2;
3116               continue;
3117             }
3118           else
3119             res2 = new_res2;
3120         }
3121
3122       /* Keep the existing pattern if it's as good as anything we'd
3123          create for SINFO2.  */
3124       if (complete_result_p (res2->pattern, sinfo2))
3125         {
3126           res2->pattern->num_users += 1;
3127           continue;
3128         }
3129
3130       /* Create a new pattern for SINFO2.  */
3131       merge_pattern_info *new_pat = new merge_pattern_info (num_transitions);
3132       merge_state_result *new_res2
3133         = new merge_state_result (new_pat, root2, res2);
3134       sinfo2->res = new_res2;
3135
3136       /* Fill in details about the pattern.  */
3137       new_pat->num_statements = !d2->test.single_outcome_p ();
3138       new_pat->num_results = 0;
3139       for (unsigned int j = 0; j < num_transitions; ++j)
3140         if (merge_state_result *to_res = sinfo2->to_states[j].res)
3141           {
3142             /* Count the target state as part of this pattern.
3143                First update the root position so that it can reach
3144                the target state's root.  */
3145             if (to_res->root)
3146               {
3147                 if (new_res2->root)
3148                   new_res2->root = common_position (new_res2->root,
3149                                                     to_res->root);
3150                 else
3151                   new_res2->root = to_res->root;
3152               }
3153             merge_pattern_info *to_pat = to_res->pattern;
3154             merge_pattern_transition *ptrans
3155               = new merge_pattern_transition (to_pat);
3156
3157             /* TO_PAT may have acquired more parameters when matching
3158                states earlier in STATES than TO_RES's, but the list is
3159                now final.  Make sure that TO_RES is up to date.  */
3160             update_parameters (to_res->params, to_pat->params);
3161
3162             /* Start out by assuming that every user of NEW_PAT will
3163                want to pass the same (constant) parameters as TO_RES.  */
3164             update_parameters (ptrans->params, to_res->params);
3165
3166             new_pat->transitions[j] = ptrans;
3167             new_pat->num_statements += to_pat->num_statements;
3168             new_pat->num_results += to_pat->num_results;
3169           }
3170         else
3171           /* The target state doesn't match anything and so is not part
3172              of the pattern.  */
3173           new_pat->num_results += 1;
3174
3175       /* See if any earlier states that match RES2's pattern also match
3176          NEW_PAT.  */
3177       bool matched_p = false;
3178       for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3179            sinfo1 = sinfo1->prev_same_test)
3180         {
3181           prune_invalid_results (sinfo1);
3182           if (sinfo1->res
3183               && sinfo1->res->pattern == res2->pattern
3184               && merge_patterns (sinfo1, sinfo2))
3185             matched_p = true;
3186         }
3187       if (!matched_p)
3188         {
3189           /* Nothing else matches NEW_PAT, so go back to the previous
3190              pattern (possibly just a single-state one).  */
3191           sinfo2->res = res2;
3192           delete new_pat;
3193           delete new_res2;
3194         }
3195       /* Assume that SINFO2 will use RES.  At this point we don't know
3196          whether earlier states that match the same pattern will use
3197          that match or a different one.  */
3198       sinfo2->res->pattern->num_users += 1;
3199     }
3200   /* Step 4: Finalize the choice of pattern for each state, ignoring
3201      patterns that were only used once.  Update each pattern's size
3202      so that it doesn't include subpatterns that are going to be split
3203      out into subroutines.  */
3204   for (unsigned int i = 0; i < states.length (); ++i)
3205     {
3206       merge_state_info *sinfo = &states[i];
3207       merge_state_result *res = sinfo->res;
3208       /* Wind past patterns that are only used by SINFO.  */
3209       while (res && res->pattern->num_users == 1)
3210         {
3211           res = res->prev;
3212           sinfo->res = res;
3213           if (res)
3214             res->pattern->num_users += 1;
3215         }
3216       if (!res)
3217         continue;
3218
3219       /* We have a shared pattern and are now committed to the match.  */
3220       merge_pattern_info *pat = res->pattern;
3221       gcc_assert (valid_result_p (pat, sinfo));
3222
3223       if (!pat->complete_p)
3224         {
3225           /* Look for subpatterns that are going to be split out and remove
3226              them from the number of statements.  */
3227           for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
3228             if (merge_pattern_transition *ptrans = pat->transitions[j])
3229               {
3230                 merge_pattern_info *to_pat = ptrans->to;
3231                 if (!same_pattern_p (pat, to_pat))
3232                   pat->num_statements -= to_pat->num_statements;
3233               }
3234           pat->complete_p = true;
3235         }
3236     }
3237   /* Step 5: Split out the patterns.  */
3238   for (unsigned int i = 0; i < states.length (); ++i)
3239     {
3240       merge_state_info *sinfo = &states[i];
3241       merge_state_result *res = sinfo->res;
3242       if (!sinfo->merged_p && res && useful_pattern_p (res->pattern))
3243         use_pattern (sinfo);
3244     }
3245   fprintf (stderr, "Shared %d out of %d states by creating %d new states,"
3246            " saving %d\n",
3247            pattern_use_states, states.length (), pattern_def_states,
3248            pattern_use_states - pattern_def_states);
3249 }
3250
3251 /* Information about a state tree that we're considering splitting into a
3252    subroutine.  */
3253 struct state_size
3254 {
3255   /* The number of pseudo-statements in the state tree.  */
3256   unsigned int num_statements;
3257
3258   /* The approximate number of nested "if" and "switch" statements that
3259      would be required if control could fall through to a later state.  */
3260   unsigned int depth;
3261 };
3262
3263 /* Pairs a transition with information about its target state.  */
3264 typedef std::pair <transition *, state_size> subroutine_candidate;
3265
3266 /* Sort two subroutine_candidates so that the one with the largest
3267    number of statements comes last.  */
3268
3269 static int
3270 subroutine_candidate_cmp (const void *a, const void *b)
3271 {
3272   return int (((const subroutine_candidate *) a)->second.num_statements
3273               - ((const subroutine_candidate *) b)->second.num_statements);
3274 }
3275
3276 /* Turn S into a subroutine of type TYPE and add it to PROCS.  Return a new
3277    state that performs a subroutine call to S.  */
3278
3279 static state *
3280 create_subroutine (routine_type type, state *s, vec <state *> &procs)
3281 {
3282   procs.safe_push (s);
3283   acceptance_type acceptance;
3284   acceptance.type = type;
3285   acceptance.partial_p = true;
3286   acceptance.u.subroutine_id = procs.length ();
3287   state *news = new state;
3288   add_decision (news, rtx_test::accept (acceptance), true, false);
3289   return news;
3290 }
3291
3292 /* Walk state tree S, of type TYPE, and look for subtrees that would be
3293    better split into subroutines.  Accumulate all such subroutines in PROCS.
3294    Return the size of the new state tree (excluding subroutines).  */
3295
3296 static state_size
3297 find_subroutines (routine_type type, state *s, vec <state *> &procs)
3298 {
3299   auto_vec <subroutine_candidate, 16> candidates;
3300   state_size size;
3301   size.num_statements = 0;
3302   size.depth = 0;
3303   for (decision *d = s->first; d; d = d->next)
3304     {
3305       if (!d->test.single_outcome_p ())
3306         size.num_statements += 1;
3307       for (transition *trans = d->first; trans; trans = trans->next)
3308         {
3309           /* Keep chains of simple decisions together if we know that no
3310              change of position is required.  We'll output this chain as a
3311              single "if" statement, so it counts as a single nesting level.  */
3312           if (d->test.pos && d->if_statement_p ())
3313             for (;;)
3314               {
3315                 decision *newd = trans->to->singleton ();
3316                 if (!newd
3317                     || (newd->test.pos
3318                         && newd->test.pos_operand < 0
3319                         && newd->test.pos != d->test.pos)
3320                     || !newd->if_statement_p ())
3321                   break;
3322                 if (!newd->test.single_outcome_p ())
3323                   size.num_statements += 1;
3324                 trans = newd->singleton ();
3325                 if (newd->test.kind == rtx_test::SET_OP
3326                     || newd->test.kind == rtx_test::ACCEPT)
3327                   break;
3328               }
3329           /* The target of TRANS is a subroutine candidate.  First recurse
3330              on it to see how big it is after subroutines have been
3331              split out.  */
3332           state_size to_size = find_subroutines (type, trans->to, procs);
3333           if (d->next && to_size.depth > MAX_DEPTH)
3334             /* Keeping the target state in the same routine would lead
3335                to an excessive nesting of "if" and "switch" statements.
3336                Split it out into a subroutine so that it can use
3337                inverted tests that return early on failure.  */
3338             trans->to = create_subroutine (type, trans->to, procs);
3339           else
3340             {
3341               size.num_statements += to_size.num_statements;
3342               if (to_size.num_statements < MIN_NUM_STATEMENTS)
3343                 /* The target state is too small to be worth splitting.
3344                    Keep it in the same routine as S.  */
3345                 size.depth = MAX (size.depth, to_size.depth);
3346               else
3347                 /* Assume for now that we'll keep the target state in the
3348                    same routine as S, but record it as a subroutine candidate
3349                    if S grows too big.  */
3350                 candidates.safe_push (subroutine_candidate (trans, to_size));
3351             }
3352         }
3353     }
3354   if (size.num_statements > MAX_NUM_STATEMENTS)
3355     {
3356       /* S is too big.  Sort the subroutine candidates so that bigger ones
3357          are nearer the end.  */
3358       candidates.qsort (subroutine_candidate_cmp);
3359       while (!candidates.is_empty ()
3360              && size.num_statements > MAX_NUM_STATEMENTS)
3361         {
3362           /* Peel off a candidate and force it into a subroutine.  */
3363           subroutine_candidate cand = candidates.pop ();
3364           size.num_statements -= cand.second.num_statements;
3365           cand.first->to = create_subroutine (type, cand.first->to, procs);
3366         }
3367     }
3368   /* Update the depth for subroutine candidates that we decided not to
3369      split out.  */
3370   for (unsigned int i = 0; i < candidates.length (); ++i)
3371     size.depth = MAX (size.depth, candidates[i].second.depth);
3372   size.depth += 1;
3373   return size;
3374 }
3375
3376 /* Return true if, for all X, PRED (X, MODE) implies that X has mode MODE.  */
3377
3378 static bool
3379 safe_predicate_mode (const struct pred_data *pred, machine_mode mode)
3380 {
3381   /* Scalar integer constants have VOIDmode.  */
3382   if (GET_MODE_CLASS (mode) == MODE_INT
3383       && (pred->codes[CONST_INT]
3384           || pred->codes[CONST_DOUBLE]
3385           || pred->codes[CONST_WIDE_INT]
3386           || pred->codes[LABEL_REF]))
3387     return false;
3388
3389   return !pred->special && mode != VOIDmode;
3390 }
3391
3392 /* Fill CODES with the set of codes that could be matched by PRED.  */
3393
3394 static void
3395 get_predicate_codes (const struct pred_data *pred, int_set *codes)
3396 {
3397   for (int i = 0; i < NUM_TRUE_RTX_CODE; ++i)
3398     if (!pred || pred->codes[i])
3399       codes->safe_push (i);
3400 }
3401
3402 /* Return true if the first path through D1 tests the same thing as D2.  */
3403
3404 static bool
3405 has_same_test_p (decision *d1, decision *d2)
3406 {
3407   do
3408     {
3409       if (d1->test == d2->test)
3410         return true;
3411       d1 = d1->first->to->first;
3412     }
3413   while (d1);
3414   return false;
3415 }
3416
3417 /* Return true if D1 and D2 cannot match the same rtx.  All states reachable
3418    from D2 have single decisions and all those decisions have single
3419    transitions.  */
3420
3421 static bool
3422 mutually_exclusive_p (decision *d1, decision *d2)
3423 {
3424   /* If one path through D1 fails to test the same thing as D2, assume
3425      that D2's test could be true for D1 and look for a later, more useful,
3426      test.  This isn't as expensive as it looks in practice.  */
3427   while (!has_same_test_p (d1, d2))
3428     {
3429       d2 = d2->singleton ()->to->singleton ();
3430       if (!d2)
3431         return false;
3432     }
3433   if (d1->test == d2->test)
3434     {
3435       /* Look for any transitions from D1 that have the same labels as
3436          the transition from D2.  */
3437       transition *trans2 = d2->singleton ();
3438       for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3439         {
3440           int_set::iterator i1 = trans1->labels.begin ();
3441           int_set::iterator end1 = trans1->labels.end ();
3442           int_set::iterator i2 = trans2->labels.begin ();
3443           int_set::iterator end2 = trans2->labels.end ();
3444           while (i1 != end1 && i2 != end2)
3445             if (*i1 < *i2)
3446               ++i1;
3447             else if (*i2 < *i1)
3448               ++i2;
3449             else
3450               {
3451                 /* TRANS1 has some labels in common with TRANS2.  Assume
3452                    that D1 and D2 could match the same rtx if the target
3453                    of TRANS1 could match the same rtx as D2.  */
3454                 for (decision *subd1 = trans1->to->first;
3455                      subd1; subd1 = subd1->next)
3456                   if (!mutually_exclusive_p (subd1, d2))
3457                     return false;
3458                 break;
3459               }
3460         }
3461       return true;
3462     }
3463   for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3464     for (decision *subd1 = trans1->to->first; subd1; subd1 = subd1->next)
3465       if (!mutually_exclusive_p (subd1, d2))
3466         return false;
3467   return true;
3468 }
3469
3470 /* Try to merge S2's decision into D1, given that they have the same test.
3471    Fail only if EXCLUDE is nonnull and the new transition would have the
3472    same labels as *EXCLUDE.  When returning true, set *NEXT_S1, *NEXT_S2
3473    and *NEXT_EXCLUDE as for merge_into_state_1, or set *NEXT_S2 to null
3474    if the merge is complete.  */
3475
3476 static bool
3477 merge_into_decision (decision *d1, state *s2, const int_set *exclude,
3478                      state **next_s1, state **next_s2,
3479                      const int_set **next_exclude)
3480 {
3481   decision *d2 = s2->singleton ();
3482   transition *trans2 = d2->singleton ();
3483
3484   /* Get a list of the transitions that intersect TRANS2.  */
3485   auto_vec <transition *, 32> intersecting;
3486   for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3487     {
3488       int_set::iterator i1 = trans1->labels.begin ();
3489       int_set::iterator end1 = trans1->labels.end ();
3490       int_set::iterator i2 = trans2->labels.begin ();
3491       int_set::iterator end2 = trans2->labels.end ();
3492       bool trans1_is_subset = true;
3493       bool trans2_is_subset = true;
3494       bool intersect_p = false;
3495       while (i1 != end1 && i2 != end2)
3496         if (*i1 < *i2)
3497           {
3498             trans1_is_subset = false;
3499             ++i1;
3500           }
3501         else if (*i2 < *i1)
3502           {
3503             trans2_is_subset = false;
3504             ++i2;
3505           }
3506         else
3507           {
3508             intersect_p = true;
3509             ++i1;
3510             ++i2;
3511           }
3512       if (i1 != end1)
3513         trans1_is_subset = false;
3514       if (i2 != end2)
3515         trans2_is_subset = false;
3516       if (trans1_is_subset && trans2_is_subset)
3517         {
3518           /* There's already a transition that matches exactly.
3519              Merge the target states.  */
3520           trans1->optional &= trans2->optional;
3521           *next_s1 = trans1->to;
3522           *next_s2 = trans2->to;
3523           *next_exclude = 0;
3524           return true;
3525         }
3526       if (trans2_is_subset)
3527         {
3528           /* TRANS1 has all the labels that TRANS2 needs.  Merge S2 into
3529              the target of TRANS1, but (to avoid infinite recursion)
3530              make sure that we don't end up creating another transition
3531              like TRANS1.  */
3532           *next_s1 = trans1->to;
3533           *next_s2 = s2;
3534           *next_exclude = &trans1->labels;
3535           return true;
3536         }
3537       if (intersect_p)
3538         intersecting.safe_push (trans1);
3539     }
3540
3541   if (intersecting.is_empty ())
3542     {
3543       /* No existing labels intersect the new ones.  We can just add
3544          TRANS2 itself.  */
3545       d1->push_back (d2->release ());
3546       *next_s1 = 0;
3547       *next_s2 = 0;
3548       *next_exclude = 0;
3549       return true;
3550     }
3551
3552   /* Take the union of the labels in INTERSECTING and TRANS2.  Store the
3553      result in COMBINED and use NEXT as a temporary.  */
3554   int_set tmp1 = trans2->labels, tmp2;
3555   int_set *combined = &tmp1, *next = &tmp2;
3556   for (unsigned int i = 0; i < intersecting.length (); ++i)
3557     {
3558       transition *trans1 = intersecting[i];
3559       next->truncate (0);
3560       next->safe_grow (trans1->labels.length () + combined->length ());
3561       int_set::iterator end
3562         = std::set_union (trans1->labels.begin (), trans1->labels.end (),
3563                           combined->begin (), combined->end (),
3564                           next->begin ());
3565       next->truncate (end - next->begin ());
3566       std::swap (next, combined);
3567     }
3568
3569   /* Stop now if we've been told not to create a transition with these
3570      labels.  */
3571   if (exclude && *combined == *exclude)
3572     return false;
3573
3574   /* Get the transition that should carry the new labels.  */
3575   transition *new_trans = intersecting[0];
3576   if (intersecting.length () == 1)
3577     {
3578       /* We're merging with one existing transition whose labels are a
3579          subset of those required.  If both transitions are optional,
3580          we can just expand the set of labels so that it's suitable
3581          for both transitions.  It isn't worth preserving the original
3582          transitions since we know that they can't be merged; we would
3583          need to backtrack to S2 if TRANS1->to fails.  In contrast,
3584          we might be able to merge the targets of the transitions
3585          without any backtracking.
3586
3587          If instead the existing transition is not optional, ensure that
3588          all target decisions are suitably protected.  Some decisions
3589          might already have a more specific requirement than NEW_TRANS,
3590          in which case there's no point testing NEW_TRANS as well.  E.g. this
3591          would have happened if a test for an (eq ...) rtx had been
3592          added to a decision that tested whether the code is suitable
3593          for comparison_operator.  The original comparison_operator
3594          transition would have been non-optional and the (eq ...) test
3595          would be performed by a second decision in the target of that
3596          transition.
3597
3598          The remaining case -- keeping the original optional transition
3599          when adding a non-optional TRANS2 -- is a wash.  Preserving
3600          the optional transition only helps if we later merge another
3601          state S3 that is mutually exclusive with S2 and whose labels
3602          belong to *COMBINED - TRANS1->labels.  We can then test the
3603          original NEW_TRANS and S3 in the same decision.  We keep the
3604          optional transition around for that case, but it occurs very
3605          rarely.  */
3606       gcc_assert (new_trans->labels != *combined);
3607       if (!new_trans->optional || !trans2->optional)
3608         {
3609           decision *start = 0;
3610           for (decision *end = new_trans->to->first; end; end = end->next)
3611             {
3612               if (!start && end->test != d1->test)
3613                 /* END belongs to a range of decisions that need to be
3614                    protected by NEW_TRANS.  */
3615                 start = end;
3616               if (start && (!end->next || end->next->test == d1->test))
3617                 {
3618                   /* Protect [START, END] with NEW_TRANS.  The decisions
3619                      move to NEW_S and NEW_D becomes part of NEW_TRANS->to.  */
3620                   state *new_s = new state;
3621                   decision *new_d = new decision (d1->test);
3622                   new_d->push_back (new transition (new_trans->labels, new_s,
3623                                                     new_trans->optional));
3624                   state::range r (start, end);
3625                   new_trans->to->replace (r, new_d);
3626                   new_s->push_back (r);
3627
3628                   /* Continue with an empty range.  */
3629                   start = 0;
3630
3631                   /* Continue from the decision after NEW_D.  */
3632                   end = new_d;
3633                 }
3634             }
3635         }
3636       new_trans->optional = true;
3637       new_trans->labels = *combined;
3638     }
3639   else
3640     {
3641       /* We're merging more than one existing transition together.
3642          Those transitions are successfully dividing the matching space
3643          and so we want to preserve them, even if they're optional.
3644
3645          Create a new transition with the union set of labels and make
3646          it go to a state that has the original transitions.  */
3647       decision *new_d = new decision (d1->test);
3648       for (unsigned int i = 0; i < intersecting.length (); ++i)
3649         new_d->push_back (d1->remove (intersecting[i]));
3650
3651       state *new_s = new state;
3652       new_s->push_back (new_d);
3653
3654       new_trans = new transition (*combined, new_s, true);
3655       d1->push_back (new_trans);
3656     }
3657
3658   /* We now have an optional transition with labels *COMBINED.  Decide
3659      whether we can use it as TRANS2 or whether we need to merge S2
3660      into the target of NEW_TRANS.  */
3661   gcc_assert (new_trans->optional);
3662   if (new_trans->labels == trans2->labels)
3663     {
3664       /* NEW_TRANS matches TRANS2.  Just merge the target states.  */
3665       new_trans->optional = trans2->optional;
3666       *next_s1 = new_trans->to;
3667       *next_s2 = trans2->to;
3668       *next_exclude = 0;
3669     }
3670   else
3671     {
3672       /* Try to merge TRANS2 into the target of the overlapping transition,
3673          but (to prevent infinite recursion or excessive redundancy) without
3674          creating another transition of the same type.  */
3675       *next_s1 = new_trans->to;
3676       *next_s2 = s2;
3677       *next_exclude = &new_trans->labels;
3678     }
3679   return true;
3680 }
3681
3682 /* Make progress in merging S2 into S1, given that each state in S2
3683    has a single decision.  If EXCLUDE is nonnull, avoid creating a new
3684    transition with the same test as S2's decision and with the labels
3685    in *EXCLUDE.
3686
3687    Return true if there is still work to do.  When returning true,
3688    set *NEXT_S1, *NEXT_S2 and *NEXT_EXCLUDE to the values that
3689    S1, S2 and EXCLUDE should have next time round.
3690
3691    If S1 and S2 both match a particular rtx, give priority to S1.  */
3692
3693 static bool
3694 merge_into_state_1 (state *s1, state *s2, const int_set *exclude,
3695                     state **next_s1, state **next_s2,
3696                     const int_set **next_exclude)
3697 {
3698   decision *d2 = s2->singleton ();
3699   if (decision *d1 = s1->last)
3700     {
3701       if (d1->test.terminal_p ())
3702         /* D1 is an unconditional return, so S2 can never match.  This can
3703            sometimes be a bug in the .md description, but might also happen
3704            if genconditions forces some conditions to true for certain
3705            configurations.  */
3706         return false;
3707
3708       /* Go backwards through the decisions in S1, stopping once we find one
3709          that could match the same thing as S2.  */
3710       while (d1->prev && mutually_exclusive_p (d1, d2))
3711         d1 = d1->prev;
3712
3713       /* Search forwards from that point, merging D2 into the first
3714          decision we can.  */
3715       for (; d1; d1 = d1->next)
3716         {
3717           /* If S2 performs some optional tests before testing the same thing
3718              as D1, those tests do not help to distinguish D1 and S2, so it's
3719              better to drop them.  Search through such optional decisions
3720              until we find something that tests the same thing as D1.  */
3721           state *sub_s2 = s2;
3722           for (;;)
3723             {
3724               decision *sub_d2 = sub_s2->singleton ();
3725               if (d1->test == sub_d2->test)
3726                 {
3727                   /* Only apply EXCLUDE if we're testing the same thing
3728                      as D2.  */
3729                   const int_set *sub_exclude = (d2 == sub_d2 ? exclude : 0);
3730
3731                   /* Try to merge SUB_S2 into D1.  This can only fail if
3732                      it would involve creating a new transition with
3733                      labels SUB_EXCLUDE.  */
3734                   if (merge_into_decision (d1, sub_s2, sub_exclude,
3735                                            next_s1, next_s2, next_exclude))
3736                     return *next_s2 != 0;
3737
3738                   /* Can't merge with D1; try a later decision.  */
3739                   break;
3740                 }
3741               transition *sub_trans2 = sub_d2->singleton ();
3742               if (!sub_trans2->optional)
3743                 /* Can't merge with D1; try a later decision.  */
3744                 break;
3745               sub_s2 = sub_trans2->to;
3746             }
3747         }
3748     }
3749
3750   /* We can't merge D2 with any existing decision.  Just add it to the end.  */
3751   s1->push_back (s2->release ());
3752   return false;
3753 }
3754
3755 /* Merge S2 into S1.  If they both match a particular rtx, give
3756    priority to S1.  Each state in S2 has a single decision.  */
3757
3758 static void
3759 merge_into_state (state *s1, state *s2)
3760 {
3761   const int_set *exclude = 0;
3762   while (s2 && merge_into_state_1 (s1, s2, exclude, &s1, &s2, &exclude))
3763     continue;
3764 }
3765
3766 /* Pairs a pattern that needs to be matched with the rtx position at
3767    which the pattern should occur.  */
3768 struct pattern_pos {
3769   pattern_pos () {}
3770   pattern_pos (rtx, position *);
3771
3772   rtx pattern;
3773   position *pos;
3774 };
3775
3776 pattern_pos::pattern_pos (rtx pattern_in, position *pos_in)
3777   : pattern (pattern_in), pos (pos_in)
3778 {}
3779
3780 /* Compare entries according to their depth-first order.  There shouldn't
3781    be two entries at the same position.  */
3782
3783 bool
3784 operator < (const pattern_pos &e1, const pattern_pos &e2)
3785 {
3786   int diff = compare_positions (e1.pos, e2.pos);
3787   gcc_assert (diff != 0 || e1.pattern == e2.pattern);
3788   return diff < 0;
3789 }
3790
3791 /* Return the name of the predicate matched by MATCH_RTX.  */
3792
3793 static const char *
3794 predicate_name (rtx match_rtx)
3795 {
3796   if (GET_CODE (match_rtx) == MATCH_SCRATCH)
3797     return "scratch_operand";
3798   else
3799     return XSTR (match_rtx, 1);
3800 }
3801
3802 /* Add new decisions to S that check whether the rtx at position POS
3803    matches PATTERN.  Return the state that is reached in that case.
3804    TOP_PATTERN is the overall pattern, as passed to match_pattern_1.  */
3805
3806 static state *
3807 match_pattern_2 (state *s, md_rtx_info *info, position *pos, rtx pattern)
3808 {
3809   auto_vec <pattern_pos, 32> worklist;
3810   auto_vec <pattern_pos, 32> pred_and_mode_tests;
3811   auto_vec <pattern_pos, 32> dup_tests;
3812
3813   worklist.safe_push (pattern_pos (pattern, pos));
3814   while (!worklist.is_empty ())
3815     {
3816       pattern_pos next = worklist.pop ();
3817       pattern = next.pattern;
3818       pos = next.pos;
3819       unsigned int reverse_s = worklist.length ();
3820
3821       enum rtx_code code = GET_CODE (pattern);
3822       switch (code)
3823         {
3824         case MATCH_OP_DUP:
3825         case MATCH_DUP:
3826         case MATCH_PAR_DUP:
3827           /* Add a test that the rtx matches the earlier one, but only
3828              after the structure and predicates have been checked.  */
3829           dup_tests.safe_push (pattern_pos (pattern, pos));
3830
3831           /* Use the same code check as the original operand.  */
3832           pattern = find_operand (info->def, XINT (pattern, 0), NULL_RTX);
3833           /* Fall through.  */
3834
3835         case MATCH_PARALLEL:
3836         case MATCH_OPERAND:
3837         case MATCH_SCRATCH:
3838         case MATCH_OPERATOR:
3839           {
3840             const char *pred_name = predicate_name (pattern);
3841             const struct pred_data *pred = 0;
3842             if (pred_name[0] != 0)
3843               {
3844                 pred = lookup_predicate (pred_name);
3845                 /* Only report errors once per rtx.  */
3846                 if (code == GET_CODE (pattern))
3847                   {
3848                     if (!pred)
3849                       error_at (info->loc, "unknown predicate '%s' used in %s",
3850                                 pred_name, GET_RTX_NAME (code));
3851                     else if (code == MATCH_PARALLEL
3852                              && pred->singleton != PARALLEL)
3853                       error_at (info->loc, "predicate '%s' used in"
3854                                 " match_parallel does not allow only PARALLEL",
3855                                 pred->name);
3856                   }
3857               }
3858
3859             if (code == MATCH_PARALLEL || code == MATCH_PAR_DUP)
3860               {
3861                 /* Check that we have a parallel with enough elements.  */
3862                 s = add_decision (s, rtx_test::code (pos), PARALLEL, false);
3863                 int min_len = XVECLEN (pattern, 2);
3864                 s = add_decision (s, rtx_test::veclen_ge (pos, min_len),
3865                                   true, false);
3866               }
3867             else
3868               {
3869                 /* Check that the rtx has one of codes accepted by the
3870                    predicate.  This is necessary when matching suboperands
3871                    of a MATCH_OPERATOR or MATCH_OP_DUP, since we can't
3872                    call XEXP (X, N) without checking that X has at least
3873                    N+1 operands.  */
3874                 int_set codes;
3875                 get_predicate_codes (pred, &codes);
3876                 bool need_codes = (pred
3877                                    && (code == MATCH_OPERATOR
3878                                        || code == MATCH_OP_DUP));
3879                 s = add_decision (s, rtx_test::code (pos), codes, !need_codes);
3880               }
3881
3882             /* Postpone the predicate check until we've checked the rest
3883                of the rtx structure.  */
3884             if (code == GET_CODE (pattern))
3885               pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3886
3887             /* If we need to match suboperands, add them to the worklist.  */
3888             if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
3889               {
3890                 position **subpos_ptr;
3891                 enum position_type pos_type;
3892                 int i;
3893                 if (code == MATCH_OPERATOR || code == MATCH_OP_DUP)
3894                   {
3895                     pos_type = POS_XEXP;
3896                     subpos_ptr = &pos->xexps;
3897                     i = (code == MATCH_OPERATOR ? 2 : 1);
3898                   }
3899                 else
3900                   {
3901                     pos_type = POS_XVECEXP0;
3902                     subpos_ptr = &pos->xvecexp0s;
3903                     i = 2;
3904                   }
3905                 for (int j = 0; j < XVECLEN (pattern, i); ++j)
3906                   {
3907                     position *subpos = next_position (subpos_ptr, pos,
3908                                                       pos_type, j);
3909                     worklist.safe_push (pattern_pos (XVECEXP (pattern, i, j),
3910                                                subpos));
3911                     subpos_ptr = &subpos->next;
3912                   }
3913               }
3914             break;
3915           }
3916
3917         default:
3918           {
3919             /* Check that the rtx has the right code.  */
3920             s = add_decision (s, rtx_test::code (pos), code, false);
3921
3922             /* Queue a test for the mode if one is specified.  */
3923             if (GET_MODE (pattern) != VOIDmode)
3924               pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3925
3926             /* Push subrtxes onto the worklist.  Match nonrtx operands now.  */
3927             const char *fmt = GET_RTX_FORMAT (code);
3928             position **subpos_ptr = &pos->xexps;
3929             for (size_t i = 0; fmt[i]; ++i)
3930               {
3931                 position *subpos = next_position (subpos_ptr, pos,
3932                                                   POS_XEXP, i);
3933                 switch (fmt[i])
3934                   {
3935                   case 'e': case 'u':
3936                     worklist.safe_push (pattern_pos (XEXP (pattern, i),
3937                                                      subpos));
3938                     break;
3939
3940                   case 'E':
3941                     {
3942                       /* Make sure the vector has the right number of
3943                          elements.  */
3944                       int length = XVECLEN (pattern, i);
3945                       s = add_decision (s, rtx_test::veclen (pos),
3946                                         length, false);
3947
3948                       position **subpos2_ptr = &pos->xvecexp0s;
3949                       for (int j = 0; j < length; j++)
3950                         {
3951                           position *subpos2 = next_position (subpos2_ptr, pos,
3952                                                              POS_XVECEXP0, j);
3953                           rtx x = XVECEXP (pattern, i, j);
3954                           worklist.safe_push (pattern_pos (x, subpos2));
3955                           subpos2_ptr = &subpos2->next;
3956                         }
3957                       break;
3958                     }
3959
3960                   case 'i':
3961                     /* Make sure that XINT (X, I) has the right value.  */
3962                     s = add_decision (s, rtx_test::int_field (pos, i),
3963                                       XINT (pattern, i), false);
3964                     break;
3965
3966                   case 'r':
3967                     /* Make sure that REGNO (X) has the right value.  */
3968                     gcc_assert (i == 0);
3969                     s = add_decision (s, rtx_test::regno_field (pos),
3970                                       REGNO (pattern), false);
3971                     break;
3972
3973                   case 'w':
3974                     /* Make sure that XWINT (X, I) has the right value.  */
3975                     s = add_decision (s, rtx_test::wide_int_field (pos, i),
3976                                       XWINT (pattern, 0), false);
3977                     break;
3978
3979                   case '0':
3980                     break;
3981
3982                   default:
3983                     gcc_unreachable ();
3984                   }
3985                 subpos_ptr = &subpos->next;
3986               }
3987           }
3988           break;
3989         }
3990       /* Operands are pushed onto the worklist so that later indices are
3991          nearer the top.  That's what we want for SETs, since a SET_SRC
3992          is a better discriminator than a SET_DEST.  In other cases it's
3993          usually better to match earlier indices first.  This is especially
3994          true of PARALLELs, where the first element tends to be the most
3995          individual.  It's also true for commutative operators, where the
3996          canonicalization rules say that the more complex operand should
3997          come first.  */
3998       if (code != SET && worklist.length () > reverse_s)
3999         std::reverse (&worklist[0] + reverse_s,
4000                       &worklist[0] + worklist.length ());
4001     }
4002
4003   /* Sort the predicate and mode tests so that they're in depth-first order.
4004      The main goal of this is to put SET_SRC match_operands after SET_DEST
4005      match_operands and after mode checks for the enclosing SET_SRC operators
4006      (such as the mode of a PLUS in an addition instruction).  The latter
4007      two types of test can determine the mode exactly, whereas a SET_SRC
4008      match_operand often has to cope with the possibility of the operand
4009      being a modeless constant integer.  E.g. something that matches
4010      register_operand (x, SImode) never matches register_operand (x, DImode),
4011      but a const_int that matches immediate_operand (x, SImode) also matches
4012      immediate_operand (x, DImode).  The register_operand cases can therefore
4013      be distinguished by a switch on the mode, but the immediate_operand
4014      cases can't.  */
4015   if (pred_and_mode_tests.length () > 1)
4016     std::sort (&pred_and_mode_tests[0],
4017                &pred_and_mode_tests[0] + pred_and_mode_tests.length ());
4018
4019   /* Add the mode and predicate tests.  */
4020   pattern_pos *e;
4021   unsigned int i;
4022   FOR_EACH_VEC_ELT (pred_and_mode_tests, i, e)
4023     {
4024       switch (GET_CODE (e->pattern))
4025         {
4026         case MATCH_PARALLEL:
4027         case MATCH_OPERAND:
4028         case MATCH_SCRATCH:
4029         case MATCH_OPERATOR:
4030           {
4031             int opno = XINT (e->pattern, 0);
4032             num_operands = MAX (num_operands, opno + 1);
4033             const char *pred_name = predicate_name (e->pattern);
4034             if (pred_name[0])
4035               {
4036                 const struct pred_data *pred = lookup_predicate (pred_name);
4037                 /* Check the mode first, to distinguish things like SImode
4038                    and DImode register_operands, as described above.  */
4039                 machine_mode mode = GET_MODE (e->pattern);
4040                 if (pred && safe_predicate_mode (pred, mode))
4041                   s = add_decision (s, rtx_test::mode (e->pos), mode, true);
4042
4043                 /* Assign to operands[] first, so that the rtx usually doesn't
4044                    need to be live across the call to the predicate.
4045
4046                    This shouldn't cause a problem with dirtying the page,
4047                    since we fully expect to assign to operands[] at some point,
4048                    and since the caller usually writes to other parts of
4049                    recog_data anyway.  */
4050                 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4051                                   true, false);
4052                 s = add_decision (s, rtx_test::predicate (e->pos, pred, mode),
4053                                   true, false);
4054               }
4055             else
4056               /* Historically we've ignored the mode when there's no
4057                  predicate.  Just set up operands[] unconditionally.  */
4058               s = add_decision (s, rtx_test::set_op (e->pos, opno),
4059                                 true, false);
4060             break;
4061           }
4062
4063         default:
4064           s = add_decision (s, rtx_test::mode (e->pos),
4065                             GET_MODE (e->pattern), false);
4066           break;
4067         }
4068     }
4069
4070   /* Finally add rtx_equal_p checks for duplicated operands.  */
4071   FOR_EACH_VEC_ELT (dup_tests, i, e)
4072     s = add_decision (s, rtx_test::duplicate (e->pos, XINT (e->pattern, 0)),
4073                       true, false);
4074   return s;
4075 }
4076
4077 /* Add new decisions to S that make it return ACCEPTANCE if:
4078
4079    (1) the rtx doesn't match anything already matched by S
4080    (2) the rtx matches TOP_PATTERN and
4081    (3) the C test required by INFO->def is true
4082
4083    For peephole2, TOP_PATTERN is a SEQUENCE of the instruction patterns
4084    to match, otherwise it is a single instruction pattern.  */
4085
4086 static void
4087 match_pattern_1 (state *s, md_rtx_info *info, rtx pattern,
4088                  acceptance_type acceptance)
4089 {
4090   if (acceptance.type == PEEPHOLE2)
4091     {
4092       /* Match each individual instruction.  */
4093       position **subpos_ptr = &peep2_insn_pos_list;
4094       int count = 0;
4095       for (int i = 0; i < XVECLEN (pattern, 0); ++i)
4096         {
4097           rtx x = XVECEXP (pattern, 0, i);
4098           position *subpos = next_position (subpos_ptr, &root_pos,
4099                                             POS_PEEP2_INSN, count);
4100           if (count > 0)
4101             s = add_decision (s, rtx_test::peep2_count (count + 1),
4102                               true, false);
4103           s = match_pattern_2 (s, info, subpos, x);
4104           subpos_ptr = &subpos->next;
4105           count += 1;
4106         }
4107       acceptance.u.full.u.match_len = count - 1;
4108     }
4109   else
4110     {
4111       /* Make the rtx itself.  */
4112       s = match_pattern_2 (s, info, &root_pos, pattern);
4113
4114       /* If the match is only valid when extra clobbers are added,
4115          make sure we're able to pass that information to the caller.  */
4116       if (acceptance.type == RECOG && acceptance.u.full.u.num_clobbers)
4117         s = add_decision (s, rtx_test::have_num_clobbers (), true, false);
4118     }
4119
4120   /* Make sure that the C test is true.  */
4121   const char *c_test = get_c_test (info->def);
4122   if (maybe_eval_c_test (c_test) != 1)
4123     s = add_decision (s, rtx_test::c_test (c_test), true, false);
4124
4125   /* Accept the pattern.  */
4126   add_decision (s, rtx_test::accept (acceptance), true, false);
4127 }
4128
4129 /* Like match_pattern_1, but (if merge_states_p) try to merge the
4130    decisions with what's already in S, to reduce the amount of
4131    backtracking.  */
4132
4133 static void
4134 match_pattern (state *s, md_rtx_info *info, rtx pattern,
4135                acceptance_type acceptance)
4136 {
4137   if (merge_states_p)
4138     {
4139       state root;
4140       /* Add the decisions to a fresh state and then merge the full tree
4141          into the existing one.  */
4142       match_pattern_1 (&root, info, pattern, acceptance);
4143       merge_into_state (s, &root);
4144     }
4145   else
4146     match_pattern_1 (s, info, pattern, acceptance);
4147 }
4148
4149 /* Begin the output file.  */
4150
4151 static void
4152 write_header (void)
4153 {
4154   puts ("\
4155 /* Generated automatically by the program `genrecog' from the target\n\
4156    machine description file.  */\n\
4157 \n\
4158 #include \"config.h\"\n\
4159 #include \"system.h\"\n\
4160 #include \"coretypes.h\"\n\
4161 #include \"backend.h\"\n\
4162 #include \"predict.h\"\n\
4163 #include \"rtl.h\"\n\
4164 #include \"tm_p.h\"\n\
4165 #include \"emit-rtl.h\"\n\
4166 #include \"insn-config.h\"\n\
4167 #include \"recog.h\"\n\
4168 #include \"output.h\"\n\
4169 #include \"flags.h\"\n\
4170 #include \"df.h\"\n\
4171 #include \"resource.h\"\n\
4172 #include \"diagnostic-core.h\"\n\
4173 #include \"reload.h\"\n\
4174 #include \"regs.h\"\n\
4175 #include \"memmodel.h\"\n\
4176 #include \"tm-constrs.h\"\n\
4177 \n");
4178
4179   puts ("\n\
4180 /* `recog' contains a decision tree that recognizes whether the rtx\n\
4181    X0 is a valid instruction.\n\
4182 \n\
4183    recog returns -1 if the rtx is not valid.  If the rtx is valid, recog\n\
4184    returns a nonnegative number which is the insn code number for the\n\
4185    pattern that matched.  This is the same as the order in the machine\n\
4186    description of the entry that matched.  This number can be used as an\n\
4187    index into `insn_data' and other tables.\n");
4188   puts ("\
4189    The third parameter to recog is an optional pointer to an int.  If\n\
4190    present, recog will accept a pattern if it matches except for missing\n\
4191    CLOBBER expressions at the end.  In that case, the value pointed to by\n\
4192    the optional pointer will be set to the number of CLOBBERs that need\n\
4193    to be added (it should be initialized to zero by the caller).  If it");
4194   puts ("\
4195    is set nonzero, the caller should allocate a PARALLEL of the\n\
4196    appropriate size, copy the initial entries, and call add_clobbers\n\
4197    (found in insn-emit.c) to fill in the CLOBBERs.\n\
4198 ");
4199
4200   puts ("\n\
4201    The function split_insns returns 0 if the rtl could not\n\
4202    be split or the split rtl as an INSN list if it can be.\n\
4203 \n\
4204    The function peephole2_insns returns 0 if the rtl could not\n\
4205    be matched. If there was a match, the new rtl is returned in an INSN list,\n\
4206    and LAST_INSN will point to the last recognized insn in the old sequence.\n\
4207 */\n\n");
4208 }
4209
4210 /* Return the C type of a parameter with type TYPE.  */
4211
4212 static const char *
4213 parameter_type_string (parameter::type_enum type)
4214 {
4215   switch (type)
4216     {
4217     case parameter::UNSET:
4218       break;
4219
4220     case parameter::CODE:
4221       return "rtx_code";
4222
4223     case parameter::MODE:
4224       return "machine_mode";
4225
4226     case parameter::INT:
4227       return "int";
4228
4229     case parameter::UINT:
4230       return "unsigned int";
4231
4232     case parameter::WIDE_INT:
4233       return "HOST_WIDE_INT";
4234     }
4235   gcc_unreachable ();
4236 }
4237
4238 /* Return true if ACCEPTANCE requires only a single C statement even in
4239    a backtracking context.  */
4240
4241 static bool
4242 single_statement_p (const acceptance_type &acceptance)
4243 {
4244   if (acceptance.partial_p)
4245     /* We need to handle failures of the subroutine.  */
4246     return false;
4247   switch (acceptance.type)
4248     {
4249     case SUBPATTERN:
4250     case SPLIT:
4251       return true;
4252
4253     case RECOG:
4254       /* False if we need to assign to pnum_clobbers.  */
4255       return acceptance.u.full.u.num_clobbers == 0;
4256
4257     case PEEPHOLE2:
4258       /* We need to assign to pmatch_len_ and handle null returns from the
4259          peephole2 routine.  */
4260       return false;
4261     }
4262   gcc_unreachable ();
4263 }
4264
4265 /* Return the C failure value for a routine of type TYPE.  */
4266
4267 static const char *
4268 get_failure_return (routine_type type)
4269 {
4270   switch (type)
4271     {
4272     case SUBPATTERN:
4273     case RECOG:
4274       return "-1";
4275
4276     case SPLIT:
4277     case PEEPHOLE2:
4278       return "NULL";
4279     }
4280   gcc_unreachable ();
4281 }
4282
4283 /* Indicates whether a block of code always returns or whether it can fall
4284    through.  */
4285
4286 enum exit_state {
4287   ES_RETURNED,
4288   ES_FALLTHROUGH
4289 };
4290
4291 /* Information used while writing out code.  */
4292
4293 struct output_state
4294 {
4295   /* The type of routine that we're generating.  */
4296   routine_type type;
4297
4298   /* Maps position ids to xN variable numbers.  The entry is only valid if
4299      it is less than the length of VAR_TO_ID, but this holds for every position
4300      tested by a state when writing out that state.  */
4301   auto_vec <unsigned int> id_to_var;
4302
4303   /* Maps xN variable numbers to position ids.  */
4304   auto_vec <unsigned int> var_to_id;
4305
4306   /* Index N is true if variable xN has already been set.  */
4307   auto_vec <bool> seen_vars;
4308 };
4309
4310 /* Return true if D is a call to a pattern routine and if there is some X
4311    such that the transition for pattern result N goes to a successful return
4312    with code X+N.  When returning true, set *BASE_OUT to this X and *COUNT_OUT
4313    to the number of return values.  (We know that every PATTERN decision has
4314    a transition for every successful return.)  */
4315
4316 static bool
4317 terminal_pattern_p (decision *d, unsigned int *base_out,
4318                     unsigned int *count_out)
4319 {
4320   if (d->test.kind != rtx_test::PATTERN)
4321     return false;
4322   unsigned int base = 0;
4323   unsigned int count = 0;
4324   for (transition *trans = d->first; trans; trans = trans->next)
4325     {
4326       if (trans->is_param || trans->labels.length () != 1)
4327         return false;
4328       decision *subd = trans->to->singleton ();
4329       if (!subd || subd->test.kind != rtx_test::ACCEPT)
4330         return false;
4331       unsigned int this_base = (subd->test.u.acceptance.u.full.code
4332                                 - trans->labels[0]);
4333       if (trans == d->first)
4334         base = this_base;
4335       else if (base != this_base)
4336         return false;
4337       count += 1;
4338     }
4339   *base_out = base;
4340   *count_out = count;
4341   return true;
4342 }
4343
4344 /* Return true if TEST doesn't test an rtx or if the rtx it tests is
4345    already available in state OS.  */
4346
4347 static bool
4348 test_position_available_p (output_state *os, const rtx_test &test)
4349 {
4350   return (!test.pos
4351           || test.pos_operand >= 0
4352           || os->seen_vars[os->id_to_var[test.pos->id]]);
4353 }
4354
4355 /* Like printf, but print INDENT spaces at the beginning.  */
4356
4357 static void ATTRIBUTE_PRINTF_2
4358 printf_indent (unsigned int indent, const char *format, ...)
4359 {
4360   va_list ap;
4361   va_start (ap, format);
4362   printf ("%*s", indent, "");
4363   vprintf (format, ap);
4364   va_end (ap);
4365 }
4366
4367 /* Emit code to initialize the variable associated with POS, if it isn't
4368    already valid in state OS.  Indent each line by INDENT spaces.  Update
4369    OS with the new state.  */
4370
4371 static void
4372 change_state (output_state *os, position *pos, unsigned int indent)
4373 {
4374   unsigned int var = os->id_to_var[pos->id];
4375   gcc_assert (var < os->var_to_id.length () && os->var_to_id[var] == pos->id);
4376   if (os->seen_vars[var])
4377     return;
4378   switch (pos->type)
4379     {
4380     case POS_PEEP2_INSN:
4381       printf_indent (indent, "x%d = PATTERN (peep2_next_insn (%d));\n",
4382                      var, pos->arg);
4383       break;
4384
4385     case POS_XEXP:
4386       change_state (os, pos->base, indent);
4387       printf_indent (indent, "x%d = XEXP (x%d, %d);\n",
4388                      var, os->id_to_var[pos->base->id], pos->arg);
4389       break;
4390
4391     case POS_XVECEXP0:
4392       change_state (os, pos->base, indent);
4393       printf_indent (indent, "x%d = XVECEXP (x%d, 0, %d);\n",
4394                      var, os->id_to_var[pos->base->id], pos->arg);
4395       break;
4396     }
4397   os->seen_vars[var] = true;
4398 }
4399
4400 /* Print the enumerator constant for CODE -- the upcase version of
4401    the name.  */
4402
4403 static void
4404 print_code (enum rtx_code code)
4405 {
4406   const char *p;
4407   for (p = GET_RTX_NAME (code); *p; p++)
4408     putchar (TOUPPER (*p));
4409 }
4410
4411 /* Emit a uint64_t as an integer constant expression.  We need to take
4412    special care to avoid "decimal constant is so large that it is unsigned"
4413    warnings in the resulting code.  */
4414
4415 static void
4416 print_host_wide_int (uint64_t val)
4417 {
4418   uint64_t min = uint64_t (1) << (HOST_BITS_PER_WIDE_INT - 1);
4419   if (val == min)
4420     printf ("(" HOST_WIDE_INT_PRINT_DEC_C " - 1)", val + 1);
4421   else
4422     printf (HOST_WIDE_INT_PRINT_DEC_C, val);
4423 }
4424
4425 /* Print the C expression for actual parameter PARAM.  */
4426
4427 static void
4428 print_parameter_value (const parameter &param)
4429 {
4430   if (param.is_param)
4431     printf ("i%d", (int) param.value + 1);
4432   else
4433     switch (param.type)
4434       {
4435       case parameter::UNSET:
4436         gcc_unreachable ();
4437         break;
4438
4439       case parameter::CODE:
4440         print_code ((enum rtx_code) param.value);
4441         break;
4442
4443       case parameter::MODE:
4444         printf ("%smode", GET_MODE_NAME ((machine_mode) param.value));
4445         break;
4446
4447       case parameter::INT:
4448         printf ("%d", (int) param.value);
4449         break;
4450
4451       case parameter::UINT:
4452         printf ("%u", (unsigned int) param.value);
4453         break;
4454
4455       case parameter::WIDE_INT:
4456         print_host_wide_int (param.value);
4457         break;
4458       }
4459 }
4460
4461 /* Print the C expression for the rtx tested by TEST.  */
4462
4463 static void
4464 print_test_rtx (output_state *os, const rtx_test &test)
4465 {
4466   if (test.pos_operand >= 0)
4467     printf ("operands[%d]", test.pos_operand);
4468   else
4469     printf ("x%d", os->id_to_var[test.pos->id]);
4470 }
4471
4472 /* Print the C expression for non-boolean test TEST.  */
4473
4474 static void
4475 print_nonbool_test (output_state *os, const rtx_test &test)
4476 {
4477   switch (test.kind)
4478     {
4479     case rtx_test::CODE:
4480       printf ("GET_CODE (");
4481       print_test_rtx (os, test);
4482       printf (")");
4483       break;
4484
4485     case rtx_test::MODE:
4486       printf ("GET_MODE (");
4487       print_test_rtx (os, test);
4488       printf (")");
4489       break;
4490
4491     case rtx_test::VECLEN:
4492       printf ("XVECLEN (");
4493       print_test_rtx (os, test);
4494       printf (", 0)");
4495       break;
4496
4497     case rtx_test::INT_FIELD:
4498       printf ("XINT (");
4499       print_test_rtx (os, test);
4500       printf (", %d)", test.u.opno);
4501       break;
4502
4503     case rtx_test::REGNO_FIELD:
4504       printf ("REGNO (");
4505       print_test_rtx (os, test);
4506       printf (")");
4507       break;
4508
4509     case rtx_test::WIDE_INT_FIELD:
4510       printf ("XWINT (");
4511       print_test_rtx (os, test);
4512       printf (", %d)", test.u.opno);
4513       break;
4514
4515     case rtx_test::PATTERN:
4516       {
4517         pattern_routine *routine = test.u.pattern->routine;
4518         printf ("pattern%d (", routine->pattern_id);
4519         const char *sep = "";
4520         if (test.pos)
4521           {
4522             print_test_rtx (os, test);
4523             sep = ", ";
4524           }
4525         if (routine->insn_p)
4526           {
4527             printf ("%sinsn", sep);
4528             sep = ", ";
4529           }
4530         if (routine->pnum_clobbers_p)
4531           {
4532             printf ("%spnum_clobbers", sep);
4533             sep = ", ";
4534           }
4535         for (unsigned int i = 0; i < test.u.pattern->params.length (); ++i)
4536           {
4537             fputs (sep, stdout);
4538             print_parameter_value (test.u.pattern->params[i]);
4539             sep = ", ";
4540           }
4541         printf (")");
4542         break;
4543       }
4544
4545     case rtx_test::PEEP2_COUNT:
4546     case rtx_test::VECLEN_GE:
4547     case rtx_test::SAVED_CONST_INT:
4548     case rtx_test::DUPLICATE:
4549     case rtx_test::PREDICATE:
4550     case rtx_test::SET_OP:
4551     case rtx_test::HAVE_NUM_CLOBBERS:
4552     case rtx_test::C_TEST:
4553     case rtx_test::ACCEPT:
4554       gcc_unreachable ();
4555     }
4556 }
4557
4558 /* IS_PARAM and LABEL are taken from a transition whose source
4559    decision performs TEST.  Print the C code for the label.  */
4560
4561 static void
4562 print_label_value (const rtx_test &test, bool is_param, uint64_t value)
4563 {
4564   print_parameter_value (parameter (transition_parameter_type (test.kind),
4565                                     is_param, value));
4566 }
4567
4568 /* If IS_PARAM, print code to compare TEST with the C variable i<VALUE+1>.
4569    If !IS_PARAM, print code to compare TEST with the C constant VALUE.
4570    Test for inequality if INVERT_P, otherwise test for equality.  */
4571
4572 static void
4573 print_test (output_state *os, const rtx_test &test, bool is_param,
4574             uint64_t value, bool invert_p)
4575 {
4576   switch (test.kind)
4577     {
4578       /* Handle the non-boolean TESTs.  */
4579     case rtx_test::CODE:
4580     case rtx_test::MODE:
4581     case rtx_test::VECLEN:
4582     case rtx_test::REGNO_FIELD:
4583     case rtx_test::INT_FIELD:
4584     case rtx_test::WIDE_INT_FIELD:
4585     case rtx_test::PATTERN:
4586       print_nonbool_test (os, test);
4587       printf (" %s ", invert_p ? "!=" : "==");
4588       print_label_value (test, is_param, value);
4589       break;
4590
4591     case rtx_test::SAVED_CONST_INT:
4592       gcc_assert (!is_param && value == 1);
4593       print_test_rtx (os, test);
4594       printf (" %s const_int_rtx[MAX_SAVED_CONST_INT + ",
4595               invert_p ? "!=" : "==");
4596       print_parameter_value (parameter (parameter::INT,
4597                                         test.u.integer.is_param,
4598                                         test.u.integer.value));
4599       printf ("]");
4600       break;
4601
4602     case rtx_test::PEEP2_COUNT:
4603       gcc_assert (!is_param && value == 1);
4604       printf ("peep2_current_count %s %d", invert_p ? "<" : ">=",
4605               test.u.min_len);
4606       break;
4607
4608     case rtx_test::VECLEN_GE:
4609       gcc_assert (!is_param && value == 1);
4610       printf ("XVECLEN (");
4611       print_test_rtx (os, test);
4612       printf (", 0) %s %d", invert_p ? "<" : ">=", test.u.min_len);
4613       break;
4614
4615     case rtx_test::PREDICATE:
4616       gcc_assert (!is_param && value == 1);
4617       printf ("%s%s (", invert_p ? "!" : "", test.u.predicate.data->name);
4618       print_test_rtx (os, test);
4619       printf (", ");
4620       print_parameter_value (parameter (parameter::MODE,
4621                                         test.u.predicate.mode_is_param,
4622                                         test.u.predicate.mode));
4623       printf (")");
4624       break;
4625
4626     case rtx_test::DUPLICATE:
4627       gcc_assert (!is_param && value == 1);
4628       printf ("%srtx_equal_p (", invert_p ? "!" : "");
4629       print_test_rtx (os, test);
4630       printf (", operands[%d])", test.u.opno);
4631       break;
4632
4633     case rtx_test::HAVE_NUM_CLOBBERS:
4634       gcc_assert (!is_param && value == 1);
4635       printf ("pnum_clobbers %s NULL", invert_p ? "==" : "!=");
4636       break;
4637
4638     case rtx_test::C_TEST:
4639       gcc_assert (!is_param && value == 1);
4640       if (invert_p)
4641         printf ("!");
4642       print_c_condition (test.u.string);
4643       break;
4644
4645     case rtx_test::ACCEPT:
4646     case rtx_test::SET_OP:
4647       gcc_unreachable ();
4648     }
4649 }
4650
4651 static exit_state print_decision (output_state *, decision *,
4652                                   unsigned int, bool);
4653
4654 /* Print code to perform S, indent each line by INDENT spaces.
4655    IS_FINAL is true if there are no fallback decisions to test on failure;
4656    if the state fails then the entire routine fails.  */
4657
4658 static exit_state
4659 print_state (output_state *os, state *s, unsigned int indent, bool is_final)
4660 {
4661   exit_state es = ES_FALLTHROUGH;
4662   for (decision *d = s->first; d; d = d->next)
4663     es = print_decision (os, d, indent, is_final && !d->next);
4664   if (es != ES_RETURNED && is_final)
4665     {
4666       printf_indent (indent, "return %s;\n", get_failure_return (os->type));
4667       es = ES_RETURNED;
4668     }
4669   return es;
4670 }
4671
4672 /* Print the code for subroutine call ACCEPTANCE (for which partial_p
4673    is known to be true).  Return the C condition that indicates a successful
4674    match.  */
4675
4676 static const char *
4677 print_subroutine_call (const acceptance_type &acceptance)
4678 {
4679   switch (acceptance.type)
4680     {
4681     case SUBPATTERN:
4682       gcc_unreachable ();
4683
4684     case RECOG:
4685       printf ("recog_%d (x1, insn, pnum_clobbers)",
4686               acceptance.u.subroutine_id);
4687       return ">= 0";
4688
4689     case SPLIT:
4690       printf ("split_%d (x1, insn)", acceptance.u.subroutine_id);
4691       return "!= NULL_RTX";
4692
4693     case PEEPHOLE2:
4694       printf ("peephole2_%d (x1, insn, pmatch_len_)",
4695               acceptance.u.subroutine_id);
4696       return "!= NULL_RTX";
4697     }
4698   gcc_unreachable ();
4699 }
4700
4701 /* Print code for the successful match described by ACCEPTANCE.
4702    INDENT and IS_FINAL are as for print_state.  */
4703
4704 static exit_state
4705 print_acceptance (const acceptance_type &acceptance, unsigned int indent,
4706                   bool is_final)
4707 {
4708   if (acceptance.partial_p)
4709     {
4710       /* Defer the rest of the match to a subroutine.  */
4711       if (is_final)
4712         {
4713           printf_indent (indent, "return ");
4714           print_subroutine_call (acceptance);
4715           printf (";\n");
4716           return ES_RETURNED;
4717         }
4718       else
4719         {
4720           printf_indent (indent, "res = ");
4721           const char *res_test = print_subroutine_call (acceptance);
4722           printf (";\n");
4723           printf_indent (indent, "if (res %s)\n", res_test);
4724           printf_indent (indent + 2, "return res;\n");
4725           return ES_FALLTHROUGH;
4726         }
4727     }
4728   switch (acceptance.type)
4729     {
4730     case SUBPATTERN:
4731       printf_indent (indent, "return %d;\n", acceptance.u.full.code);
4732       return ES_RETURNED;
4733
4734     case RECOG:
4735       if (acceptance.u.full.u.num_clobbers != 0)
4736         printf_indent (indent, "*pnum_clobbers = %d;\n",
4737                        acceptance.u.full.u.num_clobbers);
4738       printf_indent (indent, "return %d; /* %s */\n", acceptance.u.full.code,
4739                      get_insn_name (acceptance.u.full.code));
4740       return ES_RETURNED;
4741
4742     case SPLIT:
4743       printf_indent (indent, "return gen_split_%d (insn, operands);\n",
4744                      acceptance.u.full.code);
4745       return ES_RETURNED;
4746
4747     case PEEPHOLE2:
4748       printf_indent (indent, "*pmatch_len_ = %d;\n",
4749                      acceptance.u.full.u.match_len);
4750       if (is_final)
4751         {
4752           printf_indent (indent, "return gen_peephole2_%d (insn, operands);\n",
4753                          acceptance.u.full.code);
4754           return ES_RETURNED;
4755         }
4756       else
4757         {
4758           printf_indent (indent, "res = gen_peephole2_%d (insn, operands);\n",
4759                          acceptance.u.full.code);
4760           printf_indent (indent, "if (res != NULL_RTX)\n");
4761           printf_indent (indent + 2, "return res;\n");
4762           return ES_FALLTHROUGH;
4763         }
4764     }
4765   gcc_unreachable ();
4766 }
4767
4768 /* Print code to perform D.  INDENT and IS_FINAL are as for print_state.  */
4769
4770 static exit_state
4771 print_decision (output_state *os, decision *d, unsigned int indent,
4772                 bool is_final)
4773 {
4774   uint64_t label;
4775   unsigned int base, count;
4776
4777   /* Make sure the rtx under test is available either in operands[] or
4778      in an xN variable.  */
4779   if (d->test.pos && d->test.pos_operand < 0)
4780     change_state (os, d->test.pos, indent);
4781
4782   /* Look for cases where a pattern routine P1 calls another pattern routine
4783      P2 and where P1 returns X + BASE whenever P2 returns X.  If IS_FINAL
4784      is true and BASE is zero we can simply use:
4785
4786         return patternN (...);
4787
4788      Otherwise we can use:
4789
4790         res = patternN (...);
4791         if (res >= 0)
4792           return res + BASE;
4793
4794      However, if BASE is nonzero and patternN only returns 0 or -1,
4795      the usual "return BASE;" is better than "return res + BASE;".
4796      If BASE is zero, "return res;" should be better than "return 0;",
4797      since no assignment to the return register is required.  */
4798   if (os->type == SUBPATTERN
4799       && terminal_pattern_p (d, &base, &count)
4800       && (base == 0 || count > 1))
4801     {
4802       if (is_final && base == 0)
4803         {
4804           printf_indent (indent, "return ");
4805           print_nonbool_test (os, d->test);
4806           printf ("; /* [-1, %d] */\n", count - 1);
4807           return ES_RETURNED;
4808         }
4809       else
4810         {
4811           printf_indent (indent, "res = ");
4812           print_nonbool_test (os, d->test);
4813           printf (";\n");
4814           printf_indent (indent, "if (res >= 0)\n");
4815           printf_indent (indent + 2, "return res");
4816           if (base != 0)
4817             printf (" + %d", base);
4818           printf ("; /* [%d, %d] */\n", base, base + count - 1);
4819           return ES_FALLTHROUGH;
4820         }
4821     }
4822   else if (d->test.kind == rtx_test::ACCEPT)
4823     return print_acceptance (d->test.u.acceptance, indent, is_final);
4824   else if (d->test.kind == rtx_test::SET_OP)
4825     {
4826       printf_indent (indent, "operands[%d] = ", d->test.u.opno);
4827       print_test_rtx (os, d->test);
4828       printf (";\n");
4829       return print_state (os, d->singleton ()->to, indent, is_final);
4830     }
4831   /* Handle decisions with a single transition and a single transition
4832      label.  */
4833   else if (d->if_statement_p (&label))
4834     {
4835       transition *trans = d->singleton ();
4836       if (mark_optional_transitions_p && trans->optional)
4837         printf_indent (indent, "/* OPTIONAL IF */\n");
4838
4839       /* Print the condition associated with TRANS.  Invert it if IS_FINAL,
4840          so that we return immediately on failure and fall through on
4841          success.  */
4842       printf_indent (indent, "if (");
4843       print_test (os, d->test, trans->is_param, label, is_final);
4844
4845       /* Look for following states that would be handled by this code
4846          on recursion.  If they don't need any preparatory statements,
4847          include them in the current "if" statement rather than creating
4848          a new one.  */
4849       for (;;)
4850         {
4851           d = trans->to->singleton ();
4852           if (!d
4853               || d->test.kind == rtx_test::ACCEPT
4854               || d->test.kind == rtx_test::SET_OP
4855               || !d->if_statement_p (&label)
4856               || !test_position_available_p (os, d->test))
4857             break;
4858           trans = d->first;
4859           printf ("\n");
4860           if (mark_optional_transitions_p && trans->optional)
4861             printf_indent (indent + 4, "/* OPTIONAL IF */\n");
4862           printf_indent (indent + 4, "%s ", is_final ? "||" : "&&");
4863           print_test (os, d->test, trans->is_param, label, is_final);
4864         }
4865       printf (")\n");
4866
4867       /* Print the conditional code with INDENT + 2 and the fallthrough
4868          code with indent INDENT.  */
4869       state *to = trans->to;
4870       if (is_final)
4871         {
4872           /* We inverted the condition above, so return failure in the
4873              "if" body and fall through to the target of the transition.  */
4874           printf_indent (indent + 2, "return %s;\n",
4875                          get_failure_return (os->type));
4876           return print_state (os, to, indent, is_final);
4877         }
4878       else if (to->singleton ()
4879                && to->first->test.kind == rtx_test::ACCEPT
4880                && single_statement_p (to->first->test.u.acceptance))
4881         {
4882           /* The target of the transition is a simple "return" statement.
4883              It doesn't need any braces and doesn't fall through.  */
4884           if (print_acceptance (to->first->test.u.acceptance,
4885                                 indent + 2, true) != ES_RETURNED)
4886             gcc_unreachable ();
4887           return ES_FALLTHROUGH;
4888         }
4889       else
4890         {
4891           /* The general case.  Output code for the target of the transition
4892              in braces.  This will not invalidate any of the xN variables
4893              that are already valid, but we mustn't rely on any that are
4894              set by the "if" body.  */
4895           auto_vec <bool, 32> old_seen;
4896           old_seen.safe_splice (os->seen_vars);
4897
4898           printf_indent (indent + 2, "{\n");
4899           print_state (os, trans->to, indent + 4, is_final);
4900           printf_indent (indent + 2, "}\n");
4901
4902           os->seen_vars.truncate (0);
4903           os->seen_vars.splice (old_seen);
4904           return ES_FALLTHROUGH;
4905         }
4906     }
4907   else
4908     {
4909       /* Output the decision as a switch statement.  */
4910       printf_indent (indent, "switch (");
4911       print_nonbool_test (os, d->test);
4912       printf (")\n");
4913
4914       /* Each case statement starts with the same set of valid variables.
4915          These are also the only variables will be valid on fallthrough.  */
4916       auto_vec <bool, 32> old_seen;
4917       old_seen.safe_splice (os->seen_vars);
4918
4919       printf_indent (indent + 2, "{\n");
4920       for (transition *trans = d->first; trans; trans = trans->next)
4921         {
4922           gcc_assert (!trans->is_param);
4923           if (mark_optional_transitions_p && trans->optional)
4924             printf_indent (indent + 2, "/* OPTIONAL CASE */\n");
4925           for (int_set::iterator j = trans->labels.begin ();
4926                j != trans->labels.end (); ++j)
4927             {
4928               printf_indent (indent + 2, "case ");
4929               print_label_value (d->test, trans->is_param, *j);
4930               printf (":\n");
4931             }
4932           if (print_state (os, trans->to, indent + 4, is_final))
4933             {
4934               /* The state can fall through.  Add an explicit break.  */
4935               gcc_assert (!is_final);
4936               printf_indent (indent + 4, "break;\n");
4937             }
4938           printf ("\n");
4939
4940           /* Restore the original set of valid variables.  */
4941           os->seen_vars.truncate (0);
4942           os->seen_vars.splice (old_seen);
4943         }
4944       /* Add a default case.  */
4945       printf_indent (indent + 2, "default:\n");
4946       if (is_final)
4947         printf_indent (indent + 4, "return %s;\n",
4948                        get_failure_return (os->type));
4949       else
4950         printf_indent (indent + 4, "break;\n");
4951       printf_indent (indent + 2, "}\n");
4952       return is_final ? ES_RETURNED : ES_FALLTHROUGH;
4953     }
4954 }
4955
4956 /* Make sure that OS has a position variable for POS.  ROOT_P is true if
4957    POS is the root position for the routine.  */
4958
4959 static void
4960 assign_position_var (output_state *os, position *pos, bool root_p)
4961 {
4962   unsigned int idx = os->id_to_var[pos->id];
4963   if (idx < os->var_to_id.length () && os->var_to_id[idx] == pos->id)
4964     return;
4965   if (!root_p && pos->type != POS_PEEP2_INSN)
4966     assign_position_var (os, pos->base, false);
4967   os->id_to_var[pos->id] = os->var_to_id.length ();
4968   os->var_to_id.safe_push (pos->id);
4969 }
4970
4971 /* Make sure that OS has the position variables required by S.  */
4972
4973 static void
4974 assign_position_vars (output_state *os, state *s)
4975 {
4976   for (decision *d = s->first; d; d = d->next)
4977     {
4978       /* Positions associated with operands can be read from the
4979          operands[] array.  */
4980       if (d->test.pos && d->test.pos_operand < 0)
4981         assign_position_var (os, d->test.pos, false);
4982       for (transition *trans = d->first; trans; trans = trans->next)
4983         assign_position_vars (os, trans->to);
4984     }
4985 }
4986
4987 /* Print the open brace and variable definitions for a routine that
4988    implements S.  ROOT is the deepest rtx from which S can access all
4989    relevant parts of the first instruction it matches.  Initialize OS
4990    so that every relevant position has an rtx variable xN and so that
4991    only ROOT's variable has a valid value.  */
4992
4993 static void
4994 print_subroutine_start (output_state *os, state *s, position *root)
4995 {
4996   printf ("{\n  rtx * const operands ATTRIBUTE_UNUSED"
4997           " = &recog_data.operand[0];\n");
4998   os->var_to_id.truncate (0);
4999   os->seen_vars.truncate (0);
5000   if (root)
5001     {
5002       /* Create a fake entry for position 0 so that an id_to_var of 0
5003          is always invalid.  This also makes the xN variables naturally
5004          1-based rather than 0-based.  */
5005       os->var_to_id.safe_push (num_positions);
5006
5007       /* Associate ROOT with x1.  */
5008       assign_position_var (os, root, true);
5009
5010       /* Assign xN variables to all other relevant positions.  */
5011       assign_position_vars (os, s);
5012
5013       /* Output the variable declarations (except for ROOT's, which is
5014          passed in as a parameter).  */
5015       unsigned int num_vars = os->var_to_id.length ();
5016       if (num_vars > 2)
5017         {
5018           for (unsigned int i = 2; i < num_vars; ++i)
5019             /* Print 8 rtx variables to a line.  */
5020             printf ("%s x%d",
5021                     i == 2 ? "  rtx" : (i - 2) % 8 == 0 ? ";\n  rtx" : ",", i);
5022           printf (";\n");
5023         }
5024
5025       /* Say that x1 is valid and the rest aren't.  */
5026       os->seen_vars.safe_grow_cleared (num_vars);
5027       os->seen_vars[1] = true;
5028     }
5029   if (os->type == SUBPATTERN || os->type == RECOG)
5030     printf ("  int res ATTRIBUTE_UNUSED;\n");
5031   else
5032     printf ("  rtx_insn *res ATTRIBUTE_UNUSED;\n");
5033 }
5034
5035 /* Output the definition of pattern routine ROUTINE.  */
5036
5037 static void
5038 print_pattern (output_state *os, pattern_routine *routine)
5039 {
5040   printf ("\nstatic int\npattern%d (", routine->pattern_id);
5041   const char *sep = "";
5042   /* Add the top-level rtx parameter, if any.  */
5043   if (routine->pos)
5044     {
5045       printf ("%srtx x1", sep);
5046       sep = ", ";
5047     }
5048   /* Add the optional parameters.  */
5049   if (routine->insn_p)
5050     {
5051       /* We can't easily tell whether a C condition actually reads INSN,
5052          so add an ATTRIBUTE_UNUSED just in case.  */
5053       printf ("%srtx_insn *insn ATTRIBUTE_UNUSED", sep);
5054       sep = ", ";
5055     }
5056   if (routine->pnum_clobbers_p)
5057     {
5058       printf ("%sint *pnum_clobbers", sep);
5059       sep = ", ";
5060     }
5061   /* Add the "i" parameters.  */
5062   for (unsigned int i = 0; i < routine->param_types.length (); ++i)
5063     {
5064       printf ("%s%s i%d", sep,
5065               parameter_type_string (routine->param_types[i]), i + 1);
5066       sep = ", ";
5067     }
5068   printf (")\n");
5069   os->type = SUBPATTERN;
5070   print_subroutine_start (os, routine->s, routine->pos);
5071   print_state (os, routine->s, 2, true);
5072   printf ("}\n");
5073 }
5074
5075 /* Output a routine of type TYPE that implements S.  PROC_ID is the
5076    number of the subroutine associated with S, or 0 if S is the main
5077    routine.  */
5078
5079 static void
5080 print_subroutine (output_state *os, state *s, int proc_id)
5081 {
5082   /* For now, the top-level "recog" takes a plain "rtx", and performs a
5083      checked cast to "rtx_insn *" for use throughout the rest of the
5084      function and the code it calls.  */
5085   const char *insn_param
5086     = proc_id > 0 ? "rtx_insn *insn" : "rtx uncast_insn";
5087   printf ("\n");
5088   switch (os->type)
5089     {
5090     case SUBPATTERN:
5091       gcc_unreachable ();
5092
5093     case RECOG:
5094       if (proc_id)
5095         printf ("static int\nrecog_%d", proc_id);
5096       else
5097         printf ("int\nrecog");
5098       printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5099               "\t%s ATTRIBUTE_UNUSED,\n"
5100               "\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", insn_param);
5101       break;
5102
5103     case SPLIT:
5104       if (proc_id)
5105         printf ("static rtx_insn *\nsplit_%d", proc_id);
5106       else
5107         printf ("rtx_insn *\nsplit_insns");
5108       printf (" (rtx x1 ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n");
5109       break;
5110
5111     case PEEPHOLE2:
5112       if (proc_id)
5113         printf ("static rtx_insn *\npeephole2_%d", proc_id);
5114       else
5115         printf ("rtx_insn *\npeephole2_insns");
5116       printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5117               "\trtx_insn *insn ATTRIBUTE_UNUSED,\n"
5118               "\tint *pmatch_len_ ATTRIBUTE_UNUSED)\n");
5119       break;
5120     }
5121   print_subroutine_start (os, s, &root_pos);
5122   if (proc_id == 0)
5123     {
5124       printf ("  recog_data.insn = NULL;\n");
5125       if (os->type == RECOG)
5126         {
5127           printf ("  rtx_insn *insn ATTRIBUTE_UNUSED;\n");
5128           printf ("  insn = safe_as_a <rtx_insn *> (uncast_insn);\n");
5129         }
5130     }
5131   print_state (os, s, 2, true);
5132   printf ("}\n");
5133 }
5134
5135 /* Print out a routine of type TYPE that performs ROOT.  */
5136
5137 static void
5138 print_subroutine_group (output_state *os, routine_type type, state *root)
5139 {
5140   os->type = type;
5141   if (use_subroutines_p)
5142     {
5143       /* Split ROOT up into smaller pieces, both for readability and to
5144          help the compiler.  */
5145       auto_vec <state *> subroutines;
5146       find_subroutines (type, root, subroutines);
5147
5148       /* Output the subroutines (but not ROOT itself).  */
5149       unsigned int i;
5150       state *s;
5151       FOR_EACH_VEC_ELT (subroutines, i, s)
5152         print_subroutine (os, s, i + 1);
5153     }
5154   /* Output the main routine.  */
5155   print_subroutine (os, root, 0);
5156 }
5157
5158 /* Return the rtx pattern for the list of rtxes in a define_peephole2.  */
5159
5160 static rtx
5161 get_peephole2_pattern (md_rtx_info *info)
5162 {
5163   int i, j;
5164   rtvec vec = XVEC (info->def, 0);
5165   rtx pattern = rtx_alloc (SEQUENCE);
5166   XVEC (pattern, 0) = rtvec_alloc (GET_NUM_ELEM (vec));
5167   for (i = j = 0; i < GET_NUM_ELEM (vec); i++)
5168     {
5169       rtx x = RTVEC_ELT (vec, i);
5170       /* Ignore scratch register requirements.  */
5171       if (GET_CODE (x) != MATCH_SCRATCH && GET_CODE (x) != MATCH_DUP)
5172         {
5173           XVECEXP (pattern, 0, j) = x;
5174           j++;
5175         }
5176     }
5177   XVECLEN (pattern, 0) = j;
5178   if (j == 0)
5179     error_at (info->loc, "empty define_peephole2");
5180   return pattern;
5181 }
5182
5183 /* Return true if *PATTERN_PTR is a PARALLEL in which at least one trailing
5184    rtx can be added automatically by add_clobbers.  If so, update
5185    *ACCEPTANCE_PTR so that its num_clobbers field contains the number
5186    of such trailing rtxes and update *PATTERN_PTR so that it contains
5187    the pattern without those rtxes.  */
5188
5189 static bool
5190 remove_clobbers (acceptance_type *acceptance_ptr, rtx *pattern_ptr)
5191 {
5192   int i;
5193   rtx new_pattern;
5194
5195   /* Find the last non-clobber in the parallel.  */
5196   rtx pattern = *pattern_ptr;
5197   for (i = XVECLEN (pattern, 0); i > 0; i--)
5198     {
5199       rtx x = XVECEXP (pattern, 0, i - 1);
5200       if (GET_CODE (x) != CLOBBER
5201           || (!REG_P (XEXP (x, 0))
5202               && GET_CODE (XEXP (x, 0)) != MATCH_SCRATCH))
5203         break;
5204     }
5205
5206   if (i == XVECLEN (pattern, 0))
5207     return false;
5208
5209   /* Build a similar insn without the clobbers.  */
5210   if (i == 1)
5211     new_pattern = XVECEXP (pattern, 0, 0);
5212   else
5213     {
5214       new_pattern = rtx_alloc (PARALLEL);
5215       XVEC (new_pattern, 0) = rtvec_alloc (i);
5216       for (int j = 0; j < i; ++j)
5217         XVECEXP (new_pattern, 0, j) = XVECEXP (pattern, 0, j);
5218     }
5219
5220   /* Recognize it.  */
5221   acceptance_ptr->u.full.u.num_clobbers = XVECLEN (pattern, 0) - i;
5222   *pattern_ptr = new_pattern;
5223   return true;
5224 }
5225
5226 int
5227 main (int argc, char **argv)
5228 {
5229   state insn_root, split_root, peephole2_root;
5230
5231   progname = "genrecog";
5232
5233   if (!init_rtx_reader_args (argc, argv))
5234     return (FATAL_EXIT_CODE);
5235
5236   write_header ();
5237
5238   /* Read the machine description.  */
5239
5240   md_rtx_info info;
5241   while (read_md_rtx (&info))
5242     {
5243       rtx def = info.def;
5244
5245       acceptance_type acceptance;
5246       acceptance.partial_p = false;
5247       acceptance.u.full.code = info.index;
5248
5249       rtx pattern;
5250       switch (GET_CODE (def))
5251         {
5252         case DEFINE_INSN:
5253           {
5254             /* Match the instruction in the original .md form.  */
5255             acceptance.type = RECOG;
5256             acceptance.u.full.u.num_clobbers = 0;
5257             pattern = add_implicit_parallel (XVEC (def, 1));
5258             validate_pattern (pattern, &info, NULL_RTX, 0);
5259             match_pattern (&insn_root, &info, pattern, acceptance);
5260
5261             /* If the pattern is a PARALLEL with trailing CLOBBERs,
5262                allow recog_for_combine to match without the clobbers.  */
5263             if (GET_CODE (pattern) == PARALLEL
5264                 && remove_clobbers (&acceptance, &pattern))
5265               match_pattern (&insn_root, &info, pattern, acceptance);
5266             break;
5267           }
5268
5269         case DEFINE_SPLIT:
5270           acceptance.type = SPLIT;
5271           pattern = add_implicit_parallel (XVEC (def, 0));
5272           validate_pattern (pattern, &info, NULL_RTX, 0);
5273           match_pattern (&split_root, &info, pattern, acceptance);
5274
5275           /* Declare the gen_split routine that we'll call if the
5276              pattern matches.  The definition comes from insn-emit.c.  */
5277           printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
5278                   info.index);
5279           break;
5280
5281         case DEFINE_PEEPHOLE2:
5282           acceptance.type = PEEPHOLE2;
5283           pattern = get_peephole2_pattern (&info);
5284           validate_pattern (pattern, &info, NULL_RTX, 0);
5285           match_pattern (&peephole2_root, &info, pattern, acceptance);
5286
5287           /* Declare the gen_peephole2 routine that we'll call if the
5288              pattern matches.  The definition comes from insn-emit.c.  */
5289           printf ("extern rtx_insn *gen_peephole2_%d (rtx_insn *, rtx *);\n",
5290                   info.index);
5291           break;
5292
5293         default:
5294           /* do nothing */;
5295         }
5296     }
5297
5298   if (have_error)
5299     return FATAL_EXIT_CODE;
5300
5301   puts ("\n\n");
5302
5303   /* Optimize each routine in turn.  */
5304   optimize_subroutine_group ("recog", &insn_root);
5305   optimize_subroutine_group ("split_insns", &split_root);
5306   optimize_subroutine_group ("peephole2_insns", &peephole2_root);
5307
5308   output_state os;
5309   os.id_to_var.safe_grow_cleared (num_positions);
5310
5311   if (use_pattern_routines_p)
5312     {
5313       /* Look for common patterns and split them out into subroutines.  */
5314       auto_vec <merge_state_info> states;
5315       states.safe_push (&insn_root);
5316       states.safe_push (&split_root);
5317       states.safe_push (&peephole2_root);
5318       split_out_patterns (states);
5319
5320       /* Print out the routines that we just created.  */
5321       unsigned int i;
5322       pattern_routine *routine;
5323       FOR_EACH_VEC_ELT (patterns, i, routine)
5324         print_pattern (&os, routine);
5325     }
5326
5327   /* Print out the matching routines.  */
5328   print_subroutine_group (&os, RECOG, &insn_root);
5329   print_subroutine_group (&os, SPLIT, &split_root);
5330   print_subroutine_group (&os, PEEPHOLE2, &peephole2_root);
5331
5332   fflush (stdout);
5333   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
5334 }