PR target/66148
[official-gcc.git] / gcc / genrecog.c
blob4b6dee64b5a7621a1b9b0592f22588013a81b063
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 "hash-table.h"
116 #include "inchash.h"
117 #include <algorithm>
119 #undef GENERATOR_FILE
120 enum true_rtx_doe {
121 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) TRUE_##ENUM,
122 #include "rtl.def"
123 #undef DEF_RTL_EXPR
124 FIRST_GENERATOR_RTX_CODE
126 #define NUM_TRUE_RTX_CODE ((int) FIRST_GENERATOR_RTX_CODE)
127 #define GENERATOR_FILE 1
129 /* Debugging variables to control which optimizations are performed.
130 Note that disabling merge_states_p leads to very large output. */
131 static const bool merge_states_p = true;
132 static const bool collapse_optional_decisions_p = true;
133 static const bool cse_tests_p = true;
134 static const bool simplify_tests_p = true;
135 static const bool use_operand_variables_p = true;
136 static const bool use_subroutines_p = true;
137 static const bool use_pattern_routines_p = true;
139 /* Whether to add comments for optional tests that we decided to keep.
140 Can be useful when debugging the generator itself but is noise when
141 debugging the generated code. */
142 static const bool mark_optional_transitions_p = false;
144 /* Whether pattern routines should calculate positions relative to their
145 rtx parameter rather than use absolute positions. This e.g. allows
146 a pattern routine to be shared between a plain SET and a PARALLEL
147 that includes a SET.
149 In principle it sounds like this should be useful, especially for
150 recog_for_combine, where the plain SET form is generated automatically
151 from a PARALLEL of a single SET and some CLOBBERs. In practice it doesn't
152 seem to help much and leads to slightly bigger object files. */
153 static const bool relative_patterns_p = false;
155 /* Whether pattern routines should be allowed to test whether pnum_clobbers
156 is null. This requires passing pnum_clobbers around as a parameter. */
157 static const bool pattern_have_num_clobbers_p = true;
159 /* Whether pattern routines should be allowed to test .md file C conditions.
160 This requires passing insn around as a parameter, in case the C
161 condition refers to it. In practice this tends to lead to bigger
162 object files. */
163 static const bool pattern_c_test_p = false;
165 /* Whether to require each parameter passed to a pattern routine to be
166 unique. Disabling this check for example allows unary operators with
167 matching modes (like NEG) and unary operators with mismatched modes
168 (like ZERO_EXTEND) to be matched by a single pattern. However, we then
169 often have cases where the same value is passed too many times. */
170 static const bool force_unique_params_p = true;
172 /* The maximum (approximate) depth of block nesting that an individual
173 routine or subroutine should have. This limit is about keeping the
174 output readable rather than reducing compile time. */
175 static const unsigned int MAX_DEPTH = 6;
177 /* The minimum number of pseudo-statements that a state must have before
178 we split it out into a subroutine. */
179 static const unsigned int MIN_NUM_STATEMENTS = 5;
181 /* The number of pseudo-statements a state can have before we consider
182 splitting out substates into subroutines. This limit is about avoiding
183 compile-time problems with very big functions (and also about keeping
184 functions within --param optimization limits, etc.). */
185 static const unsigned int MAX_NUM_STATEMENTS = 200;
187 /* The minimum number of pseudo-statements that can be used in a pattern
188 routine. */
189 static const unsigned int MIN_COMBINE_COST = 4;
191 /* The maximum number of arguments that a pattern routine can have.
192 The idea is to prevent one pattern getting a ridiculous number of
193 arguments when it would be more beneficial to have a separate pattern
194 routine instead. */
195 static const unsigned int MAX_PATTERN_PARAMS = 5;
197 /* The maximum operand number plus one. */
198 int num_operands;
200 /* Ways of obtaining an rtx to be tested. */
201 enum position_type {
202 /* PATTERN (peep2_next_insn (ARG)). */
203 POS_PEEP2_INSN,
205 /* XEXP (BASE, ARG). */
206 POS_XEXP,
208 /* XVECEXP (BASE, 0, ARG). */
209 POS_XVECEXP0
212 /* The position of an rtx relative to X0. Each useful position is
213 represented by exactly one instance of this structure. */
214 struct position
216 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */
217 struct position *base;
219 /* A position with the same BASE and TYPE, but with the next value
220 of ARG. */
221 struct position *next;
223 /* A list of all POS_XEXP positions that use this one as their base,
224 chained by NEXT fields. The first entry represents XEXP (this, 0),
225 the second represents XEXP (this, 1), and so on. */
226 struct position *xexps;
228 /* A list of POS_XVECEXP0 positions that use this one as their base,
229 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0),
230 the second represents XVECEXP (this, 0, 1), and so on. */
231 struct position *xvecexp0s;
233 /* The type of position. */
234 enum position_type type;
236 /* The argument to TYPE (shown as ARG in the position_type comments). */
237 int arg;
239 /* The instruction to which the position belongs. */
240 unsigned int insn_id;
242 /* The depth of this position relative to the instruction pattern.
243 E.g. if the instruction pattern is a SET, the SET itself has a
244 depth of 0 while the SET_DEST and SET_SRC have depths of 1. */
245 unsigned int depth;
247 /* A unique identifier for this position. */
248 unsigned int id;
251 enum routine_type {
252 SUBPATTERN, RECOG, SPLIT, PEEPHOLE2
255 /* Next number to use as an insn_code. */
256 static int next_insn_code;
258 /* The line number of the start of the pattern currently being processed. */
259 static int pattern_lineno;
261 /* The root position (x0). */
262 static struct position root_pos;
264 /* The number of positions created. Also one higher than the maximum
265 position id. */
266 static unsigned int num_positions = 1;
268 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position,
269 since we are given that instruction's pattern as x0. */
270 static struct position *peep2_insn_pos_list = &root_pos;
272 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR
273 points to where the unique object that represents the position
274 should be stored. Create the object if it doesn't already exist,
275 otherwise reuse the object that is already there. */
277 static struct position *
278 next_position (struct position **next_ptr, struct position *base,
279 enum position_type type, int arg)
281 struct position *pos;
283 pos = *next_ptr;
284 if (!pos)
286 pos = XCNEW (struct position);
287 pos->type = type;
288 pos->arg = arg;
289 if (type == POS_PEEP2_INSN)
291 pos->base = 0;
292 pos->insn_id = arg;
293 pos->depth = base->depth;
295 else
297 pos->base = base;
298 pos->insn_id = base->insn_id;
299 pos->depth = base->depth + 1;
301 pos->id = num_positions++;
302 *next_ptr = pos;
304 return pos;
307 /* Compare positions POS1 and POS2 lexicographically. */
309 static int
310 compare_positions (struct position *pos1, struct position *pos2)
312 int diff;
314 diff = pos1->depth - pos2->depth;
315 if (diff < 0)
317 pos2 = pos2->base;
318 while (pos1->depth != pos2->depth);
319 else if (diff > 0)
321 pos1 = pos1->base;
322 while (pos1->depth != pos2->depth);
323 while (pos1 != pos2)
325 diff = (int) pos1->type - (int) pos2->type;
326 if (diff == 0)
327 diff = pos1->arg - pos2->arg;
328 pos1 = pos1->base;
329 pos2 = pos2->base;
331 return diff;
334 /* Return the most deeply-nested position that is common to both
335 POS1 and POS2. If the positions are from different instructions,
336 return the one with the lowest insn_id. */
338 static struct position *
339 common_position (struct position *pos1, struct position *pos2)
341 if (pos1->insn_id != pos2->insn_id)
342 return pos1->insn_id < pos2->insn_id ? pos1 : pos2;
343 if (pos1->depth > pos2->depth)
344 std::swap (pos1, pos2);
345 while (pos1->depth != pos2->depth)
346 pos2 = pos2->base;
347 while (pos1 != pos2)
349 pos1 = pos1->base;
350 pos2 = pos2->base;
352 return pos1;
355 /* Search for and return operand N, stop when reaching node STOP. */
357 static rtx
358 find_operand (rtx pattern, int n, rtx stop)
360 const char *fmt;
361 RTX_CODE code;
362 int i, j, len;
363 rtx r;
365 if (pattern == stop)
366 return stop;
368 code = GET_CODE (pattern);
369 if ((code == MATCH_SCRATCH
370 || code == MATCH_OPERAND
371 || code == MATCH_OPERATOR
372 || code == MATCH_PARALLEL)
373 && XINT (pattern, 0) == n)
374 return pattern;
376 fmt = GET_RTX_FORMAT (code);
377 len = GET_RTX_LENGTH (code);
378 for (i = 0; i < len; i++)
380 switch (fmt[i])
382 case 'e': case 'u':
383 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
384 return r;
385 break;
387 case 'V':
388 if (! XVEC (pattern, i))
389 break;
390 /* Fall through. */
392 case 'E':
393 for (j = 0; j < XVECLEN (pattern, i); j++)
394 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
395 != NULL_RTX)
396 return r;
397 break;
399 case 'i': case 'r': case 'w': case '0': case 's':
400 break;
402 default:
403 gcc_unreachable ();
407 return NULL;
410 /* Search for and return operand M, such that it has a matching
411 constraint for operand N. */
413 static rtx
414 find_matching_operand (rtx pattern, int n)
416 const char *fmt;
417 RTX_CODE code;
418 int i, j, len;
419 rtx r;
421 code = GET_CODE (pattern);
422 if (code == MATCH_OPERAND
423 && (XSTR (pattern, 2)[0] == '0' + n
424 || (XSTR (pattern, 2)[0] == '%'
425 && XSTR (pattern, 2)[1] == '0' + n)))
426 return pattern;
428 fmt = GET_RTX_FORMAT (code);
429 len = GET_RTX_LENGTH (code);
430 for (i = 0; i < len; i++)
432 switch (fmt[i])
434 case 'e': case 'u':
435 if ((r = find_matching_operand (XEXP (pattern, i), n)))
436 return r;
437 break;
439 case 'V':
440 if (! XVEC (pattern, i))
441 break;
442 /* Fall through. */
444 case 'E':
445 for (j = 0; j < XVECLEN (pattern, i); j++)
446 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
447 return r;
448 break;
450 case 'i': case 'r': case 'w': case '0': case 's':
451 break;
453 default:
454 gcc_unreachable ();
458 return NULL;
461 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
462 don't use the MATCH_OPERAND constraint, only the predicate.
463 This is confusing to folks doing new ports, so help them
464 not make the mistake. */
466 static bool
467 constraints_supported_in_insn_p (rtx insn)
469 return !(GET_CODE (insn) == DEFINE_EXPAND
470 || GET_CODE (insn) == DEFINE_SPLIT
471 || GET_CODE (insn) == DEFINE_PEEPHOLE2);
474 /* Check for various errors in patterns. SET is nonnull for a destination,
475 and is the complete set pattern. SET_CODE is '=' for normal sets, and
476 '+' within a context that requires in-out constraints. */
478 static void
479 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
481 const char *fmt;
482 RTX_CODE code;
483 size_t i, len;
484 int j;
486 code = GET_CODE (pattern);
487 switch (code)
489 case MATCH_SCRATCH:
491 const char constraints0 = XSTR (pattern, 1)[0];
493 if (!constraints_supported_in_insn_p (insn))
495 if (constraints0)
497 error_with_line (pattern_lineno,
498 "constraints not supported in %s",
499 rtx_name[GET_CODE (insn)]);
501 return;
504 /* If a MATCH_SCRATCH is used in a context requiring an write-only
505 or read/write register, validate that. */
506 if (set_code == '='
507 && constraints0
508 && constraints0 != '='
509 && constraints0 != '+')
511 error_with_line (pattern_lineno,
512 "operand %d missing output reload",
513 XINT (pattern, 0));
515 return;
517 case MATCH_DUP:
518 case MATCH_OP_DUP:
519 case MATCH_PAR_DUP:
520 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern)
521 error_with_line (pattern_lineno,
522 "operand %i duplicated before defined",
523 XINT (pattern, 0));
524 break;
525 case MATCH_OPERAND:
526 case MATCH_OPERATOR:
528 const char *pred_name = XSTR (pattern, 1);
529 const struct pred_data *pred;
530 const char *c_test;
532 if (GET_CODE (insn) == DEFINE_INSN)
533 c_test = XSTR (insn, 2);
534 else
535 c_test = XSTR (insn, 1);
537 if (pred_name[0] != 0)
539 pred = lookup_predicate (pred_name);
540 if (!pred)
541 error_with_line (pattern_lineno, "unknown predicate '%s'",
542 pred_name);
544 else
545 pred = 0;
547 if (code == MATCH_OPERAND)
549 const char *constraints = XSTR (pattern, 2);
550 const char constraints0 = constraints[0];
552 if (!constraints_supported_in_insn_p (insn))
554 if (constraints0)
556 error_with_line (pattern_lineno,
557 "constraints not supported in %s",
558 rtx_name[GET_CODE (insn)]);
562 /* A MATCH_OPERAND that is a SET should have an output reload. */
563 else if (set && constraints0)
565 if (set_code == '+')
567 if (constraints0 == '+')
569 /* If we've only got an output reload for this operand,
570 we'd better have a matching input operand. */
571 else if (constraints0 == '='
572 && find_matching_operand (insn, XINT (pattern, 0)))
574 else
575 error_with_line (pattern_lineno,
576 "operand %d missing in-out reload",
577 XINT (pattern, 0));
579 else if (constraints0 != '=' && constraints0 != '+')
580 error_with_line (pattern_lineno,
581 "operand %d missing output reload",
582 XINT (pattern, 0));
585 /* For matching constraint in MATCH_OPERAND, the digit must be a
586 smaller number than the number of the operand that uses it in the
587 constraint. */
588 while (1)
590 while (constraints[0]
591 && (constraints[0] == ' ' || constraints[0] == ','))
592 constraints++;
593 if (!constraints[0])
594 break;
596 if (constraints[0] >= '0' && constraints[0] <= '9')
598 int val;
600 sscanf (constraints, "%d", &val);
601 if (val >= XINT (pattern, 0))
602 error_with_line (pattern_lineno,
603 "constraint digit %d is not smaller than"
604 " operand %d",
605 val, XINT (pattern, 0));
608 while (constraints[0] && constraints[0] != ',')
609 constraints++;
613 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
614 while not likely to occur at runtime, results in less efficient
615 code from insn-recog.c. */
616 if (set && pred && pred->allows_non_lvalue)
617 error_with_line (pattern_lineno,
618 "destination operand %d allows non-lvalue",
619 XINT (pattern, 0));
621 /* A modeless MATCH_OPERAND can be handy when we can check for
622 multiple modes in the c_test. In most other cases, it is a
623 mistake. Only DEFINE_INSN is eligible, since SPLIT and
624 PEEP2 can FAIL within the output pattern. Exclude special
625 predicates, which check the mode themselves. Also exclude
626 predicates that allow only constants. Exclude the SET_DEST
627 of a call instruction, as that is a common idiom. */
629 if (GET_MODE (pattern) == VOIDmode
630 && code == MATCH_OPERAND
631 && GET_CODE (insn) == DEFINE_INSN
632 && pred
633 && !pred->special
634 && pred->allows_non_const
635 && strstr (c_test, "operands") == NULL
636 && ! (set
637 && GET_CODE (set) == SET
638 && GET_CODE (SET_SRC (set)) == CALL))
639 message_with_line (pattern_lineno,
640 "warning: operand %d missing mode?",
641 XINT (pattern, 0));
642 return;
645 case SET:
647 machine_mode dmode, smode;
648 rtx dest, src;
650 dest = SET_DEST (pattern);
651 src = SET_SRC (pattern);
653 /* STRICT_LOW_PART is a wrapper. Its argument is the real
654 destination, and it's mode should match the source. */
655 if (GET_CODE (dest) == STRICT_LOW_PART)
656 dest = XEXP (dest, 0);
658 /* Find the referent for a DUP. */
660 if (GET_CODE (dest) == MATCH_DUP
661 || GET_CODE (dest) == MATCH_OP_DUP
662 || GET_CODE (dest) == MATCH_PAR_DUP)
663 dest = find_operand (insn, XINT (dest, 0), NULL);
665 if (GET_CODE (src) == MATCH_DUP
666 || GET_CODE (src) == MATCH_OP_DUP
667 || GET_CODE (src) == MATCH_PAR_DUP)
668 src = find_operand (insn, XINT (src, 0), NULL);
670 dmode = GET_MODE (dest);
671 smode = GET_MODE (src);
673 /* The mode of an ADDRESS_OPERAND is the mode of the memory
674 reference, not the mode of the address. */
675 if (GET_CODE (src) == MATCH_OPERAND
676 && ! strcmp (XSTR (src, 1), "address_operand"))
679 /* The operands of a SET must have the same mode unless one
680 is VOIDmode. */
681 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
682 error_with_line (pattern_lineno,
683 "mode mismatch in set: %smode vs %smode",
684 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
686 /* If only one of the operands is VOIDmode, and PC or CC0 is
687 not involved, it's probably a mistake. */
688 else if (dmode != smode
689 && GET_CODE (dest) != PC
690 && GET_CODE (dest) != CC0
691 && GET_CODE (src) != PC
692 && GET_CODE (src) != CC0
693 && !CONST_INT_P (src)
694 && !CONST_WIDE_INT_P (src)
695 && GET_CODE (src) != CALL)
697 const char *which;
698 which = (dmode == VOIDmode ? "destination" : "source");
699 message_with_line (pattern_lineno,
700 "warning: %s missing a mode?", which);
703 if (dest != SET_DEST (pattern))
704 validate_pattern (dest, insn, pattern, '=');
705 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
706 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
707 return;
710 case CLOBBER:
711 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
712 return;
714 case ZERO_EXTRACT:
715 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
716 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
717 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
718 return;
720 case STRICT_LOW_PART:
721 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
722 return;
724 case LABEL_REF:
725 if (GET_MODE (LABEL_REF_LABEL (pattern)) != VOIDmode)
726 error_with_line (pattern_lineno,
727 "operand to label_ref %smode not VOIDmode",
728 GET_MODE_NAME (GET_MODE (LABEL_REF_LABEL (pattern))));
729 break;
731 default:
732 break;
735 fmt = GET_RTX_FORMAT (code);
736 len = GET_RTX_LENGTH (code);
737 for (i = 0; i < len; i++)
739 switch (fmt[i])
741 case 'e': case 'u':
742 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
743 break;
745 case 'E':
746 for (j = 0; j < XVECLEN (pattern, i); j++)
747 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
748 break;
750 case 'i': case 'r': case 'w': case '0': case 's':
751 break;
753 default:
754 gcc_unreachable ();
759 /* Simple list structure for items of type T, for use when being part
760 of a list is an inherent property of T. T must have members equivalent
761 to "T *prev, *next;" and a function "void set_parent (list_head <T> *)"
762 to set the parent list. */
763 template <typename T>
764 struct list_head
766 /* A range of linked items. */
767 struct range
769 range (T *);
770 range (T *, T *);
772 T *start, *end;
773 void set_parent (list_head *);
776 list_head ();
777 range release ();
778 void push_back (range);
779 range remove (range);
780 void replace (range, range);
781 T *singleton () const;
783 T *first, *last;
786 /* Create a range [START_IN, START_IN]. */
788 template <typename T>
789 list_head <T>::range::range (T *start_in) : start (start_in), end (start_in) {}
791 /* Create a range [START_IN, END_IN], linked by next and prev fields. */
793 template <typename T>
794 list_head <T>::range::range (T *start_in, T *end_in)
795 : start (start_in), end (end_in) {}
797 template <typename T>
798 void
799 list_head <T>::range::set_parent (list_head <T> *owner)
801 for (T *item = start; item != end; item = item->next)
802 item->set_parent (owner);
803 end->set_parent (owner);
806 template <typename T>
807 list_head <T>::list_head () : first (0), last (0) {}
809 /* Add R to the end of the list. */
811 template <typename T>
812 void
813 list_head <T>::push_back (range r)
815 if (last)
816 last->next = r.start;
817 else
818 first = r.start;
819 r.start->prev = last;
820 last = r.end;
821 r.set_parent (this);
824 /* Remove R from the list. R remains valid and can be inserted into
825 other lists. */
827 template <typename T>
828 typename list_head <T>::range
829 list_head <T>::remove (range r)
831 if (r.start->prev)
832 r.start->prev->next = r.end->next;
833 else
834 first = r.end->next;
835 if (r.end->next)
836 r.end->next->prev = r.start->prev;
837 else
838 last = r.start->prev;
839 r.start->prev = 0;
840 r.end->next = 0;
841 r.set_parent (0);
842 return r;
845 /* Replace OLDR with NEWR. OLDR remains valid and can be inserted into
846 other lists. */
848 template <typename T>
849 void
850 list_head <T>::replace (range oldr, range newr)
852 newr.start->prev = oldr.start->prev;
853 newr.end->next = oldr.end->next;
855 oldr.start->prev = 0;
856 oldr.end->next = 0;
857 oldr.set_parent (0);
859 if (newr.start->prev)
860 newr.start->prev->next = newr.start;
861 else
862 first = newr.start;
863 if (newr.end->next)
864 newr.end->next->prev = newr.end;
865 else
866 last = newr.end;
867 newr.set_parent (this);
870 /* Empty the list and return the previous contents as a range that can
871 be inserted into other lists. */
873 template <typename T>
874 typename list_head <T>::range
875 list_head <T>::release ()
877 range r (first, last);
878 first = 0;
879 last = 0;
880 r.set_parent (0);
881 return r;
884 /* If the list contains a single item, return that item, otherwise return
885 null. */
887 template <typename T>
889 list_head <T>::singleton () const
891 return first == last ? first : 0;
894 struct state;
896 /* Describes a possible successful return from a routine. */
897 struct acceptance_type
899 /* The type of routine we're returning from. */
900 routine_type type : 16;
902 /* True if this structure only really represents a partial match,
903 and if we must call a subroutine of type TYPE to complete the match.
904 In this case we'll call the subroutine and, if it succeeds, return
905 whatever the subroutine returned.
907 False if this structure presents a full match. */
908 unsigned int partial_p : 1;
910 union
912 /* If PARTIAL_P, this is the number of the subroutine to call. */
913 int subroutine_id;
915 /* Valid if !PARTIAL_P. */
916 struct
918 /* The identifier of the matching pattern. For SUBPATTERNs this
919 value belongs to an ad-hoc routine-specific enum. For the
920 others it's the number of an .md file pattern. */
921 int code;
922 union
924 /* For RECOG, the number of clobbers that must be added to the
925 pattern in order for it to match CODE. */
926 int num_clobbers;
928 /* For PEEPHOLE2, the number of additional instructions that were
929 included in the optimization. */
930 int match_len;
931 } u;
932 } full;
933 } u;
936 bool
937 operator == (const acceptance_type &a, const acceptance_type &b)
939 if (a.partial_p != b.partial_p)
940 return false;
941 if (a.partial_p)
942 return a.u.subroutine_id == b.u.subroutine_id;
943 else
944 return a.u.full.code == b.u.full.code;
947 bool
948 operator != (const acceptance_type &a, const acceptance_type &b)
950 return !operator == (a, b);
953 /* Represents a parameter to a pattern routine. */
954 struct parameter
956 /* The C type of parameter. */
957 enum type_enum {
958 /* Represents an invalid parameter. */
959 UNSET,
961 /* A machine_mode parameter. */
962 MODE,
964 /* An rtx_code parameter. */
965 CODE,
967 /* An int parameter. */
968 INT,
970 /* An unsigned int parameter. */
971 UINT,
973 /* A HOST_WIDE_INT parameter. */
974 WIDE_INT
977 parameter ();
978 parameter (type_enum, bool, uint64_t);
980 /* The type of the parameter. */
981 type_enum type;
983 /* True if the value passed is variable, false if it is constant. */
984 bool is_param;
986 /* If IS_PARAM, this is the number of the variable passed, for an "i%d"
987 format string. If !IS_PARAM, this is the constant value passed. */
988 uint64_t value;
991 parameter::parameter ()
992 : type (UNSET), is_param (false), value (0) {}
994 parameter::parameter (type_enum type_in, bool is_param_in, uint64_t value_in)
995 : type (type_in), is_param (is_param_in), value (value_in) {}
997 bool
998 operator == (const parameter &param1, const parameter &param2)
1000 return (param1.type == param2.type
1001 && param1.is_param == param2.is_param
1002 && param1.value == param2.value);
1005 bool
1006 operator != (const parameter &param1, const parameter &param2)
1008 return !operator == (param1, param2);
1011 /* Represents a routine that matches a partial rtx pattern, returning
1012 an ad-hoc enum value on success and -1 on failure. The routine can
1013 be used by any subroutine type. The match can be parameterized by
1014 things like mode, code and UNSPEC number. */
1015 struct pattern_routine
1017 /* The state that implements the pattern. */
1018 state *s;
1020 /* The deepest root position from which S can access all the rtxes it needs.
1021 This is NULL if the pattern doesn't need an rtx input, usually because
1022 all matching is done on operands[] instead. */
1023 position *pos;
1025 /* A unique identifier for the routine. */
1026 unsigned int pattern_id;
1028 /* True if the routine takes pnum_clobbers as argument. */
1029 bool pnum_clobbers_p;
1031 /* True if the routine takes the enclosing instruction as argument. */
1032 bool insn_p;
1034 /* The types of the other parameters to the routine, if any. */
1035 auto_vec <parameter::type_enum, MAX_PATTERN_PARAMS> param_types;
1038 /* All defined patterns. */
1039 static vec <pattern_routine *> patterns;
1041 /* Represents one use of a pattern routine. */
1042 struct pattern_use
1044 /* The pattern routine to use. */
1045 pattern_routine *routine;
1047 /* The values to pass as parameters. This vector has the same length
1048 as ROUTINE->PARAM_TYPES. */
1049 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
1052 /* Represents a test performed by a decision. */
1053 struct rtx_test
1055 rtx_test ();
1057 /* The types of test that can be performed. Most of them take as input
1058 an rtx X. Some also take as input a transition label LABEL; the others
1059 are booleans for which the transition label is always "true".
1061 The order of the enum isn't important. */
1062 enum kind_enum {
1063 /* Check GET_CODE (X) == LABEL. */
1064 CODE,
1066 /* Check GET_MODE (X) == LABEL. */
1067 MODE,
1069 /* Check REGNO (X) == LABEL. */
1070 REGNO_FIELD,
1072 /* Check XINT (X, u.opno) == LABEL. */
1073 INT_FIELD,
1075 /* Check XWINT (X, u.opno) == LABEL. */
1076 WIDE_INT_FIELD,
1078 /* Check XVECLEN (X, 0) == LABEL. */
1079 VECLEN,
1081 /* Check peep2_current_count >= u.min_len. */
1082 PEEP2_COUNT,
1084 /* Check XVECLEN (X, 0) >= u.min_len. */
1085 VECLEN_GE,
1087 /* Check whether X is a cached const_int with value u.integer. */
1088 SAVED_CONST_INT,
1090 /* Check u.predicate.data (X, u.predicate.mode). */
1091 PREDICATE,
1093 /* Check rtx_equal_p (X, operands[u.opno]). */
1094 DUPLICATE,
1096 /* Check whether X matches pattern u.pattern. */
1097 PATTERN,
1099 /* Check whether pnum_clobbers is nonnull (RECOG only). */
1100 HAVE_NUM_CLOBBERS,
1102 /* Check whether general C test u.string holds. In general the condition
1103 needs access to "insn" and the full operand list. */
1104 C_TEST,
1106 /* Execute operands[u.opno] = X. (Always succeeds.) */
1107 SET_OP,
1109 /* Accept u.acceptance. Always succeeds for SUBPATTERN, RECOG and SPLIT.
1110 May fail for PEEPHOLE2 if the define_peephole2 C code executes FAIL. */
1111 ACCEPT
1114 /* The position of rtx X in the above description, relative to the
1115 incoming instruction "insn". The position is null if the test
1116 doesn't take an X as input. */
1117 position *pos;
1119 /* Which element of operands[] already contains POS, or -1 if no element
1120 is known to hold POS. */
1121 int pos_operand;
1123 /* The type of test and its parameters, as described above. */
1124 kind_enum kind;
1125 union
1127 int opno;
1128 int min_len;
1129 struct
1131 bool is_param;
1132 int value;
1133 } integer;
1134 struct
1136 const struct pred_data *data;
1137 /* True if the mode is taken from a machine_mode parameter
1138 to the routine rather than a constant machine_mode. If true,
1139 MODE is the number of the parameter (for an "i%d" format string),
1140 otherwise it is the mode itself. */
1141 bool mode_is_param;
1142 unsigned int mode;
1143 } predicate;
1144 pattern_use *pattern;
1145 const char *string;
1146 acceptance_type acceptance;
1147 } u;
1149 static rtx_test code (position *);
1150 static rtx_test mode (position *);
1151 static rtx_test regno_field (position *);
1152 static rtx_test int_field (position *, int);
1153 static rtx_test wide_int_field (position *, int);
1154 static rtx_test veclen (position *);
1155 static rtx_test peep2_count (int);
1156 static rtx_test veclen_ge (position *, int);
1157 static rtx_test predicate (position *, const pred_data *, machine_mode);
1158 static rtx_test duplicate (position *, int);
1159 static rtx_test pattern (position *, pattern_use *);
1160 static rtx_test have_num_clobbers ();
1161 static rtx_test c_test (const char *);
1162 static rtx_test set_op (position *, int);
1163 static rtx_test accept (const acceptance_type &);
1165 bool terminal_p () const;
1166 bool single_outcome_p () const;
1168 private:
1169 rtx_test (position *, kind_enum);
1172 rtx_test::rtx_test () {}
1174 rtx_test::rtx_test (position *pos_in, kind_enum kind_in)
1175 : pos (pos_in), pos_operand (-1), kind (kind_in) {}
1177 rtx_test
1178 rtx_test::code (position *pos)
1180 return rtx_test (pos, rtx_test::CODE);
1183 rtx_test
1184 rtx_test::mode (position *pos)
1186 return rtx_test (pos, rtx_test::MODE);
1189 rtx_test
1190 rtx_test::regno_field (position *pos)
1192 rtx_test res (pos, rtx_test::REGNO_FIELD);
1193 return res;
1196 rtx_test
1197 rtx_test::int_field (position *pos, int opno)
1199 rtx_test res (pos, rtx_test::INT_FIELD);
1200 res.u.opno = opno;
1201 return res;
1204 rtx_test
1205 rtx_test::wide_int_field (position *pos, int opno)
1207 rtx_test res (pos, rtx_test::WIDE_INT_FIELD);
1208 res.u.opno = opno;
1209 return res;
1212 rtx_test
1213 rtx_test::veclen (position *pos)
1215 return rtx_test (pos, rtx_test::VECLEN);
1218 rtx_test
1219 rtx_test::peep2_count (int min_len)
1221 rtx_test res (0, rtx_test::PEEP2_COUNT);
1222 res.u.min_len = min_len;
1223 return res;
1226 rtx_test
1227 rtx_test::veclen_ge (position *pos, int min_len)
1229 rtx_test res (pos, rtx_test::VECLEN_GE);
1230 res.u.min_len = min_len;
1231 return res;
1234 rtx_test
1235 rtx_test::predicate (position *pos, const struct pred_data *data,
1236 machine_mode mode)
1238 rtx_test res (pos, rtx_test::PREDICATE);
1239 res.u.predicate.data = data;
1240 res.u.predicate.mode_is_param = false;
1241 res.u.predicate.mode = mode;
1242 return res;
1245 rtx_test
1246 rtx_test::duplicate (position *pos, int opno)
1248 rtx_test res (pos, rtx_test::DUPLICATE);
1249 res.u.opno = opno;
1250 return res;
1253 rtx_test
1254 rtx_test::pattern (position *pos, pattern_use *pattern)
1256 rtx_test res (pos, rtx_test::PATTERN);
1257 res.u.pattern = pattern;
1258 return res;
1261 rtx_test
1262 rtx_test::have_num_clobbers ()
1264 return rtx_test (0, rtx_test::HAVE_NUM_CLOBBERS);
1267 rtx_test
1268 rtx_test::c_test (const char *string)
1270 rtx_test res (0, rtx_test::C_TEST);
1271 res.u.string = string;
1272 return res;
1275 rtx_test
1276 rtx_test::set_op (position *pos, int opno)
1278 rtx_test res (pos, rtx_test::SET_OP);
1279 res.u.opno = opno;
1280 return res;
1283 rtx_test
1284 rtx_test::accept (const acceptance_type &acceptance)
1286 rtx_test res (0, rtx_test::ACCEPT);
1287 res.u.acceptance = acceptance;
1288 return res;
1291 /* Return true if the test represents an unconditionally successful match. */
1293 bool
1294 rtx_test::terminal_p () const
1296 return kind == rtx_test::ACCEPT && u.acceptance.type != PEEPHOLE2;
1299 /* Return true if the test is a boolean that is always true. */
1301 bool
1302 rtx_test::single_outcome_p () const
1304 return terminal_p () || kind == rtx_test::SET_OP;
1307 bool
1308 operator == (const rtx_test &a, const rtx_test &b)
1310 if (a.pos != b.pos || a.kind != b.kind)
1311 return false;
1312 switch (a.kind)
1314 case rtx_test::CODE:
1315 case rtx_test::MODE:
1316 case rtx_test::REGNO_FIELD:
1317 case rtx_test::VECLEN:
1318 case rtx_test::HAVE_NUM_CLOBBERS:
1319 return true;
1321 case rtx_test::PEEP2_COUNT:
1322 case rtx_test::VECLEN_GE:
1323 return a.u.min_len == b.u.min_len;
1325 case rtx_test::INT_FIELD:
1326 case rtx_test::WIDE_INT_FIELD:
1327 case rtx_test::DUPLICATE:
1328 case rtx_test::SET_OP:
1329 return a.u.opno == b.u.opno;
1331 case rtx_test::SAVED_CONST_INT:
1332 return (a.u.integer.is_param == b.u.integer.is_param
1333 && a.u.integer.value == b.u.integer.value);
1335 case rtx_test::PREDICATE:
1336 return (a.u.predicate.data == b.u.predicate.data
1337 && a.u.predicate.mode_is_param == b.u.predicate.mode_is_param
1338 && a.u.predicate.mode == b.u.predicate.mode);
1340 case rtx_test::PATTERN:
1341 return (a.u.pattern->routine == b.u.pattern->routine
1342 && a.u.pattern->params == b.u.pattern->params);
1344 case rtx_test::C_TEST:
1345 return strcmp (a.u.string, b.u.string) == 0;
1347 case rtx_test::ACCEPT:
1348 return a.u.acceptance == b.u.acceptance;
1350 gcc_unreachable ();
1353 bool
1354 operator != (const rtx_test &a, const rtx_test &b)
1356 return !operator == (a, b);
1359 /* A simple set of transition labels. Most transitions have a singleton
1360 label, so try to make that case as efficient as possible. */
1361 struct int_set : public auto_vec <uint64_t, 1>
1363 typedef uint64_t *iterator;
1365 int_set ();
1366 int_set (uint64_t);
1367 int_set (const int_set &);
1369 int_set &operator = (const int_set &);
1371 iterator begin ();
1372 iterator end ();
1375 int_set::int_set () {}
1377 int_set::int_set (uint64_t label)
1379 safe_push (label);
1382 int_set::int_set (const int_set &other)
1384 safe_splice (other);
1387 int_set &
1388 int_set::operator = (const int_set &other)
1390 truncate (0);
1391 safe_splice (other);
1392 return *this;
1395 int_set::iterator
1396 int_set::begin ()
1398 return address ();
1401 int_set::iterator
1402 int_set::end ()
1404 return address () + length ();
1407 bool
1408 operator == (const int_set &a, const int_set &b)
1410 if (a.length () != b.length ())
1411 return false;
1412 for (unsigned int i = 0; i < a.length (); ++i)
1413 if (a[i] != b[i])
1414 return false;
1415 return true;
1418 bool
1419 operator != (const int_set &a, const int_set &b)
1421 return !operator == (a, b);
1424 struct decision;
1426 /* Represents a transition between states, dependent on the result of
1427 a test T. */
1428 struct transition
1430 transition (const int_set &, state *, bool);
1432 void set_parent (list_head <transition> *);
1434 /* Links to other transitions for T. Always null for boolean tests. */
1435 transition *prev, *next;
1437 /* The transition should be taken when T has one of these values.
1438 E.g. for rtx_test::CODE this is a set of codes, while for booleans like
1439 rtx_test::PREDICATE it is always a singleton "true". The labels are
1440 sorted in ascending order. */
1441 int_set labels;
1443 /* The source decision. */
1444 decision *from;
1446 /* The target state. */
1447 state *to;
1449 /* True if TO would function correctly even if TEST wasn't performed.
1450 E.g. it isn't necessary to check whether GET_MODE (x1) is SImode
1451 before calling register_operand (x1, SImode), since register_operand
1452 performs its own mode check. However, checking GET_MODE can be a cheap
1453 way of disambiguating SImode and DImode register operands. */
1454 bool optional;
1456 /* True if LABELS contains parameter numbers rather than constants.
1457 E.g. if this is true for a rtx_test::CODE, the label is the number
1458 of an rtx_code parameter rather than an rtx_code itself.
1459 LABELS is always a singleton when this variable is true. */
1460 bool is_param;
1463 /* Represents a test and the action that should be taken on the result.
1464 If a transition exists for the test outcome, the machine switches
1465 to the transition's target state. If no suitable transition exists,
1466 the machine either falls through to the next decision or, if there are no
1467 more decisions to try, fails the match. */
1468 struct decision : list_head <transition>
1470 decision (const rtx_test &);
1472 void set_parent (list_head <decision> *s);
1473 bool if_statement_p (uint64_t * = 0) const;
1475 /* The state to which this decision belongs. */
1476 state *s;
1478 /* Links to other decisions in the same state. */
1479 decision *prev, *next;
1481 /* The test to perform. */
1482 rtx_test test;
1485 /* Represents one machine state. For each state the machine tries a list
1486 of decisions, in order, and acts on the first match. It fails without
1487 further backtracking if no decisions match. */
1488 struct state : list_head <decision>
1490 void set_parent (list_head <state> *) {}
1493 transition::transition (const int_set &labels_in, state *to_in,
1494 bool optional_in)
1495 : prev (0), next (0), labels (labels_in), from (0), to (to_in),
1496 optional (optional_in), is_param (false) {}
1498 /* Set the source decision of the transition. */
1500 void
1501 transition::set_parent (list_head <transition> *from_in)
1503 from = static_cast <decision *> (from_in);
1506 decision::decision (const rtx_test &test_in)
1507 : prev (0), next (0), test (test_in) {}
1509 /* Set the state to which this decision belongs. */
1511 void
1512 decision::set_parent (list_head <decision> *s_in)
1514 s = static_cast <state *> (s_in);
1517 /* Return true if the decision has a single transition with a single label.
1518 If so, return the label in *LABEL if nonnull. */
1520 inline bool
1521 decision::if_statement_p (uint64_t *label) const
1523 if (singleton () && first->labels.length () == 1)
1525 if (label)
1526 *label = first->labels[0];
1527 return true;
1529 return false;
1532 /* Add to FROM a decision that performs TEST and has a single transition
1533 TRANS. */
1535 static void
1536 add_decision (state *from, const rtx_test &test, transition *trans)
1538 decision *d = new decision (test);
1539 from->push_back (d);
1540 d->push_back (trans);
1543 /* Add a transition from FROM to a new, empty state that is taken
1544 when TEST == LABELS. OPTIONAL says whether the new transition
1545 should be optional. Return the new state. */
1547 static state *
1548 add_decision (state *from, const rtx_test &test, int_set labels, bool optional)
1550 state *to = new state;
1551 add_decision (from, test, new transition (labels, to, optional));
1552 return to;
1555 /* Insert a decision before decisions R to make them dependent on
1556 TEST == LABELS. OPTIONAL says whether the new transition should be
1557 optional. */
1559 static decision *
1560 insert_decision_before (state::range r, const rtx_test &test,
1561 const int_set &labels, bool optional)
1563 decision *newd = new decision (test);
1564 state *news = new state;
1565 newd->push_back (new transition (labels, news, optional));
1566 r.start->s->replace (r, newd);
1567 news->push_back (r);
1568 return newd;
1571 /* Remove any optional transitions from S that turned out not to be useful. */
1573 static void
1574 collapse_optional_decisions (state *s)
1576 decision *d = s->first;
1577 while (d)
1579 decision *next = d->next;
1580 for (transition *trans = d->first; trans; trans = trans->next)
1581 collapse_optional_decisions (trans->to);
1582 /* A decision with a single optional transition doesn't help
1583 partition the potential matches and so is unlikely to be
1584 worthwhile. In particular, if the decision that performs the
1585 test is the last in the state, the best it could do is reject
1586 an invalid pattern slightly earlier. If instead the decision
1587 is not the last in the state, the condition it tests could hold
1588 even for the later decisions in the state. The best it can do
1589 is save work in some cases where only the later decisions can
1590 succeed.
1592 In both cases the optional transition would add extra work to
1593 successful matches when the tested condition holds. */
1594 if (transition *trans = d->singleton ())
1595 if (trans->optional)
1596 s->replace (d, trans->to->release ());
1597 d = next;
1601 /* Try to squash several separate tests into simpler ones. */
1603 static void
1604 simplify_tests (state *s)
1606 for (decision *d = s->first; d; d = d->next)
1608 uint64_t label;
1609 /* Convert checks for GET_CODE (x) == CONST_INT and XWINT (x, 0) == N
1610 into checks for const_int_rtx[N'], if N is suitably small. */
1611 if (d->test.kind == rtx_test::CODE
1612 && d->if_statement_p (&label)
1613 && label == CONST_INT)
1614 if (decision *second = d->first->to->singleton ())
1615 if (d->test.pos == second->test.pos
1616 && second->test.kind == rtx_test::WIDE_INT_FIELD
1617 && second->test.u.opno == 0
1618 && second->if_statement_p (&label)
1619 && IN_RANGE (int64_t (label),
1620 -MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT))
1622 d->test.kind = rtx_test::SAVED_CONST_INT;
1623 d->test.u.integer.is_param = false;
1624 d->test.u.integer.value = label;
1625 d->replace (d->first, second->release ());
1626 d->first->labels[0] = true;
1628 /* If we have a CODE test followed by a PREDICATE test, rely on
1629 the predicate to test the code.
1631 This case exists for match_operators. We initially treat the
1632 CODE test for a match_operator as non-optional so that we can
1633 safely move down to its operands. It may turn out that all
1634 paths that reach that code test require the same predicate
1635 to be true. cse_tests will then put the predicate test in
1636 series with the code test. */
1637 if (d->test.kind == rtx_test::CODE)
1638 if (transition *trans = d->singleton ())
1640 state *s = trans->to;
1641 while (decision *d2 = s->singleton ())
1643 if (d->test.pos != d2->test.pos)
1644 break;
1645 transition *trans2 = d2->singleton ();
1646 if (!trans2)
1647 break;
1648 if (d2->test.kind == rtx_test::PREDICATE)
1650 d->test = d2->test;
1651 trans->labels = int_set (true);
1652 s->replace (d2, trans2->to->release ());
1653 break;
1655 s = trans2->to;
1658 for (transition *trans = d->first; trans; trans = trans->next)
1659 simplify_tests (trans->to);
1663 /* Return true if all successful returns passing through D require the
1664 condition tested by COMMON to be true.
1666 When returning true, add all transitions like COMMON in D to WHERE.
1667 WHERE may contain a partial result on failure. */
1669 static bool
1670 common_test_p (decision *d, transition *common, vec <transition *> *where)
1672 if (d->test.kind == rtx_test::ACCEPT)
1673 /* We found a successful return that didn't require COMMON. */
1674 return false;
1675 if (d->test == common->from->test)
1677 transition *trans = d->singleton ();
1678 if (!trans
1679 || trans->optional != common->optional
1680 || trans->labels != common->labels)
1681 return false;
1682 where->safe_push (trans);
1683 return true;
1685 for (transition *trans = d->first; trans; trans = trans->next)
1686 for (decision *subd = trans->to->first; subd; subd = subd->next)
1687 if (!common_test_p (subd, common, where))
1688 return false;
1689 return true;
1692 /* Indicates that we have tested GET_CODE (X) for a particular rtx X. */
1693 const unsigned char TESTED_CODE = 1;
1695 /* Indicates that we have tested XVECLEN (X, 0) for a particular rtx X. */
1696 const unsigned char TESTED_VECLEN = 2;
1698 /* Represents a set of conditions that are known to hold. */
1699 struct known_conditions
1701 /* A mask of TESTED_ values for each position, indexed by the position's
1702 id field. */
1703 auto_vec <unsigned char> position_tests;
1705 /* Index N says whether operands[N] has been set. */
1706 auto_vec <bool> set_operands;
1708 /* A guranteed lower bound on the value of peep2_current_count. */
1709 int peep2_count;
1712 /* Return true if TEST can safely be performed at D, where
1713 the conditions in KC hold. TEST is known to occur along the
1714 first path from D (i.e. always following the first transition
1715 of the first decision). Any intervening tests can be used as
1716 negative proof that hoisting isn't safe, but only KC can be used
1717 as positive proof. */
1719 static bool
1720 safe_to_hoist_p (decision *d, const rtx_test &test, known_conditions *kc)
1722 switch (test.kind)
1724 case rtx_test::C_TEST:
1725 /* In general, C tests require everything else to have been
1726 verified and all operands to have been set up. */
1727 return false;
1729 case rtx_test::ACCEPT:
1730 /* Don't accept something before all conditions have been tested. */
1731 return false;
1733 case rtx_test::PREDICATE:
1734 /* Don't move a predicate over a test for VECLEN_GE, since the
1735 predicate used in a match_parallel can legitimately expect the
1736 length to be checked first. */
1737 for (decision *subd = d;
1738 subd->test != test;
1739 subd = subd->first->to->first)
1740 if (subd->test.pos == test.pos
1741 && subd->test.kind == rtx_test::VECLEN_GE)
1742 return false;
1743 goto any_rtx;
1745 case rtx_test::DUPLICATE:
1746 /* Don't test for a match_dup until the associated operand has
1747 been set. */
1748 if (!kc->set_operands[test.u.opno])
1749 return false;
1750 goto any_rtx;
1752 case rtx_test::CODE:
1753 case rtx_test::MODE:
1754 case rtx_test::SAVED_CONST_INT:
1755 case rtx_test::SET_OP:
1756 any_rtx:
1757 /* Check whether it is safe to access the rtx under test. */
1758 switch (test.pos->type)
1760 case POS_PEEP2_INSN:
1761 return test.pos->arg < kc->peep2_count;
1763 case POS_XEXP:
1764 return kc->position_tests[test.pos->base->id] & TESTED_CODE;
1766 case POS_XVECEXP0:
1767 return kc->position_tests[test.pos->base->id] & TESTED_VECLEN;
1769 gcc_unreachable ();
1771 case rtx_test::REGNO_FIELD:
1772 case rtx_test::INT_FIELD:
1773 case rtx_test::WIDE_INT_FIELD:
1774 case rtx_test::VECLEN:
1775 case rtx_test::VECLEN_GE:
1776 /* These tests access a specific part of an rtx, so are only safe
1777 once we know what the rtx is. */
1778 return kc->position_tests[test.pos->id] & TESTED_CODE;
1780 case rtx_test::PEEP2_COUNT:
1781 case rtx_test::HAVE_NUM_CLOBBERS:
1782 /* These tests can be performed anywhere. */
1783 return true;
1785 case rtx_test::PATTERN:
1786 gcc_unreachable ();
1788 gcc_unreachable ();
1791 /* Look for a transition that is taken by all successful returns from a range
1792 of decisions starting at OUTER and that would be better performed by
1793 OUTER's state instead. On success, store all instances of that transition
1794 in WHERE and return the last decision in the range. The range could
1795 just be OUTER, or it could include later decisions as well.
1797 WITH_POSITION_P is true if only tests with position POS should be tried,
1798 false if any test should be tried. WORTHWHILE_SINGLE_P is true if the
1799 result is useful even when the range contains just a single decision
1800 with a single transition. KC are the conditions that are known to
1801 hold at OUTER. */
1803 static decision *
1804 find_common_test (decision *outer, bool with_position_p,
1805 position *pos, bool worthwhile_single_p,
1806 known_conditions *kc, vec <transition *> *where)
1808 /* After this, WORTHWHILE_SINGLE_P indicates whether a range that contains
1809 just a single decision is useful, regardless of the number of
1810 transitions it has. */
1811 if (!outer->singleton ())
1812 worthwhile_single_p = true;
1813 /* Quick exit if we don't have enough decisions to form a worthwhile
1814 range. */
1815 if (!worthwhile_single_p && !outer->next)
1816 return 0;
1817 /* Follow the first chain down, as one example of a path that needs
1818 to contain the common test. */
1819 for (decision *d = outer; d; d = d->first->to->first)
1821 transition *trans = d->singleton ();
1822 if (trans
1823 && (!with_position_p || d->test.pos == pos)
1824 && safe_to_hoist_p (outer, d->test, kc))
1826 if (common_test_p (outer, trans, where))
1828 if (!outer->next)
1829 /* We checked above whether the move is worthwhile. */
1830 return outer;
1831 /* See how many decisions in OUTER's chain could reuse
1832 the same test. */
1833 decision *outer_end = outer;
1836 unsigned int length = where->length ();
1837 if (!common_test_p (outer_end->next, trans, where))
1839 where->truncate (length);
1840 break;
1842 outer_end = outer_end->next;
1844 while (outer_end->next);
1845 /* It is worth moving TRANS if it can be shared by more than
1846 one decision. */
1847 if (outer_end != outer || worthwhile_single_p)
1848 return outer_end;
1850 where->truncate (0);
1853 return 0;
1856 /* Try to promote common subtests in S to a single, shared decision.
1857 Also try to bunch tests for the same position together. POS is the
1858 position of the rtx tested before reaching S. KC are the conditions
1859 that are known to hold on entry to S. */
1861 static void
1862 cse_tests (position *pos, state *s, known_conditions *kc)
1864 for (decision *d = s->first; d; d = d->next)
1866 auto_vec <transition *, 16> where;
1867 if (d->test.pos)
1869 /* Try to find conditions that don't depend on a particular rtx,
1870 such as pnum_clobbers != NULL or peep2_current_count >= X.
1871 It's usually better to check these conditions as soon as
1872 possible, so the change is worthwhile even if there is
1873 only one copy of the test. */
1874 decision *endd = find_common_test (d, true, 0, true, kc, &where);
1875 if (!endd && d->test.pos != pos)
1876 /* Try to find other conditions related to position POS
1877 before moving to the new position. Again, this is
1878 worthwhile even if there is only one copy of the test,
1879 since it means that fewer position variables are live
1880 at a given time. */
1881 endd = find_common_test (d, true, pos, true, kc, &where);
1882 if (!endd)
1883 /* Try to find any condition that is used more than once. */
1884 endd = find_common_test (d, false, 0, false, kc, &where);
1885 if (endd)
1887 transition *common = where[0];
1888 /* Replace [D, ENDD] with a test like COMMON. We'll recurse
1889 on the common test and see the original D again next time. */
1890 d = insert_decision_before (state::range (d, endd),
1891 common->from->test,
1892 common->labels,
1893 common->optional);
1894 /* Remove the old tests. */
1895 while (!where.is_empty ())
1897 transition *trans = where.pop ();
1898 trans->from->s->replace (trans->from, trans->to->release ());
1903 /* Make sure that safe_to_hoist_p isn't being overly conservative.
1904 It should realize that D's test is safe in the current
1905 environment. */
1906 gcc_assert (d->test.kind == rtx_test::C_TEST
1907 || d->test.kind == rtx_test::ACCEPT
1908 || safe_to_hoist_p (d, d->test, kc));
1910 /* D won't be changed any further by the current optimization.
1911 Recurse with the state temporarily updated to include D. */
1912 int prev = 0;
1913 switch (d->test.kind)
1915 case rtx_test::CODE:
1916 prev = kc->position_tests[d->test.pos->id];
1917 kc->position_tests[d->test.pos->id] |= TESTED_CODE;
1918 break;
1920 case rtx_test::VECLEN:
1921 case rtx_test::VECLEN_GE:
1922 prev = kc->position_tests[d->test.pos->id];
1923 kc->position_tests[d->test.pos->id] |= TESTED_VECLEN;
1924 break;
1926 case rtx_test::SET_OP:
1927 prev = kc->set_operands[d->test.u.opno];
1928 gcc_assert (!prev);
1929 kc->set_operands[d->test.u.opno] = true;
1930 break;
1932 case rtx_test::PEEP2_COUNT:
1933 prev = kc->peep2_count;
1934 kc->peep2_count = MAX (prev, d->test.u.min_len);
1935 break;
1937 default:
1938 break;
1940 for (transition *trans = d->first; trans; trans = trans->next)
1941 cse_tests (d->test.pos ? d->test.pos : pos, trans->to, kc);
1942 switch (d->test.kind)
1944 case rtx_test::CODE:
1945 case rtx_test::VECLEN:
1946 case rtx_test::VECLEN_GE:
1947 kc->position_tests[d->test.pos->id] = prev;
1948 break;
1950 case rtx_test::SET_OP:
1951 kc->set_operands[d->test.u.opno] = prev;
1952 break;
1954 case rtx_test::PEEP2_COUNT:
1955 kc->peep2_count = prev;
1956 break;
1958 default:
1959 break;
1964 /* Return the type of value that can be used to parameterize test KIND,
1965 or parameter::UNSET if none. */
1967 parameter::type_enum
1968 transition_parameter_type (rtx_test::kind_enum kind)
1970 switch (kind)
1972 case rtx_test::CODE:
1973 return parameter::CODE;
1975 case rtx_test::MODE:
1976 return parameter::MODE;
1978 case rtx_test::REGNO_FIELD:
1979 return parameter::UINT;
1981 case rtx_test::INT_FIELD:
1982 case rtx_test::VECLEN:
1983 case rtx_test::PATTERN:
1984 return parameter::INT;
1986 case rtx_test::WIDE_INT_FIELD:
1987 return parameter::WIDE_INT;
1989 case rtx_test::PEEP2_COUNT:
1990 case rtx_test::VECLEN_GE:
1991 case rtx_test::SAVED_CONST_INT:
1992 case rtx_test::PREDICATE:
1993 case rtx_test::DUPLICATE:
1994 case rtx_test::HAVE_NUM_CLOBBERS:
1995 case rtx_test::C_TEST:
1996 case rtx_test::SET_OP:
1997 case rtx_test::ACCEPT:
1998 return parameter::UNSET;
2000 gcc_unreachable ();
2003 /* Initialize the pos_operand fields of each state reachable from S.
2004 If OPERAND_POS[ID] >= 0, the position with id ID is stored in
2005 operands[OPERAND_POS[ID]] on entry to S. */
2007 static void
2008 find_operand_positions (state *s, vec <int> &operand_pos)
2010 for (decision *d = s->first; d; d = d->next)
2012 int this_operand = (d->test.pos ? operand_pos[d->test.pos->id] : -1);
2013 if (this_operand >= 0)
2014 d->test.pos_operand = this_operand;
2015 if (d->test.kind == rtx_test::SET_OP)
2016 operand_pos[d->test.pos->id] = d->test.u.opno;
2017 for (transition *trans = d->first; trans; trans = trans->next)
2018 find_operand_positions (trans->to, operand_pos);
2019 if (d->test.kind == rtx_test::SET_OP)
2020 operand_pos[d->test.pos->id] = this_operand;
2024 /* Statistics about a matching routine. */
2025 struct stats
2027 stats ();
2029 /* The total number of decisions in the routine, excluding trivial
2030 ones that never fail. */
2031 unsigned int num_decisions;
2033 /* The number of non-trivial decisions on the longest path through
2034 the routine, and the return value that contributes most to that
2035 long path. */
2036 unsigned int longest_path;
2037 int longest_path_code;
2039 /* The maximum number of times that a single call to the routine
2040 can backtrack, and the value returned at the end of that path.
2041 "Backtracking" here means failing one decision in state and
2042 going onto to the next. */
2043 unsigned int longest_backtrack;
2044 int longest_backtrack_code;
2047 stats::stats ()
2048 : num_decisions (0), longest_path (0), longest_path_code (-1),
2049 longest_backtrack (0), longest_backtrack_code (-1) {}
2051 /* Return statistics about S. */
2053 static stats
2054 get_stats (state *s)
2056 stats for_s;
2057 unsigned int longest_path = 0;
2058 for (decision *d = s->first; d; d = d->next)
2060 /* Work out the statistics for D. */
2061 stats for_d;
2062 for (transition *trans = d->first; trans; trans = trans->next)
2064 stats for_trans = get_stats (trans->to);
2065 for_d.num_decisions += for_trans.num_decisions;
2066 /* Each transition is mutually-exclusive, so just pick the
2067 longest of the individual paths. */
2068 if (for_d.longest_path <= for_trans.longest_path)
2070 for_d.longest_path = for_trans.longest_path;
2071 for_d.longest_path_code = for_trans.longest_path_code;
2073 /* Likewise for backtracking. */
2074 if (for_d.longest_backtrack <= for_trans.longest_backtrack)
2076 for_d.longest_backtrack = for_trans.longest_backtrack;
2077 for_d.longest_backtrack_code = for_trans.longest_backtrack_code;
2081 /* Account for D's test in its statistics. */
2082 if (!d->test.single_outcome_p ())
2084 for_d.num_decisions += 1;
2085 for_d.longest_path += 1;
2087 if (d->test.kind == rtx_test::ACCEPT)
2089 for_d.longest_path_code = d->test.u.acceptance.u.full.code;
2090 for_d.longest_backtrack_code = d->test.u.acceptance.u.full.code;
2093 /* Keep a running count of the number of backtracks. */
2094 if (d->prev)
2095 for_s.longest_backtrack += 1;
2097 /* Accumulate D's statistics into S's. */
2098 for_s.num_decisions += for_d.num_decisions;
2099 for_s.longest_path += for_d.longest_path;
2100 for_s.longest_backtrack += for_d.longest_backtrack;
2102 /* Use the code from the decision with the longest individual path,
2103 since that's more likely to be useful if trying to make the
2104 path shorter. In the event of a tie, pick the later decision,
2105 since that's closer to the end of the path. */
2106 if (longest_path <= for_d.longest_path)
2108 longest_path = for_d.longest_path;
2109 for_s.longest_path_code = for_d.longest_path_code;
2112 /* Later decisions in a state are necessarily in a longer backtrack
2113 than earlier decisions. */
2114 for_s.longest_backtrack_code = for_d.longest_backtrack_code;
2116 return for_s;
2119 /* Optimize ROOT. Use TYPE to describe ROOT in status messages. */
2121 static void
2122 optimize_subroutine_group (const char *type, state *root)
2124 /* Remove optional transitions that turned out not to be worthwhile. */
2125 if (collapse_optional_decisions_p)
2126 collapse_optional_decisions (root);
2128 /* Try to remove duplicated tests and to rearrange tests into a more
2129 logical order. */
2130 if (cse_tests_p)
2132 known_conditions kc;
2133 kc.position_tests.safe_grow_cleared (num_positions);
2134 kc.set_operands.safe_grow_cleared (num_operands);
2135 kc.peep2_count = 1;
2136 cse_tests (&root_pos, root, &kc);
2139 /* Try to simplify two or more tests into one. */
2140 if (simplify_tests_p)
2141 simplify_tests (root);
2143 /* Try to use operands[] instead of xN variables. */
2144 if (use_operand_variables_p)
2146 auto_vec <int> operand_pos (num_positions);
2147 for (unsigned int i = 0; i < num_positions; ++i)
2148 operand_pos.quick_push (-1);
2149 find_operand_positions (root, operand_pos);
2152 /* Print a summary of the new state. */
2153 stats st = get_stats (root);
2154 fprintf (stderr, "Statistics for %s:\n", type);
2155 fprintf (stderr, " Number of decisions: %6d\n", st.num_decisions);
2156 fprintf (stderr, " longest path: %6d (code: %6d)\n",
2157 st.longest_path, st.longest_path_code);
2158 fprintf (stderr, " longest backtrack: %6d (code: %6d)\n",
2159 st.longest_backtrack, st.longest_backtrack_code);
2162 struct merge_pattern_info;
2164 /* Represents a transition from one pattern to another. */
2165 struct merge_pattern_transition
2167 merge_pattern_transition (merge_pattern_info *);
2169 /* The target pattern. */
2170 merge_pattern_info *to;
2172 /* The parameters that the source pattern passes to the target pattern.
2173 "parameter (TYPE, true, I)" represents parameter I of the source
2174 pattern. */
2175 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2178 merge_pattern_transition::merge_pattern_transition (merge_pattern_info *to_in)
2179 : to (to_in)
2183 /* Represents a pattern that can might match several states. The pattern
2184 may replace parts of the test with a parameter value. It may also
2185 replace transition labels with parameters. */
2186 struct merge_pattern_info
2188 merge_pattern_info (unsigned int);
2190 /* If PARAM_TEST_P, the state's singleton test should be generalized
2191 to use the runtime value of PARAMS[PARAM_TEST]. */
2192 unsigned int param_test : 8;
2194 /* If PARAM_TRANSITION_P, the state's single transition label should
2195 be replaced by the runtime value of PARAMS[PARAM_TRANSITION]. */
2196 unsigned int param_transition : 8;
2198 /* True if we have decided to generalize the root decision's test,
2199 as per PARAM_TEST. */
2200 unsigned int param_test_p : 1;
2202 /* Likewise for the root decision's transition, as per PARAM_TRANSITION. */
2203 unsigned int param_transition_p : 1;
2205 /* True if the contents of the structure are completely filled in. */
2206 unsigned int complete_p : 1;
2208 /* The number of pseudo-statements in the pattern. Used to decide
2209 whether it's big enough to break out into a subroutine. */
2210 unsigned int num_statements;
2212 /* The number of states that use this pattern. */
2213 unsigned int num_users;
2215 /* The number of distinct success values that the pattern returns. */
2216 unsigned int num_results;
2218 /* This array has one element for each runtime parameter to the pattern.
2219 PARAMS[I] gives the default value of parameter I, which is always
2220 constant.
2222 These default parameters are used in cases where we match the
2223 pattern against some state S1, then add more parameters while
2224 matching against some state S2. S1 is then left passing fewer
2225 parameters than S2. The array gives us enough informatino to
2226 construct a full parameter list for S1 (see update_parameters).
2228 If we decide to create a subroutine for this pattern,
2229 PARAMS[I].type determines the C type of parameter I. */
2230 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2232 /* All states that match this pattern must have the same number of
2233 transitions. TRANSITIONS[I] describes the subpattern for transition
2234 number I; it is null if transition I represents a successful return
2235 from the pattern. */
2236 auto_vec <merge_pattern_transition *, 1> transitions;
2238 /* The routine associated with the pattern, or null if we haven't generated
2239 one yet. */
2240 pattern_routine *routine;
2243 merge_pattern_info::merge_pattern_info (unsigned int num_transitions)
2244 : param_test (0),
2245 param_transition (0),
2246 param_test_p (false),
2247 param_transition_p (false),
2248 complete_p (false),
2249 num_statements (0),
2250 num_users (0),
2251 num_results (0),
2252 routine (0)
2254 transitions.safe_grow_cleared (num_transitions);
2257 /* Describes one way of matching a particular state to a particular
2258 pattern. */
2259 struct merge_state_result
2261 merge_state_result (merge_pattern_info *, position *, merge_state_result *);
2263 /* A pattern that matches the state. */
2264 merge_pattern_info *pattern;
2266 /* If we decide to use this match and create a subroutine for PATTERN,
2267 the state should pass the rtx at position ROOT to the pattern's
2268 rtx parameter. A null root means that the pattern doesn't need
2269 an rtx parameter; all the rtxes it matches come from elsewhere. */
2270 position *root;
2272 /* The parameters that should be passed to PATTERN for this state.
2273 If the array is shorter than PATTERN->params, the missing entries
2274 should be taken from the corresponding element of PATTERN->params. */
2275 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2277 /* An earlier match for the same state, or null if none. Patterns
2278 matched by earlier entries are smaller than PATTERN. */
2279 merge_state_result *prev;
2282 merge_state_result::merge_state_result (merge_pattern_info *pattern_in,
2283 position *root_in,
2284 merge_state_result *prev_in)
2285 : pattern (pattern_in), root (root_in), prev (prev_in)
2288 /* Information about a state, used while trying to match it against
2289 a pattern. */
2290 struct merge_state_info
2292 merge_state_info (state *);
2294 /* The state itself. */
2295 state *s;
2297 /* Index I gives information about the target of transition I. */
2298 merge_state_info *to_states;
2300 /* The number of transitions in S. */
2301 unsigned int num_transitions;
2303 /* True if the state has been deleted in favor of a call to a
2304 pattern routine. */
2305 bool merged_p;
2307 /* The previous state that might be a merge candidate for S, or null
2308 if no previous states could be merged with S. */
2309 merge_state_info *prev_same_test;
2311 /* A list of pattern matches for this state. */
2312 merge_state_result *res;
2315 merge_state_info::merge_state_info (state *s_in)
2316 : s (s_in),
2317 to_states (0),
2318 num_transitions (0),
2319 merged_p (false),
2320 prev_same_test (0),
2321 res (0) {}
2323 /* True if PAT would be useful as a subroutine. */
2325 static bool
2326 useful_pattern_p (merge_pattern_info *pat)
2328 return pat->num_statements >= MIN_COMBINE_COST;
2331 /* PAT2 is a subpattern of PAT1. Return true if PAT2 should be inlined
2332 into PAT1's C routine. */
2334 static bool
2335 same_pattern_p (merge_pattern_info *pat1, merge_pattern_info *pat2)
2337 return pat1->num_users == pat2->num_users || !useful_pattern_p (pat2);
2340 /* PAT was previously matched against SINFO based on tentative matches
2341 for the target states of SINFO's state. Return true if the match
2342 still holds; that is, if the target states of SINFO's state still
2343 match the corresponding transitions of PAT. */
2345 static bool
2346 valid_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2348 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2349 if (merge_pattern_transition *ptrans = pat->transitions[j])
2351 merge_state_result *to_res = sinfo->to_states[j].res;
2352 if (!to_res || to_res->pattern != ptrans->to)
2353 return false;
2355 return true;
2358 /* Remove any matches that are no longer valid from the head of SINFO's
2359 list of matches. */
2361 static void
2362 prune_invalid_results (merge_state_info *sinfo)
2364 while (sinfo->res && !valid_result_p (sinfo->res->pattern, sinfo))
2366 sinfo->res = sinfo->res->prev;
2367 gcc_assert (sinfo->res);
2371 /* Return true if PAT represents the biggest posssible match for SINFO;
2372 that is, if the next action of SINFO's state on return from PAT will
2373 be something that cannot be merged with any other state. */
2375 static bool
2376 complete_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2378 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2379 if (sinfo->to_states[j].res && !pat->transitions[j])
2380 return false;
2381 return true;
2384 /* Update TO for any parameters that have been added to FROM since TO
2385 was last set. The extra parameters in FROM will be constants or
2386 instructions to duplicate earlier parameters. */
2388 static void
2389 update_parameters (vec <parameter> &to, const vec <parameter> &from)
2391 for (unsigned int i = to.length (); i < from.length (); ++i)
2392 to.quick_push (from[i]);
2395 /* Return true if A and B can be tested by a single test. If the test
2396 can be parameterised, store the parameter value for A in *PARAMA and
2397 the parameter value for B in *PARAMB, otherwise leave PARAMA and
2398 PARAMB alone. */
2400 static bool
2401 compatible_tests_p (const rtx_test &a, const rtx_test &b,
2402 parameter *parama, parameter *paramb)
2404 if (a.kind != b.kind)
2405 return false;
2406 switch (a.kind)
2408 case rtx_test::PREDICATE:
2409 if (a.u.predicate.data != b.u.predicate.data)
2410 return false;
2411 *parama = parameter (parameter::MODE, false, a.u.predicate.mode);
2412 *paramb = parameter (parameter::MODE, false, b.u.predicate.mode);
2413 return true;
2415 case rtx_test::SAVED_CONST_INT:
2416 *parama = parameter (parameter::INT, false, a.u.integer.value);
2417 *paramb = parameter (parameter::INT, false, b.u.integer.value);
2418 return true;
2420 default:
2421 return a == b;
2425 /* PARAMS is an array of the parameters that a state is going to pass
2426 to a pattern routine. It is still incomplete; index I has a kind of
2427 parameter::UNSET if we don't yet know what the state will pass
2428 as parameter I. Try to make parameter ID equal VALUE, returning
2429 true on success. */
2431 static bool
2432 set_parameter (vec <parameter> &params, unsigned int id,
2433 const parameter &value)
2435 if (params[id].type == parameter::UNSET)
2437 if (force_unique_params_p)
2438 for (unsigned int i = 0; i < params.length (); ++i)
2439 if (params[i] == value)
2440 return false;
2441 params[id] = value;
2442 return true;
2444 return params[id] == value;
2447 /* PARAMS2 is the "params" array for a pattern and PARAMS1 is the
2448 set of parameters that a particular state is going to pass to
2449 that pattern.
2451 Try to extend PARAMS1 and PARAMS2 so that there is a parameter
2452 that is equal to PARAM1 for the state and has a default value of
2453 PARAM2. Parameters beginning at START were added as part of the
2454 same match and so may be reused. */
2456 static bool
2457 add_parameter (vec <parameter> &params1, vec <parameter> &params2,
2458 const parameter &param1, const parameter &param2,
2459 unsigned int start, unsigned int *res)
2461 gcc_assert (params1.length () == params2.length ());
2462 gcc_assert (!param1.is_param && !param2.is_param);
2464 for (unsigned int i = start; i < params2.length (); ++i)
2465 if (params1[i] == param1 && params2[i] == param2)
2467 *res = i;
2468 return true;
2471 if (force_unique_params_p)
2472 for (unsigned int i = 0; i < params2.length (); ++i)
2473 if (params1[i] == param1 || params2[i] == param2)
2474 return false;
2476 if (params2.length () >= MAX_PATTERN_PARAMS)
2477 return false;
2479 *res = params2.length ();
2480 params1.quick_push (param1);
2481 params2.quick_push (param2);
2482 return true;
2485 /* If *ROOTA is nonnull, return true if the same sequence of steps are
2486 required to reach A from *ROOTA as to reach B from ROOTB. If *ROOTA
2487 is null, update it if necessary in order to make the condition hold. */
2489 static bool
2490 merge_relative_positions (position **roota, position *a,
2491 position *rootb, position *b)
2493 if (!relative_patterns_p)
2495 if (a != b)
2496 return false;
2497 if (!*roota)
2499 *roota = rootb;
2500 return true;
2502 return *roota == rootb;
2504 /* If B does not belong to the same instruction as ROOTB, we don't
2505 start with ROOTB but instead start with a call to peep2_next_insn.
2506 In that case the sequences for B and A are identical iff B and A
2507 are themselves identical. */
2508 if (rootb->insn_id != b->insn_id)
2509 return a == b;
2510 while (rootb != b)
2512 if (!a || b->type != a->type || b->arg != a->arg)
2513 return false;
2514 b = b->base;
2515 a = a->base;
2517 if (!*roota)
2518 *roota = a;
2519 return *roota == a;
2522 /* A hasher of states that treats two states as "equal" if they might be
2523 merged (but trying to be more discriminating than "return true"). */
2524 struct test_pattern_hasher : typed_noop_remove <merge_state_info>
2526 typedef merge_state_info *value_type;
2527 typedef merge_state_info *compare_type;
2528 static inline hashval_t hash (const value_type &);
2529 static inline bool equal (const value_type &, const compare_type &);
2532 hashval_t
2533 test_pattern_hasher::hash (merge_state_info *const &sinfo)
2535 inchash::hash h;
2536 decision *d = sinfo->s->singleton ();
2537 h.add_int (d->test.pos_operand + 1);
2538 if (!relative_patterns_p)
2539 h.add_int (d->test.pos ? d->test.pos->id + 1 : 0);
2540 h.add_int (d->test.kind);
2541 h.add_int (sinfo->num_transitions);
2542 return h.end ();
2545 bool
2546 test_pattern_hasher::equal (merge_state_info *const &sinfo1,
2547 merge_state_info *const &sinfo2)
2549 decision *d1 = sinfo1->s->singleton ();
2550 decision *d2 = sinfo2->s->singleton ();
2551 gcc_assert (d1 && d2);
2553 parameter new_param1, new_param2;
2554 return (d1->test.pos_operand == d2->test.pos_operand
2555 && (relative_patterns_p || d1->test.pos == d2->test.pos)
2556 && compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2)
2557 && sinfo1->num_transitions == sinfo2->num_transitions);
2560 /* Try to make the state described by SINFO1 use the same pattern as the
2561 state described by SINFO2. Return true on success.
2563 SINFO1 and SINFO2 are known to have the same hash value. */
2565 static bool
2566 merge_patterns (merge_state_info *sinfo1, merge_state_info *sinfo2)
2568 merge_state_result *res2 = sinfo2->res;
2569 merge_pattern_info *pat = res2->pattern;
2571 /* Write to temporary arrays while matching, in case we have to abort
2572 half way through. */
2573 auto_vec <parameter, MAX_PATTERN_PARAMS> params1;
2574 auto_vec <parameter, MAX_PATTERN_PARAMS> params2;
2575 params1.quick_grow_cleared (pat->params.length ());
2576 params2.splice (pat->params);
2577 unsigned int start_param = params2.length ();
2579 /* An array for recording changes to PAT->transitions[?].params.
2580 All changes involve replacing a constant parameter with some
2581 PAT->params[N], where N is the second element of the pending_param. */
2582 typedef std::pair <parameter *, unsigned int> pending_param;
2583 auto_vec <pending_param, 32> pending_params;
2585 decision *d1 = sinfo1->s->singleton ();
2586 decision *d2 = sinfo2->s->singleton ();
2587 gcc_assert (d1 && d2);
2589 /* If D2 tests a position, SINFO1's root relative to D1 is the same
2590 as SINFO2's root relative to D2. */
2591 position *root1 = 0;
2592 position *root2 = res2->root;
2593 if (d2->test.pos_operand < 0
2594 && d1->test.pos
2595 && !merge_relative_positions (&root1, d1->test.pos,
2596 root2, d2->test.pos))
2597 return false;
2599 /* Check whether the patterns have the same shape. */
2600 unsigned int num_transitions = sinfo1->num_transitions;
2601 gcc_assert (num_transitions == sinfo2->num_transitions);
2602 for (unsigned int i = 0; i < num_transitions; ++i)
2603 if (merge_pattern_transition *ptrans = pat->transitions[i])
2605 merge_state_result *to1_res = sinfo1->to_states[i].res;
2606 merge_state_result *to2_res = sinfo2->to_states[i].res;
2607 merge_pattern_info *to_pat = ptrans->to;
2608 gcc_assert (to2_res && to2_res->pattern == to_pat);
2609 if (!to1_res || to1_res->pattern != to_pat)
2610 return false;
2611 if (to2_res->root
2612 && !merge_relative_positions (&root1, to1_res->root,
2613 root2, to2_res->root))
2614 return false;
2615 /* Match the parameters that TO1_RES passes to TO_PAT with the
2616 parameters that PAT passes to TO_PAT. */
2617 update_parameters (to1_res->params, to_pat->params);
2618 for (unsigned int j = 0; j < to1_res->params.length (); ++j)
2620 const parameter &param1 = to1_res->params[j];
2621 const parameter &param2 = ptrans->params[j];
2622 gcc_assert (!param1.is_param);
2623 if (param2.is_param)
2625 if (!set_parameter (params1, param2.value, param1))
2626 return false;
2628 else if (param1 != param2)
2630 unsigned int id;
2631 if (!add_parameter (params1, params2,
2632 param1, param2, start_param, &id))
2633 return false;
2634 /* Record that PAT should now pass parameter ID to TO_PAT,
2635 instead of the current contents of *PARAM2. We only
2636 make the change if the rest of the match succeeds. */
2637 pending_params.safe_push
2638 (pending_param (&ptrans->params[j], id));
2643 unsigned int param_test = pat->param_test;
2644 unsigned int param_transition = pat->param_transition;
2645 bool param_test_p = pat->param_test_p;
2646 bool param_transition_p = pat->param_transition_p;
2648 /* If the tests don't match exactly, try to parameterize them. */
2649 parameter new_param1, new_param2;
2650 if (!compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2))
2651 gcc_unreachable ();
2652 if (new_param1.type != parameter::UNSET)
2654 /* If the test has not already been parameterized, all existing
2655 matches use constant NEW_PARAM2. */
2656 if (param_test_p)
2658 if (!set_parameter (params1, param_test, new_param1))
2659 return false;
2661 else if (new_param1 != new_param2)
2663 if (!add_parameter (params1, params2, new_param1, new_param2,
2664 start_param, &param_test))
2665 return false;
2666 param_test_p = true;
2670 /* Match the transitions. */
2671 transition *trans1 = d1->first;
2672 transition *trans2 = d2->first;
2673 for (unsigned int i = 0; i < num_transitions; ++i)
2675 if (param_transition_p || trans1->labels != trans2->labels)
2677 /* We can only generalize a single transition with a single
2678 label. */
2679 if (num_transitions != 1
2680 || trans1->labels.length () != 1
2681 || trans2->labels.length () != 1)
2682 return false;
2684 /* Although we can match wide-int fields, in practice it leads
2685 to some odd results for const_vectors. We end up
2686 parameterizing the first N const_ints of the vector
2687 and then (once we reach the maximum number of parameters)
2688 we go on to match the other elements exactly. */
2689 if (d1->test.kind == rtx_test::WIDE_INT_FIELD)
2690 return false;
2692 /* See whether the label has a generalizable type. */
2693 parameter::type_enum param_type
2694 = transition_parameter_type (d1->test.kind);
2695 if (param_type == parameter::UNSET)
2696 return false;
2698 /* Match the labels using parameters. */
2699 new_param1 = parameter (param_type, false, trans1->labels[0]);
2700 if (param_transition_p)
2702 if (!set_parameter (params1, param_transition, new_param1))
2703 return false;
2705 else
2707 new_param2 = parameter (param_type, false, trans2->labels[0]);
2708 if (!add_parameter (params1, params2, new_param1, new_param2,
2709 start_param, &param_transition))
2710 return false;
2711 param_transition_p = true;
2714 trans1 = trans1->next;
2715 trans2 = trans2->next;
2718 /* Set any unset parameters to their default values. This occurs if some
2719 other state needed something to be parameterized in order to match SINFO2,
2720 but SINFO1 on its own does not. */
2721 for (unsigned int i = 0; i < params1.length (); ++i)
2722 if (params1[i].type == parameter::UNSET)
2723 params1[i] = params2[i];
2725 /* The match was successful. Commit all pending changes to PAT. */
2726 update_parameters (pat->params, params2);
2728 pending_param *pp;
2729 unsigned int i;
2730 FOR_EACH_VEC_ELT (pending_params, i, pp)
2731 *pp->first = parameter (pp->first->type, true, pp->second);
2733 pat->param_test = param_test;
2734 pat->param_transition = param_transition;
2735 pat->param_test_p = param_test_p;
2736 pat->param_transition_p = param_transition_p;
2738 /* Record the match of SINFO1. */
2739 merge_state_result *new_res1 = new merge_state_result (pat, root1,
2740 sinfo1->res);
2741 new_res1->params.splice (params1);
2742 sinfo1->res = new_res1;
2743 return true;
2746 /* The number of states that were removed by calling pattern routines. */
2747 static unsigned int pattern_use_states;
2749 /* The number of states used while defining pattern routines. */
2750 static unsigned int pattern_def_states;
2752 /* Information used while constructing a use or definition of a pattern
2753 routine. */
2754 struct create_pattern_info
2756 /* The routine itself. */
2757 pattern_routine *routine;
2759 /* The first unclaimed return value for this particular use or definition.
2760 We walk the substates of uses and definitions in the same order
2761 so each return value always refers to the same position within
2762 the pattern. */
2763 unsigned int next_result;
2766 static void populate_pattern_routine (create_pattern_info *,
2767 merge_state_info *, state *,
2768 const vec <parameter> &);
2770 /* SINFO matches a pattern for which we've decided to create a C routine.
2771 Return a decision that performs a call to the pattern routine,
2772 but leave the caller to add the transitions to it. Initialize CPI
2773 for this purpose. Also create a definition for the pattern routine,
2774 if it doesn't already have one.
2776 PARAMS are the parameters that SINFO passes to its pattern. */
2778 static decision *
2779 init_pattern_use (create_pattern_info *cpi, merge_state_info *sinfo,
2780 const vec <parameter> &params)
2782 state *s = sinfo->s;
2783 merge_state_result *res = sinfo->res;
2784 merge_pattern_info *pat = res->pattern;
2785 cpi->routine = pat->routine;
2786 if (!cpi->routine)
2788 /* We haven't defined the pattern routine yet, so create
2789 a definition now. */
2790 pattern_routine *routine = new pattern_routine;
2791 pat->routine = routine;
2792 cpi->routine = routine;
2793 routine->s = new state;
2794 routine->insn_p = false;
2795 routine->pnum_clobbers_p = false;
2797 /* Create an "idempotent" mapping of parameter I to parameter I.
2798 Also record the C type of each parameter to the routine. */
2799 auto_vec <parameter, MAX_PATTERN_PARAMS> def_params;
2800 for (unsigned int i = 0; i < pat->params.length (); ++i)
2802 def_params.quick_push (parameter (pat->params[i].type, true, i));
2803 routine->param_types.quick_push (pat->params[i].type);
2806 /* Any of the states that match the pattern could be used to
2807 create the routine definition. We might as well use SINFO
2808 since it's already to hand. This means that all positions
2809 in the definition will be relative to RES->root. */
2810 routine->pos = res->root;
2811 cpi->next_result = 0;
2812 populate_pattern_routine (cpi, sinfo, routine->s, def_params);
2813 gcc_assert (cpi->next_result == pat->num_results);
2815 /* Add the routine to the global list, after the subroutines
2816 that it calls. */
2817 routine->pattern_id = patterns.length ();
2818 patterns.safe_push (routine);
2821 /* Create a decision to call the routine, passing PARAMS to it. */
2822 pattern_use *use = new pattern_use;
2823 use->routine = pat->routine;
2824 use->params.splice (params);
2825 decision *d = new decision (rtx_test::pattern (res->root, use));
2827 /* If the original decision could use an element of operands[] instead
2828 of an rtx variable, try to transfer it to the new decision. */
2829 if (s->first->test.pos && res->root == s->first->test.pos)
2830 d->test.pos_operand = s->first->test.pos_operand;
2832 cpi->next_result = 0;
2833 return d;
2836 /* Make S return the next unclaimed pattern routine result for CPI. */
2838 static void
2839 add_pattern_acceptance (create_pattern_info *cpi, state *s)
2841 acceptance_type acceptance;
2842 acceptance.type = SUBPATTERN;
2843 acceptance.partial_p = false;
2844 acceptance.u.full.code = cpi->next_result;
2845 add_decision (s, rtx_test::accept (acceptance), true, false);
2846 cpi->next_result += 1;
2849 /* Initialize new empty state NEWS so that it implements SINFO's pattern
2850 (here referred to as "P"). P may be the top level of a pattern routine
2851 or a subpattern that should be inlined into its parent pattern's routine
2852 (as per same_pattern_p). The choice of SINFO for a top-level pattern is
2853 arbitrary; it could be any of the states that use P. The choice for
2854 subpatterns follows the choice for the parent pattern.
2856 PARAMS gives the value of each parameter to P in terms of the parameters
2857 to the top-level pattern. If P itself is the top level pattern, PARAMS[I]
2858 is always "parameter (TYPE, true, I)". */
2860 static void
2861 populate_pattern_routine (create_pattern_info *cpi, merge_state_info *sinfo,
2862 state *news, const vec <parameter> &params)
2864 pattern_def_states += 1;
2866 decision *d = sinfo->s->singleton ();
2867 merge_pattern_info *pat = sinfo->res->pattern;
2868 pattern_routine *routine = cpi->routine;
2870 /* Create a copy of D's test for the pattern routine and generalize it
2871 as appropriate. */
2872 decision *newd = new decision (d->test);
2873 gcc_assert (newd->test.pos_operand >= 0
2874 || !newd->test.pos
2875 || common_position (newd->test.pos,
2876 routine->pos) == routine->pos);
2877 if (pat->param_test_p)
2879 const parameter &param = params[pat->param_test];
2880 switch (newd->test.kind)
2882 case rtx_test::PREDICATE:
2883 newd->test.u.predicate.mode_is_param = param.is_param;
2884 newd->test.u.predicate.mode = param.value;
2885 break;
2887 case rtx_test::SAVED_CONST_INT:
2888 newd->test.u.integer.is_param = param.is_param;
2889 newd->test.u.integer.value = param.value;
2890 break;
2892 default:
2893 gcc_unreachable ();
2894 break;
2897 if (d->test.kind == rtx_test::C_TEST)
2898 routine->insn_p = true;
2899 else if (d->test.kind == rtx_test::HAVE_NUM_CLOBBERS)
2900 routine->pnum_clobbers_p = true;
2901 news->push_back (newd);
2903 /* Fill in the transitions of NEWD. */
2904 unsigned int i = 0;
2905 for (transition *trans = d->first; trans; trans = trans->next)
2907 /* Create a new state to act as the target of the new transition. */
2908 state *to_news = new state;
2909 if (merge_pattern_transition *ptrans = pat->transitions[i])
2911 /* The pattern hasn't finished matching yet. Get the target
2912 pattern and the corresponding target state of SINFO. */
2913 merge_pattern_info *to_pat = ptrans->to;
2914 merge_state_info *to = sinfo->to_states + i;
2915 gcc_assert (to->res->pattern == to_pat);
2916 gcc_assert (ptrans->params.length () == to_pat->params.length ());
2918 /* Express the parameters to TO_PAT in terms of the parameters
2919 to the top-level pattern. */
2920 auto_vec <parameter, MAX_PATTERN_PARAMS> to_params;
2921 for (unsigned int j = 0; j < ptrans->params.length (); ++j)
2923 const parameter &param = ptrans->params[j];
2924 to_params.quick_push (param.is_param
2925 ? params[param.value]
2926 : param);
2929 if (same_pattern_p (pat, to_pat))
2930 /* TO_PAT is part of the current routine, so just recurse. */
2931 populate_pattern_routine (cpi, to, to_news, to_params);
2932 else
2934 /* TO_PAT should be matched by calling a separate routine. */
2935 create_pattern_info sub_cpi;
2936 decision *subd = init_pattern_use (&sub_cpi, to, to_params);
2937 routine->insn_p |= sub_cpi.routine->insn_p;
2938 routine->pnum_clobbers_p |= sub_cpi.routine->pnum_clobbers_p;
2940 /* Add the pattern routine call to the new target state. */
2941 to_news->push_back (subd);
2943 /* Add a transition for each successful call result. */
2944 for (unsigned int j = 0; j < to_pat->num_results; ++j)
2946 state *res = new state;
2947 add_pattern_acceptance (cpi, res);
2948 subd->push_back (new transition (j, res, false));
2952 else
2953 /* This transition corresponds to a successful match. */
2954 add_pattern_acceptance (cpi, to_news);
2956 /* Create the transition itself, generalizing as necessary. */
2957 transition *new_trans = new transition (trans->labels, to_news,
2958 trans->optional);
2959 if (pat->param_transition_p)
2961 const parameter &param = params[pat->param_transition];
2962 new_trans->is_param = param.is_param;
2963 new_trans->labels[0] = param.value;
2965 newd->push_back (new_trans);
2966 i += 1;
2970 /* USE is a decision that calls a pattern routine and SINFO is part of the
2971 original state tree that the call is supposed to replace. Add the
2972 transitions for SINFO and its substates to USE. */
2974 static void
2975 populate_pattern_use (create_pattern_info *cpi, decision *use,
2976 merge_state_info *sinfo)
2978 pattern_use_states += 1;
2979 gcc_assert (!sinfo->merged_p);
2980 sinfo->merged_p = true;
2981 merge_state_result *res = sinfo->res;
2982 merge_pattern_info *pat = res->pattern;
2983 decision *d = sinfo->s->singleton ();
2984 unsigned int i = 0;
2985 for (transition *trans = d->first; trans; trans = trans->next)
2987 if (pat->transitions[i])
2988 /* The target state is also part of the pattern. */
2989 populate_pattern_use (cpi, use, sinfo->to_states + i);
2990 else
2992 /* The transition corresponds to a successful return from the
2993 pattern routine. */
2994 use->push_back (new transition (cpi->next_result, trans->to, false));
2995 cpi->next_result += 1;
2997 i += 1;
3001 /* We have decided to replace SINFO's state with a call to a pattern
3002 routine. Make the change, creating a definition of the pattern routine
3003 if it doesn't have one already. */
3005 static void
3006 use_pattern (merge_state_info *sinfo)
3008 merge_state_result *res = sinfo->res;
3009 merge_pattern_info *pat = res->pattern;
3010 state *s = sinfo->s;
3012 /* The pattern may have acquired new parameters after it was matched
3013 against SINFO. Update the parameters that SINFO passes accordingly. */
3014 update_parameters (res->params, pat->params);
3016 create_pattern_info cpi;
3017 decision *d = init_pattern_use (&cpi, sinfo, res->params);
3018 populate_pattern_use (&cpi, d, sinfo);
3019 s->release ();
3020 s->push_back (d);
3023 /* Look through the state trees in STATES for common patterns and
3024 split them into subroutines. */
3026 static void
3027 split_out_patterns (vec <merge_state_info> &states)
3029 unsigned int first_transition = states.length ();
3030 hash_table <test_pattern_hasher> hashtab (128);
3031 /* Stage 1: Create an order in which parent states come before their child
3032 states and in which sibling states are at consecutive locations.
3033 Having consecutive sibling states allows merge_state_info to have
3034 a single to_states pointer. */
3035 for (unsigned int i = 0; i < states.length (); ++i)
3036 for (decision *d = states[i].s->first; d; d = d->next)
3037 for (transition *trans = d->first; trans; trans = trans->next)
3039 states.safe_push (trans->to);
3040 states[i].num_transitions += 1;
3042 /* Stage 2: Now that the addresses are stable, set up the to_states
3043 pointers. Look for states that might be merged and enter them
3044 into the hash table. */
3045 for (unsigned int i = 0; i < states.length (); ++i)
3047 merge_state_info *sinfo = &states[i];
3048 if (sinfo->num_transitions)
3050 sinfo->to_states = &states[first_transition];
3051 first_transition += sinfo->num_transitions;
3053 /* For simplicity, we only try to merge states that have a single
3054 decision. This is in any case the best we can do for peephole2,
3055 since whether a peephole2 ACCEPT succeeds or not depends on the
3056 specific peephole2 pattern (which is unique to each ACCEPT
3057 and so couldn't be shared between states). */
3058 if (decision *d = sinfo->s->singleton ())
3059 /* ACCEPT states are unique, so don't even try to merge them. */
3060 if (d->test.kind != rtx_test::ACCEPT
3061 && (pattern_have_num_clobbers_p
3062 || d->test.kind != rtx_test::HAVE_NUM_CLOBBERS)
3063 && (pattern_c_test_p
3064 || d->test.kind != rtx_test::C_TEST))
3066 merge_state_info **slot = hashtab.find_slot (sinfo, INSERT);
3067 sinfo->prev_same_test = *slot;
3068 *slot = sinfo;
3071 /* Stage 3: Walk backwards through the list of states and try to merge
3072 them. This is a greedy, bottom-up match; parent nodes can only start
3073 a new leaf pattern if they fail to match when combined with all child
3074 nodes that have matching patterns.
3076 For each state we keep a list of potential matches, with each
3077 potential match being larger (and deeper) than the next match in
3078 the list. The final element in the list is a leaf pattern that
3079 matches just a single state.
3081 Each candidate pattern created in this loop is unique -- it won't
3082 have been seen by an earlier iteration. We try to match each pattern
3083 with every state that appears earlier in STATES.
3085 Because the patterns created in the loop are unique, any state
3086 that already has a match must have a final potential match that
3087 is different from any new leaf pattern. Therefore, when matching
3088 leaf patterns, we need only consider states whose list of matches
3089 is empty.
3091 The non-leaf patterns that we try are as deep as possible
3092 and are an extension of the state's previous best candidate match (PB).
3093 We need only consider states whose current potential match is also PB;
3094 any states that don't match as much as PB cannnot match the new pattern,
3095 while any states that already match more than PB must be different from
3096 the new pattern. */
3097 for (unsigned int i2 = states.length (); i2-- > 0; )
3099 merge_state_info *sinfo2 = &states[i2];
3101 /* Enforce the bottom-upness of the match: remove matches with later
3102 states if SINFO2's child states ended up finding a better match. */
3103 prune_invalid_results (sinfo2);
3105 /* Do nothing if the state doesn't match a later one and if there are
3106 no earlier states it could match. */
3107 if (!sinfo2->res && !sinfo2->prev_same_test)
3108 continue;
3110 merge_state_result *res2 = sinfo2->res;
3111 decision *d2 = sinfo2->s->singleton ();
3112 position *root2 = (d2->test.pos_operand < 0 ? d2->test.pos : 0);
3113 unsigned int num_transitions = sinfo2->num_transitions;
3115 /* If RES2 is null then SINFO2's test in isolation has not been seen
3116 before. First try matching that on its own. */
3117 if (!res2)
3119 merge_pattern_info *new_pat
3120 = new merge_pattern_info (num_transitions);
3121 merge_state_result *new_res2
3122 = new merge_state_result (new_pat, root2, res2);
3123 sinfo2->res = new_res2;
3125 new_pat->num_statements = !d2->test.single_outcome_p ();
3126 new_pat->num_results = num_transitions;
3127 bool matched_p = false;
3128 /* Look for states that don't currently match anything but
3129 can be made to match SINFO2 on its own. */
3130 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3131 sinfo1 = sinfo1->prev_same_test)
3132 if (!sinfo1->res && merge_patterns (sinfo1, sinfo2))
3133 matched_p = true;
3134 if (!matched_p)
3136 /* No other states match. */
3137 sinfo2->res = res2;
3138 delete new_pat;
3139 delete new_res2;
3140 continue;
3142 else
3143 res2 = new_res2;
3146 /* Keep the existing pattern if it's as good as anything we'd
3147 create for SINFO2. */
3148 if (complete_result_p (res2->pattern, sinfo2))
3150 res2->pattern->num_users += 1;
3151 continue;
3154 /* Create a new pattern for SINFO2. */
3155 merge_pattern_info *new_pat = new merge_pattern_info (num_transitions);
3156 merge_state_result *new_res2
3157 = new merge_state_result (new_pat, root2, res2);
3158 sinfo2->res = new_res2;
3160 /* Fill in details about the pattern. */
3161 new_pat->num_statements = !d2->test.single_outcome_p ();
3162 new_pat->num_results = 0;
3163 for (unsigned int j = 0; j < num_transitions; ++j)
3164 if (merge_state_result *to_res = sinfo2->to_states[j].res)
3166 /* Count the target state as part of this pattern.
3167 First update the root position so that it can reach
3168 the target state's root. */
3169 if (to_res->root)
3171 if (new_res2->root)
3172 new_res2->root = common_position (new_res2->root,
3173 to_res->root);
3174 else
3175 new_res2->root = to_res->root;
3177 merge_pattern_info *to_pat = to_res->pattern;
3178 merge_pattern_transition *ptrans
3179 = new merge_pattern_transition (to_pat);
3181 /* TO_PAT may have acquired more parameters when matching
3182 states earlier in STATES than TO_RES's, but the list is
3183 now final. Make sure that TO_RES is up to date. */
3184 update_parameters (to_res->params, to_pat->params);
3186 /* Start out by assuming that every user of NEW_PAT will
3187 want to pass the same (constant) parameters as TO_RES. */
3188 update_parameters (ptrans->params, to_res->params);
3190 new_pat->transitions[j] = ptrans;
3191 new_pat->num_statements += to_pat->num_statements;
3192 new_pat->num_results += to_pat->num_results;
3194 else
3195 /* The target state doesn't match anything and so is not part
3196 of the pattern. */
3197 new_pat->num_results += 1;
3199 /* See if any earlier states that match RES2's pattern also match
3200 NEW_PAT. */
3201 bool matched_p = false;
3202 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3203 sinfo1 = sinfo1->prev_same_test)
3205 prune_invalid_results (sinfo1);
3206 if (sinfo1->res
3207 && sinfo1->res->pattern == res2->pattern
3208 && merge_patterns (sinfo1, sinfo2))
3209 matched_p = true;
3211 if (!matched_p)
3213 /* Nothing else matches NEW_PAT, so go back to the previous
3214 pattern (possibly just a single-state one). */
3215 sinfo2->res = res2;
3216 delete new_pat;
3217 delete new_res2;
3219 /* Assume that SINFO2 will use RES. At this point we don't know
3220 whether earlier states that match the same pattern will use
3221 that match or a different one. */
3222 sinfo2->res->pattern->num_users += 1;
3224 /* Step 4: Finalize the choice of pattern for each state, ignoring
3225 patterns that were only used once. Update each pattern's size
3226 so that it doesn't include subpatterns that are going to be split
3227 out into subroutines. */
3228 for (unsigned int i = 0; i < states.length (); ++i)
3230 merge_state_info *sinfo = &states[i];
3231 merge_state_result *res = sinfo->res;
3232 /* Wind past patterns that are only used by SINFO. */
3233 while (res && res->pattern->num_users == 1)
3235 res = res->prev;
3236 sinfo->res = res;
3237 if (res)
3238 res->pattern->num_users += 1;
3240 if (!res)
3241 continue;
3243 /* We have a shared pattern and are now committed to the match. */
3244 merge_pattern_info *pat = res->pattern;
3245 gcc_assert (valid_result_p (pat, sinfo));
3247 if (!pat->complete_p)
3249 /* Look for subpatterns that are going to be split out and remove
3250 them from the number of statements. */
3251 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
3252 if (merge_pattern_transition *ptrans = pat->transitions[j])
3254 merge_pattern_info *to_pat = ptrans->to;
3255 if (!same_pattern_p (pat, to_pat))
3256 pat->num_statements -= to_pat->num_statements;
3258 pat->complete_p = true;
3261 /* Step 5: Split out the patterns. */
3262 for (unsigned int i = 0; i < states.length (); ++i)
3264 merge_state_info *sinfo = &states[i];
3265 merge_state_result *res = sinfo->res;
3266 if (!sinfo->merged_p && res && useful_pattern_p (res->pattern))
3267 use_pattern (sinfo);
3269 fprintf (stderr, "Shared %d out of %d states by creating %d new states,"
3270 " saving %d\n",
3271 pattern_use_states, states.length (), pattern_def_states,
3272 pattern_use_states - pattern_def_states);
3275 /* Information about a state tree that we're considering splitting into a
3276 subroutine. */
3277 struct state_size
3279 /* The number of pseudo-statements in the state tree. */
3280 unsigned int num_statements;
3282 /* The approximate number of nested "if" and "switch" statements that
3283 would be required if control could fall through to a later state. */
3284 unsigned int depth;
3287 /* Pairs a transition with information about its target state. */
3288 typedef std::pair <transition *, state_size> subroutine_candidate;
3290 /* Sort two subroutine_candidates so that the one with the largest
3291 number of statements comes last. */
3293 static int
3294 subroutine_candidate_cmp (const void *a, const void *b)
3296 return int (((const subroutine_candidate *) a)->second.num_statements
3297 - ((const subroutine_candidate *) b)->second.num_statements);
3300 /* Turn S into a subroutine of type TYPE and add it to PROCS. Return a new
3301 state that performs a subroutine call to S. */
3303 static state *
3304 create_subroutine (routine_type type, state *s, vec <state *> &procs)
3306 procs.safe_push (s);
3307 acceptance_type acceptance;
3308 acceptance.type = type;
3309 acceptance.partial_p = true;
3310 acceptance.u.subroutine_id = procs.length ();
3311 state *news = new state;
3312 add_decision (news, rtx_test::accept (acceptance), true, false);
3313 return news;
3316 /* Walk state tree S, of type TYPE, and look for subtrees that would be
3317 better split into subroutines. Accumulate all such subroutines in PROCS.
3318 Return the size of the new state tree (excluding subroutines). */
3320 static state_size
3321 find_subroutines (routine_type type, state *s, vec <state *> &procs)
3323 auto_vec <subroutine_candidate, 16> candidates;
3324 state_size size;
3325 size.num_statements = 0;
3326 size.depth = 0;
3327 for (decision *d = s->first; d; d = d->next)
3329 if (!d->test.single_outcome_p ())
3330 size.num_statements += 1;
3331 for (transition *trans = d->first; trans; trans = trans->next)
3333 /* Keep chains of simple decisions together if we know that no
3334 change of position is required. We'll output this chain as a
3335 single "if" statement, so it counts as a single nesting level. */
3336 if (d->test.pos && d->if_statement_p ())
3337 for (;;)
3339 decision *newd = trans->to->singleton ();
3340 if (!newd
3341 || (newd->test.pos
3342 && newd->test.pos_operand < 0
3343 && newd->test.pos != d->test.pos)
3344 || !newd->if_statement_p ())
3345 break;
3346 if (!newd->test.single_outcome_p ())
3347 size.num_statements += 1;
3348 trans = newd->singleton ();
3349 if (newd->test.kind == rtx_test::SET_OP
3350 || newd->test.kind == rtx_test::ACCEPT)
3351 break;
3353 /* The target of TRANS is a subroutine candidate. First recurse
3354 on it to see how big it is after subroutines have been
3355 split out. */
3356 state_size to_size = find_subroutines (type, trans->to, procs);
3357 if (d->next && to_size.depth > MAX_DEPTH)
3358 /* Keeping the target state in the same routine would lead
3359 to an excessive nesting of "if" and "switch" statements.
3360 Split it out into a subroutine so that it can use
3361 inverted tests that return early on failure. */
3362 trans->to = create_subroutine (type, trans->to, procs);
3363 else
3365 size.num_statements += to_size.num_statements;
3366 if (to_size.num_statements < MIN_NUM_STATEMENTS)
3367 /* The target state is too small to be worth splitting.
3368 Keep it in the same routine as S. */
3369 size.depth = MAX (size.depth, to_size.depth);
3370 else
3371 /* Assume for now that we'll keep the target state in the
3372 same routine as S, but record it as a subroutine candidate
3373 if S grows too big. */
3374 candidates.safe_push (subroutine_candidate (trans, to_size));
3378 if (size.num_statements > MAX_NUM_STATEMENTS)
3380 /* S is too big. Sort the subroutine candidates so that bigger ones
3381 are nearer the end. */
3382 candidates.qsort (subroutine_candidate_cmp);
3383 while (!candidates.is_empty ()
3384 && size.num_statements > MAX_NUM_STATEMENTS)
3386 /* Peel off a candidate and force it into a subroutine. */
3387 subroutine_candidate cand = candidates.pop ();
3388 size.num_statements -= cand.second.num_statements;
3389 cand.first->to = create_subroutine (type, cand.first->to, procs);
3392 /* Update the depth for subroutine candidates that we decided not to
3393 split out. */
3394 for (unsigned int i = 0; i < candidates.length (); ++i)
3395 size.depth = MAX (size.depth, candidates[i].second.depth);
3396 size.depth += 1;
3397 return size;
3400 /* Return true if, for all X, PRED (X, MODE) implies that X has mode MODE. */
3402 static bool
3403 safe_predicate_mode (const struct pred_data *pred, machine_mode mode)
3405 /* Scalar integer constants have VOIDmode. */
3406 if (GET_MODE_CLASS (mode) == MODE_INT
3407 && (pred->codes[CONST_INT]
3408 || pred->codes[CONST_DOUBLE]
3409 || pred->codes[CONST_WIDE_INT]))
3410 return false;
3412 return !pred->special && mode != VOIDmode;
3415 /* Fill CODES with the set of codes that could be matched by PRED. */
3417 static void
3418 get_predicate_codes (const struct pred_data *pred, int_set *codes)
3420 for (int i = 0; i < NUM_TRUE_RTX_CODE; ++i)
3421 if (!pred || pred->codes[i])
3422 codes->safe_push (i);
3425 /* Return true if the first path through D1 tests the same thing as D2. */
3427 static bool
3428 has_same_test_p (decision *d1, decision *d2)
3432 if (d1->test == d2->test)
3433 return true;
3434 d1 = d1->first->to->first;
3436 while (d1);
3437 return false;
3440 /* Return true if D1 and D2 cannot match the same rtx. All states reachable
3441 from D2 have single decisions and all those decisions have single
3442 transitions. */
3444 static bool
3445 mutually_exclusive_p (decision *d1, decision *d2)
3447 /* If one path through D1 fails to test the same thing as D2, assume
3448 that D2's test could be true for D1 and look for a later, more useful,
3449 test. This isn't as expensive as it looks in practice. */
3450 while (!has_same_test_p (d1, d2))
3452 d2 = d2->singleton ()->to->singleton ();
3453 if (!d2)
3454 return false;
3456 if (d1->test == d2->test)
3458 /* Look for any transitions from D1 that have the same labels as
3459 the transition from D2. */
3460 transition *trans2 = d2->singleton ();
3461 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3463 int_set::iterator i1 = trans1->labels.begin ();
3464 int_set::iterator end1 = trans1->labels.end ();
3465 int_set::iterator i2 = trans2->labels.begin ();
3466 int_set::iterator end2 = trans2->labels.end ();
3467 while (i1 != end1 && i2 != end2)
3468 if (*i1 < *i2)
3469 ++i1;
3470 else if (*i2 < *i1)
3471 ++i2;
3472 else
3474 /* TRANS1 has some labels in common with TRANS2. Assume
3475 that D1 and D2 could match the same rtx if the target
3476 of TRANS1 could match the same rtx as D2. */
3477 for (decision *subd1 = trans1->to->first;
3478 subd1; subd1 = subd1->next)
3479 if (!mutually_exclusive_p (subd1, d2))
3480 return false;
3481 break;
3484 return true;
3486 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3487 for (decision *subd1 = trans1->to->first; subd1; subd1 = subd1->next)
3488 if (!mutually_exclusive_p (subd1, d2))
3489 return false;
3490 return true;
3493 /* Try to merge S2's decision into D1, given that they have the same test.
3494 Fail only if EXCLUDE is nonnull and the new transition would have the
3495 same labels as *EXCLUDE. When returning true, set *NEXT_S1, *NEXT_S2
3496 and *NEXT_EXCLUDE as for merge_into_state_1, or set *NEXT_S2 to null
3497 if the merge is complete. */
3499 static bool
3500 merge_into_decision (decision *d1, state *s2, const int_set *exclude,
3501 state **next_s1, state **next_s2,
3502 const int_set **next_exclude)
3504 decision *d2 = s2->singleton ();
3505 transition *trans2 = d2->singleton ();
3507 /* Get a list of the transitions that intersect TRANS2. */
3508 auto_vec <transition *, 32> intersecting;
3509 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3511 int_set::iterator i1 = trans1->labels.begin ();
3512 int_set::iterator end1 = trans1->labels.end ();
3513 int_set::iterator i2 = trans2->labels.begin ();
3514 int_set::iterator end2 = trans2->labels.end ();
3515 bool trans1_is_subset = true;
3516 bool trans2_is_subset = true;
3517 bool intersect_p = false;
3518 while (i1 != end1 && i2 != end2)
3519 if (*i1 < *i2)
3521 trans1_is_subset = false;
3522 ++i1;
3524 else if (*i2 < *i1)
3526 trans2_is_subset = false;
3527 ++i2;
3529 else
3531 intersect_p = true;
3532 ++i1;
3533 ++i2;
3535 if (i1 != end1)
3536 trans1_is_subset = false;
3537 if (i2 != end2)
3538 trans2_is_subset = false;
3539 if (trans1_is_subset && trans2_is_subset)
3541 /* There's already a transition that matches exactly.
3542 Merge the target states. */
3543 trans1->optional &= trans2->optional;
3544 *next_s1 = trans1->to;
3545 *next_s2 = trans2->to;
3546 *next_exclude = 0;
3547 return true;
3549 if (trans2_is_subset)
3551 /* TRANS1 has all the labels that TRANS2 needs. Merge S2 into
3552 the target of TRANS1, but (to avoid infinite recursion)
3553 make sure that we don't end up creating another transition
3554 like TRANS1. */
3555 *next_s1 = trans1->to;
3556 *next_s2 = s2;
3557 *next_exclude = &trans1->labels;
3558 return true;
3560 if (intersect_p)
3561 intersecting.safe_push (trans1);
3564 if (intersecting.is_empty ())
3566 /* No existing labels intersect the new ones. We can just add
3567 TRANS2 itself. */
3568 d1->push_back (d2->release ());
3569 *next_s1 = 0;
3570 *next_s2 = 0;
3571 *next_exclude = 0;
3572 return true;
3575 /* Take the union of the labels in INTERSECTING and TRANS2. Store the
3576 result in COMBINED and use NEXT as a temporary. */
3577 int_set tmp1 = trans2->labels, tmp2;
3578 int_set *combined = &tmp1, *next = &tmp2;
3579 for (unsigned int i = 0; i < intersecting.length (); ++i)
3581 transition *trans1 = intersecting[i];
3582 next->truncate (0);
3583 next->safe_grow (trans1->labels.length () + combined->length ());
3584 int_set::iterator end
3585 = std::set_union (trans1->labels.begin (), trans1->labels.end (),
3586 combined->begin (), combined->end (),
3587 next->begin ());
3588 next->truncate (end - next->begin ());
3589 std::swap (next, combined);
3592 /* Stop now if we've been told not to create a transition with these
3593 labels. */
3594 if (exclude && *combined == *exclude)
3595 return false;
3597 /* Get the transition that should carry the new labels. */
3598 transition *new_trans = intersecting[0];
3599 if (intersecting.length () == 1)
3601 /* We're merging with one existing transition whose labels are a
3602 subset of those required. If both transitions are optional,
3603 we can just expand the set of labels so that it's suitable
3604 for both transitions. It isn't worth preserving the original
3605 transitions since we know that they can't be merged; we would
3606 need to backtrack to S2 if TRANS1->to fails. In contrast,
3607 we might be able to merge the targets of the transitions
3608 without any backtracking.
3610 If instead the existing transition is not optional, ensure that
3611 all target decisions are suitably protected. Some decisions
3612 might already have a more specific requirement than NEW_TRANS,
3613 in which case there's no point testing NEW_TRANS as well. E.g. this
3614 would have happened if a test for an (eq ...) rtx had been
3615 added to a decision that tested whether the code is suitable
3616 for comparison_operator. The original comparison_operator
3617 transition would have been non-optional and the (eq ...) test
3618 would be performed by a second decision in the target of that
3619 transition.
3621 The remaining case -- keeping the original optional transition
3622 when adding a non-optional TRANS2 -- is a wash. Preserving
3623 the optional transition only helps if we later merge another
3624 state S3 that is mutually exclusive with S2 and whose labels
3625 belong to *COMBINED - TRANS1->labels. We can then test the
3626 original NEW_TRANS and S3 in the same decision. We keep the
3627 optional transition around for that case, but it occurs very
3628 rarely. */
3629 gcc_assert (new_trans->labels != *combined);
3630 if (!new_trans->optional || !trans2->optional)
3632 decision *start = 0;
3633 for (decision *end = new_trans->to->first; end; end = end->next)
3635 if (!start && end->test != d1->test)
3636 /* END belongs to a range of decisions that need to be
3637 protected by NEW_TRANS. */
3638 start = end;
3639 if (start && (!end->next || end->next->test == d1->test))
3641 /* Protect [START, END] with NEW_TRANS. The decisions
3642 move to NEW_S and NEW_D becomes part of NEW_TRANS->to. */
3643 state *new_s = new state;
3644 decision *new_d = new decision (d1->test);
3645 new_d->push_back (new transition (new_trans->labels, new_s,
3646 new_trans->optional));
3647 state::range r (start, end);
3648 new_trans->to->replace (r, new_d);
3649 new_s->push_back (r);
3651 /* Continue with an empty range. */
3652 start = 0;
3654 /* Continue from the decision after NEW_D. */
3655 end = new_d;
3659 new_trans->optional = true;
3660 new_trans->labels = *combined;
3662 else
3664 /* We're merging more than one existing transition together.
3665 Those transitions are successfully dividing the matching space
3666 and so we want to preserve them, even if they're optional.
3668 Create a new transition with the union set of labels and make
3669 it go to a state that has the original transitions. */
3670 decision *new_d = new decision (d1->test);
3671 for (unsigned int i = 0; i < intersecting.length (); ++i)
3672 new_d->push_back (d1->remove (intersecting[i]));
3674 state *new_s = new state;
3675 new_s->push_back (new_d);
3677 new_trans = new transition (*combined, new_s, true);
3678 d1->push_back (new_trans);
3681 /* We now have an optional transition with labels *COMBINED. Decide
3682 whether we can use it as TRANS2 or whether we need to merge S2
3683 into the target of NEW_TRANS. */
3684 gcc_assert (new_trans->optional);
3685 if (new_trans->labels == trans2->labels)
3687 /* NEW_TRANS matches TRANS2. Just merge the target states. */
3688 new_trans->optional = trans2->optional;
3689 *next_s1 = new_trans->to;
3690 *next_s2 = trans2->to;
3691 *next_exclude = 0;
3693 else
3695 /* Try to merge TRANS2 into the target of the overlapping transition,
3696 but (to prevent infinite recursion or excessive redundancy) without
3697 creating another transition of the same type. */
3698 *next_s1 = new_trans->to;
3699 *next_s2 = s2;
3700 *next_exclude = &new_trans->labels;
3702 return true;
3705 /* Make progress in merging S2 into S1, given that each state in S2
3706 has a single decision. If EXCLUDE is nonnull, avoid creating a new
3707 transition with the same test as S2's decision and with the labels
3708 in *EXCLUDE.
3710 Return true if there is still work to do. When returning true,
3711 set *NEXT_S1, *NEXT_S2 and *NEXT_EXCLUDE to the values that
3712 S1, S2 and EXCLUDE should have next time round.
3714 If S1 and S2 both match a particular rtx, give priority to S1. */
3716 static bool
3717 merge_into_state_1 (state *s1, state *s2, const int_set *exclude,
3718 state **next_s1, state **next_s2,
3719 const int_set **next_exclude)
3721 decision *d2 = s2->singleton ();
3722 if (decision *d1 = s1->last)
3724 if (d1->test.terminal_p ())
3725 /* D1 is an unconditional return, so S2 can never match. This can
3726 sometimes be a bug in the .md description, but might also happen
3727 if genconditions forces some conditions to true for certain
3728 configurations. */
3729 return false;
3731 /* Go backwards through the decisions in S1, stopping once we find one
3732 that could match the same thing as S2. */
3733 while (d1->prev && mutually_exclusive_p (d1, d2))
3734 d1 = d1->prev;
3736 /* Search forwards from that point, merging D2 into the first
3737 decision we can. */
3738 for (; d1; d1 = d1->next)
3740 /* If S2 performs some optional tests before testing the same thing
3741 as D1, those tests do not help to distinguish D1 and S2, so it's
3742 better to drop them. Search through such optional decisions
3743 until we find something that tests the same thing as D1. */
3744 state *sub_s2 = s2;
3745 for (;;)
3747 decision *sub_d2 = sub_s2->singleton ();
3748 if (d1->test == sub_d2->test)
3750 /* Only apply EXCLUDE if we're testing the same thing
3751 as D2. */
3752 const int_set *sub_exclude = (d2 == sub_d2 ? exclude : 0);
3754 /* Try to merge SUB_S2 into D1. This can only fail if
3755 it would involve creating a new transition with
3756 labels SUB_EXCLUDE. */
3757 if (merge_into_decision (d1, sub_s2, sub_exclude,
3758 next_s1, next_s2, next_exclude))
3759 return *next_s2 != 0;
3761 /* Can't merge with D1; try a later decision. */
3762 break;
3764 transition *sub_trans2 = sub_d2->singleton ();
3765 if (!sub_trans2->optional)
3766 /* Can't merge with D1; try a later decision. */
3767 break;
3768 sub_s2 = sub_trans2->to;
3773 /* We can't merge D2 with any existing decision. Just add it to the end. */
3774 s1->push_back (s2->release ());
3775 return false;
3778 /* Merge S2 into S1. If they both match a particular rtx, give
3779 priority to S1. Each state in S2 has a single decision. */
3781 static void
3782 merge_into_state (state *s1, state *s2)
3784 const int_set *exclude = 0;
3785 while (s2 && merge_into_state_1 (s1, s2, exclude, &s1, &s2, &exclude))
3786 continue;
3789 /* Pairs a pattern that needs to be matched with the rtx position at
3790 which the pattern should occur. */
3791 struct pattern_pos {
3792 pattern_pos () {}
3793 pattern_pos (rtx, position *);
3795 rtx pattern;
3796 position *pos;
3799 pattern_pos::pattern_pos (rtx pattern_in, position *pos_in)
3800 : pattern (pattern_in), pos (pos_in)
3803 /* Compare entries according to their depth-first order. There shouldn't
3804 be two entries at the same position. */
3806 bool
3807 operator < (const pattern_pos &e1, const pattern_pos &e2)
3809 int diff = compare_positions (e1.pos, e2.pos);
3810 gcc_assert (diff != 0 || e1.pattern == e2.pattern);
3811 return diff < 0;
3814 /* Return the name of the predicate matched by MATCH_RTX. */
3816 static const char *
3817 predicate_name (rtx match_rtx)
3819 if (GET_CODE (match_rtx) == MATCH_SCRATCH)
3820 return "scratch_operand";
3821 else
3822 return XSTR (match_rtx, 1);
3825 /* Add new decisions to S that check whether the rtx at position POS
3826 matches PATTERN. Return the state that is reached in that case.
3827 TOP_PATTERN is the overall pattern, as passed to match_pattern_1. */
3829 static state *
3830 match_pattern_2 (state *s, rtx top_pattern, position *pos, rtx pattern)
3832 auto_vec <pattern_pos, 32> worklist;
3833 auto_vec <pattern_pos, 32> pred_and_mode_tests;
3834 auto_vec <pattern_pos, 32> dup_tests;
3836 worklist.safe_push (pattern_pos (pattern, pos));
3837 while (!worklist.is_empty ())
3839 pattern_pos next = worklist.pop ();
3840 pattern = next.pattern;
3841 pos = next.pos;
3842 unsigned int reverse_s = worklist.length ();
3844 enum rtx_code code = GET_CODE (pattern);
3845 switch (code)
3847 case MATCH_OP_DUP:
3848 case MATCH_DUP:
3849 case MATCH_PAR_DUP:
3850 /* Add a test that the rtx matches the earlier one, but only
3851 after the structure and predicates have been checked. */
3852 dup_tests.safe_push (pattern_pos (pattern, pos));
3854 /* Use the same code check as the original operand. */
3855 pattern = find_operand (top_pattern, XINT (pattern, 0), NULL_RTX);
3856 /* Fall through. */
3858 case MATCH_PARALLEL:
3859 case MATCH_OPERAND:
3860 case MATCH_SCRATCH:
3861 case MATCH_OPERATOR:
3863 const char *pred_name = predicate_name (pattern);
3864 const struct pred_data *pred = 0;
3865 if (pred_name[0] != 0)
3867 pred = lookup_predicate (pred_name);
3868 /* Only report errors once per rtx. */
3869 if (code == GET_CODE (pattern))
3871 if (!pred)
3872 error_with_line (pattern_lineno,
3873 "unknown predicate '%s'"
3874 " in '%s' expression",
3875 pred_name, GET_RTX_NAME (code));
3876 else if (code == MATCH_PARALLEL
3877 && pred->singleton != PARALLEL)
3878 error_with_line (pattern_lineno,
3879 "predicate '%s' used in match_parallel"
3880 " does not allow only PARALLEL",
3881 pred->name);
3885 if (code == MATCH_PARALLEL || code == MATCH_PAR_DUP)
3887 /* Check that we have a parallel with enough elements. */
3888 s = add_decision (s, rtx_test::code (pos), PARALLEL, false);
3889 int min_len = XVECLEN (pattern, 2);
3890 s = add_decision (s, rtx_test::veclen_ge (pos, min_len),
3891 true, false);
3893 else
3895 /* Check that the rtx has one of codes accepted by the
3896 predicate. This is necessary when matching suboperands
3897 of a MATCH_OPERATOR or MATCH_OP_DUP, since we can't
3898 call XEXP (X, N) without checking that X has at least
3899 N+1 operands. */
3900 int_set codes;
3901 get_predicate_codes (pred, &codes);
3902 bool need_codes = (pred
3903 && (code == MATCH_OPERATOR
3904 || code == MATCH_OP_DUP));
3905 s = add_decision (s, rtx_test::code (pos), codes, !need_codes);
3908 /* Postpone the predicate check until we've checked the rest
3909 of the rtx structure. */
3910 if (code == GET_CODE (pattern))
3911 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3913 /* If we need to match suboperands, add them to the worklist. */
3914 if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
3916 position **subpos_ptr;
3917 enum position_type pos_type;
3918 int i;
3919 if (code == MATCH_OPERATOR || code == MATCH_OP_DUP)
3921 pos_type = POS_XEXP;
3922 subpos_ptr = &pos->xexps;
3923 i = (code == MATCH_OPERATOR ? 2 : 1);
3925 else
3927 pos_type = POS_XVECEXP0;
3928 subpos_ptr = &pos->xvecexp0s;
3929 i = 2;
3931 for (int j = 0; j < XVECLEN (pattern, i); ++j)
3933 position *subpos = next_position (subpos_ptr, pos,
3934 pos_type, j);
3935 worklist.safe_push (pattern_pos (XVECEXP (pattern, i, j),
3936 subpos));
3937 subpos_ptr = &subpos->next;
3940 break;
3943 default:
3945 /* Check that the rtx has the right code. */
3946 s = add_decision (s, rtx_test::code (pos), code, false);
3948 /* Queue a test for the mode if one is specified. */
3949 if (GET_MODE (pattern) != VOIDmode)
3950 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3952 /* Push subrtxes onto the worklist. Match nonrtx operands now. */
3953 const char *fmt = GET_RTX_FORMAT (code);
3954 position **subpos_ptr = &pos->xexps;
3955 for (size_t i = 0; fmt[i]; ++i)
3957 position *subpos = next_position (subpos_ptr, pos,
3958 POS_XEXP, i);
3959 switch (fmt[i])
3961 case 'e': case 'u':
3962 worklist.safe_push (pattern_pos (XEXP (pattern, i),
3963 subpos));
3964 break;
3966 case 'E':
3968 /* Make sure the vector has the right number of
3969 elements. */
3970 int length = XVECLEN (pattern, i);
3971 s = add_decision (s, rtx_test::veclen (pos),
3972 length, false);
3974 position **subpos2_ptr = &pos->xvecexp0s;
3975 for (int j = 0; j < length; j++)
3977 position *subpos2 = next_position (subpos2_ptr, pos,
3978 POS_XVECEXP0, j);
3979 rtx x = XVECEXP (pattern, i, j);
3980 worklist.safe_push (pattern_pos (x, subpos2));
3981 subpos2_ptr = &subpos2->next;
3983 break;
3986 case 'i':
3987 /* Make sure that XINT (X, I) has the right value. */
3988 s = add_decision (s, rtx_test::int_field (pos, i),
3989 XINT (pattern, i), false);
3990 break;
3992 case 'r':
3993 /* Make sure that REGNO (X) has the right value. */
3994 gcc_assert (i == 0);
3995 s = add_decision (s, rtx_test::regno_field (pos),
3996 REGNO (pattern), false);
3997 break;
3999 case 'w':
4000 /* Make sure that XWINT (X, I) has the right value. */
4001 s = add_decision (s, rtx_test::wide_int_field (pos, i),
4002 XWINT (pattern, 0), false);
4003 break;
4005 case '0':
4006 break;
4008 default:
4009 gcc_unreachable ();
4011 subpos_ptr = &subpos->next;
4014 break;
4016 /* Operands are pushed onto the worklist so that later indices are
4017 nearer the top. That's what we want for SETs, since a SET_SRC
4018 is a better discriminator than a SET_DEST. In other cases it's
4019 usually better to match earlier indices first. This is especially
4020 true of PARALLELs, where the first element tends to be the most
4021 individual. It's also true for commutative operators, where the
4022 canonicalization rules say that the more complex operand should
4023 come first. */
4024 if (code != SET && worklist.length () > reverse_s)
4025 std::reverse (&worklist[0] + reverse_s,
4026 &worklist[0] + worklist.length ());
4029 /* Sort the predicate and mode tests so that they're in depth-first order.
4030 The main goal of this is to put SET_SRC match_operands after SET_DEST
4031 match_operands and after mode checks for the enclosing SET_SRC operators
4032 (such as the mode of a PLUS in an addition instruction). The latter
4033 two types of test can determine the mode exactly, whereas a SET_SRC
4034 match_operand often has to cope with the possibility of the operand
4035 being a modeless constant integer. E.g. something that matches
4036 register_operand (x, SImode) never matches register_operand (x, DImode),
4037 but a const_int that matches immediate_operand (x, SImode) also matches
4038 immediate_operand (x, DImode). The register_operand cases can therefore
4039 be distinguished by a switch on the mode, but the immediate_operand
4040 cases can't. */
4041 if (pred_and_mode_tests.length () > 1)
4042 std::sort (&pred_and_mode_tests[0],
4043 &pred_and_mode_tests[0] + pred_and_mode_tests.length ());
4045 /* Add the mode and predicate tests. */
4046 pattern_pos *e;
4047 unsigned int i;
4048 FOR_EACH_VEC_ELT (pred_and_mode_tests, i, e)
4050 switch (GET_CODE (e->pattern))
4052 case MATCH_PARALLEL:
4053 case MATCH_OPERAND:
4054 case MATCH_SCRATCH:
4055 case MATCH_OPERATOR:
4057 int opno = XINT (e->pattern, 0);
4058 num_operands = MAX (num_operands, opno + 1);
4059 const char *pred_name = predicate_name (e->pattern);
4060 if (pred_name[0])
4062 const struct pred_data *pred = lookup_predicate (pred_name);
4063 /* Check the mode first, to distinguish things like SImode
4064 and DImode register_operands, as described above. */
4065 machine_mode mode = GET_MODE (e->pattern);
4066 if (safe_predicate_mode (pred, mode))
4067 s = add_decision (s, rtx_test::mode (e->pos), mode, true);
4069 /* Assign to operands[] first, so that the rtx usually doesn't
4070 need to be live across the call to the predicate.
4072 This shouldn't cause a problem with dirtying the page,
4073 since we fully expect to assign to operands[] at some point,
4074 and since the caller usually writes to other parts of
4075 recog_data anyway. */
4076 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4077 true, false);
4078 s = add_decision (s, rtx_test::predicate (e->pos, pred, mode),
4079 true, false);
4081 else
4082 /* Historically we've ignored the mode when there's no
4083 predicate. Just set up operands[] unconditionally. */
4084 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4085 true, false);
4086 break;
4089 default:
4090 s = add_decision (s, rtx_test::mode (e->pos),
4091 GET_MODE (e->pattern), false);
4092 break;
4096 /* Finally add rtx_equal_p checks for duplicated operands. */
4097 FOR_EACH_VEC_ELT (dup_tests, i, e)
4098 s = add_decision (s, rtx_test::duplicate (e->pos, XINT (e->pattern, 0)),
4099 true, false);
4100 return s;
4103 /* Add new decisions to S that make it return ACCEPTANCE if:
4105 (1) the rtx doesn't match anything already matched by S
4106 (2) the rtx matches TOP_PATTERN and
4107 (3) C_TEST is true.
4109 For peephole2, TOP_PATTERN is a SEQUENCE of the instruction patterns
4110 to match, otherwise it is a single instruction pattern. */
4112 static void
4113 match_pattern_1 (state *s, rtx top_pattern, const char *c_test,
4114 acceptance_type acceptance)
4116 if (acceptance.type == PEEPHOLE2)
4118 /* Match each individual instruction. */
4119 position **subpos_ptr = &peep2_insn_pos_list;
4120 int count = 0;
4121 for (int i = 0; i < XVECLEN (top_pattern, 0); ++i)
4123 rtx x = XVECEXP (top_pattern, 0, i);
4124 position *subpos = next_position (subpos_ptr, &root_pos,
4125 POS_PEEP2_INSN, count);
4126 if (count > 0)
4127 s = add_decision (s, rtx_test::peep2_count (count + 1),
4128 true, false);
4129 s = match_pattern_2 (s, top_pattern, subpos, x);
4130 subpos_ptr = &subpos->next;
4131 count += 1;
4133 acceptance.u.full.u.match_len = count - 1;
4135 else
4137 /* Make the rtx itself. */
4138 s = match_pattern_2 (s, top_pattern, &root_pos, top_pattern);
4140 /* If the match is only valid when extra clobbers are added,
4141 make sure we're able to pass that information to the caller. */
4142 if (acceptance.type == RECOG && acceptance.u.full.u.num_clobbers)
4143 s = add_decision (s, rtx_test::have_num_clobbers (), true, false);
4146 /* Make sure that the C test is true. */
4147 if (maybe_eval_c_test (c_test) != 1)
4148 s = add_decision (s, rtx_test::c_test (c_test), true, false);
4150 /* Accept the pattern. */
4151 add_decision (s, rtx_test::accept (acceptance), true, false);
4154 /* Like match_pattern_1, but (if merge_states_p) try to merge the
4155 decisions with what's already in S, to reduce the amount of
4156 backtracking. */
4158 static void
4159 match_pattern (state *s, rtx top_pattern, const char *c_test,
4160 acceptance_type acceptance)
4162 if (merge_states_p)
4164 state root;
4165 /* Add the decisions to a fresh state and then merge the full tree
4166 into the existing one. */
4167 match_pattern_1 (&root, top_pattern, c_test, acceptance);
4168 merge_into_state (s, &root);
4170 else
4171 match_pattern_1 (s, top_pattern, c_test, acceptance);
4174 /* Begin the output file. */
4176 static void
4177 write_header (void)
4179 puts ("\
4180 /* Generated automatically by the program `genrecog' from the target\n\
4181 machine description file. */\n\
4183 #include \"config.h\"\n\
4184 #include \"system.h\"\n\
4185 #include \"coretypes.h\"\n\
4186 #include \"tm.h\"\n\
4187 #include \"rtl.h\"\n\
4188 #include \"tm_p.h\"\n\
4189 #include \"hashtab.h\"\n\
4190 #include \"hash-set.h\"\n\
4191 #include \"vec.h\"\n\
4192 #include \"machmode.h\"\n\
4193 #include \"hard-reg-set.h\"\n\
4194 #include \"input.h\"\n\
4195 #include \"function.h\"\n\
4196 #include \"insn-config.h\"\n\
4197 #include \"recog.h\"\n\
4198 #include \"output.h\"\n\
4199 #include \"flags.h\"\n\
4200 #include \"hard-reg-set.h\"\n\
4201 #include \"predict.h\"\n\
4202 #include \"basic-block.h\"\n\
4203 #include \"resource.h\"\n\
4204 #include \"diagnostic-core.h\"\n\
4205 #include \"reload.h\"\n\
4206 #include \"regs.h\"\n\
4207 #include \"tm-constrs.h\"\n\
4208 #include \"predict.h\"\n\
4209 \n");
4211 puts ("\n\
4212 /* `recog' contains a decision tree that recognizes whether the rtx\n\
4213 X0 is a valid instruction.\n\
4215 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
4216 returns a nonnegative number which is the insn code number for the\n\
4217 pattern that matched. This is the same as the order in the machine\n\
4218 description of the entry that matched. This number can be used as an\n\
4219 index into `insn_data' and other tables.\n");
4220 puts ("\
4221 The third parameter to recog is an optional pointer to an int. If\n\
4222 present, recog will accept a pattern if it matches except for missing\n\
4223 CLOBBER expressions at the end. In that case, the value pointed to by\n\
4224 the optional pointer will be set to the number of CLOBBERs that need\n\
4225 to be added (it should be initialized to zero by the caller). If it");
4226 puts ("\
4227 is set nonzero, the caller should allocate a PARALLEL of the\n\
4228 appropriate size, copy the initial entries, and call add_clobbers\n\
4229 (found in insn-emit.c) to fill in the CLOBBERs.\n\
4232 puts ("\n\
4233 The function split_insns returns 0 if the rtl could not\n\
4234 be split or the split rtl as an INSN list if it can be.\n\
4236 The function peephole2_insns returns 0 if the rtl could not\n\
4237 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
4238 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
4239 */\n\n");
4242 /* Return the C type of a parameter with type TYPE. */
4244 static const char *
4245 parameter_type_string (parameter::type_enum type)
4247 switch (type)
4249 case parameter::UNSET:
4250 break;
4252 case parameter::CODE:
4253 return "rtx_code";
4255 case parameter::MODE:
4256 return "machine_mode";
4258 case parameter::INT:
4259 return "int";
4261 case parameter::UINT:
4262 return "unsigned int";
4264 case parameter::WIDE_INT:
4265 return "HOST_WIDE_INT";
4267 gcc_unreachable ();
4270 /* Return true if ACCEPTANCE requires only a single C statement even in
4271 a backtracking context. */
4273 static bool
4274 single_statement_p (const acceptance_type &acceptance)
4276 if (acceptance.partial_p)
4277 /* We need to handle failures of the subroutine. */
4278 return false;
4279 switch (acceptance.type)
4281 case SUBPATTERN:
4282 case SPLIT:
4283 return true;
4285 case RECOG:
4286 /* False if we need to assign to pnum_clobbers. */
4287 return acceptance.u.full.u.num_clobbers == 0;
4289 case PEEPHOLE2:
4290 /* We need to assign to pmatch_len_ and handle null returns from the
4291 peephole2 routine. */
4292 return false;
4294 gcc_unreachable ();
4297 /* Return the C failure value for a routine of type TYPE. */
4299 static const char *
4300 get_failure_return (routine_type type)
4302 switch (type)
4304 case SUBPATTERN:
4305 case RECOG:
4306 return "-1";
4308 case SPLIT:
4309 case PEEPHOLE2:
4310 return "NULL_RTX";
4312 gcc_unreachable ();
4315 /* Indicates whether a block of code always returns or whether it can fall
4316 through. */
4318 enum exit_state {
4319 ES_RETURNED,
4320 ES_FALLTHROUGH
4323 /* Information used while writing out code. */
4325 struct output_state
4327 /* The type of routine that we're generating. */
4328 routine_type type;
4330 /* Maps position ids to xN variable numbers. The entry is only valid if
4331 it is less than the length of VAR_TO_ID, but this holds for every position
4332 tested by a state when writing out that state. */
4333 auto_vec <unsigned int> id_to_var;
4335 /* Maps xN variable numbers to position ids. */
4336 auto_vec <unsigned int> var_to_id;
4338 /* Index N is true if variable xN has already been set. */
4339 auto_vec <bool> seen_vars;
4342 /* Return true if D is a call to a pattern routine and if there is some X
4343 such that the transition for pattern result N goes to a successful return
4344 with code X+N. When returning true, set *BASE_OUT to this X and *COUNT_OUT
4345 to the number of return values. (We know that every PATTERN decision has
4346 a transition for every successful return.) */
4348 static bool
4349 terminal_pattern_p (decision *d, unsigned int *base_out,
4350 unsigned int *count_out)
4352 if (d->test.kind != rtx_test::PATTERN)
4353 return false;
4354 unsigned int base = 0;
4355 unsigned int count = 0;
4356 for (transition *trans = d->first; trans; trans = trans->next)
4358 if (trans->is_param || trans->labels.length () != 1)
4359 return false;
4360 decision *subd = trans->to->singleton ();
4361 if (!subd || subd->test.kind != rtx_test::ACCEPT)
4362 return false;
4363 unsigned int this_base = (subd->test.u.acceptance.u.full.code
4364 - trans->labels[0]);
4365 if (trans == d->first)
4366 base = this_base;
4367 else if (base != this_base)
4368 return false;
4369 count += 1;
4371 *base_out = base;
4372 *count_out = count;
4373 return true;
4376 /* Return true if TEST doesn't test an rtx or if the rtx it tests is
4377 already available in state OS. */
4379 static bool
4380 test_position_available_p (output_state *os, const rtx_test &test)
4382 return (!test.pos
4383 || test.pos_operand >= 0
4384 || os->seen_vars[os->id_to_var[test.pos->id]]);
4387 /* Like printf, but print INDENT spaces at the beginning. */
4389 static void ATTRIBUTE_PRINTF_2
4390 printf_indent (unsigned int indent, const char *format, ...)
4392 va_list ap;
4393 va_start (ap, format);
4394 printf ("%*s", indent, "");
4395 vprintf (format, ap);
4396 va_end (ap);
4399 /* Emit code to initialize the variable associated with POS, if it isn't
4400 already valid in state OS. Indent each line by INDENT spaces. Update
4401 OS with the new state. */
4403 static void
4404 change_state (output_state *os, position *pos, unsigned int indent)
4406 unsigned int var = os->id_to_var[pos->id];
4407 gcc_assert (var < os->var_to_id.length () && os->var_to_id[var] == pos->id);
4408 if (os->seen_vars[var])
4409 return;
4410 switch (pos->type)
4412 case POS_PEEP2_INSN:
4413 printf_indent (indent, "x%d = PATTERN (peep2_next_insn (%d));\n",
4414 var, pos->arg);
4415 break;
4417 case POS_XEXP:
4418 change_state (os, pos->base, indent);
4419 printf_indent (indent, "x%d = XEXP (x%d, %d);\n",
4420 var, os->id_to_var[pos->base->id], pos->arg);
4421 break;
4423 case POS_XVECEXP0:
4424 change_state (os, pos->base, indent);
4425 printf_indent (indent, "x%d = XVECEXP (x%d, 0, %d);\n",
4426 var, os->id_to_var[pos->base->id], pos->arg);
4427 break;
4429 os->seen_vars[var] = true;
4432 /* Print the enumerator constant for CODE -- the upcase version of
4433 the name. */
4435 static void
4436 print_code (enum rtx_code code)
4438 const char *p;
4439 for (p = GET_RTX_NAME (code); *p; p++)
4440 putchar (TOUPPER (*p));
4443 /* Emit a uint64_t as an integer constant expression. We need to take
4444 special care to avoid "decimal constant is so large that it is unsigned"
4445 warnings in the resulting code. */
4447 static void
4448 print_host_wide_int (uint64_t val)
4450 uint64_t min = uint64_t (1) << (HOST_BITS_PER_WIDE_INT - 1);
4451 if (val == min)
4452 printf ("(" HOST_WIDE_INT_PRINT_DEC_C " - 1)", val + 1);
4453 else
4454 printf (HOST_WIDE_INT_PRINT_DEC_C, val);
4457 /* Print the C expression for actual parameter PARAM. */
4459 static void
4460 print_parameter_value (const parameter &param)
4462 if (param.is_param)
4463 printf ("i%d", (int) param.value + 1);
4464 else
4465 switch (param.type)
4467 case parameter::UNSET:
4468 gcc_unreachable ();
4469 break;
4471 case parameter::CODE:
4472 print_code ((enum rtx_code) param.value);
4473 break;
4475 case parameter::MODE:
4476 printf ("%smode", GET_MODE_NAME ((machine_mode) param.value));
4477 break;
4479 case parameter::INT:
4480 printf ("%d", (int) param.value);
4481 break;
4483 case parameter::UINT:
4484 printf ("%u", (unsigned int) param.value);
4485 break;
4487 case parameter::WIDE_INT:
4488 print_host_wide_int (param.value);
4489 break;
4493 /* Print the C expression for the rtx tested by TEST. */
4495 static void
4496 print_test_rtx (output_state *os, const rtx_test &test)
4498 if (test.pos_operand >= 0)
4499 printf ("operands[%d]", test.pos_operand);
4500 else
4501 printf ("x%d", os->id_to_var[test.pos->id]);
4504 /* Print the C expression for non-boolean test TEST. */
4506 static void
4507 print_nonbool_test (output_state *os, const rtx_test &test)
4509 switch (test.kind)
4511 case rtx_test::CODE:
4512 printf ("GET_CODE (");
4513 print_test_rtx (os, test);
4514 printf (")");
4515 break;
4517 case rtx_test::MODE:
4518 printf ("GET_MODE (");
4519 print_test_rtx (os, test);
4520 printf (")");
4521 break;
4523 case rtx_test::VECLEN:
4524 printf ("XVECLEN (");
4525 print_test_rtx (os, test);
4526 printf (", 0)");
4527 break;
4529 case rtx_test::INT_FIELD:
4530 printf ("XINT (");
4531 print_test_rtx (os, test);
4532 printf (", %d)", test.u.opno);
4533 break;
4535 case rtx_test::REGNO_FIELD:
4536 printf ("REGNO (");
4537 print_test_rtx (os, test);
4538 printf (")");
4539 break;
4541 case rtx_test::WIDE_INT_FIELD:
4542 printf ("XWINT (");
4543 print_test_rtx (os, test);
4544 printf (", %d)", test.u.opno);
4545 break;
4547 case rtx_test::PATTERN:
4549 pattern_routine *routine = test.u.pattern->routine;
4550 printf ("pattern%d (", routine->pattern_id);
4551 const char *sep = "";
4552 if (test.pos)
4554 print_test_rtx (os, test);
4555 sep = ", ";
4557 if (routine->insn_p)
4559 printf ("%sinsn", sep);
4560 sep = ", ";
4562 if (routine->pnum_clobbers_p)
4564 printf ("%spnum_clobbers", sep);
4565 sep = ", ";
4567 for (unsigned int i = 0; i < test.u.pattern->params.length (); ++i)
4569 fputs (sep, stdout);
4570 print_parameter_value (test.u.pattern->params[i]);
4571 sep = ", ";
4573 printf (")");
4574 break;
4577 case rtx_test::PEEP2_COUNT:
4578 case rtx_test::VECLEN_GE:
4579 case rtx_test::SAVED_CONST_INT:
4580 case rtx_test::DUPLICATE:
4581 case rtx_test::PREDICATE:
4582 case rtx_test::SET_OP:
4583 case rtx_test::HAVE_NUM_CLOBBERS:
4584 case rtx_test::C_TEST:
4585 case rtx_test::ACCEPT:
4586 gcc_unreachable ();
4590 /* IS_PARAM and LABEL are taken from a transition whose source
4591 decision performs TEST. Print the C code for the label. */
4593 static void
4594 print_label_value (const rtx_test &test, bool is_param, uint64_t value)
4596 print_parameter_value (parameter (transition_parameter_type (test.kind),
4597 is_param, value));
4600 /* If IS_PARAM, print code to compare TEST with the C variable i<VALUE+1>.
4601 If !IS_PARAM, print code to compare TEST with the C constant VALUE.
4602 Test for inequality if INVERT_P, otherwise test for equality. */
4604 static void
4605 print_test (output_state *os, const rtx_test &test, bool is_param,
4606 uint64_t value, bool invert_p)
4608 switch (test.kind)
4610 /* Handle the non-boolean TESTs. */
4611 case rtx_test::CODE:
4612 case rtx_test::MODE:
4613 case rtx_test::VECLEN:
4614 case rtx_test::REGNO_FIELD:
4615 case rtx_test::INT_FIELD:
4616 case rtx_test::WIDE_INT_FIELD:
4617 case rtx_test::PATTERN:
4618 print_nonbool_test (os, test);
4619 printf (" %s ", invert_p ? "!=" : "==");
4620 print_label_value (test, is_param, value);
4621 break;
4623 case rtx_test::SAVED_CONST_INT:
4624 gcc_assert (!is_param && value == 1);
4625 print_test_rtx (os, test);
4626 printf (" %s const_int_rtx[MAX_SAVED_CONST_INT + ",
4627 invert_p ? "!=" : "==");
4628 print_parameter_value (parameter (parameter::INT,
4629 test.u.integer.is_param,
4630 test.u.integer.value));
4631 printf ("]");
4632 break;
4634 case rtx_test::PEEP2_COUNT:
4635 gcc_assert (!is_param && value == 1);
4636 printf ("peep2_current_count %s %d", invert_p ? "<" : ">=",
4637 test.u.min_len);
4638 break;
4640 case rtx_test::VECLEN_GE:
4641 gcc_assert (!is_param && value == 1);
4642 printf ("XVECLEN (");
4643 print_test_rtx (os, test);
4644 printf (", 0) %s %d", invert_p ? "<" : ">=", test.u.min_len);
4645 break;
4647 case rtx_test::PREDICATE:
4648 gcc_assert (!is_param && value == 1);
4649 printf ("%s%s (", invert_p ? "!" : "", test.u.predicate.data->name);
4650 print_test_rtx (os, test);
4651 printf (", ");
4652 print_parameter_value (parameter (parameter::MODE,
4653 test.u.predicate.mode_is_param,
4654 test.u.predicate.mode));
4655 printf (")");
4656 break;
4658 case rtx_test::DUPLICATE:
4659 gcc_assert (!is_param && value == 1);
4660 printf ("%srtx_equal_p (", invert_p ? "!" : "");
4661 print_test_rtx (os, test);
4662 printf (", operands[%d])", test.u.opno);
4663 break;
4665 case rtx_test::HAVE_NUM_CLOBBERS:
4666 gcc_assert (!is_param && value == 1);
4667 printf ("pnum_clobbers %s NULL", invert_p ? "==" : "!=");
4668 break;
4670 case rtx_test::C_TEST:
4671 gcc_assert (!is_param && value == 1);
4672 if (invert_p)
4673 printf ("!");
4674 print_c_condition (test.u.string);
4675 break;
4677 case rtx_test::ACCEPT:
4678 case rtx_test::SET_OP:
4679 gcc_unreachable ();
4683 static exit_state print_decision (output_state *, decision *,
4684 unsigned int, bool);
4686 /* Print code to perform S, indent each line by INDENT spaces.
4687 IS_FINAL is true if there are no fallback decisions to test on failure;
4688 if the state fails then the entire routine fails. */
4690 static exit_state
4691 print_state (output_state *os, state *s, unsigned int indent, bool is_final)
4693 exit_state es = ES_FALLTHROUGH;
4694 for (decision *d = s->first; d; d = d->next)
4695 es = print_decision (os, d, indent, is_final && !d->next);
4696 if (es != ES_RETURNED && is_final)
4698 printf_indent (indent, "return %s;\n", get_failure_return (os->type));
4699 es = ES_RETURNED;
4701 return es;
4704 /* Print the code for subroutine call ACCEPTANCE (for which partial_p
4705 is known to be true). Return the C condition that indicates a successful
4706 match. */
4708 static const char *
4709 print_subroutine_call (const acceptance_type &acceptance)
4711 switch (acceptance.type)
4713 case SUBPATTERN:
4714 gcc_unreachable ();
4716 case RECOG:
4717 printf ("recog_%d (x1, insn, pnum_clobbers)",
4718 acceptance.u.subroutine_id);
4719 return ">= 0";
4721 case SPLIT:
4722 printf ("split_%d (x1, insn)", acceptance.u.subroutine_id);
4723 return "!= NULL_RTX";
4725 case PEEPHOLE2:
4726 printf ("peephole2_%d (x1, insn, pmatch_len_)",
4727 acceptance.u.subroutine_id);
4728 return "!= NULL_RTX";
4730 gcc_unreachable ();
4733 /* Print code for the successful match described by ACCEPTANCE.
4734 INDENT and IS_FINAL are as for print_state. */
4736 static exit_state
4737 print_acceptance (const acceptance_type &acceptance, unsigned int indent,
4738 bool is_final)
4740 if (acceptance.partial_p)
4742 /* Defer the rest of the match to a subroutine. */
4743 if (is_final)
4745 printf_indent (indent, "return ");
4746 print_subroutine_call (acceptance);
4747 printf (";\n");
4748 return ES_RETURNED;
4750 else
4752 printf_indent (indent, "res = ");
4753 const char *res_test = print_subroutine_call (acceptance);
4754 printf (";\n");
4755 printf_indent (indent, "if (res %s)\n", res_test);
4756 printf_indent (indent + 2, "return res;\n");
4757 return ES_FALLTHROUGH;
4760 switch (acceptance.type)
4762 case SUBPATTERN:
4763 printf_indent (indent, "return %d;\n", acceptance.u.full.code);
4764 return ES_RETURNED;
4766 case RECOG:
4767 if (acceptance.u.full.u.num_clobbers != 0)
4768 printf_indent (indent, "*pnum_clobbers = %d;\n",
4769 acceptance.u.full.u.num_clobbers);
4770 printf_indent (indent, "return %d; /* %s */\n", acceptance.u.full.code,
4771 get_insn_name (acceptance.u.full.code));
4772 return ES_RETURNED;
4774 case SPLIT:
4775 printf_indent (indent, "return gen_split_%d (insn, operands);\n",
4776 acceptance.u.full.code);
4777 return ES_RETURNED;
4779 case PEEPHOLE2:
4780 printf_indent (indent, "*pmatch_len_ = %d;\n",
4781 acceptance.u.full.u.match_len);
4782 if (is_final)
4784 printf_indent (indent, "return gen_peephole2_%d (insn, operands);\n",
4785 acceptance.u.full.code);
4786 return ES_RETURNED;
4788 else
4790 printf_indent (indent, "res = gen_peephole2_%d (insn, operands);\n",
4791 acceptance.u.full.code);
4792 printf_indent (indent, "if (res != NULL_RTX)\n");
4793 printf_indent (indent + 2, "return res;\n");
4794 return ES_FALLTHROUGH;
4797 gcc_unreachable ();
4800 /* Print code to perform D. INDENT and IS_FINAL are as for print_state. */
4802 static exit_state
4803 print_decision (output_state *os, decision *d, unsigned int indent,
4804 bool is_final)
4806 uint64_t label;
4807 unsigned int base, count;
4809 /* Make sure the rtx under test is available either in operands[] or
4810 in an xN variable. */
4811 if (d->test.pos && d->test.pos_operand < 0)
4812 change_state (os, d->test.pos, indent);
4814 /* Look for cases where a pattern routine P1 calls another pattern routine
4815 P2 and where P1 returns X + BASE whenever P2 returns X. If IS_FINAL
4816 is true and BASE is zero we can simply use:
4818 return patternN (...);
4820 Otherwise we can use:
4822 res = patternN (...);
4823 if (res >= 0)
4824 return res + BASE;
4826 However, if BASE is nonzero and patternN only returns 0 or -1,
4827 the usual "return BASE;" is better than "return res + BASE;".
4828 If BASE is zero, "return res;" should be better than "return 0;",
4829 since no assignment to the return register is required. */
4830 if (os->type == SUBPATTERN
4831 && terminal_pattern_p (d, &base, &count)
4832 && (base == 0 || count > 1))
4834 if (is_final && base == 0)
4836 printf_indent (indent, "return ");
4837 print_nonbool_test (os, d->test);
4838 printf ("; /* [-1, %d] */\n", count - 1);
4839 return ES_RETURNED;
4841 else
4843 printf_indent (indent, "res = ");
4844 print_nonbool_test (os, d->test);
4845 printf (";\n");
4846 printf_indent (indent, "if (res >= 0)\n");
4847 printf_indent (indent + 2, "return res");
4848 if (base != 0)
4849 printf (" + %d", base);
4850 printf ("; /* [%d, %d] */\n", base, base + count - 1);
4851 return ES_FALLTHROUGH;
4854 else if (d->test.kind == rtx_test::ACCEPT)
4855 return print_acceptance (d->test.u.acceptance, indent, is_final);
4856 else if (d->test.kind == rtx_test::SET_OP)
4858 printf_indent (indent, "operands[%d] = ", d->test.u.opno);
4859 print_test_rtx (os, d->test);
4860 printf (";\n");
4861 return print_state (os, d->singleton ()->to, indent, is_final);
4863 /* Handle decisions with a single transition and a single transition
4864 label. */
4865 else if (d->if_statement_p (&label))
4867 transition *trans = d->singleton ();
4868 if (mark_optional_transitions_p && trans->optional)
4869 printf_indent (indent, "/* OPTIONAL IF */\n");
4871 /* Print the condition associated with TRANS. Invert it if IS_FINAL,
4872 so that we return immediately on failure and fall through on
4873 success. */
4874 printf_indent (indent, "if (");
4875 print_test (os, d->test, trans->is_param, label, is_final);
4877 /* Look for following states that would be handled by this code
4878 on recursion. If they don't need any preparatory statements,
4879 include them in the current "if" statement rather than creating
4880 a new one. */
4881 for (;;)
4883 d = trans->to->singleton ();
4884 if (!d
4885 || d->test.kind == rtx_test::ACCEPT
4886 || d->test.kind == rtx_test::SET_OP
4887 || !d->if_statement_p (&label)
4888 || !test_position_available_p (os, d->test))
4889 break;
4890 trans = d->first;
4891 printf ("\n");
4892 if (mark_optional_transitions_p && trans->optional)
4893 printf_indent (indent + 4, "/* OPTIONAL IF */\n");
4894 printf_indent (indent + 4, "%s ", is_final ? "||" : "&&");
4895 print_test (os, d->test, trans->is_param, label, is_final);
4897 printf (")\n");
4899 /* Print the conditional code with INDENT + 2 and the fallthrough
4900 code with indent INDENT. */
4901 state *to = trans->to;
4902 if (is_final)
4904 /* We inverted the condition above, so return failure in the
4905 "if" body and fall through to the target of the transition. */
4906 printf_indent (indent + 2, "return %s;\n",
4907 get_failure_return (os->type));
4908 return print_state (os, to, indent, is_final);
4910 else if (to->singleton ()
4911 && to->first->test.kind == rtx_test::ACCEPT
4912 && single_statement_p (to->first->test.u.acceptance))
4914 /* The target of the transition is a simple "return" statement.
4915 It doesn't need any braces and doesn't fall through. */
4916 if (print_acceptance (to->first->test.u.acceptance,
4917 indent + 2, true) != ES_RETURNED)
4918 gcc_unreachable ();
4919 return ES_FALLTHROUGH;
4921 else
4923 /* The general case. Output code for the target of the transition
4924 in braces. This will not invalidate any of the xN variables
4925 that are already valid, but we mustn't rely on any that are
4926 set by the "if" body. */
4927 auto_vec <bool, 32> old_seen;
4928 old_seen.safe_splice (os->seen_vars);
4930 printf_indent (indent + 2, "{\n");
4931 print_state (os, trans->to, indent + 4, is_final);
4932 printf_indent (indent + 2, "}\n");
4934 os->seen_vars.truncate (0);
4935 os->seen_vars.splice (old_seen);
4936 return ES_FALLTHROUGH;
4939 else
4941 /* Output the decision as a switch statement. */
4942 printf_indent (indent, "switch (");
4943 print_nonbool_test (os, d->test);
4944 printf (")\n");
4946 /* Each case statement starts with the same set of valid variables.
4947 These are also the only variables will be valid on fallthrough. */
4948 auto_vec <bool, 32> old_seen;
4949 old_seen.safe_splice (os->seen_vars);
4951 printf_indent (indent + 2, "{\n");
4952 for (transition *trans = d->first; trans; trans = trans->next)
4954 gcc_assert (!trans->is_param);
4955 if (mark_optional_transitions_p && trans->optional)
4956 printf_indent (indent + 2, "/* OPTIONAL CASE */\n");
4957 for (int_set::iterator j = trans->labels.begin ();
4958 j != trans->labels.end (); ++j)
4960 printf_indent (indent + 2, "case ");
4961 print_label_value (d->test, trans->is_param, *j);
4962 printf (":\n");
4964 if (print_state (os, trans->to, indent + 4, is_final))
4966 /* The state can fall through. Add an explicit break. */
4967 gcc_assert (!is_final);
4968 printf_indent (indent + 4, "break;\n");
4970 printf ("\n");
4972 /* Restore the original set of valid variables. */
4973 os->seen_vars.truncate (0);
4974 os->seen_vars.splice (old_seen);
4976 /* Add a default case. */
4977 printf_indent (indent + 2, "default:\n");
4978 if (is_final)
4979 printf_indent (indent + 4, "return %s;\n",
4980 get_failure_return (os->type));
4981 else
4982 printf_indent (indent + 4, "break;\n");
4983 printf_indent (indent + 2, "}\n");
4984 return is_final ? ES_RETURNED : ES_FALLTHROUGH;
4988 /* Make sure that OS has a position variable for POS. ROOT_P is true if
4989 POS is the root position for the routine. */
4991 static void
4992 assign_position_var (output_state *os, position *pos, bool root_p)
4994 unsigned int idx = os->id_to_var[pos->id];
4995 if (idx < os->var_to_id.length () && os->var_to_id[idx] == pos->id)
4996 return;
4997 if (!root_p && pos->type != POS_PEEP2_INSN)
4998 assign_position_var (os, pos->base, false);
4999 os->id_to_var[pos->id] = os->var_to_id.length ();
5000 os->var_to_id.safe_push (pos->id);
5003 /* Make sure that OS has the position variables required by S. */
5005 static void
5006 assign_position_vars (output_state *os, state *s)
5008 for (decision *d = s->first; d; d = d->next)
5010 /* Positions associated with operands can be read from the
5011 operands[] array. */
5012 if (d->test.pos && d->test.pos_operand < 0)
5013 assign_position_var (os, d->test.pos, false);
5014 for (transition *trans = d->first; trans; trans = trans->next)
5015 assign_position_vars (os, trans->to);
5019 /* Print the open brace and variable definitions for a routine that
5020 implements S. ROOT is the deepest rtx from which S can access all
5021 relevant parts of the first instruction it matches. Initialize OS
5022 so that every relevant position has an rtx variable xN and so that
5023 only ROOT's variable has a valid value. */
5025 static void
5026 print_subroutine_start (output_state *os, state *s, position *root)
5028 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED"
5029 " = &recog_data.operand[0];\n");
5030 os->var_to_id.truncate (0);
5031 os->seen_vars.truncate (0);
5032 if (root)
5034 /* Create a fake entry for position 0 so that an id_to_var of 0
5035 is always invalid. This also makes the xN variables naturally
5036 1-based rather than 0-based. */
5037 os->var_to_id.safe_push (num_positions);
5039 /* Associate ROOT with x1. */
5040 assign_position_var (os, root, true);
5042 /* Assign xN variables to all other relevant positions. */
5043 assign_position_vars (os, s);
5045 /* Output the variable declarations (except for ROOT's, which is
5046 passed in as a parameter). */
5047 unsigned int num_vars = os->var_to_id.length ();
5048 if (num_vars > 2)
5050 for (unsigned int i = 2; i < num_vars; ++i)
5051 /* Print 8 rtx variables to a line. */
5052 printf ("%s x%d",
5053 i == 2 ? " rtx" : (i - 2) % 8 == 0 ? ";\n rtx" : ",", i);
5054 printf (";\n");
5057 /* Say that x1 is valid and the rest aren't. */
5058 os->seen_vars.safe_grow_cleared (num_vars);
5059 os->seen_vars[1] = true;
5061 if (os->type == SUBPATTERN || os->type == RECOG)
5062 printf (" int res ATTRIBUTE_UNUSED;\n");
5063 else
5064 printf (" rtx res ATTRIBUTE_UNUSED;\n");
5067 /* Output the definition of pattern routine ROUTINE. */
5069 static void
5070 print_pattern (output_state *os, pattern_routine *routine)
5072 printf ("\nstatic int\npattern%d (", routine->pattern_id);
5073 const char *sep = "";
5074 /* Add the top-level rtx parameter, if any. */
5075 if (routine->pos)
5077 printf ("%srtx x1", sep);
5078 sep = ", ";
5080 /* Add the optional parameters. */
5081 if (routine->insn_p)
5083 /* We can't easily tell whether a C condition actually reads INSN,
5084 so add an ATTRIBUTE_UNUSED just in case. */
5085 printf ("%srtx_insn *insn ATTRIBUTE_UNUSED", sep);
5086 sep = ", ";
5088 if (routine->pnum_clobbers_p)
5090 printf ("%sint *pnum_clobbers", sep);
5091 sep = ", ";
5093 /* Add the "i" parameters. */
5094 for (unsigned int i = 0; i < routine->param_types.length (); ++i)
5096 printf ("%s%s i%d", sep,
5097 parameter_type_string (routine->param_types[i]), i + 1);
5098 sep = ", ";
5100 printf (")\n");
5101 os->type = SUBPATTERN;
5102 print_subroutine_start (os, routine->s, routine->pos);
5103 print_state (os, routine->s, 2, true);
5104 printf ("}\n");
5107 /* Output a routine of type TYPE that implements S. PROC_ID is the
5108 number of the subroutine associated with S, or 0 if S is the main
5109 routine. */
5111 static void
5112 print_subroutine (output_state *os, state *s, int proc_id)
5114 /* For now, the top-level functions take a plain "rtx", and perform a
5115 checked cast to "rtx_insn *" for use throughout the rest of the
5116 function and the code it calls. */
5117 const char *insn_param
5118 = proc_id > 0 ? "rtx_insn *insn" : "rtx uncast_insn";
5119 printf ("\n");
5120 switch (os->type)
5122 case SUBPATTERN:
5123 gcc_unreachable ();
5125 case RECOG:
5126 if (proc_id)
5127 printf ("static int\nrecog_%d", proc_id);
5128 else
5129 printf ("int\nrecog");
5130 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5131 "\t%s ATTRIBUTE_UNUSED,\n"
5132 "\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", insn_param);
5133 break;
5135 case SPLIT:
5136 if (proc_id)
5137 printf ("static rtx\nsplit_%d", proc_id);
5138 else
5139 printf ("rtx\nsplit_insns");
5140 printf (" (rtx x1 ATTRIBUTE_UNUSED, %s ATTRIBUTE_UNUSED)\n",
5141 insn_param);
5142 break;
5144 case PEEPHOLE2:
5145 if (proc_id)
5146 printf ("static rtx\npeephole2_%d", proc_id);
5147 else
5148 printf ("rtx\npeephole2_insns");
5149 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5150 "\t%s ATTRIBUTE_UNUSED,\n"
5151 "\tint *pmatch_len_ ATTRIBUTE_UNUSED)\n", insn_param);
5152 break;
5154 print_subroutine_start (os, s, &root_pos);
5155 if (proc_id == 0)
5157 printf (" recog_data.insn = NULL;\n");
5158 printf (" rtx_insn *insn ATTRIBUTE_UNUSED;\n");
5159 printf (" insn = safe_as_a <rtx_insn *> (uncast_insn);\n");
5161 print_state (os, s, 2, true);
5162 printf ("}\n");
5165 /* Print out a routine of type TYPE that performs ROOT. */
5167 static void
5168 print_subroutine_group (output_state *os, routine_type type, state *root)
5170 os->type = type;
5171 if (use_subroutines_p)
5173 /* Split ROOT up into smaller pieces, both for readability and to
5174 help the compiler. */
5175 auto_vec <state *> subroutines;
5176 find_subroutines (type, root, subroutines);
5178 /* Output the subroutines (but not ROOT itself). */
5179 unsigned int i;
5180 state *s;
5181 FOR_EACH_VEC_ELT (subroutines, i, s)
5182 print_subroutine (os, s, i + 1);
5184 /* Output the main routine. */
5185 print_subroutine (os, root, 0);
5188 /* Return the rtx pattern specified by the list of rtxes in a
5189 define_insn or define_split. */
5191 static rtx
5192 add_implicit_parallel (rtvec vec)
5194 if (GET_NUM_ELEM (vec) == 1)
5195 return RTVEC_ELT (vec, 0);
5196 else
5198 rtx pattern = rtx_alloc (PARALLEL);
5199 XVEC (pattern, 0) = vec;
5200 return pattern;
5204 /* Return the rtx pattern for the list of rtxes in a define_peephole2. */
5206 static rtx
5207 get_peephole2_pattern (rtvec vec)
5209 int i, j;
5210 rtx pattern = rtx_alloc (SEQUENCE);
5211 XVEC (pattern, 0) = rtvec_alloc (GET_NUM_ELEM (vec));
5212 for (i = j = 0; i < GET_NUM_ELEM (vec); i++)
5214 rtx x = RTVEC_ELT (vec, i);
5215 /* Ignore scratch register requirements. */
5216 if (GET_CODE (x) != MATCH_SCRATCH && GET_CODE (x) != MATCH_DUP)
5218 XVECEXP (pattern, 0, j) = x;
5219 j++;
5222 XVECLEN (pattern, 0) = j;
5223 if (j == 0)
5224 error_with_line (pattern_lineno, "empty define_peephole2");
5225 return pattern;
5228 /* Return true if *PATTERN_PTR is a PARALLEL in which at least one trailing
5229 rtx can be added automatically by add_clobbers. If so, update
5230 *ACCEPTANCE_PTR so that its num_clobbers field contains the number
5231 of such trailing rtxes and update *PATTERN_PTR so that it contains
5232 the pattern without those rtxes. */
5234 static bool
5235 remove_clobbers (acceptance_type *acceptance_ptr, rtx *pattern_ptr)
5237 int i;
5238 rtx new_pattern;
5240 /* Find the last non-clobber in the parallel. */
5241 rtx pattern = *pattern_ptr;
5242 for (i = XVECLEN (pattern, 0); i > 0; i--)
5244 rtx x = XVECEXP (pattern, 0, i - 1);
5245 if (GET_CODE (x) != CLOBBER
5246 || (!REG_P (XEXP (x, 0))
5247 && GET_CODE (XEXP (x, 0)) != MATCH_SCRATCH))
5248 break;
5251 if (i == XVECLEN (pattern, 0))
5252 return false;
5254 /* Build a similar insn without the clobbers. */
5255 if (i == 1)
5256 new_pattern = XVECEXP (pattern, 0, 0);
5257 else
5259 new_pattern = rtx_alloc (PARALLEL);
5260 XVEC (new_pattern, 0) = rtvec_alloc (i);
5261 for (int j = 0; j < i; ++j)
5262 XVECEXP (new_pattern, 0, j) = XVECEXP (pattern, 0, j);
5265 /* Recognize it. */
5266 acceptance_ptr->u.full.u.num_clobbers = XVECLEN (pattern, 0) - i;
5267 *pattern_ptr = new_pattern;
5268 return true;
5272 main (int argc, char **argv)
5274 rtx desc;
5275 state insn_root, split_root, peephole2_root;
5277 progname = "genrecog";
5279 if (!init_rtx_reader_args (argc, argv))
5280 return (FATAL_EXIT_CODE);
5282 next_insn_code = 0;
5284 write_header ();
5286 /* Read the machine description. */
5288 while (1)
5290 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
5291 if (desc == NULL)
5292 break;
5294 acceptance_type acceptance;
5295 acceptance.partial_p = false;
5296 acceptance.u.full.code = next_insn_code;
5298 rtx pattern;
5299 switch (GET_CODE (desc))
5301 case DEFINE_INSN:
5303 /* Match the instruction in the original .md form. */
5304 acceptance.type = RECOG;
5305 acceptance.u.full.u.num_clobbers = 0;
5306 pattern = add_implicit_parallel (XVEC (desc, 1));
5307 validate_pattern (pattern, desc, NULL_RTX, 0);
5308 match_pattern (&insn_root, pattern, XSTR (desc, 2), acceptance);
5310 /* If the pattern is a PARALLEL with trailing CLOBBERs,
5311 allow recog_for_combine to match without the clobbers. */
5312 if (GET_CODE (pattern) == PARALLEL
5313 && remove_clobbers (&acceptance, &pattern))
5314 match_pattern (&insn_root, pattern, XSTR (desc, 2), acceptance);
5315 break;
5318 case DEFINE_SPLIT:
5319 acceptance.type = SPLIT;
5320 pattern = add_implicit_parallel (XVEC (desc, 0));
5321 validate_pattern (pattern, desc, NULL_RTX, 0);
5322 match_pattern (&split_root, pattern, XSTR (desc, 1), acceptance);
5324 /* Declare the gen_split routine that we'll call if the
5325 pattern matches. The definition comes from insn-emit.c. */
5326 printf ("extern rtx gen_split_%d (rtx_insn *, rtx *);\n",
5327 next_insn_code);
5328 break;
5330 case DEFINE_PEEPHOLE2:
5331 acceptance.type = PEEPHOLE2;
5332 pattern = get_peephole2_pattern (XVEC (desc, 0));
5333 validate_pattern (pattern, desc, NULL_RTX, 0);
5334 match_pattern (&peephole2_root, pattern, XSTR (desc, 1), acceptance);
5336 /* Declare the gen_peephole2 routine that we'll call if the
5337 pattern matches. The definition comes from insn-emit.c. */
5338 printf ("extern rtx gen_peephole2_%d (rtx_insn *, rtx *);\n",
5339 next_insn_code);
5340 break;
5342 default:
5343 /* do nothing */;
5347 if (have_error)
5348 return FATAL_EXIT_CODE;
5350 puts ("\n\n");
5352 /* Optimize each routine in turn. */
5353 optimize_subroutine_group ("recog", &insn_root);
5354 optimize_subroutine_group ("split_insns", &split_root);
5355 optimize_subroutine_group ("peephole2_insns", &peephole2_root);
5357 output_state os;
5358 os.id_to_var.safe_grow_cleared (num_positions);
5360 if (use_pattern_routines_p)
5362 /* Look for common patterns and split them out into subroutines. */
5363 auto_vec <merge_state_info> states;
5364 states.safe_push (&insn_root);
5365 states.safe_push (&split_root);
5366 states.safe_push (&peephole2_root);
5367 split_out_patterns (states);
5369 /* Print out the routines that we just created. */
5370 unsigned int i;
5371 pattern_routine *routine;
5372 FOR_EACH_VEC_ELT (patterns, i, routine)
5373 print_pattern (&os, routine);
5376 /* Print out the matching routines. */
5377 print_subroutine_group (&os, RECOG, &insn_root);
5378 print_subroutine_group (&os, SPLIT, &split_root);
5379 print_subroutine_group (&os, PEEPHOLE2, &peephole2_root);
5381 fflush (stdout);
5382 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);