* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / genrecog.c
blobb2e233571b994f21459b41b157fa696596a584ed
1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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.
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).
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.
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.
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.
52 At a high level, the algorithm used in this file is as follows:
54 1. Build up a decision tree for each routine, using the following
55 approach to matching an rtx:
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.
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.
68 - Next check match_dups.
70 - Finally check the C condition and (where appropriate) pnum_clobbers.
72 2. Try to optimize the tree by removing redundant tests, CSEing tests,
73 folding tests together, etc.
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:
82 (plus:SI (match_operand:SI 1 "register_operand")
83 (match_operand:SI 2 "register_operand"))
85 we can split the associated matching code out into a subroutine.
86 If a pattern contains:
88 (minus:DI (match_operand:DI 1 "register_operand")
89 (match_operand:DI 2 "register_operand"))
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.
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.
99 4. Split the matching trees into functions, trying to limit the
100 size of each function to a sensible amount.
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.)
105 5. Write out C++ code for each function. */
107 #include "bconfig.h"
108 #include "system.h"
109 #include "coretypes.h"
110 #include "tm.h"
111 #include "rtl.h"
112 #include "errors.h"
113 #include "read-md.h"
114 #include "gensupport.h"
115 #include <algorithm>
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
124 #define NUM_TRUE_RTX_CODE ((int) FIRST_GENERATOR_RTX_CODE)
125 #define GENERATOR_FILE 1
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;
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;
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.
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;
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;
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;
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;
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;
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;
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;
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;
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;
195 /* The maximum operand number plus one. */
196 int num_operands;
198 /* Ways of obtaining an rtx to be tested. */
199 enum position_type {
200 /* PATTERN (peep2_next_insn (ARG)). */
201 POS_PEEP2_INSN,
203 /* XEXP (BASE, ARG). */
204 POS_XEXP,
206 /* XVECEXP (BASE, 0, ARG). */
207 POS_XVECEXP0
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
214 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */
215 struct position *base;
217 /* A position with the same BASE and TYPE, but with the next value
218 of ARG. */
219 struct position *next;
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;
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;
231 /* The type of position. */
232 enum position_type type;
234 /* The argument to TYPE (shown as ARG in the position_type comments). */
235 int arg;
237 /* The instruction to which the position belongs. */
238 unsigned int insn_id;
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;
245 /* A unique identifier for this position. */
246 unsigned int id;
249 enum routine_type {
250 SUBPATTERN, RECOG, SPLIT, PEEPHOLE2
253 /* Next number to use as an insn_code. */
254 static int next_insn_code;
256 /* The line number of the start of the pattern currently being processed. */
257 static int pattern_lineno;
259 /* The root position (x0). */
260 static struct position root_pos;
262 /* The number of positions created. Also one higher than the maximum
263 position id. */
264 static unsigned int num_positions = 1;
266 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position,
267 since we are given that instruction's pattern as x0. */
268 static struct position *peep2_insn_pos_list = &root_pos;
270 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR
271 points to where the unique object that represents the position
272 should be stored. Create the object if it doesn't already exist,
273 otherwise reuse the object that is already there. */
275 static struct position *
276 next_position (struct position **next_ptr, struct position *base,
277 enum position_type type, int arg)
279 struct position *pos;
281 pos = *next_ptr;
282 if (!pos)
284 pos = XCNEW (struct position);
285 pos->type = type;
286 pos->arg = arg;
287 if (type == POS_PEEP2_INSN)
289 pos->base = 0;
290 pos->insn_id = arg;
291 pos->depth = base->depth;
293 else
295 pos->base = base;
296 pos->insn_id = base->insn_id;
297 pos->depth = base->depth + 1;
299 pos->id = num_positions++;
300 *next_ptr = pos;
302 return pos;
305 /* Compare positions POS1 and POS2 lexicographically. */
307 static int
308 compare_positions (struct position *pos1, struct position *pos2)
310 int diff;
312 diff = pos1->depth - pos2->depth;
313 if (diff < 0)
315 pos2 = pos2->base;
316 while (pos1->depth != pos2->depth);
317 else if (diff > 0)
319 pos1 = pos1->base;
320 while (pos1->depth != pos2->depth);
321 while (pos1 != pos2)
323 diff = (int) pos1->type - (int) pos2->type;
324 if (diff == 0)
325 diff = pos1->arg - pos2->arg;
326 pos1 = pos1->base;
327 pos2 = pos2->base;
329 return diff;
332 /* Return the most deeply-nested position that is common to both
333 POS1 and POS2. If the positions are from different instructions,
334 return the one with the lowest insn_id. */
336 static struct position *
337 common_position (struct position *pos1, struct position *pos2)
339 if (pos1->insn_id != pos2->insn_id)
340 return pos1->insn_id < pos2->insn_id ? pos1 : pos2;
341 if (pos1->depth > pos2->depth)
342 std::swap (pos1, pos2);
343 while (pos1->depth != pos2->depth)
344 pos2 = pos2->base;
345 while (pos1 != pos2)
347 pos1 = pos1->base;
348 pos2 = pos2->base;
350 return pos1;
353 /* Search for and return operand N, stop when reaching node STOP. */
355 static rtx
356 find_operand (rtx pattern, int n, rtx stop)
358 const char *fmt;
359 RTX_CODE code;
360 int i, j, len;
361 rtx r;
363 if (pattern == stop)
364 return stop;
366 code = GET_CODE (pattern);
367 if ((code == MATCH_SCRATCH
368 || code == MATCH_OPERAND
369 || code == MATCH_OPERATOR
370 || code == MATCH_PARALLEL)
371 && XINT (pattern, 0) == n)
372 return pattern;
374 fmt = GET_RTX_FORMAT (code);
375 len = GET_RTX_LENGTH (code);
376 for (i = 0; i < len; i++)
378 switch (fmt[i])
380 case 'e': case 'u':
381 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
382 return r;
383 break;
385 case 'V':
386 if (! XVEC (pattern, i))
387 break;
388 /* Fall through. */
390 case 'E':
391 for (j = 0; j < XVECLEN (pattern, i); j++)
392 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
393 != NULL_RTX)
394 return r;
395 break;
397 case 'i': case 'r': case 'w': case '0': case 's':
398 break;
400 default:
401 gcc_unreachable ();
405 return NULL;
408 /* Search for and return operand M, such that it has a matching
409 constraint for operand N. */
411 static rtx
412 find_matching_operand (rtx pattern, int n)
414 const char *fmt;
415 RTX_CODE code;
416 int i, j, len;
417 rtx r;
419 code = GET_CODE (pattern);
420 if (code == MATCH_OPERAND
421 && (XSTR (pattern, 2)[0] == '0' + n
422 || (XSTR (pattern, 2)[0] == '%'
423 && XSTR (pattern, 2)[1] == '0' + n)))
424 return pattern;
426 fmt = GET_RTX_FORMAT (code);
427 len = GET_RTX_LENGTH (code);
428 for (i = 0; i < len; i++)
430 switch (fmt[i])
432 case 'e': case 'u':
433 if ((r = find_matching_operand (XEXP (pattern, i), n)))
434 return r;
435 break;
437 case 'V':
438 if (! XVEC (pattern, i))
439 break;
440 /* Fall through. */
442 case 'E':
443 for (j = 0; j < XVECLEN (pattern, i); j++)
444 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
445 return r;
446 break;
448 case 'i': case 'r': case 'w': case '0': case 's':
449 break;
451 default:
452 gcc_unreachable ();
456 return NULL;
459 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
460 don't use the MATCH_OPERAND constraint, only the predicate.
461 This is confusing to folks doing new ports, so help them
462 not make the mistake. */
464 static bool
465 constraints_supported_in_insn_p (rtx insn)
467 return !(GET_CODE (insn) == DEFINE_EXPAND
468 || GET_CODE (insn) == DEFINE_SPLIT
469 || GET_CODE (insn) == DEFINE_PEEPHOLE2);
472 /* Check for various errors in patterns. SET is nonnull for a destination,
473 and is the complete set pattern. SET_CODE is '=' for normal sets, and
474 '+' within a context that requires in-out constraints. */
476 static void
477 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
479 const char *fmt;
480 RTX_CODE code;
481 size_t i, len;
482 int j;
484 code = GET_CODE (pattern);
485 switch (code)
487 case MATCH_SCRATCH:
489 const char constraints0 = XSTR (pattern, 1)[0];
491 if (!constraints_supported_in_insn_p (insn))
493 if (constraints0)
495 error_with_line (pattern_lineno,
496 "constraints not supported in %s",
497 rtx_name[GET_CODE (insn)]);
499 return;
502 /* If a MATCH_SCRATCH is used in a context requiring an write-only
503 or read/write register, validate that. */
504 if (set_code == '='
505 && constraints0
506 && constraints0 != '='
507 && constraints0 != '+')
509 error_with_line (pattern_lineno,
510 "operand %d missing output reload",
511 XINT (pattern, 0));
513 return;
515 case MATCH_DUP:
516 case MATCH_OP_DUP:
517 case MATCH_PAR_DUP:
518 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern)
519 error_with_line (pattern_lineno,
520 "operand %i duplicated before defined",
521 XINT (pattern, 0));
522 break;
523 case MATCH_OPERAND:
524 case MATCH_OPERATOR:
526 const char *pred_name = XSTR (pattern, 1);
527 const struct pred_data *pred;
528 const char *c_test;
530 if (GET_CODE (insn) == DEFINE_INSN)
531 c_test = XSTR (insn, 2);
532 else
533 c_test = XSTR (insn, 1);
535 if (pred_name[0] != 0)
537 pred = lookup_predicate (pred_name);
538 if (!pred)
539 error_with_line (pattern_lineno, "unknown predicate '%s'",
540 pred_name);
542 else
543 pred = 0;
545 if (code == MATCH_OPERAND)
547 const char *constraints = XSTR (pattern, 2);
548 const char constraints0 = constraints[0];
550 if (!constraints_supported_in_insn_p (insn))
552 if (constraints0)
554 error_with_line (pattern_lineno,
555 "constraints not supported in %s",
556 rtx_name[GET_CODE (insn)]);
560 /* A MATCH_OPERAND that is a SET should have an output reload. */
561 else if (set && constraints0)
563 if (set_code == '+')
565 if (constraints0 == '+')
567 /* If we've only got an output reload for this operand,
568 we'd better have a matching input operand. */
569 else if (constraints0 == '='
570 && find_matching_operand (insn, XINT (pattern, 0)))
572 else
573 error_with_line (pattern_lineno,
574 "operand %d missing in-out reload",
575 XINT (pattern, 0));
577 else if (constraints0 != '=' && constraints0 != '+')
578 error_with_line (pattern_lineno,
579 "operand %d missing output reload",
580 XINT (pattern, 0));
583 /* For matching constraint in MATCH_OPERAND, the digit must be a
584 smaller number than the number of the operand that uses it in the
585 constraint. */
586 while (1)
588 while (constraints[0]
589 && (constraints[0] == ' ' || constraints[0] == ','))
590 constraints++;
591 if (!constraints[0])
592 break;
594 if (constraints[0] >= '0' && constraints[0] <= '9')
596 int val;
598 sscanf (constraints, "%d", &val);
599 if (val >= XINT (pattern, 0))
600 error_with_line (pattern_lineno,
601 "constraint digit %d is not smaller than"
602 " operand %d",
603 val, XINT (pattern, 0));
606 while (constraints[0] && constraints[0] != ',')
607 constraints++;
611 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
612 while not likely to occur at runtime, results in less efficient
613 code from insn-recog.c. */
614 if (set && pred && pred->allows_non_lvalue)
615 error_with_line (pattern_lineno,
616 "destination operand %d allows non-lvalue",
617 XINT (pattern, 0));
619 /* A modeless MATCH_OPERAND can be handy when we can check for
620 multiple modes in the c_test. In most other cases, it is a
621 mistake. Only DEFINE_INSN is eligible, since SPLIT and
622 PEEP2 can FAIL within the output pattern. Exclude special
623 predicates, which check the mode themselves. Also exclude
624 predicates that allow only constants. Exclude the SET_DEST
625 of a call instruction, as that is a common idiom. */
627 if (GET_MODE (pattern) == VOIDmode
628 && code == MATCH_OPERAND
629 && GET_CODE (insn) == DEFINE_INSN
630 && pred
631 && !pred->special
632 && pred->allows_non_const
633 && strstr (c_test, "operands") == NULL
634 && ! (set
635 && GET_CODE (set) == SET
636 && GET_CODE (SET_SRC (set)) == CALL))
637 message_with_line (pattern_lineno,
638 "warning: operand %d missing mode?",
639 XINT (pattern, 0));
640 return;
643 case SET:
645 machine_mode dmode, smode;
646 rtx dest, src;
648 dest = SET_DEST (pattern);
649 src = SET_SRC (pattern);
651 /* STRICT_LOW_PART is a wrapper. Its argument is the real
652 destination, and it's mode should match the source. */
653 if (GET_CODE (dest) == STRICT_LOW_PART)
654 dest = XEXP (dest, 0);
656 /* Find the referent for a DUP. */
658 if (GET_CODE (dest) == MATCH_DUP
659 || GET_CODE (dest) == MATCH_OP_DUP
660 || GET_CODE (dest) == MATCH_PAR_DUP)
661 dest = find_operand (insn, XINT (dest, 0), NULL);
663 if (GET_CODE (src) == MATCH_DUP
664 || GET_CODE (src) == MATCH_OP_DUP
665 || GET_CODE (src) == MATCH_PAR_DUP)
666 src = find_operand (insn, XINT (src, 0), NULL);
668 dmode = GET_MODE (dest);
669 smode = GET_MODE (src);
671 /* The mode of an ADDRESS_OPERAND is the mode of the memory
672 reference, not the mode of the address. */
673 if (GET_CODE (src) == MATCH_OPERAND
674 && ! strcmp (XSTR (src, 1), "address_operand"))
677 /* The operands of a SET must have the same mode unless one
678 is VOIDmode. */
679 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
680 error_with_line (pattern_lineno,
681 "mode mismatch in set: %smode vs %smode",
682 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
684 /* If only one of the operands is VOIDmode, and PC or CC0 is
685 not involved, it's probably a mistake. */
686 else if (dmode != smode
687 && GET_CODE (dest) != PC
688 && GET_CODE (dest) != CC0
689 && GET_CODE (src) != PC
690 && GET_CODE (src) != CC0
691 && !CONST_INT_P (src)
692 && !CONST_WIDE_INT_P (src)
693 && GET_CODE (src) != CALL)
695 const char *which;
696 which = (dmode == VOIDmode ? "destination" : "source");
697 message_with_line (pattern_lineno,
698 "warning: %s missing a mode?", which);
701 if (dest != SET_DEST (pattern))
702 validate_pattern (dest, insn, pattern, '=');
703 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
704 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
705 return;
708 case CLOBBER:
709 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
710 return;
712 case ZERO_EXTRACT:
713 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
714 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
715 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
716 return;
718 case STRICT_LOW_PART:
719 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
720 return;
722 case LABEL_REF:
723 if (GET_MODE (LABEL_REF_LABEL (pattern)) != VOIDmode)
724 error_with_line (pattern_lineno,
725 "operand to label_ref %smode not VOIDmode",
726 GET_MODE_NAME (GET_MODE (LABEL_REF_LABEL (pattern))));
727 break;
729 default:
730 break;
733 fmt = GET_RTX_FORMAT (code);
734 len = GET_RTX_LENGTH (code);
735 for (i = 0; i < len; i++)
737 switch (fmt[i])
739 case 'e': case 'u':
740 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
741 break;
743 case 'E':
744 for (j = 0; j < XVECLEN (pattern, i); j++)
745 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
746 break;
748 case 'i': case 'r': case 'w': case '0': case 's':
749 break;
751 default:
752 gcc_unreachable ();
757 /* Simple list structure for items of type T, for use when being part
758 of a list is an inherent property of T. T must have members equivalent
759 to "T *prev, *next;" and a function "void set_parent (list_head <T> *)"
760 to set the parent list. */
761 template <typename T>
762 struct list_head
764 /* A range of linked items. */
765 struct range
767 range (T *);
768 range (T *, T *);
770 T *start, *end;
771 void set_parent (list_head *);
774 list_head ();
775 range release ();
776 void push_back (range);
777 range remove (range);
778 void replace (range, range);
779 T *singleton () const;
781 T *first, *last;
784 /* Create a range [START_IN, START_IN]. */
786 template <typename T>
787 list_head <T>::range::range (T *start_in) : start (start_in), end (start_in) {}
789 /* Create a range [START_IN, END_IN], linked by next and prev fields. */
791 template <typename T>
792 list_head <T>::range::range (T *start_in, T *end_in)
793 : start (start_in), end (end_in) {}
795 template <typename T>
796 void
797 list_head <T>::range::set_parent (list_head <T> *owner)
799 for (T *item = start; item != end; item = item->next)
800 item->set_parent (owner);
801 end->set_parent (owner);
804 template <typename T>
805 list_head <T>::list_head () : first (0), last (0) {}
807 /* Add R to the end of the list. */
809 template <typename T>
810 void
811 list_head <T>::push_back (range r)
813 if (last)
814 last->next = r.start;
815 else
816 first = r.start;
817 r.start->prev = last;
818 last = r.end;
819 r.set_parent (this);
822 /* Remove R from the list. R remains valid and can be inserted into
823 other lists. */
825 template <typename T>
826 typename list_head <T>::range
827 list_head <T>::remove (range r)
829 if (r.start->prev)
830 r.start->prev->next = r.end->next;
831 else
832 first = r.end->next;
833 if (r.end->next)
834 r.end->next->prev = r.start->prev;
835 else
836 last = r.start->prev;
837 r.start->prev = 0;
838 r.end->next = 0;
839 r.set_parent (0);
840 return r;
843 /* Replace OLDR with NEWR. OLDR remains valid and can be inserted into
844 other lists. */
846 template <typename T>
847 void
848 list_head <T>::replace (range oldr, range newr)
850 newr.start->prev = oldr.start->prev;
851 newr.end->next = oldr.end->next;
853 oldr.start->prev = 0;
854 oldr.end->next = 0;
855 oldr.set_parent (0);
857 if (newr.start->prev)
858 newr.start->prev->next = newr.start;
859 else
860 first = newr.start;
861 if (newr.end->next)
862 newr.end->next->prev = newr.end;
863 else
864 last = newr.end;
865 newr.set_parent (this);
868 /* Empty the list and return the previous contents as a range that can
869 be inserted into other lists. */
871 template <typename T>
872 typename list_head <T>::range
873 list_head <T>::release ()
875 range r (first, last);
876 first = 0;
877 last = 0;
878 r.set_parent (0);
879 return r;
882 /* If the list contains a single item, return that item, otherwise return
883 null. */
885 template <typename T>
887 list_head <T>::singleton () const
889 return first == last ? first : 0;
892 struct state;
894 /* Describes a possible successful return from a routine. */
895 struct acceptance_type
897 /* The type of routine we're returning from. */
898 routine_type type : 16;
900 /* True if this structure only really represents a partial match,
901 and if we must call a subroutine of type TYPE to complete the match.
902 In this case we'll call the subroutine and, if it succeeds, return
903 whatever the subroutine returned.
905 False if this structure presents a full match. */
906 unsigned int partial_p : 1;
908 union
910 /* If PARTIAL_P, this is the number of the subroutine to call. */
911 int subroutine_id;
913 /* Valid if !PARTIAL_P. */
914 struct
916 /* The identifier of the matching pattern. For SUBPATTERNs this
917 value belongs to an ad-hoc routine-specific enum. For the
918 others it's the number of an .md file pattern. */
919 int code;
920 union
922 /* For RECOG, the number of clobbers that must be added to the
923 pattern in order for it to match CODE. */
924 int num_clobbers;
926 /* For PEEPHOLE2, the number of additional instructions that were
927 included in the optimization. */
928 int match_len;
929 } u;
930 } full;
931 } u;
934 bool
935 operator == (const acceptance_type &a, const acceptance_type &b)
937 if (a.partial_p != b.partial_p)
938 return false;
939 if (a.partial_p)
940 return a.u.subroutine_id == b.u.subroutine_id;
941 else
942 return a.u.full.code == b.u.full.code;
945 bool
946 operator != (const acceptance_type &a, const acceptance_type &b)
948 return !operator == (a, b);
951 /* Represents a parameter to a pattern routine. */
952 struct parameter
954 /* The C type of parameter. */
955 enum type_enum {
956 /* Represents an invalid parameter. */
957 UNSET,
959 /* A machine_mode parameter. */
960 MODE,
962 /* An rtx_code parameter. */
963 CODE,
965 /* An int parameter. */
966 INT,
968 /* An unsigned int parameter. */
969 UINT,
971 /* A HOST_WIDE_INT parameter. */
972 WIDE_INT
975 parameter ();
976 parameter (type_enum, bool, uint64_t);
978 /* The type of the parameter. */
979 type_enum type;
981 /* True if the value passed is variable, false if it is constant. */
982 bool is_param;
984 /* If IS_PARAM, this is the number of the variable passed, for an "i%d"
985 format string. If !IS_PARAM, this is the constant value passed. */
986 uint64_t value;
989 parameter::parameter ()
990 : type (UNSET), is_param (false), value (0) {}
992 parameter::parameter (type_enum type_in, bool is_param_in, uint64_t value_in)
993 : type (type_in), is_param (is_param_in), value (value_in) {}
995 bool
996 operator == (const parameter &param1, const parameter &param2)
998 return (param1.type == param2.type
999 && param1.is_param == param2.is_param
1000 && param1.value == param2.value);
1003 bool
1004 operator != (const parameter &param1, const parameter &param2)
1006 return !operator == (param1, param2);
1009 /* Represents a routine that matches a partial rtx pattern, returning
1010 an ad-hoc enum value on success and -1 on failure. The routine can
1011 be used by any subroutine type. The match can be parameterized by
1012 things like mode, code and UNSPEC number. */
1013 struct pattern_routine
1015 /* The state that implements the pattern. */
1016 state *s;
1018 /* The deepest root position from which S can access all the rtxes it needs.
1019 This is NULL if the pattern doesn't need an rtx input, usually because
1020 all matching is done on operands[] instead. */
1021 position *pos;
1023 /* A unique identifier for the routine. */
1024 unsigned int pattern_id;
1026 /* True if the routine takes pnum_clobbers as argument. */
1027 bool pnum_clobbers_p;
1029 /* True if the routine takes the enclosing instruction as argument. */
1030 bool insn_p;
1032 /* The types of the other parameters to the routine, if any. */
1033 auto_vec <parameter::type_enum, MAX_PATTERN_PARAMS> param_types;
1036 /* All defined patterns. */
1037 static vec <pattern_routine *> patterns;
1039 /* Represents one use of a pattern routine. */
1040 struct pattern_use
1042 /* The pattern routine to use. */
1043 pattern_routine *routine;
1045 /* The values to pass as parameters. This vector has the same length
1046 as ROUTINE->PARAM_TYPES. */
1047 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
1050 /* Represents a test performed by a decision. */
1051 struct rtx_test
1053 rtx_test ();
1055 /* The types of test that can be performed. Most of them take as input
1056 an rtx X. Some also take as input a transition label LABEL; the others
1057 are booleans for which the transition label is always "true".
1059 The order of the enum isn't important. */
1060 enum kind_enum {
1061 /* Check GET_CODE (X) == LABEL. */
1062 CODE,
1064 /* Check GET_MODE (X) == LABEL. */
1065 MODE,
1067 /* Check REGNO (X) == LABEL. */
1068 REGNO_FIELD,
1070 /* Check XINT (X, u.opno) == LABEL. */
1071 INT_FIELD,
1073 /* Check XWINT (X, u.opno) == LABEL. */
1074 WIDE_INT_FIELD,
1076 /* Check XVECLEN (X, 0) == LABEL. */
1077 VECLEN,
1079 /* Check peep2_current_count >= u.min_len. */
1080 PEEP2_COUNT,
1082 /* Check XVECLEN (X, 0) >= u.min_len. */
1083 VECLEN_GE,
1085 /* Check whether X is a cached const_int with value u.integer. */
1086 SAVED_CONST_INT,
1088 /* Check u.predicate.data (X, u.predicate.mode). */
1089 PREDICATE,
1091 /* Check rtx_equal_p (X, operands[u.opno]). */
1092 DUPLICATE,
1094 /* Check whether X matches pattern u.pattern. */
1095 PATTERN,
1097 /* Check whether pnum_clobbers is nonnull (RECOG only). */
1098 HAVE_NUM_CLOBBERS,
1100 /* Check whether general C test u.string holds. In general the condition
1101 needs access to "insn" and the full operand list. */
1102 C_TEST,
1104 /* Execute operands[u.opno] = X. (Always succeeds.) */
1105 SET_OP,
1107 /* Accept u.acceptance. Always succeeds for SUBPATTERN, RECOG and SPLIT.
1108 May fail for PEEPHOLE2 if the define_peephole2 C code executes FAIL. */
1109 ACCEPT
1112 /* The position of rtx X in the above description, relative to the
1113 incoming instruction "insn". The position is null if the test
1114 doesn't take an X as input. */
1115 position *pos;
1117 /* Which element of operands[] already contains POS, or -1 if no element
1118 is known to hold POS. */
1119 int pos_operand;
1121 /* The type of test and its parameters, as described above. */
1122 kind_enum kind;
1123 union
1125 int opno;
1126 int min_len;
1127 struct
1129 bool is_param;
1130 int value;
1131 } integer;
1132 struct
1134 const struct pred_data *data;
1135 /* True if the mode is taken from a machine_mode parameter
1136 to the routine rather than a constant machine_mode. If true,
1137 MODE is the number of the parameter (for an "i%d" format string),
1138 otherwise it is the mode itself. */
1139 bool mode_is_param;
1140 unsigned int mode;
1141 } predicate;
1142 pattern_use *pattern;
1143 const char *string;
1144 acceptance_type acceptance;
1145 } u;
1147 static rtx_test code (position *);
1148 static rtx_test mode (position *);
1149 static rtx_test regno_field (position *);
1150 static rtx_test int_field (position *, int);
1151 static rtx_test wide_int_field (position *, int);
1152 static rtx_test veclen (position *);
1153 static rtx_test peep2_count (int);
1154 static rtx_test veclen_ge (position *, int);
1155 static rtx_test predicate (position *, const pred_data *, machine_mode);
1156 static rtx_test duplicate (position *, int);
1157 static rtx_test pattern (position *, pattern_use *);
1158 static rtx_test have_num_clobbers ();
1159 static rtx_test c_test (const char *);
1160 static rtx_test set_op (position *, int);
1161 static rtx_test accept (const acceptance_type &);
1163 bool terminal_p () const;
1164 bool single_outcome_p () const;
1166 private:
1167 rtx_test (position *, kind_enum);
1170 rtx_test::rtx_test () {}
1172 rtx_test::rtx_test (position *pos_in, kind_enum kind_in)
1173 : pos (pos_in), pos_operand (-1), kind (kind_in) {}
1175 rtx_test
1176 rtx_test::code (position *pos)
1178 return rtx_test (pos, rtx_test::CODE);
1181 rtx_test
1182 rtx_test::mode (position *pos)
1184 return rtx_test (pos, rtx_test::MODE);
1187 rtx_test
1188 rtx_test::regno_field (position *pos)
1190 rtx_test res (pos, rtx_test::REGNO_FIELD);
1191 return res;
1194 rtx_test
1195 rtx_test::int_field (position *pos, int opno)
1197 rtx_test res (pos, rtx_test::INT_FIELD);
1198 res.u.opno = opno;
1199 return res;
1202 rtx_test
1203 rtx_test::wide_int_field (position *pos, int opno)
1205 rtx_test res (pos, rtx_test::WIDE_INT_FIELD);
1206 res.u.opno = opno;
1207 return res;
1210 rtx_test
1211 rtx_test::veclen (position *pos)
1213 return rtx_test (pos, rtx_test::VECLEN);
1216 rtx_test
1217 rtx_test::peep2_count (int min_len)
1219 rtx_test res (0, rtx_test::PEEP2_COUNT);
1220 res.u.min_len = min_len;
1221 return res;
1224 rtx_test
1225 rtx_test::veclen_ge (position *pos, int min_len)
1227 rtx_test res (pos, rtx_test::VECLEN_GE);
1228 res.u.min_len = min_len;
1229 return res;
1232 rtx_test
1233 rtx_test::predicate (position *pos, const struct pred_data *data,
1234 machine_mode mode)
1236 rtx_test res (pos, rtx_test::PREDICATE);
1237 res.u.predicate.data = data;
1238 res.u.predicate.mode_is_param = false;
1239 res.u.predicate.mode = mode;
1240 return res;
1243 rtx_test
1244 rtx_test::duplicate (position *pos, int opno)
1246 rtx_test res (pos, rtx_test::DUPLICATE);
1247 res.u.opno = opno;
1248 return res;
1251 rtx_test
1252 rtx_test::pattern (position *pos, pattern_use *pattern)
1254 rtx_test res (pos, rtx_test::PATTERN);
1255 res.u.pattern = pattern;
1256 return res;
1259 rtx_test
1260 rtx_test::have_num_clobbers ()
1262 return rtx_test (0, rtx_test::HAVE_NUM_CLOBBERS);
1265 rtx_test
1266 rtx_test::c_test (const char *string)
1268 rtx_test res (0, rtx_test::C_TEST);
1269 res.u.string = string;
1270 return res;
1273 rtx_test
1274 rtx_test::set_op (position *pos, int opno)
1276 rtx_test res (pos, rtx_test::SET_OP);
1277 res.u.opno = opno;
1278 return res;
1281 rtx_test
1282 rtx_test::accept (const acceptance_type &acceptance)
1284 rtx_test res (0, rtx_test::ACCEPT);
1285 res.u.acceptance = acceptance;
1286 return res;
1289 /* Return true if the test represents an unconditionally successful match. */
1291 bool
1292 rtx_test::terminal_p () const
1294 return kind == rtx_test::ACCEPT && u.acceptance.type != PEEPHOLE2;
1297 /* Return true if the test is a boolean that is always true. */
1299 bool
1300 rtx_test::single_outcome_p () const
1302 return terminal_p () || kind == rtx_test::SET_OP;
1305 bool
1306 operator == (const rtx_test &a, const rtx_test &b)
1308 if (a.pos != b.pos || a.kind != b.kind)
1309 return false;
1310 switch (a.kind)
1312 case rtx_test::CODE:
1313 case rtx_test::MODE:
1314 case rtx_test::REGNO_FIELD:
1315 case rtx_test::VECLEN:
1316 case rtx_test::HAVE_NUM_CLOBBERS:
1317 return true;
1319 case rtx_test::PEEP2_COUNT:
1320 case rtx_test::VECLEN_GE:
1321 return a.u.min_len == b.u.min_len;
1323 case rtx_test::INT_FIELD:
1324 case rtx_test::WIDE_INT_FIELD:
1325 case rtx_test::DUPLICATE:
1326 case rtx_test::SET_OP:
1327 return a.u.opno == b.u.opno;
1329 case rtx_test::SAVED_CONST_INT:
1330 return (a.u.integer.is_param == b.u.integer.is_param
1331 && a.u.integer.value == b.u.integer.value);
1333 case rtx_test::PREDICATE:
1334 return (a.u.predicate.data == b.u.predicate.data
1335 && a.u.predicate.mode_is_param == b.u.predicate.mode_is_param
1336 && a.u.predicate.mode == b.u.predicate.mode);
1338 case rtx_test::PATTERN:
1339 return (a.u.pattern->routine == b.u.pattern->routine
1340 && a.u.pattern->params == b.u.pattern->params);
1342 case rtx_test::C_TEST:
1343 return strcmp (a.u.string, b.u.string) == 0;
1345 case rtx_test::ACCEPT:
1346 return a.u.acceptance == b.u.acceptance;
1348 gcc_unreachable ();
1351 bool
1352 operator != (const rtx_test &a, const rtx_test &b)
1354 return !operator == (a, b);
1357 /* A simple set of transition labels. Most transitions have a singleton
1358 label, so try to make that case as efficient as possible. */
1359 struct int_set : public auto_vec <uint64_t, 1>
1361 typedef uint64_t *iterator;
1363 int_set ();
1364 int_set (uint64_t);
1365 int_set (const int_set &);
1367 int_set &operator = (const int_set &);
1369 iterator begin ();
1370 iterator end ();
1373 int_set::int_set () {}
1375 int_set::int_set (uint64_t label)
1377 safe_push (label);
1380 int_set::int_set (const int_set &other)
1382 safe_splice (other);
1385 int_set &
1386 int_set::operator = (const int_set &other)
1388 truncate (0);
1389 safe_splice (other);
1390 return *this;
1393 int_set::iterator
1394 int_set::begin ()
1396 return address ();
1399 int_set::iterator
1400 int_set::end ()
1402 return address () + length ();
1405 bool
1406 operator == (const int_set &a, const int_set &b)
1408 if (a.length () != b.length ())
1409 return false;
1410 for (unsigned int i = 0; i < a.length (); ++i)
1411 if (a[i] != b[i])
1412 return false;
1413 return true;
1416 bool
1417 operator != (const int_set &a, const int_set &b)
1419 return !operator == (a, b);
1422 struct decision;
1424 /* Represents a transition between states, dependent on the result of
1425 a test T. */
1426 struct transition
1428 transition (const int_set &, state *, bool);
1430 void set_parent (list_head <transition> *);
1432 /* Links to other transitions for T. Always null for boolean tests. */
1433 transition *prev, *next;
1435 /* The transition should be taken when T has one of these values.
1436 E.g. for rtx_test::CODE this is a set of codes, while for booleans like
1437 rtx_test::PREDICATE it is always a singleton "true". The labels are
1438 sorted in ascending order. */
1439 int_set labels;
1441 /* The source decision. */
1442 decision *from;
1444 /* The target state. */
1445 state *to;
1447 /* True if TO would function correctly even if TEST wasn't performed.
1448 E.g. it isn't necessary to check whether GET_MODE (x1) is SImode
1449 before calling register_operand (x1, SImode), since register_operand
1450 performs its own mode check. However, checking GET_MODE can be a cheap
1451 way of disambiguating SImode and DImode register operands. */
1452 bool optional;
1454 /* True if LABELS contains parameter numbers rather than constants.
1455 E.g. if this is true for a rtx_test::CODE, the label is the number
1456 of an rtx_code parameter rather than an rtx_code itself.
1457 LABELS is always a singleton when this variable is true. */
1458 bool is_param;
1461 /* Represents a test and the action that should be taken on the result.
1462 If a transition exists for the test outcome, the machine switches
1463 to the transition's target state. If no suitable transition exists,
1464 the machine either falls through to the next decision or, if there are no
1465 more decisions to try, fails the match. */
1466 struct decision : list_head <transition>
1468 decision (const rtx_test &);
1470 void set_parent (list_head <decision> *s);
1471 bool if_statement_p (uint64_t * = 0) const;
1473 /* The state to which this decision belongs. */
1474 state *s;
1476 /* Links to other decisions in the same state. */
1477 decision *prev, *next;
1479 /* The test to perform. */
1480 rtx_test test;
1483 /* Represents one machine state. For each state the machine tries a list
1484 of decisions, in order, and acts on the first match. It fails without
1485 further backtracking if no decisions match. */
1486 struct state : list_head <decision>
1488 void set_parent (list_head <state> *) {}
1491 transition::transition (const int_set &labels_in, state *to_in,
1492 bool optional_in)
1493 : prev (0), next (0), labels (labels_in), from (0), to (to_in),
1494 optional (optional_in), is_param (false) {}
1496 /* Set the source decision of the transition. */
1498 void
1499 transition::set_parent (list_head <transition> *from_in)
1501 from = static_cast <decision *> (from_in);
1504 decision::decision (const rtx_test &test_in)
1505 : prev (0), next (0), test (test_in) {}
1507 /* Set the state to which this decision belongs. */
1509 void
1510 decision::set_parent (list_head <decision> *s_in)
1512 s = static_cast <state *> (s_in);
1515 /* Return true if the decision has a single transition with a single label.
1516 If so, return the label in *LABEL if nonnull. */
1518 inline bool
1519 decision::if_statement_p (uint64_t *label) const
1521 if (singleton () && first->labels.length () == 1)
1523 if (label)
1524 *label = first->labels[0];
1525 return true;
1527 return false;
1530 /* Add to FROM a decision that performs TEST and has a single transition
1531 TRANS. */
1533 static void
1534 add_decision (state *from, const rtx_test &test, transition *trans)
1536 decision *d = new decision (test);
1537 from->push_back (d);
1538 d->push_back (trans);
1541 /* Add a transition from FROM to a new, empty state that is taken
1542 when TEST == LABELS. OPTIONAL says whether the new transition
1543 should be optional. Return the new state. */
1545 static state *
1546 add_decision (state *from, const rtx_test &test, int_set labels, bool optional)
1548 state *to = new state;
1549 add_decision (from, test, new transition (labels, to, optional));
1550 return to;
1553 /* Insert a decision before decisions R to make them dependent on
1554 TEST == LABELS. OPTIONAL says whether the new transition should be
1555 optional. */
1557 static decision *
1558 insert_decision_before (state::range r, const rtx_test &test,
1559 const int_set &labels, bool optional)
1561 decision *newd = new decision (test);
1562 state *news = new state;
1563 newd->push_back (new transition (labels, news, optional));
1564 r.start->s->replace (r, newd);
1565 news->push_back (r);
1566 return newd;
1569 /* Remove any optional transitions from S that turned out not to be useful. */
1571 static void
1572 collapse_optional_decisions (state *s)
1574 decision *d = s->first;
1575 while (d)
1577 decision *next = d->next;
1578 for (transition *trans = d->first; trans; trans = trans->next)
1579 collapse_optional_decisions (trans->to);
1580 /* A decision with a single optional transition doesn't help
1581 partition the potential matches and so is unlikely to be
1582 worthwhile. In particular, if the decision that performs the
1583 test is the last in the state, the best it could do is reject
1584 an invalid pattern slightly earlier. If instead the decision
1585 is not the last in the state, the condition it tests could hold
1586 even for the later decisions in the state. The best it can do
1587 is save work in some cases where only the later decisions can
1588 succeed.
1590 In both cases the optional transition would add extra work to
1591 successful matches when the tested condition holds. */
1592 if (transition *trans = d->singleton ())
1593 if (trans->optional)
1594 s->replace (d, trans->to->release ());
1595 d = next;
1599 /* Try to squash several separate tests into simpler ones. */
1601 static void
1602 simplify_tests (state *s)
1604 for (decision *d = s->first; d; d = d->next)
1606 uint64_t label;
1607 /* Convert checks for GET_CODE (x) == CONST_INT and XWINT (x, 0) == N
1608 into checks for const_int_rtx[N'], if N is suitably small. */
1609 if (d->test.kind == rtx_test::CODE
1610 && d->if_statement_p (&label)
1611 && label == CONST_INT)
1612 if (decision *second = d->first->to->singleton ())
1613 if (d->test.pos == second->test.pos
1614 && second->test.kind == rtx_test::WIDE_INT_FIELD
1615 && second->test.u.opno == 0
1616 && second->if_statement_p (&label)
1617 && IN_RANGE (int64_t (label),
1618 -MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT))
1620 d->test.kind = rtx_test::SAVED_CONST_INT;
1621 d->test.u.integer.is_param = false;
1622 d->test.u.integer.value = label;
1623 d->replace (d->first, second->release ());
1624 d->first->labels[0] = true;
1626 /* If we have a CODE test followed by a PREDICATE test, rely on
1627 the predicate to test the code.
1629 This case exists for match_operators. We initially treat the
1630 CODE test for a match_operator as non-optional so that we can
1631 safely move down to its operands. It may turn out that all
1632 paths that reach that code test require the same predicate
1633 to be true. cse_tests will then put the predicate test in
1634 series with the code test. */
1635 if (d->test.kind == rtx_test::CODE)
1636 if (transition *trans = d->singleton ())
1638 state *s = trans->to;
1639 while (decision *d2 = s->singleton ())
1641 if (d->test.pos != d2->test.pos)
1642 break;
1643 transition *trans2 = d2->singleton ();
1644 if (!trans2)
1645 break;
1646 if (d2->test.kind == rtx_test::PREDICATE)
1648 d->test = d2->test;
1649 trans->labels = int_set (true);
1650 s->replace (d2, trans2->to->release ());
1651 break;
1653 s = trans2->to;
1656 for (transition *trans = d->first; trans; trans = trans->next)
1657 simplify_tests (trans->to);
1661 /* Return true if all successful returns passing through D require the
1662 condition tested by COMMON to be true.
1664 When returning true, add all transitions like COMMON in D to WHERE.
1665 WHERE may contain a partial result on failure. */
1667 static bool
1668 common_test_p (decision *d, transition *common, vec <transition *> *where)
1670 if (d->test.kind == rtx_test::ACCEPT)
1671 /* We found a successful return that didn't require COMMON. */
1672 return false;
1673 if (d->test == common->from->test)
1675 transition *trans = d->singleton ();
1676 if (!trans
1677 || trans->optional != common->optional
1678 || trans->labels != common->labels)
1679 return false;
1680 where->safe_push (trans);
1681 return true;
1683 for (transition *trans = d->first; trans; trans = trans->next)
1684 for (decision *subd = trans->to->first; subd; subd = subd->next)
1685 if (!common_test_p (subd, common, where))
1686 return false;
1687 return true;
1690 /* Indicates that we have tested GET_CODE (X) for a particular rtx X. */
1691 const unsigned char TESTED_CODE = 1;
1693 /* Indicates that we have tested XVECLEN (X, 0) for a particular rtx X. */
1694 const unsigned char TESTED_VECLEN = 2;
1696 /* Represents a set of conditions that are known to hold. */
1697 struct known_conditions
1699 /* A mask of TESTED_ values for each position, indexed by the position's
1700 id field. */
1701 auto_vec <unsigned char> position_tests;
1703 /* Index N says whether operands[N] has been set. */
1704 auto_vec <bool> set_operands;
1706 /* A guranteed lower bound on the value of peep2_current_count. */
1707 int peep2_count;
1710 /* Return true if TEST can safely be performed at D, where
1711 the conditions in KC hold. TEST is known to occur along the
1712 first path from D (i.e. always following the first transition
1713 of the first decision). Any intervening tests can be used as
1714 negative proof that hoisting isn't safe, but only KC can be used
1715 as positive proof. */
1717 static bool
1718 safe_to_hoist_p (decision *d, const rtx_test &test, known_conditions *kc)
1720 switch (test.kind)
1722 case rtx_test::C_TEST:
1723 /* In general, C tests require everything else to have been
1724 verified and all operands to have been set up. */
1725 return false;
1727 case rtx_test::ACCEPT:
1728 /* Don't accept something before all conditions have been tested. */
1729 return false;
1731 case rtx_test::PREDICATE:
1732 /* Don't move a predicate over a test for VECLEN_GE, since the
1733 predicate used in a match_parallel can legitimately expect the
1734 length to be checked first. */
1735 for (decision *subd = d;
1736 subd->test != test;
1737 subd = subd->first->to->first)
1738 if (subd->test.pos == test.pos
1739 && subd->test.kind == rtx_test::VECLEN_GE)
1740 return false;
1741 goto any_rtx;
1743 case rtx_test::DUPLICATE:
1744 /* Don't test for a match_dup until the associated operand has
1745 been set. */
1746 if (!kc->set_operands[test.u.opno])
1747 return false;
1748 goto any_rtx;
1750 case rtx_test::CODE:
1751 case rtx_test::MODE:
1752 case rtx_test::SAVED_CONST_INT:
1753 case rtx_test::SET_OP:
1754 any_rtx:
1755 /* Check whether it is safe to access the rtx under test. */
1756 switch (test.pos->type)
1758 case POS_PEEP2_INSN:
1759 return test.pos->arg < kc->peep2_count;
1761 case POS_XEXP:
1762 return kc->position_tests[test.pos->base->id] & TESTED_CODE;
1764 case POS_XVECEXP0:
1765 return kc->position_tests[test.pos->base->id] & TESTED_VECLEN;
1767 gcc_unreachable ();
1769 case rtx_test::REGNO_FIELD:
1770 case rtx_test::INT_FIELD:
1771 case rtx_test::WIDE_INT_FIELD:
1772 case rtx_test::VECLEN:
1773 case rtx_test::VECLEN_GE:
1774 /* These tests access a specific part of an rtx, so are only safe
1775 once we know what the rtx is. */
1776 return kc->position_tests[test.pos->id] & TESTED_CODE;
1778 case rtx_test::PEEP2_COUNT:
1779 case rtx_test::HAVE_NUM_CLOBBERS:
1780 /* These tests can be performed anywhere. */
1781 return true;
1783 case rtx_test::PATTERN:
1784 gcc_unreachable ();
1786 gcc_unreachable ();
1789 /* Look for a transition that is taken by all successful returns from a range
1790 of decisions starting at OUTER and that would be better performed by
1791 OUTER's state instead. On success, store all instances of that transition
1792 in WHERE and return the last decision in the range. The range could
1793 just be OUTER, or it could include later decisions as well.
1795 WITH_POSITION_P is true if only tests with position POS should be tried,
1796 false if any test should be tried. WORTHWHILE_SINGLE_P is true if the
1797 result is useful even when the range contains just a single decision
1798 with a single transition. KC are the conditions that are known to
1799 hold at OUTER. */
1801 static decision *
1802 find_common_test (decision *outer, bool with_position_p,
1803 position *pos, bool worthwhile_single_p,
1804 known_conditions *kc, vec <transition *> *where)
1806 /* After this, WORTHWHILE_SINGLE_P indicates whether a range that contains
1807 just a single decision is useful, regardless of the number of
1808 transitions it has. */
1809 if (!outer->singleton ())
1810 worthwhile_single_p = true;
1811 /* Quick exit if we don't have enough decisions to form a worthwhile
1812 range. */
1813 if (!worthwhile_single_p && !outer->next)
1814 return 0;
1815 /* Follow the first chain down, as one example of a path that needs
1816 to contain the common test. */
1817 for (decision *d = outer; d; d = d->first->to->first)
1819 transition *trans = d->singleton ();
1820 if (trans
1821 && (!with_position_p || d->test.pos == pos)
1822 && safe_to_hoist_p (outer, d->test, kc))
1824 if (common_test_p (outer, trans, where))
1826 if (!outer->next)
1827 /* We checked above whether the move is worthwhile. */
1828 return outer;
1829 /* See how many decisions in OUTER's chain could reuse
1830 the same test. */
1831 decision *outer_end = outer;
1834 unsigned int length = where->length ();
1835 if (!common_test_p (outer_end->next, trans, where))
1837 where->truncate (length);
1838 break;
1840 outer_end = outer_end->next;
1842 while (outer_end->next);
1843 /* It is worth moving TRANS if it can be shared by more than
1844 one decision. */
1845 if (outer_end != outer || worthwhile_single_p)
1846 return outer_end;
1848 where->truncate (0);
1851 return 0;
1854 /* Try to promote common subtests in S to a single, shared decision.
1855 Also try to bunch tests for the same position together. POS is the
1856 position of the rtx tested before reaching S. KC are the conditions
1857 that are known to hold on entry to S. */
1859 static void
1860 cse_tests (position *pos, state *s, known_conditions *kc)
1862 for (decision *d = s->first; d; d = d->next)
1864 auto_vec <transition *, 16> where;
1865 if (d->test.pos)
1867 /* Try to find conditions that don't depend on a particular rtx,
1868 such as pnum_clobbers != NULL or peep2_current_count >= X.
1869 It's usually better to check these conditions as soon as
1870 possible, so the change is worthwhile even if there is
1871 only one copy of the test. */
1872 decision *endd = find_common_test (d, true, 0, true, kc, &where);
1873 if (!endd && d->test.pos != pos)
1874 /* Try to find other conditions related to position POS
1875 before moving to the new position. Again, this is
1876 worthwhile even if there is only one copy of the test,
1877 since it means that fewer position variables are live
1878 at a given time. */
1879 endd = find_common_test (d, true, pos, true, kc, &where);
1880 if (!endd)
1881 /* Try to find any condition that is used more than once. */
1882 endd = find_common_test (d, false, 0, false, kc, &where);
1883 if (endd)
1885 transition *common = where[0];
1886 /* Replace [D, ENDD] with a test like COMMON. We'll recurse
1887 on the common test and see the original D again next time. */
1888 d = insert_decision_before (state::range (d, endd),
1889 common->from->test,
1890 common->labels,
1891 common->optional);
1892 /* Remove the old tests. */
1893 while (!where.is_empty ())
1895 transition *trans = where.pop ();
1896 trans->from->s->replace (trans->from, trans->to->release ());
1901 /* Make sure that safe_to_hoist_p isn't being overly conservative.
1902 It should realize that D's test is safe in the current
1903 environment. */
1904 gcc_assert (d->test.kind == rtx_test::C_TEST
1905 || d->test.kind == rtx_test::ACCEPT
1906 || safe_to_hoist_p (d, d->test, kc));
1908 /* D won't be changed any further by the current optimization.
1909 Recurse with the state temporarily updated to include D. */
1910 int prev = 0;
1911 switch (d->test.kind)
1913 case rtx_test::CODE:
1914 prev = kc->position_tests[d->test.pos->id];
1915 kc->position_tests[d->test.pos->id] |= TESTED_CODE;
1916 break;
1918 case rtx_test::VECLEN:
1919 case rtx_test::VECLEN_GE:
1920 prev = kc->position_tests[d->test.pos->id];
1921 kc->position_tests[d->test.pos->id] |= TESTED_VECLEN;
1922 break;
1924 case rtx_test::SET_OP:
1925 prev = kc->set_operands[d->test.u.opno];
1926 gcc_assert (!prev);
1927 kc->set_operands[d->test.u.opno] = true;
1928 break;
1930 case rtx_test::PEEP2_COUNT:
1931 prev = kc->peep2_count;
1932 kc->peep2_count = MAX (prev, d->test.u.min_len);
1933 break;
1935 default:
1936 break;
1938 for (transition *trans = d->first; trans; trans = trans->next)
1939 cse_tests (d->test.pos ? d->test.pos : pos, trans->to, kc);
1940 switch (d->test.kind)
1942 case rtx_test::CODE:
1943 case rtx_test::VECLEN:
1944 case rtx_test::VECLEN_GE:
1945 kc->position_tests[d->test.pos->id] = prev;
1946 break;
1948 case rtx_test::SET_OP:
1949 kc->set_operands[d->test.u.opno] = prev;
1950 break;
1952 case rtx_test::PEEP2_COUNT:
1953 kc->peep2_count = prev;
1954 break;
1956 default:
1957 break;
1962 /* Return the type of value that can be used to parameterize test KIND,
1963 or parameter::UNSET if none. */
1965 parameter::type_enum
1966 transition_parameter_type (rtx_test::kind_enum kind)
1968 switch (kind)
1970 case rtx_test::CODE:
1971 return parameter::CODE;
1973 case rtx_test::MODE:
1974 return parameter::MODE;
1976 case rtx_test::REGNO_FIELD:
1977 return parameter::UINT;
1979 case rtx_test::INT_FIELD:
1980 case rtx_test::VECLEN:
1981 case rtx_test::PATTERN:
1982 return parameter::INT;
1984 case rtx_test::WIDE_INT_FIELD:
1985 return parameter::WIDE_INT;
1987 case rtx_test::PEEP2_COUNT:
1988 case rtx_test::VECLEN_GE:
1989 case rtx_test::SAVED_CONST_INT:
1990 case rtx_test::PREDICATE:
1991 case rtx_test::DUPLICATE:
1992 case rtx_test::HAVE_NUM_CLOBBERS:
1993 case rtx_test::C_TEST:
1994 case rtx_test::SET_OP:
1995 case rtx_test::ACCEPT:
1996 return parameter::UNSET;
1998 gcc_unreachable ();
2001 /* Initialize the pos_operand fields of each state reachable from S.
2002 If OPERAND_POS[ID] >= 0, the position with id ID is stored in
2003 operands[OPERAND_POS[ID]] on entry to S. */
2005 static void
2006 find_operand_positions (state *s, vec <int> &operand_pos)
2008 for (decision *d = s->first; d; d = d->next)
2010 int this_operand = (d->test.pos ? operand_pos[d->test.pos->id] : -1);
2011 if (this_operand >= 0)
2012 d->test.pos_operand = this_operand;
2013 if (d->test.kind == rtx_test::SET_OP)
2014 operand_pos[d->test.pos->id] = d->test.u.opno;
2015 for (transition *trans = d->first; trans; trans = trans->next)
2016 find_operand_positions (trans->to, operand_pos);
2017 if (d->test.kind == rtx_test::SET_OP)
2018 operand_pos[d->test.pos->id] = this_operand;
2022 /* Statistics about a matching routine. */
2023 struct stats
2025 stats ();
2027 /* The total number of decisions in the routine, excluding trivial
2028 ones that never fail. */
2029 unsigned int num_decisions;
2031 /* The number of non-trivial decisions on the longest path through
2032 the routine, and the return value that contributes most to that
2033 long path. */
2034 unsigned int longest_path;
2035 int longest_path_code;
2037 /* The maximum number of times that a single call to the routine
2038 can backtrack, and the value returned at the end of that path.
2039 "Backtracking" here means failing one decision in state and
2040 going onto to the next. */
2041 unsigned int longest_backtrack;
2042 int longest_backtrack_code;
2045 stats::stats ()
2046 : num_decisions (0), longest_path (0), longest_path_code (-1),
2047 longest_backtrack (0), longest_backtrack_code (-1) {}
2049 /* Return statistics about S. */
2051 static stats
2052 get_stats (state *s)
2054 stats for_s;
2055 unsigned int longest_path = 0;
2056 for (decision *d = s->first; d; d = d->next)
2058 /* Work out the statistics for D. */
2059 stats for_d;
2060 for (transition *trans = d->first; trans; trans = trans->next)
2062 stats for_trans = get_stats (trans->to);
2063 for_d.num_decisions += for_trans.num_decisions;
2064 /* Each transition is mutually-exclusive, so just pick the
2065 longest of the individual paths. */
2066 if (for_d.longest_path <= for_trans.longest_path)
2068 for_d.longest_path = for_trans.longest_path;
2069 for_d.longest_path_code = for_trans.longest_path_code;
2071 /* Likewise for backtracking. */
2072 if (for_d.longest_backtrack <= for_trans.longest_backtrack)
2074 for_d.longest_backtrack = for_trans.longest_backtrack;
2075 for_d.longest_backtrack_code = for_trans.longest_backtrack_code;
2079 /* Account for D's test in its statistics. */
2080 if (!d->test.single_outcome_p ())
2082 for_d.num_decisions += 1;
2083 for_d.longest_path += 1;
2085 if (d->test.kind == rtx_test::ACCEPT)
2087 for_d.longest_path_code = d->test.u.acceptance.u.full.code;
2088 for_d.longest_backtrack_code = d->test.u.acceptance.u.full.code;
2091 /* Keep a running count of the number of backtracks. */
2092 if (d->prev)
2093 for_s.longest_backtrack += 1;
2095 /* Accumulate D's statistics into S's. */
2096 for_s.num_decisions += for_d.num_decisions;
2097 for_s.longest_path += for_d.longest_path;
2098 for_s.longest_backtrack += for_d.longest_backtrack;
2100 /* Use the code from the decision with the longest individual path,
2101 since that's more likely to be useful if trying to make the
2102 path shorter. In the event of a tie, pick the later decision,
2103 since that's closer to the end of the path. */
2104 if (longest_path <= for_d.longest_path)
2106 longest_path = for_d.longest_path;
2107 for_s.longest_path_code = for_d.longest_path_code;
2110 /* Later decisions in a state are necessarily in a longer backtrack
2111 than earlier decisions. */
2112 for_s.longest_backtrack_code = for_d.longest_backtrack_code;
2114 return for_s;
2117 /* Optimize ROOT. Use TYPE to describe ROOT in status messages. */
2119 static void
2120 optimize_subroutine_group (const char *type, state *root)
2122 /* Remove optional transitions that turned out not to be worthwhile. */
2123 if (collapse_optional_decisions_p)
2124 collapse_optional_decisions (root);
2126 /* Try to remove duplicated tests and to rearrange tests into a more
2127 logical order. */
2128 if (cse_tests_p)
2130 known_conditions kc;
2131 kc.position_tests.safe_grow_cleared (num_positions);
2132 kc.set_operands.safe_grow_cleared (num_operands);
2133 kc.peep2_count = 1;
2134 cse_tests (&root_pos, root, &kc);
2137 /* Try to simplify two or more tests into one. */
2138 if (simplify_tests_p)
2139 simplify_tests (root);
2141 /* Try to use operands[] instead of xN variables. */
2142 if (use_operand_variables_p)
2144 auto_vec <int> operand_pos (num_positions);
2145 for (unsigned int i = 0; i < num_positions; ++i)
2146 operand_pos.quick_push (-1);
2147 find_operand_positions (root, operand_pos);
2150 /* Print a summary of the new state. */
2151 stats st = get_stats (root);
2152 fprintf (stderr, "Statistics for %s:\n", type);
2153 fprintf (stderr, " Number of decisions: %6d\n", st.num_decisions);
2154 fprintf (stderr, " longest path: %6d (code: %6d)\n",
2155 st.longest_path, st.longest_path_code);
2156 fprintf (stderr, " longest backtrack: %6d (code: %6d)\n",
2157 st.longest_backtrack, st.longest_backtrack_code);
2160 struct merge_pattern_info;
2162 /* Represents a transition from one pattern to another. */
2163 struct merge_pattern_transition
2165 merge_pattern_transition (merge_pattern_info *);
2167 /* The target pattern. */
2168 merge_pattern_info *to;
2170 /* The parameters that the source pattern passes to the target pattern.
2171 "parameter (TYPE, true, I)" represents parameter I of the source
2172 pattern. */
2173 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2176 merge_pattern_transition::merge_pattern_transition (merge_pattern_info *to_in)
2177 : to (to_in)
2181 /* Represents a pattern that can might match several states. The pattern
2182 may replace parts of the test with a parameter value. It may also
2183 replace transition labels with parameters. */
2184 struct merge_pattern_info
2186 merge_pattern_info (unsigned int);
2188 /* If PARAM_TEST_P, the state's singleton test should be generalized
2189 to use the runtime value of PARAMS[PARAM_TEST]. */
2190 unsigned int param_test : 8;
2192 /* If PARAM_TRANSITION_P, the state's single transition label should
2193 be replaced by the runtime value of PARAMS[PARAM_TRANSITION]. */
2194 unsigned int param_transition : 8;
2196 /* True if we have decided to generalize the root decision's test,
2197 as per PARAM_TEST. */
2198 unsigned int param_test_p : 1;
2200 /* Likewise for the root decision's transition, as per PARAM_TRANSITION. */
2201 unsigned int param_transition_p : 1;
2203 /* True if the contents of the structure are completely filled in. */
2204 unsigned int complete_p : 1;
2206 /* The number of pseudo-statements in the pattern. Used to decide
2207 whether it's big enough to break out into a subroutine. */
2208 unsigned int num_statements;
2210 /* The number of states that use this pattern. */
2211 unsigned int num_users;
2213 /* The number of distinct success values that the pattern returns. */
2214 unsigned int num_results;
2216 /* This array has one element for each runtime parameter to the pattern.
2217 PARAMS[I] gives the default value of parameter I, which is always
2218 constant.
2220 These default parameters are used in cases where we match the
2221 pattern against some state S1, then add more parameters while
2222 matching against some state S2. S1 is then left passing fewer
2223 parameters than S2. The array gives us enough informatino to
2224 construct a full parameter list for S1 (see update_parameters).
2226 If we decide to create a subroutine for this pattern,
2227 PARAMS[I].type determines the C type of parameter I. */
2228 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2230 /* All states that match this pattern must have the same number of
2231 transitions. TRANSITIONS[I] describes the subpattern for transition
2232 number I; it is null if transition I represents a successful return
2233 from the pattern. */
2234 auto_vec <merge_pattern_transition *, 1> transitions;
2236 /* The routine associated with the pattern, or null if we haven't generated
2237 one yet. */
2238 pattern_routine *routine;
2241 merge_pattern_info::merge_pattern_info (unsigned int num_transitions)
2242 : param_test (0),
2243 param_transition (0),
2244 param_test_p (false),
2245 param_transition_p (false),
2246 complete_p (false),
2247 num_statements (0),
2248 num_users (0),
2249 num_results (0),
2250 routine (0)
2252 transitions.safe_grow_cleared (num_transitions);
2255 /* Describes one way of matching a particular state to a particular
2256 pattern. */
2257 struct merge_state_result
2259 merge_state_result (merge_pattern_info *, position *, merge_state_result *);
2261 /* A pattern that matches the state. */
2262 merge_pattern_info *pattern;
2264 /* If we decide to use this match and create a subroutine for PATTERN,
2265 the state should pass the rtx at position ROOT to the pattern's
2266 rtx parameter. A null root means that the pattern doesn't need
2267 an rtx parameter; all the rtxes it matches come from elsewhere. */
2268 position *root;
2270 /* The parameters that should be passed to PATTERN for this state.
2271 If the array is shorter than PATTERN->params, the missing entries
2272 should be taken from the corresponding element of PATTERN->params. */
2273 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2275 /* An earlier match for the same state, or null if none. Patterns
2276 matched by earlier entries are smaller than PATTERN. */
2277 merge_state_result *prev;
2280 merge_state_result::merge_state_result (merge_pattern_info *pattern_in,
2281 position *root_in,
2282 merge_state_result *prev_in)
2283 : pattern (pattern_in), root (root_in), prev (prev_in)
2286 /* Information about a state, used while trying to match it against
2287 a pattern. */
2288 struct merge_state_info
2290 merge_state_info (state *);
2292 /* The state itself. */
2293 state *s;
2295 /* Index I gives information about the target of transition I. */
2296 merge_state_info *to_states;
2298 /* The number of transitions in S. */
2299 unsigned int num_transitions;
2301 /* True if the state has been deleted in favor of a call to a
2302 pattern routine. */
2303 bool merged_p;
2305 /* The previous state that might be a merge candidate for S, or null
2306 if no previous states could be merged with S. */
2307 merge_state_info *prev_same_test;
2309 /* A list of pattern matches for this state. */
2310 merge_state_result *res;
2313 merge_state_info::merge_state_info (state *s_in)
2314 : s (s_in),
2315 to_states (0),
2316 num_transitions (0),
2317 merged_p (false),
2318 prev_same_test (0),
2319 res (0) {}
2321 /* True if PAT would be useful as a subroutine. */
2323 static bool
2324 useful_pattern_p (merge_pattern_info *pat)
2326 return pat->num_statements >= MIN_COMBINE_COST;
2329 /* PAT2 is a subpattern of PAT1. Return true if PAT2 should be inlined
2330 into PAT1's C routine. */
2332 static bool
2333 same_pattern_p (merge_pattern_info *pat1, merge_pattern_info *pat2)
2335 return pat1->num_users == pat2->num_users || !useful_pattern_p (pat2);
2338 /* PAT was previously matched against SINFO based on tentative matches
2339 for the target states of SINFO's state. Return true if the match
2340 still holds; that is, if the target states of SINFO's state still
2341 match the corresponding transitions of PAT. */
2343 static bool
2344 valid_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2346 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2347 if (merge_pattern_transition *ptrans = pat->transitions[j])
2349 merge_state_result *to_res = sinfo->to_states[j].res;
2350 if (!to_res || to_res->pattern != ptrans->to)
2351 return false;
2353 return true;
2356 /* Remove any matches that are no longer valid from the head of SINFO's
2357 list of matches. */
2359 static void
2360 prune_invalid_results (merge_state_info *sinfo)
2362 while (sinfo->res && !valid_result_p (sinfo->res->pattern, sinfo))
2364 sinfo->res = sinfo->res->prev;
2365 gcc_assert (sinfo->res);
2369 /* Return true if PAT represents the biggest posssible match for SINFO;
2370 that is, if the next action of SINFO's state on return from PAT will
2371 be something that cannot be merged with any other state. */
2373 static bool
2374 complete_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2376 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2377 if (sinfo->to_states[j].res && !pat->transitions[j])
2378 return false;
2379 return true;
2382 /* Update TO for any parameters that have been added to FROM since TO
2383 was last set. The extra parameters in FROM will be constants or
2384 instructions to duplicate earlier parameters. */
2386 static void
2387 update_parameters (vec <parameter> &to, const vec <parameter> &from)
2389 for (unsigned int i = to.length (); i < from.length (); ++i)
2390 to.quick_push (from[i]);
2393 /* Return true if A and B can be tested by a single test. If the test
2394 can be parameterised, store the parameter value for A in *PARAMA and
2395 the parameter value for B in *PARAMB, otherwise leave PARAMA and
2396 PARAMB alone. */
2398 static bool
2399 compatible_tests_p (const rtx_test &a, const rtx_test &b,
2400 parameter *parama, parameter *paramb)
2402 if (a.kind != b.kind)
2403 return false;
2404 switch (a.kind)
2406 case rtx_test::PREDICATE:
2407 if (a.u.predicate.data != b.u.predicate.data)
2408 return false;
2409 *parama = parameter (parameter::MODE, false, a.u.predicate.mode);
2410 *paramb = parameter (parameter::MODE, false, b.u.predicate.mode);
2411 return true;
2413 case rtx_test::SAVED_CONST_INT:
2414 *parama = parameter (parameter::INT, false, a.u.integer.value);
2415 *paramb = parameter (parameter::INT, false, b.u.integer.value);
2416 return true;
2418 default:
2419 return a == b;
2423 /* PARAMS is an array of the parameters that a state is going to pass
2424 to a pattern routine. It is still incomplete; index I has a kind of
2425 parameter::UNSET if we don't yet know what the state will pass
2426 as parameter I. Try to make parameter ID equal VALUE, returning
2427 true on success. */
2429 static bool
2430 set_parameter (vec <parameter> &params, unsigned int id,
2431 const parameter &value)
2433 if (params[id].type == parameter::UNSET)
2435 if (force_unique_params_p)
2436 for (unsigned int i = 0; i < params.length (); ++i)
2437 if (params[i] == value)
2438 return false;
2439 params[id] = value;
2440 return true;
2442 return params[id] == value;
2445 /* PARAMS2 is the "params" array for a pattern and PARAMS1 is the
2446 set of parameters that a particular state is going to pass to
2447 that pattern.
2449 Try to extend PARAMS1 and PARAMS2 so that there is a parameter
2450 that is equal to PARAM1 for the state and has a default value of
2451 PARAM2. Parameters beginning at START were added as part of the
2452 same match and so may be reused. */
2454 static bool
2455 add_parameter (vec <parameter> &params1, vec <parameter> &params2,
2456 const parameter &param1, const parameter &param2,
2457 unsigned int start, unsigned int *res)
2459 gcc_assert (params1.length () == params2.length ());
2460 gcc_assert (!param1.is_param && !param2.is_param);
2462 for (unsigned int i = start; i < params2.length (); ++i)
2463 if (params1[i] == param1 && params2[i] == param2)
2465 *res = i;
2466 return true;
2469 if (force_unique_params_p)
2470 for (unsigned int i = 0; i < params2.length (); ++i)
2471 if (params1[i] == param1 || params2[i] == param2)
2472 return false;
2474 if (params2.length () >= MAX_PATTERN_PARAMS)
2475 return false;
2477 *res = params2.length ();
2478 params1.quick_push (param1);
2479 params2.quick_push (param2);
2480 return true;
2483 /* If *ROOTA is nonnull, return true if the same sequence of steps are
2484 required to reach A from *ROOTA as to reach B from ROOTB. If *ROOTA
2485 is null, update it if necessary in order to make the condition hold. */
2487 static bool
2488 merge_relative_positions (position **roota, position *a,
2489 position *rootb, position *b)
2491 if (!relative_patterns_p)
2493 if (a != b)
2494 return false;
2495 if (!*roota)
2497 *roota = rootb;
2498 return true;
2500 return *roota == rootb;
2502 /* If B does not belong to the same instruction as ROOTB, we don't
2503 start with ROOTB but instead start with a call to peep2_next_insn.
2504 In that case the sequences for B and A are identical iff B and A
2505 are themselves identical. */
2506 if (rootb->insn_id != b->insn_id)
2507 return a == b;
2508 while (rootb != b)
2510 if (!a || b->type != a->type || b->arg != a->arg)
2511 return false;
2512 b = b->base;
2513 a = a->base;
2515 if (!*roota)
2516 *roota = a;
2517 return *roota == a;
2520 /* A hasher of states that treats two states as "equal" if they might be
2521 merged (but trying to be more discriminating than "return true"). */
2522 struct test_pattern_hasher : nofree_ptr_hash <merge_state_info>
2524 static inline hashval_t hash (const value_type &);
2525 static inline bool equal (const value_type &, const compare_type &);
2528 hashval_t
2529 test_pattern_hasher::hash (merge_state_info *const &sinfo)
2531 inchash::hash h;
2532 decision *d = sinfo->s->singleton ();
2533 h.add_int (d->test.pos_operand + 1);
2534 if (!relative_patterns_p)
2535 h.add_int (d->test.pos ? d->test.pos->id + 1 : 0);
2536 h.add_int (d->test.kind);
2537 h.add_int (sinfo->num_transitions);
2538 return h.end ();
2541 bool
2542 test_pattern_hasher::equal (merge_state_info *const &sinfo1,
2543 merge_state_info *const &sinfo2)
2545 decision *d1 = sinfo1->s->singleton ();
2546 decision *d2 = sinfo2->s->singleton ();
2547 gcc_assert (d1 && d2);
2549 parameter new_param1, new_param2;
2550 return (d1->test.pos_operand == d2->test.pos_operand
2551 && (relative_patterns_p || d1->test.pos == d2->test.pos)
2552 && compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2)
2553 && sinfo1->num_transitions == sinfo2->num_transitions);
2556 /* Try to make the state described by SINFO1 use the same pattern as the
2557 state described by SINFO2. Return true on success.
2559 SINFO1 and SINFO2 are known to have the same hash value. */
2561 static bool
2562 merge_patterns (merge_state_info *sinfo1, merge_state_info *sinfo2)
2564 merge_state_result *res2 = sinfo2->res;
2565 merge_pattern_info *pat = res2->pattern;
2567 /* Write to temporary arrays while matching, in case we have to abort
2568 half way through. */
2569 auto_vec <parameter, MAX_PATTERN_PARAMS> params1;
2570 auto_vec <parameter, MAX_PATTERN_PARAMS> params2;
2571 params1.quick_grow_cleared (pat->params.length ());
2572 params2.splice (pat->params);
2573 unsigned int start_param = params2.length ();
2575 /* An array for recording changes to PAT->transitions[?].params.
2576 All changes involve replacing a constant parameter with some
2577 PAT->params[N], where N is the second element of the pending_param. */
2578 typedef std::pair <parameter *, unsigned int> pending_param;
2579 auto_vec <pending_param, 32> pending_params;
2581 decision *d1 = sinfo1->s->singleton ();
2582 decision *d2 = sinfo2->s->singleton ();
2583 gcc_assert (d1 && d2);
2585 /* If D2 tests a position, SINFO1's root relative to D1 is the same
2586 as SINFO2's root relative to D2. */
2587 position *root1 = 0;
2588 position *root2 = res2->root;
2589 if (d2->test.pos_operand < 0
2590 && d1->test.pos
2591 && !merge_relative_positions (&root1, d1->test.pos,
2592 root2, d2->test.pos))
2593 return false;
2595 /* Check whether the patterns have the same shape. */
2596 unsigned int num_transitions = sinfo1->num_transitions;
2597 gcc_assert (num_transitions == sinfo2->num_transitions);
2598 for (unsigned int i = 0; i < num_transitions; ++i)
2599 if (merge_pattern_transition *ptrans = pat->transitions[i])
2601 merge_state_result *to1_res = sinfo1->to_states[i].res;
2602 merge_state_result *to2_res = sinfo2->to_states[i].res;
2603 merge_pattern_info *to_pat = ptrans->to;
2604 gcc_assert (to2_res && to2_res->pattern == to_pat);
2605 if (!to1_res || to1_res->pattern != to_pat)
2606 return false;
2607 if (to2_res->root
2608 && !merge_relative_positions (&root1, to1_res->root,
2609 root2, to2_res->root))
2610 return false;
2611 /* Match the parameters that TO1_RES passes to TO_PAT with the
2612 parameters that PAT passes to TO_PAT. */
2613 update_parameters (to1_res->params, to_pat->params);
2614 for (unsigned int j = 0; j < to1_res->params.length (); ++j)
2616 const parameter &param1 = to1_res->params[j];
2617 const parameter &param2 = ptrans->params[j];
2618 gcc_assert (!param1.is_param);
2619 if (param2.is_param)
2621 if (!set_parameter (params1, param2.value, param1))
2622 return false;
2624 else if (param1 != param2)
2626 unsigned int id;
2627 if (!add_parameter (params1, params2,
2628 param1, param2, start_param, &id))
2629 return false;
2630 /* Record that PAT should now pass parameter ID to TO_PAT,
2631 instead of the current contents of *PARAM2. We only
2632 make the change if the rest of the match succeeds. */
2633 pending_params.safe_push
2634 (pending_param (&ptrans->params[j], id));
2639 unsigned int param_test = pat->param_test;
2640 unsigned int param_transition = pat->param_transition;
2641 bool param_test_p = pat->param_test_p;
2642 bool param_transition_p = pat->param_transition_p;
2644 /* If the tests don't match exactly, try to parameterize them. */
2645 parameter new_param1, new_param2;
2646 if (!compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2))
2647 gcc_unreachable ();
2648 if (new_param1.type != parameter::UNSET)
2650 /* If the test has not already been parameterized, all existing
2651 matches use constant NEW_PARAM2. */
2652 if (param_test_p)
2654 if (!set_parameter (params1, param_test, new_param1))
2655 return false;
2657 else if (new_param1 != new_param2)
2659 if (!add_parameter (params1, params2, new_param1, new_param2,
2660 start_param, &param_test))
2661 return false;
2662 param_test_p = true;
2666 /* Match the transitions. */
2667 transition *trans1 = d1->first;
2668 transition *trans2 = d2->first;
2669 for (unsigned int i = 0; i < num_transitions; ++i)
2671 if (param_transition_p || trans1->labels != trans2->labels)
2673 /* We can only generalize a single transition with a single
2674 label. */
2675 if (num_transitions != 1
2676 || trans1->labels.length () != 1
2677 || trans2->labels.length () != 1)
2678 return false;
2680 /* Although we can match wide-int fields, in practice it leads
2681 to some odd results for const_vectors. We end up
2682 parameterizing the first N const_ints of the vector
2683 and then (once we reach the maximum number of parameters)
2684 we go on to match the other elements exactly. */
2685 if (d1->test.kind == rtx_test::WIDE_INT_FIELD)
2686 return false;
2688 /* See whether the label has a generalizable type. */
2689 parameter::type_enum param_type
2690 = transition_parameter_type (d1->test.kind);
2691 if (param_type == parameter::UNSET)
2692 return false;
2694 /* Match the labels using parameters. */
2695 new_param1 = parameter (param_type, false, trans1->labels[0]);
2696 if (param_transition_p)
2698 if (!set_parameter (params1, param_transition, new_param1))
2699 return false;
2701 else
2703 new_param2 = parameter (param_type, false, trans2->labels[0]);
2704 if (!add_parameter (params1, params2, new_param1, new_param2,
2705 start_param, &param_transition))
2706 return false;
2707 param_transition_p = true;
2710 trans1 = trans1->next;
2711 trans2 = trans2->next;
2714 /* Set any unset parameters to their default values. This occurs if some
2715 other state needed something to be parameterized in order to match SINFO2,
2716 but SINFO1 on its own does not. */
2717 for (unsigned int i = 0; i < params1.length (); ++i)
2718 if (params1[i].type == parameter::UNSET)
2719 params1[i] = params2[i];
2721 /* The match was successful. Commit all pending changes to PAT. */
2722 update_parameters (pat->params, params2);
2724 pending_param *pp;
2725 unsigned int i;
2726 FOR_EACH_VEC_ELT (pending_params, i, pp)
2727 *pp->first = parameter (pp->first->type, true, pp->second);
2729 pat->param_test = param_test;
2730 pat->param_transition = param_transition;
2731 pat->param_test_p = param_test_p;
2732 pat->param_transition_p = param_transition_p;
2734 /* Record the match of SINFO1. */
2735 merge_state_result *new_res1 = new merge_state_result (pat, root1,
2736 sinfo1->res);
2737 new_res1->params.splice (params1);
2738 sinfo1->res = new_res1;
2739 return true;
2742 /* The number of states that were removed by calling pattern routines. */
2743 static unsigned int pattern_use_states;
2745 /* The number of states used while defining pattern routines. */
2746 static unsigned int pattern_def_states;
2748 /* Information used while constructing a use or definition of a pattern
2749 routine. */
2750 struct create_pattern_info
2752 /* The routine itself. */
2753 pattern_routine *routine;
2755 /* The first unclaimed return value for this particular use or definition.
2756 We walk the substates of uses and definitions in the same order
2757 so each return value always refers to the same position within
2758 the pattern. */
2759 unsigned int next_result;
2762 static void populate_pattern_routine (create_pattern_info *,
2763 merge_state_info *, state *,
2764 const vec <parameter> &);
2766 /* SINFO matches a pattern for which we've decided to create a C routine.
2767 Return a decision that performs a call to the pattern routine,
2768 but leave the caller to add the transitions to it. Initialize CPI
2769 for this purpose. Also create a definition for the pattern routine,
2770 if it doesn't already have one.
2772 PARAMS are the parameters that SINFO passes to its pattern. */
2774 static decision *
2775 init_pattern_use (create_pattern_info *cpi, merge_state_info *sinfo,
2776 const vec <parameter> &params)
2778 state *s = sinfo->s;
2779 merge_state_result *res = sinfo->res;
2780 merge_pattern_info *pat = res->pattern;
2781 cpi->routine = pat->routine;
2782 if (!cpi->routine)
2784 /* We haven't defined the pattern routine yet, so create
2785 a definition now. */
2786 pattern_routine *routine = new pattern_routine;
2787 pat->routine = routine;
2788 cpi->routine = routine;
2789 routine->s = new state;
2790 routine->insn_p = false;
2791 routine->pnum_clobbers_p = false;
2793 /* Create an "idempotent" mapping of parameter I to parameter I.
2794 Also record the C type of each parameter to the routine. */
2795 auto_vec <parameter, MAX_PATTERN_PARAMS> def_params;
2796 for (unsigned int i = 0; i < pat->params.length (); ++i)
2798 def_params.quick_push (parameter (pat->params[i].type, true, i));
2799 routine->param_types.quick_push (pat->params[i].type);
2802 /* Any of the states that match the pattern could be used to
2803 create the routine definition. We might as well use SINFO
2804 since it's already to hand. This means that all positions
2805 in the definition will be relative to RES->root. */
2806 routine->pos = res->root;
2807 cpi->next_result = 0;
2808 populate_pattern_routine (cpi, sinfo, routine->s, def_params);
2809 gcc_assert (cpi->next_result == pat->num_results);
2811 /* Add the routine to the global list, after the subroutines
2812 that it calls. */
2813 routine->pattern_id = patterns.length ();
2814 patterns.safe_push (routine);
2817 /* Create a decision to call the routine, passing PARAMS to it. */
2818 pattern_use *use = new pattern_use;
2819 use->routine = pat->routine;
2820 use->params.splice (params);
2821 decision *d = new decision (rtx_test::pattern (res->root, use));
2823 /* If the original decision could use an element of operands[] instead
2824 of an rtx variable, try to transfer it to the new decision. */
2825 if (s->first->test.pos && res->root == s->first->test.pos)
2826 d->test.pos_operand = s->first->test.pos_operand;
2828 cpi->next_result = 0;
2829 return d;
2832 /* Make S return the next unclaimed pattern routine result for CPI. */
2834 static void
2835 add_pattern_acceptance (create_pattern_info *cpi, state *s)
2837 acceptance_type acceptance;
2838 acceptance.type = SUBPATTERN;
2839 acceptance.partial_p = false;
2840 acceptance.u.full.code = cpi->next_result;
2841 add_decision (s, rtx_test::accept (acceptance), true, false);
2842 cpi->next_result += 1;
2845 /* Initialize new empty state NEWS so that it implements SINFO's pattern
2846 (here referred to as "P"). P may be the top level of a pattern routine
2847 or a subpattern that should be inlined into its parent pattern's routine
2848 (as per same_pattern_p). The choice of SINFO for a top-level pattern is
2849 arbitrary; it could be any of the states that use P. The choice for
2850 subpatterns follows the choice for the parent pattern.
2852 PARAMS gives the value of each parameter to P in terms of the parameters
2853 to the top-level pattern. If P itself is the top level pattern, PARAMS[I]
2854 is always "parameter (TYPE, true, I)". */
2856 static void
2857 populate_pattern_routine (create_pattern_info *cpi, merge_state_info *sinfo,
2858 state *news, const vec <parameter> &params)
2860 pattern_def_states += 1;
2862 decision *d = sinfo->s->singleton ();
2863 merge_pattern_info *pat = sinfo->res->pattern;
2864 pattern_routine *routine = cpi->routine;
2866 /* Create a copy of D's test for the pattern routine and generalize it
2867 as appropriate. */
2868 decision *newd = new decision (d->test);
2869 gcc_assert (newd->test.pos_operand >= 0
2870 || !newd->test.pos
2871 || common_position (newd->test.pos,
2872 routine->pos) == routine->pos);
2873 if (pat->param_test_p)
2875 const parameter &param = params[pat->param_test];
2876 switch (newd->test.kind)
2878 case rtx_test::PREDICATE:
2879 newd->test.u.predicate.mode_is_param = param.is_param;
2880 newd->test.u.predicate.mode = param.value;
2881 break;
2883 case rtx_test::SAVED_CONST_INT:
2884 newd->test.u.integer.is_param = param.is_param;
2885 newd->test.u.integer.value = param.value;
2886 break;
2888 default:
2889 gcc_unreachable ();
2890 break;
2893 if (d->test.kind == rtx_test::C_TEST)
2894 routine->insn_p = true;
2895 else if (d->test.kind == rtx_test::HAVE_NUM_CLOBBERS)
2896 routine->pnum_clobbers_p = true;
2897 news->push_back (newd);
2899 /* Fill in the transitions of NEWD. */
2900 unsigned int i = 0;
2901 for (transition *trans = d->first; trans; trans = trans->next)
2903 /* Create a new state to act as the target of the new transition. */
2904 state *to_news = new state;
2905 if (merge_pattern_transition *ptrans = pat->transitions[i])
2907 /* The pattern hasn't finished matching yet. Get the target
2908 pattern and the corresponding target state of SINFO. */
2909 merge_pattern_info *to_pat = ptrans->to;
2910 merge_state_info *to = sinfo->to_states + i;
2911 gcc_assert (to->res->pattern == to_pat);
2912 gcc_assert (ptrans->params.length () == to_pat->params.length ());
2914 /* Express the parameters to TO_PAT in terms of the parameters
2915 to the top-level pattern. */
2916 auto_vec <parameter, MAX_PATTERN_PARAMS> to_params;
2917 for (unsigned int j = 0; j < ptrans->params.length (); ++j)
2919 const parameter &param = ptrans->params[j];
2920 to_params.quick_push (param.is_param
2921 ? params[param.value]
2922 : param);
2925 if (same_pattern_p (pat, to_pat))
2926 /* TO_PAT is part of the current routine, so just recurse. */
2927 populate_pattern_routine (cpi, to, to_news, to_params);
2928 else
2930 /* TO_PAT should be matched by calling a separate routine. */
2931 create_pattern_info sub_cpi;
2932 decision *subd = init_pattern_use (&sub_cpi, to, to_params);
2933 routine->insn_p |= sub_cpi.routine->insn_p;
2934 routine->pnum_clobbers_p |= sub_cpi.routine->pnum_clobbers_p;
2936 /* Add the pattern routine call to the new target state. */
2937 to_news->push_back (subd);
2939 /* Add a transition for each successful call result. */
2940 for (unsigned int j = 0; j < to_pat->num_results; ++j)
2942 state *res = new state;
2943 add_pattern_acceptance (cpi, res);
2944 subd->push_back (new transition (j, res, false));
2948 else
2949 /* This transition corresponds to a successful match. */
2950 add_pattern_acceptance (cpi, to_news);
2952 /* Create the transition itself, generalizing as necessary. */
2953 transition *new_trans = new transition (trans->labels, to_news,
2954 trans->optional);
2955 if (pat->param_transition_p)
2957 const parameter &param = params[pat->param_transition];
2958 new_trans->is_param = param.is_param;
2959 new_trans->labels[0] = param.value;
2961 newd->push_back (new_trans);
2962 i += 1;
2966 /* USE is a decision that calls a pattern routine and SINFO is part of the
2967 original state tree that the call is supposed to replace. Add the
2968 transitions for SINFO and its substates to USE. */
2970 static void
2971 populate_pattern_use (create_pattern_info *cpi, decision *use,
2972 merge_state_info *sinfo)
2974 pattern_use_states += 1;
2975 gcc_assert (!sinfo->merged_p);
2976 sinfo->merged_p = true;
2977 merge_state_result *res = sinfo->res;
2978 merge_pattern_info *pat = res->pattern;
2979 decision *d = sinfo->s->singleton ();
2980 unsigned int i = 0;
2981 for (transition *trans = d->first; trans; trans = trans->next)
2983 if (pat->transitions[i])
2984 /* The target state is also part of the pattern. */
2985 populate_pattern_use (cpi, use, sinfo->to_states + i);
2986 else
2988 /* The transition corresponds to a successful return from the
2989 pattern routine. */
2990 use->push_back (new transition (cpi->next_result, trans->to, false));
2991 cpi->next_result += 1;
2993 i += 1;
2997 /* We have decided to replace SINFO's state with a call to a pattern
2998 routine. Make the change, creating a definition of the pattern routine
2999 if it doesn't have one already. */
3001 static void
3002 use_pattern (merge_state_info *sinfo)
3004 merge_state_result *res = sinfo->res;
3005 merge_pattern_info *pat = res->pattern;
3006 state *s = sinfo->s;
3008 /* The pattern may have acquired new parameters after it was matched
3009 against SINFO. Update the parameters that SINFO passes accordingly. */
3010 update_parameters (res->params, pat->params);
3012 create_pattern_info cpi;
3013 decision *d = init_pattern_use (&cpi, sinfo, res->params);
3014 populate_pattern_use (&cpi, d, sinfo);
3015 s->release ();
3016 s->push_back (d);
3019 /* Look through the state trees in STATES for common patterns and
3020 split them into subroutines. */
3022 static void
3023 split_out_patterns (vec <merge_state_info> &states)
3025 unsigned int first_transition = states.length ();
3026 hash_table <test_pattern_hasher> hashtab (128);
3027 /* Stage 1: Create an order in which parent states come before their child
3028 states and in which sibling states are at consecutive locations.
3029 Having consecutive sibling states allows merge_state_info to have
3030 a single to_states pointer. */
3031 for (unsigned int i = 0; i < states.length (); ++i)
3032 for (decision *d = states[i].s->first; d; d = d->next)
3033 for (transition *trans = d->first; trans; trans = trans->next)
3035 states.safe_push (trans->to);
3036 states[i].num_transitions += 1;
3038 /* Stage 2: Now that the addresses are stable, set up the to_states
3039 pointers. Look for states that might be merged and enter them
3040 into the hash table. */
3041 for (unsigned int i = 0; i < states.length (); ++i)
3043 merge_state_info *sinfo = &states[i];
3044 if (sinfo->num_transitions)
3046 sinfo->to_states = &states[first_transition];
3047 first_transition += sinfo->num_transitions;
3049 /* For simplicity, we only try to merge states that have a single
3050 decision. This is in any case the best we can do for peephole2,
3051 since whether a peephole2 ACCEPT succeeds or not depends on the
3052 specific peephole2 pattern (which is unique to each ACCEPT
3053 and so couldn't be shared between states). */
3054 if (decision *d = sinfo->s->singleton ())
3055 /* ACCEPT states are unique, so don't even try to merge them. */
3056 if (d->test.kind != rtx_test::ACCEPT
3057 && (pattern_have_num_clobbers_p
3058 || d->test.kind != rtx_test::HAVE_NUM_CLOBBERS)
3059 && (pattern_c_test_p
3060 || d->test.kind != rtx_test::C_TEST))
3062 merge_state_info **slot = hashtab.find_slot (sinfo, INSERT);
3063 sinfo->prev_same_test = *slot;
3064 *slot = sinfo;
3067 /* Stage 3: Walk backwards through the list of states and try to merge
3068 them. This is a greedy, bottom-up match; parent nodes can only start
3069 a new leaf pattern if they fail to match when combined with all child
3070 nodes that have matching patterns.
3072 For each state we keep a list of potential matches, with each
3073 potential match being larger (and deeper) than the next match in
3074 the list. The final element in the list is a leaf pattern that
3075 matches just a single state.
3077 Each candidate pattern created in this loop is unique -- it won't
3078 have been seen by an earlier iteration. We try to match each pattern
3079 with every state that appears earlier in STATES.
3081 Because the patterns created in the loop are unique, any state
3082 that already has a match must have a final potential match that
3083 is different from any new leaf pattern. Therefore, when matching
3084 leaf patterns, we need only consider states whose list of matches
3085 is empty.
3087 The non-leaf patterns that we try are as deep as possible
3088 and are an extension of the state's previous best candidate match (PB).
3089 We need only consider states whose current potential match is also PB;
3090 any states that don't match as much as PB cannnot match the new pattern,
3091 while any states that already match more than PB must be different from
3092 the new pattern. */
3093 for (unsigned int i2 = states.length (); i2-- > 0; )
3095 merge_state_info *sinfo2 = &states[i2];
3097 /* Enforce the bottom-upness of the match: remove matches with later
3098 states if SINFO2's child states ended up finding a better match. */
3099 prune_invalid_results (sinfo2);
3101 /* Do nothing if the state doesn't match a later one and if there are
3102 no earlier states it could match. */
3103 if (!sinfo2->res && !sinfo2->prev_same_test)
3104 continue;
3106 merge_state_result *res2 = sinfo2->res;
3107 decision *d2 = sinfo2->s->singleton ();
3108 position *root2 = (d2->test.pos_operand < 0 ? d2->test.pos : 0);
3109 unsigned int num_transitions = sinfo2->num_transitions;
3111 /* If RES2 is null then SINFO2's test in isolation has not been seen
3112 before. First try matching that on its own. */
3113 if (!res2)
3115 merge_pattern_info *new_pat
3116 = new merge_pattern_info (num_transitions);
3117 merge_state_result *new_res2
3118 = new merge_state_result (new_pat, root2, res2);
3119 sinfo2->res = new_res2;
3121 new_pat->num_statements = !d2->test.single_outcome_p ();
3122 new_pat->num_results = num_transitions;
3123 bool matched_p = false;
3124 /* Look for states that don't currently match anything but
3125 can be made to match SINFO2 on its own. */
3126 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3127 sinfo1 = sinfo1->prev_same_test)
3128 if (!sinfo1->res && merge_patterns (sinfo1, sinfo2))
3129 matched_p = true;
3130 if (!matched_p)
3132 /* No other states match. */
3133 sinfo2->res = res2;
3134 delete new_pat;
3135 delete new_res2;
3136 continue;
3138 else
3139 res2 = new_res2;
3142 /* Keep the existing pattern if it's as good as anything we'd
3143 create for SINFO2. */
3144 if (complete_result_p (res2->pattern, sinfo2))
3146 res2->pattern->num_users += 1;
3147 continue;
3150 /* Create a new pattern for SINFO2. */
3151 merge_pattern_info *new_pat = new merge_pattern_info (num_transitions);
3152 merge_state_result *new_res2
3153 = new merge_state_result (new_pat, root2, res2);
3154 sinfo2->res = new_res2;
3156 /* Fill in details about the pattern. */
3157 new_pat->num_statements = !d2->test.single_outcome_p ();
3158 new_pat->num_results = 0;
3159 for (unsigned int j = 0; j < num_transitions; ++j)
3160 if (merge_state_result *to_res = sinfo2->to_states[j].res)
3162 /* Count the target state as part of this pattern.
3163 First update the root position so that it can reach
3164 the target state's root. */
3165 if (to_res->root)
3167 if (new_res2->root)
3168 new_res2->root = common_position (new_res2->root,
3169 to_res->root);
3170 else
3171 new_res2->root = to_res->root;
3173 merge_pattern_info *to_pat = to_res->pattern;
3174 merge_pattern_transition *ptrans
3175 = new merge_pattern_transition (to_pat);
3177 /* TO_PAT may have acquired more parameters when matching
3178 states earlier in STATES than TO_RES's, but the list is
3179 now final. Make sure that TO_RES is up to date. */
3180 update_parameters (to_res->params, to_pat->params);
3182 /* Start out by assuming that every user of NEW_PAT will
3183 want to pass the same (constant) parameters as TO_RES. */
3184 update_parameters (ptrans->params, to_res->params);
3186 new_pat->transitions[j] = ptrans;
3187 new_pat->num_statements += to_pat->num_statements;
3188 new_pat->num_results += to_pat->num_results;
3190 else
3191 /* The target state doesn't match anything and so is not part
3192 of the pattern. */
3193 new_pat->num_results += 1;
3195 /* See if any earlier states that match RES2's pattern also match
3196 NEW_PAT. */
3197 bool matched_p = false;
3198 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3199 sinfo1 = sinfo1->prev_same_test)
3201 prune_invalid_results (sinfo1);
3202 if (sinfo1->res
3203 && sinfo1->res->pattern == res2->pattern
3204 && merge_patterns (sinfo1, sinfo2))
3205 matched_p = true;
3207 if (!matched_p)
3209 /* Nothing else matches NEW_PAT, so go back to the previous
3210 pattern (possibly just a single-state one). */
3211 sinfo2->res = res2;
3212 delete new_pat;
3213 delete new_res2;
3215 /* Assume that SINFO2 will use RES. At this point we don't know
3216 whether earlier states that match the same pattern will use
3217 that match or a different one. */
3218 sinfo2->res->pattern->num_users += 1;
3220 /* Step 4: Finalize the choice of pattern for each state, ignoring
3221 patterns that were only used once. Update each pattern's size
3222 so that it doesn't include subpatterns that are going to be split
3223 out into subroutines. */
3224 for (unsigned int i = 0; i < states.length (); ++i)
3226 merge_state_info *sinfo = &states[i];
3227 merge_state_result *res = sinfo->res;
3228 /* Wind past patterns that are only used by SINFO. */
3229 while (res && res->pattern->num_users == 1)
3231 res = res->prev;
3232 sinfo->res = res;
3233 if (res)
3234 res->pattern->num_users += 1;
3236 if (!res)
3237 continue;
3239 /* We have a shared pattern and are now committed to the match. */
3240 merge_pattern_info *pat = res->pattern;
3241 gcc_assert (valid_result_p (pat, sinfo));
3243 if (!pat->complete_p)
3245 /* Look for subpatterns that are going to be split out and remove
3246 them from the number of statements. */
3247 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
3248 if (merge_pattern_transition *ptrans = pat->transitions[j])
3250 merge_pattern_info *to_pat = ptrans->to;
3251 if (!same_pattern_p (pat, to_pat))
3252 pat->num_statements -= to_pat->num_statements;
3254 pat->complete_p = true;
3257 /* Step 5: Split out the patterns. */
3258 for (unsigned int i = 0; i < states.length (); ++i)
3260 merge_state_info *sinfo = &states[i];
3261 merge_state_result *res = sinfo->res;
3262 if (!sinfo->merged_p && res && useful_pattern_p (res->pattern))
3263 use_pattern (sinfo);
3265 fprintf (stderr, "Shared %d out of %d states by creating %d new states,"
3266 " saving %d\n",
3267 pattern_use_states, states.length (), pattern_def_states,
3268 pattern_use_states - pattern_def_states);
3271 /* Information about a state tree that we're considering splitting into a
3272 subroutine. */
3273 struct state_size
3275 /* The number of pseudo-statements in the state tree. */
3276 unsigned int num_statements;
3278 /* The approximate number of nested "if" and "switch" statements that
3279 would be required if control could fall through to a later state. */
3280 unsigned int depth;
3283 /* Pairs a transition with information about its target state. */
3284 typedef std::pair <transition *, state_size> subroutine_candidate;
3286 /* Sort two subroutine_candidates so that the one with the largest
3287 number of statements comes last. */
3289 static int
3290 subroutine_candidate_cmp (const void *a, const void *b)
3292 return int (((const subroutine_candidate *) a)->second.num_statements
3293 - ((const subroutine_candidate *) b)->second.num_statements);
3296 /* Turn S into a subroutine of type TYPE and add it to PROCS. Return a new
3297 state that performs a subroutine call to S. */
3299 static state *
3300 create_subroutine (routine_type type, state *s, vec <state *> &procs)
3302 procs.safe_push (s);
3303 acceptance_type acceptance;
3304 acceptance.type = type;
3305 acceptance.partial_p = true;
3306 acceptance.u.subroutine_id = procs.length ();
3307 state *news = new state;
3308 add_decision (news, rtx_test::accept (acceptance), true, false);
3309 return news;
3312 /* Walk state tree S, of type TYPE, and look for subtrees that would be
3313 better split into subroutines. Accumulate all such subroutines in PROCS.
3314 Return the size of the new state tree (excluding subroutines). */
3316 static state_size
3317 find_subroutines (routine_type type, state *s, vec <state *> &procs)
3319 auto_vec <subroutine_candidate, 16> candidates;
3320 state_size size;
3321 size.num_statements = 0;
3322 size.depth = 0;
3323 for (decision *d = s->first; d; d = d->next)
3325 if (!d->test.single_outcome_p ())
3326 size.num_statements += 1;
3327 for (transition *trans = d->first; trans; trans = trans->next)
3329 /* Keep chains of simple decisions together if we know that no
3330 change of position is required. We'll output this chain as a
3331 single "if" statement, so it counts as a single nesting level. */
3332 if (d->test.pos && d->if_statement_p ())
3333 for (;;)
3335 decision *newd = trans->to->singleton ();
3336 if (!newd
3337 || (newd->test.pos
3338 && newd->test.pos_operand < 0
3339 && newd->test.pos != d->test.pos)
3340 || !newd->if_statement_p ())
3341 break;
3342 if (!newd->test.single_outcome_p ())
3343 size.num_statements += 1;
3344 trans = newd->singleton ();
3345 if (newd->test.kind == rtx_test::SET_OP
3346 || newd->test.kind == rtx_test::ACCEPT)
3347 break;
3349 /* The target of TRANS is a subroutine candidate. First recurse
3350 on it to see how big it is after subroutines have been
3351 split out. */
3352 state_size to_size = find_subroutines (type, trans->to, procs);
3353 if (d->next && to_size.depth > MAX_DEPTH)
3354 /* Keeping the target state in the same routine would lead
3355 to an excessive nesting of "if" and "switch" statements.
3356 Split it out into a subroutine so that it can use
3357 inverted tests that return early on failure. */
3358 trans->to = create_subroutine (type, trans->to, procs);
3359 else
3361 size.num_statements += to_size.num_statements;
3362 if (to_size.num_statements < MIN_NUM_STATEMENTS)
3363 /* The target state is too small to be worth splitting.
3364 Keep it in the same routine as S. */
3365 size.depth = MAX (size.depth, to_size.depth);
3366 else
3367 /* Assume for now that we'll keep the target state in the
3368 same routine as S, but record it as a subroutine candidate
3369 if S grows too big. */
3370 candidates.safe_push (subroutine_candidate (trans, to_size));
3374 if (size.num_statements > MAX_NUM_STATEMENTS)
3376 /* S is too big. Sort the subroutine candidates so that bigger ones
3377 are nearer the end. */
3378 candidates.qsort (subroutine_candidate_cmp);
3379 while (!candidates.is_empty ()
3380 && size.num_statements > MAX_NUM_STATEMENTS)
3382 /* Peel off a candidate and force it into a subroutine. */
3383 subroutine_candidate cand = candidates.pop ();
3384 size.num_statements -= cand.second.num_statements;
3385 cand.first->to = create_subroutine (type, cand.first->to, procs);
3388 /* Update the depth for subroutine candidates that we decided not to
3389 split out. */
3390 for (unsigned int i = 0; i < candidates.length (); ++i)
3391 size.depth = MAX (size.depth, candidates[i].second.depth);
3392 size.depth += 1;
3393 return size;
3396 /* Return true if, for all X, PRED (X, MODE) implies that X has mode MODE. */
3398 static bool
3399 safe_predicate_mode (const struct pred_data *pred, machine_mode mode)
3401 /* Scalar integer constants have VOIDmode. */
3402 if (GET_MODE_CLASS (mode) == MODE_INT
3403 && (pred->codes[CONST_INT]
3404 || pred->codes[CONST_DOUBLE]
3405 || pred->codes[CONST_WIDE_INT]))
3406 return false;
3408 return !pred->special && mode != VOIDmode;
3411 /* Fill CODES with the set of codes that could be matched by PRED. */
3413 static void
3414 get_predicate_codes (const struct pred_data *pred, int_set *codes)
3416 for (int i = 0; i < NUM_TRUE_RTX_CODE; ++i)
3417 if (!pred || pred->codes[i])
3418 codes->safe_push (i);
3421 /* Return true if the first path through D1 tests the same thing as D2. */
3423 static bool
3424 has_same_test_p (decision *d1, decision *d2)
3428 if (d1->test == d2->test)
3429 return true;
3430 d1 = d1->first->to->first;
3432 while (d1);
3433 return false;
3436 /* Return true if D1 and D2 cannot match the same rtx. All states reachable
3437 from D2 have single decisions and all those decisions have single
3438 transitions. */
3440 static bool
3441 mutually_exclusive_p (decision *d1, decision *d2)
3443 /* If one path through D1 fails to test the same thing as D2, assume
3444 that D2's test could be true for D1 and look for a later, more useful,
3445 test. This isn't as expensive as it looks in practice. */
3446 while (!has_same_test_p (d1, d2))
3448 d2 = d2->singleton ()->to->singleton ();
3449 if (!d2)
3450 return false;
3452 if (d1->test == d2->test)
3454 /* Look for any transitions from D1 that have the same labels as
3455 the transition from D2. */
3456 transition *trans2 = d2->singleton ();
3457 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3459 int_set::iterator i1 = trans1->labels.begin ();
3460 int_set::iterator end1 = trans1->labels.end ();
3461 int_set::iterator i2 = trans2->labels.begin ();
3462 int_set::iterator end2 = trans2->labels.end ();
3463 while (i1 != end1 && i2 != end2)
3464 if (*i1 < *i2)
3465 ++i1;
3466 else if (*i2 < *i1)
3467 ++i2;
3468 else
3470 /* TRANS1 has some labels in common with TRANS2. Assume
3471 that D1 and D2 could match the same rtx if the target
3472 of TRANS1 could match the same rtx as D2. */
3473 for (decision *subd1 = trans1->to->first;
3474 subd1; subd1 = subd1->next)
3475 if (!mutually_exclusive_p (subd1, d2))
3476 return false;
3477 break;
3480 return true;
3482 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3483 for (decision *subd1 = trans1->to->first; subd1; subd1 = subd1->next)
3484 if (!mutually_exclusive_p (subd1, d2))
3485 return false;
3486 return true;
3489 /* Try to merge S2's decision into D1, given that they have the same test.
3490 Fail only if EXCLUDE is nonnull and the new transition would have the
3491 same labels as *EXCLUDE. When returning true, set *NEXT_S1, *NEXT_S2
3492 and *NEXT_EXCLUDE as for merge_into_state_1, or set *NEXT_S2 to null
3493 if the merge is complete. */
3495 static bool
3496 merge_into_decision (decision *d1, state *s2, const int_set *exclude,
3497 state **next_s1, state **next_s2,
3498 const int_set **next_exclude)
3500 decision *d2 = s2->singleton ();
3501 transition *trans2 = d2->singleton ();
3503 /* Get a list of the transitions that intersect TRANS2. */
3504 auto_vec <transition *, 32> intersecting;
3505 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3507 int_set::iterator i1 = trans1->labels.begin ();
3508 int_set::iterator end1 = trans1->labels.end ();
3509 int_set::iterator i2 = trans2->labels.begin ();
3510 int_set::iterator end2 = trans2->labels.end ();
3511 bool trans1_is_subset = true;
3512 bool trans2_is_subset = true;
3513 bool intersect_p = false;
3514 while (i1 != end1 && i2 != end2)
3515 if (*i1 < *i2)
3517 trans1_is_subset = false;
3518 ++i1;
3520 else if (*i2 < *i1)
3522 trans2_is_subset = false;
3523 ++i2;
3525 else
3527 intersect_p = true;
3528 ++i1;
3529 ++i2;
3531 if (i1 != end1)
3532 trans1_is_subset = false;
3533 if (i2 != end2)
3534 trans2_is_subset = false;
3535 if (trans1_is_subset && trans2_is_subset)
3537 /* There's already a transition that matches exactly.
3538 Merge the target states. */
3539 trans1->optional &= trans2->optional;
3540 *next_s1 = trans1->to;
3541 *next_s2 = trans2->to;
3542 *next_exclude = 0;
3543 return true;
3545 if (trans2_is_subset)
3547 /* TRANS1 has all the labels that TRANS2 needs. Merge S2 into
3548 the target of TRANS1, but (to avoid infinite recursion)
3549 make sure that we don't end up creating another transition
3550 like TRANS1. */
3551 *next_s1 = trans1->to;
3552 *next_s2 = s2;
3553 *next_exclude = &trans1->labels;
3554 return true;
3556 if (intersect_p)
3557 intersecting.safe_push (trans1);
3560 if (intersecting.is_empty ())
3562 /* No existing labels intersect the new ones. We can just add
3563 TRANS2 itself. */
3564 d1->push_back (d2->release ());
3565 *next_s1 = 0;
3566 *next_s2 = 0;
3567 *next_exclude = 0;
3568 return true;
3571 /* Take the union of the labels in INTERSECTING and TRANS2. Store the
3572 result in COMBINED and use NEXT as a temporary. */
3573 int_set tmp1 = trans2->labels, tmp2;
3574 int_set *combined = &tmp1, *next = &tmp2;
3575 for (unsigned int i = 0; i < intersecting.length (); ++i)
3577 transition *trans1 = intersecting[i];
3578 next->truncate (0);
3579 next->safe_grow (trans1->labels.length () + combined->length ());
3580 int_set::iterator end
3581 = std::set_union (trans1->labels.begin (), trans1->labels.end (),
3582 combined->begin (), combined->end (),
3583 next->begin ());
3584 next->truncate (end - next->begin ());
3585 std::swap (next, combined);
3588 /* Stop now if we've been told not to create a transition with these
3589 labels. */
3590 if (exclude && *combined == *exclude)
3591 return false;
3593 /* Get the transition that should carry the new labels. */
3594 transition *new_trans = intersecting[0];
3595 if (intersecting.length () == 1)
3597 /* We're merging with one existing transition whose labels are a
3598 subset of those required. If both transitions are optional,
3599 we can just expand the set of labels so that it's suitable
3600 for both transitions. It isn't worth preserving the original
3601 transitions since we know that they can't be merged; we would
3602 need to backtrack to S2 if TRANS1->to fails. In contrast,
3603 we might be able to merge the targets of the transitions
3604 without any backtracking.
3606 If instead the existing transition is not optional, ensure that
3607 all target decisions are suitably protected. Some decisions
3608 might already have a more specific requirement than NEW_TRANS,
3609 in which case there's no point testing NEW_TRANS as well. E.g. this
3610 would have happened if a test for an (eq ...) rtx had been
3611 added to a decision that tested whether the code is suitable
3612 for comparison_operator. The original comparison_operator
3613 transition would have been non-optional and the (eq ...) test
3614 would be performed by a second decision in the target of that
3615 transition.
3617 The remaining case -- keeping the original optional transition
3618 when adding a non-optional TRANS2 -- is a wash. Preserving
3619 the optional transition only helps if we later merge another
3620 state S3 that is mutually exclusive with S2 and whose labels
3621 belong to *COMBINED - TRANS1->labels. We can then test the
3622 original NEW_TRANS and S3 in the same decision. We keep the
3623 optional transition around for that case, but it occurs very
3624 rarely. */
3625 gcc_assert (new_trans->labels != *combined);
3626 if (!new_trans->optional || !trans2->optional)
3628 decision *start = 0;
3629 for (decision *end = new_trans->to->first; end; end = end->next)
3631 if (!start && end->test != d1->test)
3632 /* END belongs to a range of decisions that need to be
3633 protected by NEW_TRANS. */
3634 start = end;
3635 if (start && (!end->next || end->next->test == d1->test))
3637 /* Protect [START, END] with NEW_TRANS. The decisions
3638 move to NEW_S and NEW_D becomes part of NEW_TRANS->to. */
3639 state *new_s = new state;
3640 decision *new_d = new decision (d1->test);
3641 new_d->push_back (new transition (new_trans->labels, new_s,
3642 new_trans->optional));
3643 state::range r (start, end);
3644 new_trans->to->replace (r, new_d);
3645 new_s->push_back (r);
3647 /* Continue with an empty range. */
3648 start = 0;
3650 /* Continue from the decision after NEW_D. */
3651 end = new_d;
3655 new_trans->optional = true;
3656 new_trans->labels = *combined;
3658 else
3660 /* We're merging more than one existing transition together.
3661 Those transitions are successfully dividing the matching space
3662 and so we want to preserve them, even if they're optional.
3664 Create a new transition with the union set of labels and make
3665 it go to a state that has the original transitions. */
3666 decision *new_d = new decision (d1->test);
3667 for (unsigned int i = 0; i < intersecting.length (); ++i)
3668 new_d->push_back (d1->remove (intersecting[i]));
3670 state *new_s = new state;
3671 new_s->push_back (new_d);
3673 new_trans = new transition (*combined, new_s, true);
3674 d1->push_back (new_trans);
3677 /* We now have an optional transition with labels *COMBINED. Decide
3678 whether we can use it as TRANS2 or whether we need to merge S2
3679 into the target of NEW_TRANS. */
3680 gcc_assert (new_trans->optional);
3681 if (new_trans->labels == trans2->labels)
3683 /* NEW_TRANS matches TRANS2. Just merge the target states. */
3684 new_trans->optional = trans2->optional;
3685 *next_s1 = new_trans->to;
3686 *next_s2 = trans2->to;
3687 *next_exclude = 0;
3689 else
3691 /* Try to merge TRANS2 into the target of the overlapping transition,
3692 but (to prevent infinite recursion or excessive redundancy) without
3693 creating another transition of the same type. */
3694 *next_s1 = new_trans->to;
3695 *next_s2 = s2;
3696 *next_exclude = &new_trans->labels;
3698 return true;
3701 /* Make progress in merging S2 into S1, given that each state in S2
3702 has a single decision. If EXCLUDE is nonnull, avoid creating a new
3703 transition with the same test as S2's decision and with the labels
3704 in *EXCLUDE.
3706 Return true if there is still work to do. When returning true,
3707 set *NEXT_S1, *NEXT_S2 and *NEXT_EXCLUDE to the values that
3708 S1, S2 and EXCLUDE should have next time round.
3710 If S1 and S2 both match a particular rtx, give priority to S1. */
3712 static bool
3713 merge_into_state_1 (state *s1, state *s2, const int_set *exclude,
3714 state **next_s1, state **next_s2,
3715 const int_set **next_exclude)
3717 decision *d2 = s2->singleton ();
3718 if (decision *d1 = s1->last)
3720 if (d1->test.terminal_p ())
3721 /* D1 is an unconditional return, so S2 can never match. This can
3722 sometimes be a bug in the .md description, but might also happen
3723 if genconditions forces some conditions to true for certain
3724 configurations. */
3725 return false;
3727 /* Go backwards through the decisions in S1, stopping once we find one
3728 that could match the same thing as S2. */
3729 while (d1->prev && mutually_exclusive_p (d1, d2))
3730 d1 = d1->prev;
3732 /* Search forwards from that point, merging D2 into the first
3733 decision we can. */
3734 for (; d1; d1 = d1->next)
3736 /* If S2 performs some optional tests before testing the same thing
3737 as D1, those tests do not help to distinguish D1 and S2, so it's
3738 better to drop them. Search through such optional decisions
3739 until we find something that tests the same thing as D1. */
3740 state *sub_s2 = s2;
3741 for (;;)
3743 decision *sub_d2 = sub_s2->singleton ();
3744 if (d1->test == sub_d2->test)
3746 /* Only apply EXCLUDE if we're testing the same thing
3747 as D2. */
3748 const int_set *sub_exclude = (d2 == sub_d2 ? exclude : 0);
3750 /* Try to merge SUB_S2 into D1. This can only fail if
3751 it would involve creating a new transition with
3752 labels SUB_EXCLUDE. */
3753 if (merge_into_decision (d1, sub_s2, sub_exclude,
3754 next_s1, next_s2, next_exclude))
3755 return *next_s2 != 0;
3757 /* Can't merge with D1; try a later decision. */
3758 break;
3760 transition *sub_trans2 = sub_d2->singleton ();
3761 if (!sub_trans2->optional)
3762 /* Can't merge with D1; try a later decision. */
3763 break;
3764 sub_s2 = sub_trans2->to;
3769 /* We can't merge D2 with any existing decision. Just add it to the end. */
3770 s1->push_back (s2->release ());
3771 return false;
3774 /* Merge S2 into S1. If they both match a particular rtx, give
3775 priority to S1. Each state in S2 has a single decision. */
3777 static void
3778 merge_into_state (state *s1, state *s2)
3780 const int_set *exclude = 0;
3781 while (s2 && merge_into_state_1 (s1, s2, exclude, &s1, &s2, &exclude))
3782 continue;
3785 /* Pairs a pattern that needs to be matched with the rtx position at
3786 which the pattern should occur. */
3787 struct pattern_pos {
3788 pattern_pos () {}
3789 pattern_pos (rtx, position *);
3791 rtx pattern;
3792 position *pos;
3795 pattern_pos::pattern_pos (rtx pattern_in, position *pos_in)
3796 : pattern (pattern_in), pos (pos_in)
3799 /* Compare entries according to their depth-first order. There shouldn't
3800 be two entries at the same position. */
3802 bool
3803 operator < (const pattern_pos &e1, const pattern_pos &e2)
3805 int diff = compare_positions (e1.pos, e2.pos);
3806 gcc_assert (diff != 0 || e1.pattern == e2.pattern);
3807 return diff < 0;
3810 /* Return the name of the predicate matched by MATCH_RTX. */
3812 static const char *
3813 predicate_name (rtx match_rtx)
3815 if (GET_CODE (match_rtx) == MATCH_SCRATCH)
3816 return "scratch_operand";
3817 else
3818 return XSTR (match_rtx, 1);
3821 /* Add new decisions to S that check whether the rtx at position POS
3822 matches PATTERN. Return the state that is reached in that case.
3823 TOP_PATTERN is the overall pattern, as passed to match_pattern_1. */
3825 static state *
3826 match_pattern_2 (state *s, rtx top_pattern, position *pos, rtx pattern)
3828 auto_vec <pattern_pos, 32> worklist;
3829 auto_vec <pattern_pos, 32> pred_and_mode_tests;
3830 auto_vec <pattern_pos, 32> dup_tests;
3832 worklist.safe_push (pattern_pos (pattern, pos));
3833 while (!worklist.is_empty ())
3835 pattern_pos next = worklist.pop ();
3836 pattern = next.pattern;
3837 pos = next.pos;
3838 unsigned int reverse_s = worklist.length ();
3840 enum rtx_code code = GET_CODE (pattern);
3841 switch (code)
3843 case MATCH_OP_DUP:
3844 case MATCH_DUP:
3845 case MATCH_PAR_DUP:
3846 /* Add a test that the rtx matches the earlier one, but only
3847 after the structure and predicates have been checked. */
3848 dup_tests.safe_push (pattern_pos (pattern, pos));
3850 /* Use the same code check as the original operand. */
3851 pattern = find_operand (top_pattern, XINT (pattern, 0), NULL_RTX);
3852 /* Fall through. */
3854 case MATCH_PARALLEL:
3855 case MATCH_OPERAND:
3856 case MATCH_SCRATCH:
3857 case MATCH_OPERATOR:
3859 const char *pred_name = predicate_name (pattern);
3860 const struct pred_data *pred = 0;
3861 if (pred_name[0] != 0)
3863 pred = lookup_predicate (pred_name);
3864 /* Only report errors once per rtx. */
3865 if (code == GET_CODE (pattern))
3867 if (!pred)
3868 error_with_line (pattern_lineno,
3869 "unknown predicate '%s'"
3870 " in '%s' expression",
3871 pred_name, GET_RTX_NAME (code));
3872 else if (code == MATCH_PARALLEL
3873 && pred->singleton != PARALLEL)
3874 error_with_line (pattern_lineno,
3875 "predicate '%s' used in match_parallel"
3876 " does not allow only PARALLEL",
3877 pred->name);
3881 if (code == MATCH_PARALLEL || code == MATCH_PAR_DUP)
3883 /* Check that we have a parallel with enough elements. */
3884 s = add_decision (s, rtx_test::code (pos), PARALLEL, false);
3885 int min_len = XVECLEN (pattern, 2);
3886 s = add_decision (s, rtx_test::veclen_ge (pos, min_len),
3887 true, false);
3889 else
3891 /* Check that the rtx has one of codes accepted by the
3892 predicate. This is necessary when matching suboperands
3893 of a MATCH_OPERATOR or MATCH_OP_DUP, since we can't
3894 call XEXP (X, N) without checking that X has at least
3895 N+1 operands. */
3896 int_set codes;
3897 get_predicate_codes (pred, &codes);
3898 bool need_codes = (pred
3899 && (code == MATCH_OPERATOR
3900 || code == MATCH_OP_DUP));
3901 s = add_decision (s, rtx_test::code (pos), codes, !need_codes);
3904 /* Postpone the predicate check until we've checked the rest
3905 of the rtx structure. */
3906 if (code == GET_CODE (pattern))
3907 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3909 /* If we need to match suboperands, add them to the worklist. */
3910 if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
3912 position **subpos_ptr;
3913 enum position_type pos_type;
3914 int i;
3915 if (code == MATCH_OPERATOR || code == MATCH_OP_DUP)
3917 pos_type = POS_XEXP;
3918 subpos_ptr = &pos->xexps;
3919 i = (code == MATCH_OPERATOR ? 2 : 1);
3921 else
3923 pos_type = POS_XVECEXP0;
3924 subpos_ptr = &pos->xvecexp0s;
3925 i = 2;
3927 for (int j = 0; j < XVECLEN (pattern, i); ++j)
3929 position *subpos = next_position (subpos_ptr, pos,
3930 pos_type, j);
3931 worklist.safe_push (pattern_pos (XVECEXP (pattern, i, j),
3932 subpos));
3933 subpos_ptr = &subpos->next;
3936 break;
3939 default:
3941 /* Check that the rtx has the right code. */
3942 s = add_decision (s, rtx_test::code (pos), code, false);
3944 /* Queue a test for the mode if one is specified. */
3945 if (GET_MODE (pattern) != VOIDmode)
3946 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3948 /* Push subrtxes onto the worklist. Match nonrtx operands now. */
3949 const char *fmt = GET_RTX_FORMAT (code);
3950 position **subpos_ptr = &pos->xexps;
3951 for (size_t i = 0; fmt[i]; ++i)
3953 position *subpos = next_position (subpos_ptr, pos,
3954 POS_XEXP, i);
3955 switch (fmt[i])
3957 case 'e': case 'u':
3958 worklist.safe_push (pattern_pos (XEXP (pattern, i),
3959 subpos));
3960 break;
3962 case 'E':
3964 /* Make sure the vector has the right number of
3965 elements. */
3966 int length = XVECLEN (pattern, i);
3967 s = add_decision (s, rtx_test::veclen (pos),
3968 length, false);
3970 position **subpos2_ptr = &pos->xvecexp0s;
3971 for (int j = 0; j < length; j++)
3973 position *subpos2 = next_position (subpos2_ptr, pos,
3974 POS_XVECEXP0, j);
3975 rtx x = XVECEXP (pattern, i, j);
3976 worklist.safe_push (pattern_pos (x, subpos2));
3977 subpos2_ptr = &subpos2->next;
3979 break;
3982 case 'i':
3983 /* Make sure that XINT (X, I) has the right value. */
3984 s = add_decision (s, rtx_test::int_field (pos, i),
3985 XINT (pattern, i), false);
3986 break;
3988 case 'r':
3989 /* Make sure that REGNO (X) has the right value. */
3990 gcc_assert (i == 0);
3991 s = add_decision (s, rtx_test::regno_field (pos),
3992 REGNO (pattern), false);
3993 break;
3995 case 'w':
3996 /* Make sure that XWINT (X, I) has the right value. */
3997 s = add_decision (s, rtx_test::wide_int_field (pos, i),
3998 XWINT (pattern, 0), false);
3999 break;
4001 case '0':
4002 break;
4004 default:
4005 gcc_unreachable ();
4007 subpos_ptr = &subpos->next;
4010 break;
4012 /* Operands are pushed onto the worklist so that later indices are
4013 nearer the top. That's what we want for SETs, since a SET_SRC
4014 is a better discriminator than a SET_DEST. In other cases it's
4015 usually better to match earlier indices first. This is especially
4016 true of PARALLELs, where the first element tends to be the most
4017 individual. It's also true for commutative operators, where the
4018 canonicalization rules say that the more complex operand should
4019 come first. */
4020 if (code != SET && worklist.length () > reverse_s)
4021 std::reverse (&worklist[0] + reverse_s,
4022 &worklist[0] + worklist.length ());
4025 /* Sort the predicate and mode tests so that they're in depth-first order.
4026 The main goal of this is to put SET_SRC match_operands after SET_DEST
4027 match_operands and after mode checks for the enclosing SET_SRC operators
4028 (such as the mode of a PLUS in an addition instruction). The latter
4029 two types of test can determine the mode exactly, whereas a SET_SRC
4030 match_operand often has to cope with the possibility of the operand
4031 being a modeless constant integer. E.g. something that matches
4032 register_operand (x, SImode) never matches register_operand (x, DImode),
4033 but a const_int that matches immediate_operand (x, SImode) also matches
4034 immediate_operand (x, DImode). The register_operand cases can therefore
4035 be distinguished by a switch on the mode, but the immediate_operand
4036 cases can't. */
4037 if (pred_and_mode_tests.length () > 1)
4038 std::sort (&pred_and_mode_tests[0],
4039 &pred_and_mode_tests[0] + pred_and_mode_tests.length ());
4041 /* Add the mode and predicate tests. */
4042 pattern_pos *e;
4043 unsigned int i;
4044 FOR_EACH_VEC_ELT (pred_and_mode_tests, i, e)
4046 switch (GET_CODE (e->pattern))
4048 case MATCH_PARALLEL:
4049 case MATCH_OPERAND:
4050 case MATCH_SCRATCH:
4051 case MATCH_OPERATOR:
4053 int opno = XINT (e->pattern, 0);
4054 num_operands = MAX (num_operands, opno + 1);
4055 const char *pred_name = predicate_name (e->pattern);
4056 if (pred_name[0])
4058 const struct pred_data *pred = lookup_predicate (pred_name);
4059 /* Check the mode first, to distinguish things like SImode
4060 and DImode register_operands, as described above. */
4061 machine_mode mode = GET_MODE (e->pattern);
4062 if (safe_predicate_mode (pred, mode))
4063 s = add_decision (s, rtx_test::mode (e->pos), mode, true);
4065 /* Assign to operands[] first, so that the rtx usually doesn't
4066 need to be live across the call to the predicate.
4068 This shouldn't cause a problem with dirtying the page,
4069 since we fully expect to assign to operands[] at some point,
4070 and since the caller usually writes to other parts of
4071 recog_data anyway. */
4072 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4073 true, false);
4074 s = add_decision (s, rtx_test::predicate (e->pos, pred, mode),
4075 true, false);
4077 else
4078 /* Historically we've ignored the mode when there's no
4079 predicate. Just set up operands[] unconditionally. */
4080 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4081 true, false);
4082 break;
4085 default:
4086 s = add_decision (s, rtx_test::mode (e->pos),
4087 GET_MODE (e->pattern), false);
4088 break;
4092 /* Finally add rtx_equal_p checks for duplicated operands. */
4093 FOR_EACH_VEC_ELT (dup_tests, i, e)
4094 s = add_decision (s, rtx_test::duplicate (e->pos, XINT (e->pattern, 0)),
4095 true, false);
4096 return s;
4099 /* Add new decisions to S that make it return ACCEPTANCE if:
4101 (1) the rtx doesn't match anything already matched by S
4102 (2) the rtx matches TOP_PATTERN and
4103 (3) C_TEST is true.
4105 For peephole2, TOP_PATTERN is a SEQUENCE of the instruction patterns
4106 to match, otherwise it is a single instruction pattern. */
4108 static void
4109 match_pattern_1 (state *s, rtx top_pattern, const char *c_test,
4110 acceptance_type acceptance)
4112 if (acceptance.type == PEEPHOLE2)
4114 /* Match each individual instruction. */
4115 position **subpos_ptr = &peep2_insn_pos_list;
4116 int count = 0;
4117 for (int i = 0; i < XVECLEN (top_pattern, 0); ++i)
4119 rtx x = XVECEXP (top_pattern, 0, i);
4120 position *subpos = next_position (subpos_ptr, &root_pos,
4121 POS_PEEP2_INSN, count);
4122 if (count > 0)
4123 s = add_decision (s, rtx_test::peep2_count (count + 1),
4124 true, false);
4125 s = match_pattern_2 (s, top_pattern, subpos, x);
4126 subpos_ptr = &subpos->next;
4127 count += 1;
4129 acceptance.u.full.u.match_len = count - 1;
4131 else
4133 /* Make the rtx itself. */
4134 s = match_pattern_2 (s, top_pattern, &root_pos, top_pattern);
4136 /* If the match is only valid when extra clobbers are added,
4137 make sure we're able to pass that information to the caller. */
4138 if (acceptance.type == RECOG && acceptance.u.full.u.num_clobbers)
4139 s = add_decision (s, rtx_test::have_num_clobbers (), true, false);
4142 /* Make sure that the C test is true. */
4143 if (maybe_eval_c_test (c_test) != 1)
4144 s = add_decision (s, rtx_test::c_test (c_test), true, false);
4146 /* Accept the pattern. */
4147 add_decision (s, rtx_test::accept (acceptance), true, false);
4150 /* Like match_pattern_1, but (if merge_states_p) try to merge the
4151 decisions with what's already in S, to reduce the amount of
4152 backtracking. */
4154 static void
4155 match_pattern (state *s, rtx top_pattern, const char *c_test,
4156 acceptance_type acceptance)
4158 if (merge_states_p)
4160 state root;
4161 /* Add the decisions to a fresh state and then merge the full tree
4162 into the existing one. */
4163 match_pattern_1 (&root, top_pattern, c_test, acceptance);
4164 merge_into_state (s, &root);
4166 else
4167 match_pattern_1 (s, top_pattern, c_test, acceptance);
4170 /* Begin the output file. */
4172 static void
4173 write_header (void)
4175 puts ("\
4176 /* Generated automatically by the program `genrecog' from the target\n\
4177 machine description file. */\n\
4179 #include \"config.h\"\n\
4180 #include \"system.h\"\n\
4181 #include \"coretypes.h\"\n\
4182 #include \"tm.h\"\n\
4183 #include \"rtl.h\"\n\
4184 #include \"tm_p.h\"\n\
4185 #include \"hashtab.h\"\n\
4186 #include \"hash-set.h\"\n\
4187 #include \"vec.h\"\n\
4188 #include \"machmode.h\"\n\
4189 #include \"hard-reg-set.h\"\n\
4190 #include \"input.h\"\n\
4191 #include \"function.h\"\n\
4192 #include \"emit-rtl.h\"\n\
4193 #include \"insn-config.h\"\n\
4194 #include \"recog.h\"\n\
4195 #include \"output.h\"\n\
4196 #include \"flags.h\"\n\
4197 #include \"hard-reg-set.h\"\n\
4198 #include \"predict.h\"\n\
4199 #include \"basic-block.h\"\n\
4200 #include \"resource.h\"\n\
4201 #include \"diagnostic-core.h\"\n\
4202 #include \"reload.h\"\n\
4203 #include \"regs.h\"\n\
4204 #include \"tm-constrs.h\"\n\
4205 #include \"predict.h\"\n\
4206 \n");
4208 puts ("\n\
4209 /* `recog' contains a decision tree that recognizes whether the rtx\n\
4210 X0 is a valid instruction.\n\
4212 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
4213 returns a nonnegative number which is the insn code number for the\n\
4214 pattern that matched. This is the same as the order in the machine\n\
4215 description of the entry that matched. This number can be used as an\n\
4216 index into `insn_data' and other tables.\n");
4217 puts ("\
4218 The third parameter to recog is an optional pointer to an int. If\n\
4219 present, recog will accept a pattern if it matches except for missing\n\
4220 CLOBBER expressions at the end. In that case, the value pointed to by\n\
4221 the optional pointer will be set to the number of CLOBBERs that need\n\
4222 to be added (it should be initialized to zero by the caller). If it");
4223 puts ("\
4224 is set nonzero, the caller should allocate a PARALLEL of the\n\
4225 appropriate size, copy the initial entries, and call add_clobbers\n\
4226 (found in insn-emit.c) to fill in the CLOBBERs.\n\
4229 puts ("\n\
4230 The function split_insns returns 0 if the rtl could not\n\
4231 be split or the split rtl as an INSN list if it can be.\n\
4233 The function peephole2_insns returns 0 if the rtl could not\n\
4234 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
4235 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
4236 */\n\n");
4239 /* Return the C type of a parameter with type TYPE. */
4241 static const char *
4242 parameter_type_string (parameter::type_enum type)
4244 switch (type)
4246 case parameter::UNSET:
4247 break;
4249 case parameter::CODE:
4250 return "rtx_code";
4252 case parameter::MODE:
4253 return "machine_mode";
4255 case parameter::INT:
4256 return "int";
4258 case parameter::UINT:
4259 return "unsigned int";
4261 case parameter::WIDE_INT:
4262 return "HOST_WIDE_INT";
4264 gcc_unreachable ();
4267 /* Return true if ACCEPTANCE requires only a single C statement even in
4268 a backtracking context. */
4270 static bool
4271 single_statement_p (const acceptance_type &acceptance)
4273 if (acceptance.partial_p)
4274 /* We need to handle failures of the subroutine. */
4275 return false;
4276 switch (acceptance.type)
4278 case SUBPATTERN:
4279 case SPLIT:
4280 return true;
4282 case RECOG:
4283 /* False if we need to assign to pnum_clobbers. */
4284 return acceptance.u.full.u.num_clobbers == 0;
4286 case PEEPHOLE2:
4287 /* We need to assign to pmatch_len_ and handle null returns from the
4288 peephole2 routine. */
4289 return false;
4291 gcc_unreachable ();
4294 /* Return the C failure value for a routine of type TYPE. */
4296 static const char *
4297 get_failure_return (routine_type type)
4299 switch (type)
4301 case SUBPATTERN:
4302 case RECOG:
4303 return "-1";
4305 case SPLIT:
4306 case PEEPHOLE2:
4307 return "NULL";
4309 gcc_unreachable ();
4312 /* Indicates whether a block of code always returns or whether it can fall
4313 through. */
4315 enum exit_state {
4316 ES_RETURNED,
4317 ES_FALLTHROUGH
4320 /* Information used while writing out code. */
4322 struct output_state
4324 /* The type of routine that we're generating. */
4325 routine_type type;
4327 /* Maps position ids to xN variable numbers. The entry is only valid if
4328 it is less than the length of VAR_TO_ID, but this holds for every position
4329 tested by a state when writing out that state. */
4330 auto_vec <unsigned int> id_to_var;
4332 /* Maps xN variable numbers to position ids. */
4333 auto_vec <unsigned int> var_to_id;
4335 /* Index N is true if variable xN has already been set. */
4336 auto_vec <bool> seen_vars;
4339 /* Return true if D is a call to a pattern routine and if there is some X
4340 such that the transition for pattern result N goes to a successful return
4341 with code X+N. When returning true, set *BASE_OUT to this X and *COUNT_OUT
4342 to the number of return values. (We know that every PATTERN decision has
4343 a transition for every successful return.) */
4345 static bool
4346 terminal_pattern_p (decision *d, unsigned int *base_out,
4347 unsigned int *count_out)
4349 if (d->test.kind != rtx_test::PATTERN)
4350 return false;
4351 unsigned int base = 0;
4352 unsigned int count = 0;
4353 for (transition *trans = d->first; trans; trans = trans->next)
4355 if (trans->is_param || trans->labels.length () != 1)
4356 return false;
4357 decision *subd = trans->to->singleton ();
4358 if (!subd || subd->test.kind != rtx_test::ACCEPT)
4359 return false;
4360 unsigned int this_base = (subd->test.u.acceptance.u.full.code
4361 - trans->labels[0]);
4362 if (trans == d->first)
4363 base = this_base;
4364 else if (base != this_base)
4365 return false;
4366 count += 1;
4368 *base_out = base;
4369 *count_out = count;
4370 return true;
4373 /* Return true if TEST doesn't test an rtx or if the rtx it tests is
4374 already available in state OS. */
4376 static bool
4377 test_position_available_p (output_state *os, const rtx_test &test)
4379 return (!test.pos
4380 || test.pos_operand >= 0
4381 || os->seen_vars[os->id_to_var[test.pos->id]]);
4384 /* Like printf, but print INDENT spaces at the beginning. */
4386 static void ATTRIBUTE_PRINTF_2
4387 printf_indent (unsigned int indent, const char *format, ...)
4389 va_list ap;
4390 va_start (ap, format);
4391 printf ("%*s", indent, "");
4392 vprintf (format, ap);
4393 va_end (ap);
4396 /* Emit code to initialize the variable associated with POS, if it isn't
4397 already valid in state OS. Indent each line by INDENT spaces. Update
4398 OS with the new state. */
4400 static void
4401 change_state (output_state *os, position *pos, unsigned int indent)
4403 unsigned int var = os->id_to_var[pos->id];
4404 gcc_assert (var < os->var_to_id.length () && os->var_to_id[var] == pos->id);
4405 if (os->seen_vars[var])
4406 return;
4407 switch (pos->type)
4409 case POS_PEEP2_INSN:
4410 printf_indent (indent, "x%d = PATTERN (peep2_next_insn (%d));\n",
4411 var, pos->arg);
4412 break;
4414 case POS_XEXP:
4415 change_state (os, pos->base, indent);
4416 printf_indent (indent, "x%d = XEXP (x%d, %d);\n",
4417 var, os->id_to_var[pos->base->id], pos->arg);
4418 break;
4420 case POS_XVECEXP0:
4421 change_state (os, pos->base, indent);
4422 printf_indent (indent, "x%d = XVECEXP (x%d, 0, %d);\n",
4423 var, os->id_to_var[pos->base->id], pos->arg);
4424 break;
4426 os->seen_vars[var] = true;
4429 /* Print the enumerator constant for CODE -- the upcase version of
4430 the name. */
4432 static void
4433 print_code (enum rtx_code code)
4435 const char *p;
4436 for (p = GET_RTX_NAME (code); *p; p++)
4437 putchar (TOUPPER (*p));
4440 /* Emit a uint64_t as an integer constant expression. We need to take
4441 special care to avoid "decimal constant is so large that it is unsigned"
4442 warnings in the resulting code. */
4444 static void
4445 print_host_wide_int (uint64_t val)
4447 uint64_t min = uint64_t (1) << (HOST_BITS_PER_WIDE_INT - 1);
4448 if (val == min)
4449 printf ("(" HOST_WIDE_INT_PRINT_DEC_C " - 1)", val + 1);
4450 else
4451 printf (HOST_WIDE_INT_PRINT_DEC_C, val);
4454 /* Print the C expression for actual parameter PARAM. */
4456 static void
4457 print_parameter_value (const parameter &param)
4459 if (param.is_param)
4460 printf ("i%d", (int) param.value + 1);
4461 else
4462 switch (param.type)
4464 case parameter::UNSET:
4465 gcc_unreachable ();
4466 break;
4468 case parameter::CODE:
4469 print_code ((enum rtx_code) param.value);
4470 break;
4472 case parameter::MODE:
4473 printf ("%smode", GET_MODE_NAME ((machine_mode) param.value));
4474 break;
4476 case parameter::INT:
4477 printf ("%d", (int) param.value);
4478 break;
4480 case parameter::UINT:
4481 printf ("%u", (unsigned int) param.value);
4482 break;
4484 case parameter::WIDE_INT:
4485 print_host_wide_int (param.value);
4486 break;
4490 /* Print the C expression for the rtx tested by TEST. */
4492 static void
4493 print_test_rtx (output_state *os, const rtx_test &test)
4495 if (test.pos_operand >= 0)
4496 printf ("operands[%d]", test.pos_operand);
4497 else
4498 printf ("x%d", os->id_to_var[test.pos->id]);
4501 /* Print the C expression for non-boolean test TEST. */
4503 static void
4504 print_nonbool_test (output_state *os, const rtx_test &test)
4506 switch (test.kind)
4508 case rtx_test::CODE:
4509 printf ("GET_CODE (");
4510 print_test_rtx (os, test);
4511 printf (")");
4512 break;
4514 case rtx_test::MODE:
4515 printf ("GET_MODE (");
4516 print_test_rtx (os, test);
4517 printf (")");
4518 break;
4520 case rtx_test::VECLEN:
4521 printf ("XVECLEN (");
4522 print_test_rtx (os, test);
4523 printf (", 0)");
4524 break;
4526 case rtx_test::INT_FIELD:
4527 printf ("XINT (");
4528 print_test_rtx (os, test);
4529 printf (", %d)", test.u.opno);
4530 break;
4532 case rtx_test::REGNO_FIELD:
4533 printf ("REGNO (");
4534 print_test_rtx (os, test);
4535 printf (")");
4536 break;
4538 case rtx_test::WIDE_INT_FIELD:
4539 printf ("XWINT (");
4540 print_test_rtx (os, test);
4541 printf (", %d)", test.u.opno);
4542 break;
4544 case rtx_test::PATTERN:
4546 pattern_routine *routine = test.u.pattern->routine;
4547 printf ("pattern%d (", routine->pattern_id);
4548 const char *sep = "";
4549 if (test.pos)
4551 print_test_rtx (os, test);
4552 sep = ", ";
4554 if (routine->insn_p)
4556 printf ("%sinsn", sep);
4557 sep = ", ";
4559 if (routine->pnum_clobbers_p)
4561 printf ("%spnum_clobbers", sep);
4562 sep = ", ";
4564 for (unsigned int i = 0; i < test.u.pattern->params.length (); ++i)
4566 fputs (sep, stdout);
4567 print_parameter_value (test.u.pattern->params[i]);
4568 sep = ", ";
4570 printf (")");
4571 break;
4574 case rtx_test::PEEP2_COUNT:
4575 case rtx_test::VECLEN_GE:
4576 case rtx_test::SAVED_CONST_INT:
4577 case rtx_test::DUPLICATE:
4578 case rtx_test::PREDICATE:
4579 case rtx_test::SET_OP:
4580 case rtx_test::HAVE_NUM_CLOBBERS:
4581 case rtx_test::C_TEST:
4582 case rtx_test::ACCEPT:
4583 gcc_unreachable ();
4587 /* IS_PARAM and LABEL are taken from a transition whose source
4588 decision performs TEST. Print the C code for the label. */
4590 static void
4591 print_label_value (const rtx_test &test, bool is_param, uint64_t value)
4593 print_parameter_value (parameter (transition_parameter_type (test.kind),
4594 is_param, value));
4597 /* If IS_PARAM, print code to compare TEST with the C variable i<VALUE+1>.
4598 If !IS_PARAM, print code to compare TEST with the C constant VALUE.
4599 Test for inequality if INVERT_P, otherwise test for equality. */
4601 static void
4602 print_test (output_state *os, const rtx_test &test, bool is_param,
4603 uint64_t value, bool invert_p)
4605 switch (test.kind)
4607 /* Handle the non-boolean TESTs. */
4608 case rtx_test::CODE:
4609 case rtx_test::MODE:
4610 case rtx_test::VECLEN:
4611 case rtx_test::REGNO_FIELD:
4612 case rtx_test::INT_FIELD:
4613 case rtx_test::WIDE_INT_FIELD:
4614 case rtx_test::PATTERN:
4615 print_nonbool_test (os, test);
4616 printf (" %s ", invert_p ? "!=" : "==");
4617 print_label_value (test, is_param, value);
4618 break;
4620 case rtx_test::SAVED_CONST_INT:
4621 gcc_assert (!is_param && value == 1);
4622 print_test_rtx (os, test);
4623 printf (" %s const_int_rtx[MAX_SAVED_CONST_INT + ",
4624 invert_p ? "!=" : "==");
4625 print_parameter_value (parameter (parameter::INT,
4626 test.u.integer.is_param,
4627 test.u.integer.value));
4628 printf ("]");
4629 break;
4631 case rtx_test::PEEP2_COUNT:
4632 gcc_assert (!is_param && value == 1);
4633 printf ("peep2_current_count %s %d", invert_p ? "<" : ">=",
4634 test.u.min_len);
4635 break;
4637 case rtx_test::VECLEN_GE:
4638 gcc_assert (!is_param && value == 1);
4639 printf ("XVECLEN (");
4640 print_test_rtx (os, test);
4641 printf (", 0) %s %d", invert_p ? "<" : ">=", test.u.min_len);
4642 break;
4644 case rtx_test::PREDICATE:
4645 gcc_assert (!is_param && value == 1);
4646 printf ("%s%s (", invert_p ? "!" : "", test.u.predicate.data->name);
4647 print_test_rtx (os, test);
4648 printf (", ");
4649 print_parameter_value (parameter (parameter::MODE,
4650 test.u.predicate.mode_is_param,
4651 test.u.predicate.mode));
4652 printf (")");
4653 break;
4655 case rtx_test::DUPLICATE:
4656 gcc_assert (!is_param && value == 1);
4657 printf ("%srtx_equal_p (", invert_p ? "!" : "");
4658 print_test_rtx (os, test);
4659 printf (", operands[%d])", test.u.opno);
4660 break;
4662 case rtx_test::HAVE_NUM_CLOBBERS:
4663 gcc_assert (!is_param && value == 1);
4664 printf ("pnum_clobbers %s NULL", invert_p ? "==" : "!=");
4665 break;
4667 case rtx_test::C_TEST:
4668 gcc_assert (!is_param && value == 1);
4669 if (invert_p)
4670 printf ("!");
4671 print_c_condition (test.u.string);
4672 break;
4674 case rtx_test::ACCEPT:
4675 case rtx_test::SET_OP:
4676 gcc_unreachable ();
4680 static exit_state print_decision (output_state *, decision *,
4681 unsigned int, bool);
4683 /* Print code to perform S, indent each line by INDENT spaces.
4684 IS_FINAL is true if there are no fallback decisions to test on failure;
4685 if the state fails then the entire routine fails. */
4687 static exit_state
4688 print_state (output_state *os, state *s, unsigned int indent, bool is_final)
4690 exit_state es = ES_FALLTHROUGH;
4691 for (decision *d = s->first; d; d = d->next)
4692 es = print_decision (os, d, indent, is_final && !d->next);
4693 if (es != ES_RETURNED && is_final)
4695 printf_indent (indent, "return %s;\n", get_failure_return (os->type));
4696 es = ES_RETURNED;
4698 return es;
4701 /* Print the code for subroutine call ACCEPTANCE (for which partial_p
4702 is known to be true). Return the C condition that indicates a successful
4703 match. */
4705 static const char *
4706 print_subroutine_call (const acceptance_type &acceptance)
4708 switch (acceptance.type)
4710 case SUBPATTERN:
4711 gcc_unreachable ();
4713 case RECOG:
4714 printf ("recog_%d (x1, insn, pnum_clobbers)",
4715 acceptance.u.subroutine_id);
4716 return ">= 0";
4718 case SPLIT:
4719 printf ("split_%d (x1, insn)", acceptance.u.subroutine_id);
4720 return "!= NULL_RTX";
4722 case PEEPHOLE2:
4723 printf ("peephole2_%d (x1, insn, pmatch_len_)",
4724 acceptance.u.subroutine_id);
4725 return "!= NULL_RTX";
4727 gcc_unreachable ();
4730 /* Print code for the successful match described by ACCEPTANCE.
4731 INDENT and IS_FINAL are as for print_state. */
4733 static exit_state
4734 print_acceptance (const acceptance_type &acceptance, unsigned int indent,
4735 bool is_final)
4737 if (acceptance.partial_p)
4739 /* Defer the rest of the match to a subroutine. */
4740 if (is_final)
4742 printf_indent (indent, "return ");
4743 print_subroutine_call (acceptance);
4744 printf (";\n");
4745 return ES_RETURNED;
4747 else
4749 printf_indent (indent, "res = ");
4750 const char *res_test = print_subroutine_call (acceptance);
4751 printf (";\n");
4752 printf_indent (indent, "if (res %s)\n", res_test);
4753 printf_indent (indent + 2, "return res;\n");
4754 return ES_FALLTHROUGH;
4757 switch (acceptance.type)
4759 case SUBPATTERN:
4760 printf_indent (indent, "return %d;\n", acceptance.u.full.code);
4761 return ES_RETURNED;
4763 case RECOG:
4764 if (acceptance.u.full.u.num_clobbers != 0)
4765 printf_indent (indent, "*pnum_clobbers = %d;\n",
4766 acceptance.u.full.u.num_clobbers);
4767 printf_indent (indent, "return %d; /* %s */\n", acceptance.u.full.code,
4768 get_insn_name (acceptance.u.full.code));
4769 return ES_RETURNED;
4771 case SPLIT:
4772 printf_indent (indent, "return gen_split_%d (insn, operands);\n",
4773 acceptance.u.full.code);
4774 return ES_RETURNED;
4776 case PEEPHOLE2:
4777 printf_indent (indent, "*pmatch_len_ = %d;\n",
4778 acceptance.u.full.u.match_len);
4779 if (is_final)
4781 printf_indent (indent, "return gen_peephole2_%d (insn, operands);\n",
4782 acceptance.u.full.code);
4783 return ES_RETURNED;
4785 else
4787 printf_indent (indent, "res = gen_peephole2_%d (insn, operands);\n",
4788 acceptance.u.full.code);
4789 printf_indent (indent, "if (res != NULL_RTX)\n");
4790 printf_indent (indent + 2, "return res;\n");
4791 return ES_FALLTHROUGH;
4794 gcc_unreachable ();
4797 /* Print code to perform D. INDENT and IS_FINAL are as for print_state. */
4799 static exit_state
4800 print_decision (output_state *os, decision *d, unsigned int indent,
4801 bool is_final)
4803 uint64_t label;
4804 unsigned int base, count;
4806 /* Make sure the rtx under test is available either in operands[] or
4807 in an xN variable. */
4808 if (d->test.pos && d->test.pos_operand < 0)
4809 change_state (os, d->test.pos, indent);
4811 /* Look for cases where a pattern routine P1 calls another pattern routine
4812 P2 and where P1 returns X + BASE whenever P2 returns X. If IS_FINAL
4813 is true and BASE is zero we can simply use:
4815 return patternN (...);
4817 Otherwise we can use:
4819 res = patternN (...);
4820 if (res >= 0)
4821 return res + BASE;
4823 However, if BASE is nonzero and patternN only returns 0 or -1,
4824 the usual "return BASE;" is better than "return res + BASE;".
4825 If BASE is zero, "return res;" should be better than "return 0;",
4826 since no assignment to the return register is required. */
4827 if (os->type == SUBPATTERN
4828 && terminal_pattern_p (d, &base, &count)
4829 && (base == 0 || count > 1))
4831 if (is_final && base == 0)
4833 printf_indent (indent, "return ");
4834 print_nonbool_test (os, d->test);
4835 printf ("; /* [-1, %d] */\n", count - 1);
4836 return ES_RETURNED;
4838 else
4840 printf_indent (indent, "res = ");
4841 print_nonbool_test (os, d->test);
4842 printf (";\n");
4843 printf_indent (indent, "if (res >= 0)\n");
4844 printf_indent (indent + 2, "return res");
4845 if (base != 0)
4846 printf (" + %d", base);
4847 printf ("; /* [%d, %d] */\n", base, base + count - 1);
4848 return ES_FALLTHROUGH;
4851 else if (d->test.kind == rtx_test::ACCEPT)
4852 return print_acceptance (d->test.u.acceptance, indent, is_final);
4853 else if (d->test.kind == rtx_test::SET_OP)
4855 printf_indent (indent, "operands[%d] = ", d->test.u.opno);
4856 print_test_rtx (os, d->test);
4857 printf (";\n");
4858 return print_state (os, d->singleton ()->to, indent, is_final);
4860 /* Handle decisions with a single transition and a single transition
4861 label. */
4862 else if (d->if_statement_p (&label))
4864 transition *trans = d->singleton ();
4865 if (mark_optional_transitions_p && trans->optional)
4866 printf_indent (indent, "/* OPTIONAL IF */\n");
4868 /* Print the condition associated with TRANS. Invert it if IS_FINAL,
4869 so that we return immediately on failure and fall through on
4870 success. */
4871 printf_indent (indent, "if (");
4872 print_test (os, d->test, trans->is_param, label, is_final);
4874 /* Look for following states that would be handled by this code
4875 on recursion. If they don't need any preparatory statements,
4876 include them in the current "if" statement rather than creating
4877 a new one. */
4878 for (;;)
4880 d = trans->to->singleton ();
4881 if (!d
4882 || d->test.kind == rtx_test::ACCEPT
4883 || d->test.kind == rtx_test::SET_OP
4884 || !d->if_statement_p (&label)
4885 || !test_position_available_p (os, d->test))
4886 break;
4887 trans = d->first;
4888 printf ("\n");
4889 if (mark_optional_transitions_p && trans->optional)
4890 printf_indent (indent + 4, "/* OPTIONAL IF */\n");
4891 printf_indent (indent + 4, "%s ", is_final ? "||" : "&&");
4892 print_test (os, d->test, trans->is_param, label, is_final);
4894 printf (")\n");
4896 /* Print the conditional code with INDENT + 2 and the fallthrough
4897 code with indent INDENT. */
4898 state *to = trans->to;
4899 if (is_final)
4901 /* We inverted the condition above, so return failure in the
4902 "if" body and fall through to the target of the transition. */
4903 printf_indent (indent + 2, "return %s;\n",
4904 get_failure_return (os->type));
4905 return print_state (os, to, indent, is_final);
4907 else if (to->singleton ()
4908 && to->first->test.kind == rtx_test::ACCEPT
4909 && single_statement_p (to->first->test.u.acceptance))
4911 /* The target of the transition is a simple "return" statement.
4912 It doesn't need any braces and doesn't fall through. */
4913 if (print_acceptance (to->first->test.u.acceptance,
4914 indent + 2, true) != ES_RETURNED)
4915 gcc_unreachable ();
4916 return ES_FALLTHROUGH;
4918 else
4920 /* The general case. Output code for the target of the transition
4921 in braces. This will not invalidate any of the xN variables
4922 that are already valid, but we mustn't rely on any that are
4923 set by the "if" body. */
4924 auto_vec <bool, 32> old_seen;
4925 old_seen.safe_splice (os->seen_vars);
4927 printf_indent (indent + 2, "{\n");
4928 print_state (os, trans->to, indent + 4, is_final);
4929 printf_indent (indent + 2, "}\n");
4931 os->seen_vars.truncate (0);
4932 os->seen_vars.splice (old_seen);
4933 return ES_FALLTHROUGH;
4936 else
4938 /* Output the decision as a switch statement. */
4939 printf_indent (indent, "switch (");
4940 print_nonbool_test (os, d->test);
4941 printf (")\n");
4943 /* Each case statement starts with the same set of valid variables.
4944 These are also the only variables will be valid on fallthrough. */
4945 auto_vec <bool, 32> old_seen;
4946 old_seen.safe_splice (os->seen_vars);
4948 printf_indent (indent + 2, "{\n");
4949 for (transition *trans = d->first; trans; trans = trans->next)
4951 gcc_assert (!trans->is_param);
4952 if (mark_optional_transitions_p && trans->optional)
4953 printf_indent (indent + 2, "/* OPTIONAL CASE */\n");
4954 for (int_set::iterator j = trans->labels.begin ();
4955 j != trans->labels.end (); ++j)
4957 printf_indent (indent + 2, "case ");
4958 print_label_value (d->test, trans->is_param, *j);
4959 printf (":\n");
4961 if (print_state (os, trans->to, indent + 4, is_final))
4963 /* The state can fall through. Add an explicit break. */
4964 gcc_assert (!is_final);
4965 printf_indent (indent + 4, "break;\n");
4967 printf ("\n");
4969 /* Restore the original set of valid variables. */
4970 os->seen_vars.truncate (0);
4971 os->seen_vars.splice (old_seen);
4973 /* Add a default case. */
4974 printf_indent (indent + 2, "default:\n");
4975 if (is_final)
4976 printf_indent (indent + 4, "return %s;\n",
4977 get_failure_return (os->type));
4978 else
4979 printf_indent (indent + 4, "break;\n");
4980 printf_indent (indent + 2, "}\n");
4981 return is_final ? ES_RETURNED : ES_FALLTHROUGH;
4985 /* Make sure that OS has a position variable for POS. ROOT_P is true if
4986 POS is the root position for the routine. */
4988 static void
4989 assign_position_var (output_state *os, position *pos, bool root_p)
4991 unsigned int idx = os->id_to_var[pos->id];
4992 if (idx < os->var_to_id.length () && os->var_to_id[idx] == pos->id)
4993 return;
4994 if (!root_p && pos->type != POS_PEEP2_INSN)
4995 assign_position_var (os, pos->base, false);
4996 os->id_to_var[pos->id] = os->var_to_id.length ();
4997 os->var_to_id.safe_push (pos->id);
5000 /* Make sure that OS has the position variables required by S. */
5002 static void
5003 assign_position_vars (output_state *os, state *s)
5005 for (decision *d = s->first; d; d = d->next)
5007 /* Positions associated with operands can be read from the
5008 operands[] array. */
5009 if (d->test.pos && d->test.pos_operand < 0)
5010 assign_position_var (os, d->test.pos, false);
5011 for (transition *trans = d->first; trans; trans = trans->next)
5012 assign_position_vars (os, trans->to);
5016 /* Print the open brace and variable definitions for a routine that
5017 implements S. ROOT is the deepest rtx from which S can access all
5018 relevant parts of the first instruction it matches. Initialize OS
5019 so that every relevant position has an rtx variable xN and so that
5020 only ROOT's variable has a valid value. */
5022 static void
5023 print_subroutine_start (output_state *os, state *s, position *root)
5025 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED"
5026 " = &recog_data.operand[0];\n");
5027 os->var_to_id.truncate (0);
5028 os->seen_vars.truncate (0);
5029 if (root)
5031 /* Create a fake entry for position 0 so that an id_to_var of 0
5032 is always invalid. This also makes the xN variables naturally
5033 1-based rather than 0-based. */
5034 os->var_to_id.safe_push (num_positions);
5036 /* Associate ROOT with x1. */
5037 assign_position_var (os, root, true);
5039 /* Assign xN variables to all other relevant positions. */
5040 assign_position_vars (os, s);
5042 /* Output the variable declarations (except for ROOT's, which is
5043 passed in as a parameter). */
5044 unsigned int num_vars = os->var_to_id.length ();
5045 if (num_vars > 2)
5047 for (unsigned int i = 2; i < num_vars; ++i)
5048 /* Print 8 rtx variables to a line. */
5049 printf ("%s x%d",
5050 i == 2 ? " rtx" : (i - 2) % 8 == 0 ? ";\n rtx" : ",", i);
5051 printf (";\n");
5054 /* Say that x1 is valid and the rest aren't. */
5055 os->seen_vars.safe_grow_cleared (num_vars);
5056 os->seen_vars[1] = true;
5058 if (os->type == SUBPATTERN || os->type == RECOG)
5059 printf (" int res ATTRIBUTE_UNUSED;\n");
5060 else
5061 printf (" rtx_insn *res ATTRIBUTE_UNUSED;\n");
5064 /* Output the definition of pattern routine ROUTINE. */
5066 static void
5067 print_pattern (output_state *os, pattern_routine *routine)
5069 printf ("\nstatic int\npattern%d (", routine->pattern_id);
5070 const char *sep = "";
5071 /* Add the top-level rtx parameter, if any. */
5072 if (routine->pos)
5074 printf ("%srtx x1", sep);
5075 sep = ", ";
5077 /* Add the optional parameters. */
5078 if (routine->insn_p)
5080 /* We can't easily tell whether a C condition actually reads INSN,
5081 so add an ATTRIBUTE_UNUSED just in case. */
5082 printf ("%srtx_insn *insn ATTRIBUTE_UNUSED", sep);
5083 sep = ", ";
5085 if (routine->pnum_clobbers_p)
5087 printf ("%sint *pnum_clobbers", sep);
5088 sep = ", ";
5090 /* Add the "i" parameters. */
5091 for (unsigned int i = 0; i < routine->param_types.length (); ++i)
5093 printf ("%s%s i%d", sep,
5094 parameter_type_string (routine->param_types[i]), i + 1);
5095 sep = ", ";
5097 printf (")\n");
5098 os->type = SUBPATTERN;
5099 print_subroutine_start (os, routine->s, routine->pos);
5100 print_state (os, routine->s, 2, true);
5101 printf ("}\n");
5104 /* Output a routine of type TYPE that implements S. PROC_ID is the
5105 number of the subroutine associated with S, or 0 if S is the main
5106 routine. */
5108 static void
5109 print_subroutine (output_state *os, state *s, int proc_id)
5111 /* For now, the top-level "recog" takes a plain "rtx", and performs a
5112 checked cast to "rtx_insn *" for use throughout the rest of the
5113 function and the code it calls. */
5114 const char *insn_param
5115 = proc_id > 0 ? "rtx_insn *insn" : "rtx uncast_insn";
5116 printf ("\n");
5117 switch (os->type)
5119 case SUBPATTERN:
5120 gcc_unreachable ();
5122 case RECOG:
5123 if (proc_id)
5124 printf ("static int\nrecog_%d", proc_id);
5125 else
5126 printf ("int\nrecog");
5127 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5128 "\t%s ATTRIBUTE_UNUSED,\n"
5129 "\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", insn_param);
5130 break;
5132 case SPLIT:
5133 if (proc_id)
5134 printf ("static rtx_insn *\nsplit_%d", proc_id);
5135 else
5136 printf ("rtx_insn *\nsplit_insns");
5137 printf (" (rtx x1 ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n");
5138 break;
5140 case PEEPHOLE2:
5141 if (proc_id)
5142 printf ("static rtx_insn *\npeephole2_%d", proc_id);
5143 else
5144 printf ("rtx_insn *\npeephole2_insns");
5145 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5146 "\trtx_insn *insn ATTRIBUTE_UNUSED,\n"
5147 "\tint *pmatch_len_ ATTRIBUTE_UNUSED)\n");
5148 break;
5150 print_subroutine_start (os, s, &root_pos);
5151 if (proc_id == 0)
5153 printf (" recog_data.insn = NULL;\n");
5154 if (os->type == RECOG)
5156 printf (" rtx_insn *insn ATTRIBUTE_UNUSED;\n");
5157 printf (" insn = safe_as_a <rtx_insn *> (uncast_insn);\n");
5160 print_state (os, s, 2, true);
5161 printf ("}\n");
5164 /* Print out a routine of type TYPE that performs ROOT. */
5166 static void
5167 print_subroutine_group (output_state *os, routine_type type, state *root)
5169 os->type = type;
5170 if (use_subroutines_p)
5172 /* Split ROOT up into smaller pieces, both for readability and to
5173 help the compiler. */
5174 auto_vec <state *> subroutines;
5175 find_subroutines (type, root, subroutines);
5177 /* Output the subroutines (but not ROOT itself). */
5178 unsigned int i;
5179 state *s;
5180 FOR_EACH_VEC_ELT (subroutines, i, s)
5181 print_subroutine (os, s, i + 1);
5183 /* Output the main routine. */
5184 print_subroutine (os, root, 0);
5187 /* Return the rtx pattern for the list of rtxes in a define_peephole2. */
5189 static rtx
5190 get_peephole2_pattern (rtvec vec)
5192 int i, j;
5193 rtx pattern = rtx_alloc (SEQUENCE);
5194 XVEC (pattern, 0) = rtvec_alloc (GET_NUM_ELEM (vec));
5195 for (i = j = 0; i < GET_NUM_ELEM (vec); i++)
5197 rtx x = RTVEC_ELT (vec, i);
5198 /* Ignore scratch register requirements. */
5199 if (GET_CODE (x) != MATCH_SCRATCH && GET_CODE (x) != MATCH_DUP)
5201 XVECEXP (pattern, 0, j) = x;
5202 j++;
5205 XVECLEN (pattern, 0) = j;
5206 if (j == 0)
5207 error_with_line (pattern_lineno, "empty define_peephole2");
5208 return pattern;
5211 /* Return true if *PATTERN_PTR is a PARALLEL in which at least one trailing
5212 rtx can be added automatically by add_clobbers. If so, update
5213 *ACCEPTANCE_PTR so that its num_clobbers field contains the number
5214 of such trailing rtxes and update *PATTERN_PTR so that it contains
5215 the pattern without those rtxes. */
5217 static bool
5218 remove_clobbers (acceptance_type *acceptance_ptr, rtx *pattern_ptr)
5220 int i;
5221 rtx new_pattern;
5223 /* Find the last non-clobber in the parallel. */
5224 rtx pattern = *pattern_ptr;
5225 for (i = XVECLEN (pattern, 0); i > 0; i--)
5227 rtx x = XVECEXP (pattern, 0, i - 1);
5228 if (GET_CODE (x) != CLOBBER
5229 || (!REG_P (XEXP (x, 0))
5230 && GET_CODE (XEXP (x, 0)) != MATCH_SCRATCH))
5231 break;
5234 if (i == XVECLEN (pattern, 0))
5235 return false;
5237 /* Build a similar insn without the clobbers. */
5238 if (i == 1)
5239 new_pattern = XVECEXP (pattern, 0, 0);
5240 else
5242 new_pattern = rtx_alloc (PARALLEL);
5243 XVEC (new_pattern, 0) = rtvec_alloc (i);
5244 for (int j = 0; j < i; ++j)
5245 XVECEXP (new_pattern, 0, j) = XVECEXP (pattern, 0, j);
5248 /* Recognize it. */
5249 acceptance_ptr->u.full.u.num_clobbers = XVECLEN (pattern, 0) - i;
5250 *pattern_ptr = new_pattern;
5251 return true;
5255 main (int argc, char **argv)
5257 rtx desc;
5258 state insn_root, split_root, peephole2_root;
5260 progname = "genrecog";
5262 if (!init_rtx_reader_args (argc, argv))
5263 return (FATAL_EXIT_CODE);
5265 next_insn_code = 0;
5267 write_header ();
5269 /* Read the machine description. */
5271 while (1)
5273 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
5274 if (desc == NULL)
5275 break;
5277 acceptance_type acceptance;
5278 acceptance.partial_p = false;
5279 acceptance.u.full.code = next_insn_code;
5281 rtx pattern;
5282 switch (GET_CODE (desc))
5284 case DEFINE_INSN:
5286 /* Match the instruction in the original .md form. */
5287 acceptance.type = RECOG;
5288 acceptance.u.full.u.num_clobbers = 0;
5289 pattern = add_implicit_parallel (XVEC (desc, 1));
5290 validate_pattern (pattern, desc, NULL_RTX, 0);
5291 match_pattern (&insn_root, pattern, XSTR (desc, 2), acceptance);
5293 /* If the pattern is a PARALLEL with trailing CLOBBERs,
5294 allow recog_for_combine to match without the clobbers. */
5295 if (GET_CODE (pattern) == PARALLEL
5296 && remove_clobbers (&acceptance, &pattern))
5297 match_pattern (&insn_root, pattern, XSTR (desc, 2), acceptance);
5298 break;
5301 case DEFINE_SPLIT:
5302 acceptance.type = SPLIT;
5303 pattern = add_implicit_parallel (XVEC (desc, 0));
5304 validate_pattern (pattern, desc, NULL_RTX, 0);
5305 match_pattern (&split_root, pattern, XSTR (desc, 1), acceptance);
5307 /* Declare the gen_split routine that we'll call if the
5308 pattern matches. The definition comes from insn-emit.c. */
5309 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
5310 next_insn_code);
5311 break;
5313 case DEFINE_PEEPHOLE2:
5314 acceptance.type = PEEPHOLE2;
5315 pattern = get_peephole2_pattern (XVEC (desc, 0));
5316 validate_pattern (pattern, desc, NULL_RTX, 0);
5317 match_pattern (&peephole2_root, pattern, XSTR (desc, 1), acceptance);
5319 /* Declare the gen_peephole2 routine that we'll call if the
5320 pattern matches. The definition comes from insn-emit.c. */
5321 printf ("extern rtx_insn *gen_peephole2_%d (rtx_insn *, rtx *);\n",
5322 next_insn_code);
5323 break;
5325 default:
5326 /* do nothing */;
5330 if (have_error)
5331 return FATAL_EXIT_CODE;
5333 puts ("\n\n");
5335 /* Optimize each routine in turn. */
5336 optimize_subroutine_group ("recog", &insn_root);
5337 optimize_subroutine_group ("split_insns", &split_root);
5338 optimize_subroutine_group ("peephole2_insns", &peephole2_root);
5340 output_state os;
5341 os.id_to_var.safe_grow_cleared (num_positions);
5343 if (use_pattern_routines_p)
5345 /* Look for common patterns and split them out into subroutines. */
5346 auto_vec <merge_state_info> states;
5347 states.safe_push (&insn_root);
5348 states.safe_push (&split_root);
5349 states.safe_push (&peephole2_root);
5350 split_out_patterns (states);
5352 /* Print out the routines that we just created. */
5353 unsigned int i;
5354 pattern_routine *routine;
5355 FOR_EACH_VEC_ELT (patterns, i, routine)
5356 print_pattern (&os, routine);
5359 /* Print out the matching routines. */
5360 print_subroutine_group (&os, RECOG, &insn_root);
5361 print_subroutine_group (&os, SPLIT, &split_root);
5362 print_subroutine_group (&os, PEEPHOLE2, &peephole2_root);
5364 fflush (stdout);
5365 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);