Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / genrecog.c
blobd8432283915aba9eddb8713301c288ecdf473bd5
1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This program is used to produce insn-recog.c, which contains a
22 function called `recog' plus its subroutines. These functions
23 contain a decision tree that recognizes whether an rtx, the
24 argument given to recog, is a valid instruction.
26 recog returns -1 if the rtx is not valid. If the rtx is valid,
27 recog returns a nonnegative number which is the insn code number
28 for the pattern that matched. This is the same as the order in the
29 machine description of the entry that matched. This number can be
30 used as an index into various insn_* tables, such as insn_template,
31 insn_outfun, and insn_n_operands (found in insn-output.c).
33 The third argument to recog is an optional pointer to an int. If
34 present, recog will accept a pattern if it matches except for
35 missing CLOBBER expressions at the end. In that case, the value
36 pointed to by the optional pointer will be set to the number of
37 CLOBBERs that need to be added (it should be initialized to zero by
38 the caller). If it is set nonzero, the caller should allocate a
39 PARALLEL of the appropriate size, copy the initial entries, and
40 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
42 This program also generates the function `split_insns', which
43 returns 0 if the rtl could not be split, or it returns the split
44 rtl as an INSN list.
46 This program also generates the function `peephole2_insns', which
47 returns 0 if the rtl could not be matched. If there was a match,
48 the new rtl is returned in an INSN list, and LAST_INSN will point
49 to the last recognized insn in the old sequence.
52 At a high level, the algorithm used in this file is as follows:
54 1. Build up a decision tree for each routine, using the following
55 approach to matching an rtx:
57 - First determine the "shape" of the rtx, based on GET_CODE,
58 XVECLEN and XINT. This phase examines SET_SRCs before SET_DESTs
59 since SET_SRCs tend to be more distinctive. It examines other
60 operands in numerical order, since the canonicalization rules
61 prefer putting complex operands of commutative operators first.
63 - Next check modes and predicates. This phase examines all
64 operands in numerical order, even for SETs, since the mode of a
65 SET_DEST is exact while the mode of a SET_SRC can be VOIDmode
66 for constant integers.
68 - Next check match_dups.
70 - Finally check the C condition and (where appropriate) pnum_clobbers.
72 2. Try to optimize the tree by removing redundant tests, CSEing tests,
73 folding tests together, etc.
75 3. Look for common subtrees and split them out into "pattern" routines.
76 These common subtrees can be identical or they can differ in mode,
77 code, or integer (usually an UNSPEC or UNSPEC_VOLATILE code).
78 In the latter case the users of the pattern routine pass the
79 appropriate mode, etc., as argument. For example, if two patterns
80 contain:
82 (plus:SI (match_operand:SI 1 "register_operand")
83 (match_operand:SI 2 "register_operand"))
85 we can split the associated matching code out into a subroutine.
86 If a pattern contains:
88 (minus:DI (match_operand:DI 1 "register_operand")
89 (match_operand:DI 2 "register_operand"))
91 then we can consider using the same matching routine for both
92 the plus and minus expressions, passing PLUS and SImode in the
93 former case and MINUS and DImode in the latter case.
95 The main aim of this phase is to reduce the compile time of the
96 insn-recog.c code and to reduce the amount of object code in
97 insn-recog.o.
99 4. Split the matching trees into functions, trying to limit the
100 size of each function to a sensible amount.
102 Again, the main aim of this phase is to reduce the compile time
103 of insn-recog.c. (It doesn't help with the size of insn-recog.o.)
105 5. Write out C++ code for each function. */
107 #include "bconfig.h"
108 #include "system.h"
109 #include "coretypes.h"
110 #include "tm.h"
111 #include "rtl.h"
112 #include "errors.h"
113 #include "read-md.h"
114 #include "gensupport.h"
115 #include <algorithm>
117 #undef GENERATOR_FILE
118 enum true_rtx_doe {
119 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) TRUE_##ENUM,
120 #include "rtl.def"
121 #undef DEF_RTL_EXPR
122 FIRST_GENERATOR_RTX_CODE
124 #define NUM_TRUE_RTX_CODE ((int) FIRST_GENERATOR_RTX_CODE)
125 #define GENERATOR_FILE 1
127 /* Debugging variables to control which optimizations are performed.
128 Note that disabling merge_states_p leads to very large output. */
129 static const bool merge_states_p = true;
130 static const bool collapse_optional_decisions_p = true;
131 static const bool cse_tests_p = true;
132 static const bool simplify_tests_p = true;
133 static const bool use_operand_variables_p = true;
134 static const bool use_subroutines_p = true;
135 static const bool use_pattern_routines_p = true;
137 /* Whether to add comments for optional tests that we decided to keep.
138 Can be useful when debugging the generator itself but is noise when
139 debugging the generated code. */
140 static const bool mark_optional_transitions_p = false;
142 /* Whether pattern routines should calculate positions relative to their
143 rtx parameter rather than use absolute positions. This e.g. allows
144 a pattern routine to be shared between a plain SET and a PARALLEL
145 that includes a SET.
147 In principle it sounds like this should be useful, especially for
148 recog_for_combine, where the plain SET form is generated automatically
149 from a PARALLEL of a single SET and some CLOBBERs. In practice it doesn't
150 seem to help much and leads to slightly bigger object files. */
151 static const bool relative_patterns_p = false;
153 /* Whether pattern routines should be allowed to test whether pnum_clobbers
154 is null. This requires passing pnum_clobbers around as a parameter. */
155 static const bool pattern_have_num_clobbers_p = true;
157 /* Whether pattern routines should be allowed to test .md file C conditions.
158 This requires passing insn around as a parameter, in case the C
159 condition refers to it. In practice this tends to lead to bigger
160 object files. */
161 static const bool pattern_c_test_p = false;
163 /* Whether to require each parameter passed to a pattern routine to be
164 unique. Disabling this check for example allows unary operators with
165 matching modes (like NEG) and unary operators with mismatched modes
166 (like ZERO_EXTEND) to be matched by a single pattern. However, we then
167 often have cases where the same value is passed too many times. */
168 static const bool force_unique_params_p = true;
170 /* The maximum (approximate) depth of block nesting that an individual
171 routine or subroutine should have. This limit is about keeping the
172 output readable rather than reducing compile time. */
173 static const unsigned int MAX_DEPTH = 6;
175 /* The minimum number of pseudo-statements that a state must have before
176 we split it out into a subroutine. */
177 static const unsigned int MIN_NUM_STATEMENTS = 5;
179 /* The number of pseudo-statements a state can have before we consider
180 splitting out substates into subroutines. This limit is about avoiding
181 compile-time problems with very big functions (and also about keeping
182 functions within --param optimization limits, etc.). */
183 static const unsigned int MAX_NUM_STATEMENTS = 200;
185 /* The minimum number of pseudo-statements that can be used in a pattern
186 routine. */
187 static const unsigned int MIN_COMBINE_COST = 4;
189 /* The maximum number of arguments that a pattern routine can have.
190 The idea is to prevent one pattern getting a ridiculous number of
191 arguments when it would be more beneficial to have a separate pattern
192 routine instead. */
193 static const unsigned int MAX_PATTERN_PARAMS = 5;
195 /* The maximum operand number plus one. */
196 int num_operands;
198 /* Ways of obtaining an rtx to be tested. */
199 enum position_type {
200 /* PATTERN (peep2_next_insn (ARG)). */
201 POS_PEEP2_INSN,
203 /* XEXP (BASE, ARG). */
204 POS_XEXP,
206 /* XVECEXP (BASE, 0, ARG). */
207 POS_XVECEXP0
210 /* The position of an rtx relative to X0. Each useful position is
211 represented by exactly one instance of this structure. */
212 struct position
214 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */
215 struct position *base;
217 /* A position with the same BASE and TYPE, but with the next value
218 of ARG. */
219 struct position *next;
221 /* A list of all POS_XEXP positions that use this one as their base,
222 chained by NEXT fields. The first entry represents XEXP (this, 0),
223 the second represents XEXP (this, 1), and so on. */
224 struct position *xexps;
226 /* A list of POS_XVECEXP0 positions that use this one as their base,
227 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0),
228 the second represents XVECEXP (this, 0, 1), and so on. */
229 struct position *xvecexp0s;
231 /* The type of position. */
232 enum position_type type;
234 /* The argument to TYPE (shown as ARG in the position_type comments). */
235 int arg;
237 /* The instruction to which the position belongs. */
238 unsigned int insn_id;
240 /* The depth of this position relative to the instruction pattern.
241 E.g. if the instruction pattern is a SET, the SET itself has a
242 depth of 0 while the SET_DEST and SET_SRC have depths of 1. */
243 unsigned int depth;
245 /* A unique identifier for this position. */
246 unsigned int id;
249 enum routine_type {
250 SUBPATTERN, RECOG, SPLIT, PEEPHOLE2
253 /* Next number to use as an insn_code. */
254 static int next_insn_code;
256 /* The line number of the start of the pattern currently being processed. */
257 static int pattern_lineno;
259 /* The root position (x0). */
260 static struct position root_pos;
262 /* The number of positions created. Also one higher than the maximum
263 position id. */
264 static unsigned int num_positions = 1;
266 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position,
267 since we are given that instruction's pattern as x0. */
268 static struct position *peep2_insn_pos_list = &root_pos;
270 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR
271 points to where the unique object that represents the position
272 should be stored. Create the object if it doesn't already exist,
273 otherwise reuse the object that is already there. */
275 static struct position *
276 next_position (struct position **next_ptr, struct position *base,
277 enum position_type type, int arg)
279 struct position *pos;
281 pos = *next_ptr;
282 if (!pos)
284 pos = XCNEW (struct position);
285 pos->type = type;
286 pos->arg = arg;
287 if (type == POS_PEEP2_INSN)
289 pos->base = 0;
290 pos->insn_id = arg;
291 pos->depth = base->depth;
293 else
295 pos->base = base;
296 pos->insn_id = base->insn_id;
297 pos->depth = base->depth + 1;
299 pos->id = num_positions++;
300 *next_ptr = pos;
302 return pos;
305 /* Compare positions POS1 and POS2 lexicographically. */
307 static int
308 compare_positions (struct position *pos1, struct position *pos2)
310 int diff;
312 diff = pos1->depth - pos2->depth;
313 if (diff < 0)
315 pos2 = pos2->base;
316 while (pos1->depth != pos2->depth);
317 else if (diff > 0)
319 pos1 = pos1->base;
320 while (pos1->depth != pos2->depth);
321 while (pos1 != pos2)
323 diff = (int) pos1->type - (int) pos2->type;
324 if (diff == 0)
325 diff = pos1->arg - pos2->arg;
326 pos1 = pos1->base;
327 pos2 = pos2->base;
329 return diff;
332 /* Return the most deeply-nested position that is common to both
333 POS1 and POS2. If the positions are from different instructions,
334 return the one with the lowest insn_id. */
336 static struct position *
337 common_position (struct position *pos1, struct position *pos2)
339 if (pos1->insn_id != pos2->insn_id)
340 return pos1->insn_id < pos2->insn_id ? pos1 : pos2;
341 if (pos1->depth > pos2->depth)
342 std::swap (pos1, pos2);
343 while (pos1->depth != pos2->depth)
344 pos2 = pos2->base;
345 while (pos1 != pos2)
347 pos1 = pos1->base;
348 pos2 = pos2->base;
350 return pos1;
353 /* Search for and return operand N, stop when reaching node STOP. */
355 static rtx
356 find_operand (rtx pattern, int n, rtx stop)
358 const char *fmt;
359 RTX_CODE code;
360 int i, j, len;
361 rtx r;
363 if (pattern == stop)
364 return stop;
366 code = GET_CODE (pattern);
367 if ((code == MATCH_SCRATCH
368 || code == MATCH_OPERAND
369 || code == MATCH_OPERATOR
370 || code == MATCH_PARALLEL)
371 && XINT (pattern, 0) == n)
372 return pattern;
374 fmt = GET_RTX_FORMAT (code);
375 len = GET_RTX_LENGTH (code);
376 for (i = 0; i < len; i++)
378 switch (fmt[i])
380 case 'e': case 'u':
381 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
382 return r;
383 break;
385 case 'V':
386 if (! XVEC (pattern, i))
387 break;
388 /* Fall through. */
390 case 'E':
391 for (j = 0; j < XVECLEN (pattern, i); j++)
392 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
393 != NULL_RTX)
394 return r;
395 break;
397 case 'i': case 'r': case 'w': case '0': case 's':
398 break;
400 default:
401 gcc_unreachable ();
405 return NULL;
408 /* Search for and return operand M, such that it has a matching
409 constraint for operand N. */
411 static rtx
412 find_matching_operand (rtx pattern, int n)
414 const char *fmt;
415 RTX_CODE code;
416 int i, j, len;
417 rtx r;
419 code = GET_CODE (pattern);
420 if (code == MATCH_OPERAND
421 && (XSTR (pattern, 2)[0] == '0' + n
422 || (XSTR (pattern, 2)[0] == '%'
423 && XSTR (pattern, 2)[1] == '0' + n)))
424 return pattern;
426 fmt = GET_RTX_FORMAT (code);
427 len = GET_RTX_LENGTH (code);
428 for (i = 0; i < len; i++)
430 switch (fmt[i])
432 case 'e': case 'u':
433 if ((r = find_matching_operand (XEXP (pattern, i), n)))
434 return r;
435 break;
437 case 'V':
438 if (! XVEC (pattern, i))
439 break;
440 /* Fall through. */
442 case 'E':
443 for (j = 0; j < XVECLEN (pattern, i); j++)
444 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
445 return r;
446 break;
448 case 'i': case 'r': case 'w': case '0': case 's':
449 break;
451 default:
452 gcc_unreachable ();
456 return NULL;
459 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
460 don't use the MATCH_OPERAND constraint, only the predicate.
461 This is confusing to folks doing new ports, so help them
462 not make the mistake. */
464 static bool
465 constraints_supported_in_insn_p (rtx insn)
467 return !(GET_CODE (insn) == DEFINE_EXPAND
468 || GET_CODE (insn) == DEFINE_SPLIT
469 || GET_CODE (insn) == DEFINE_PEEPHOLE2);
472 /* Check for various errors in patterns. SET is nonnull for a destination,
473 and is the complete set pattern. SET_CODE is '=' for normal sets, and
474 '+' within a context that requires in-out constraints. */
476 static void
477 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
479 const char *fmt;
480 RTX_CODE code;
481 size_t i, len;
482 int j;
484 code = GET_CODE (pattern);
485 switch (code)
487 case MATCH_SCRATCH:
489 const char constraints0 = XSTR (pattern, 1)[0];
491 if (!constraints_supported_in_insn_p (insn))
493 if (constraints0)
495 error_with_line (pattern_lineno,
496 "constraints not supported in %s",
497 rtx_name[GET_CODE (insn)]);
499 return;
502 /* If a MATCH_SCRATCH is used in a context requiring an write-only
503 or read/write register, validate that. */
504 if (set_code == '='
505 && constraints0
506 && constraints0 != '='
507 && constraints0 != '+')
509 error_with_line (pattern_lineno,
510 "operand %d missing output reload",
511 XINT (pattern, 0));
513 return;
515 case MATCH_DUP:
516 case MATCH_OP_DUP:
517 case MATCH_PAR_DUP:
518 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern)
519 error_with_line (pattern_lineno,
520 "operand %i duplicated before defined",
521 XINT (pattern, 0));
522 break;
523 case MATCH_OPERAND:
524 case MATCH_OPERATOR:
526 const char *pred_name = XSTR (pattern, 1);
527 const struct pred_data *pred;
528 const char *c_test;
530 if (GET_CODE (insn) == DEFINE_INSN)
531 c_test = XSTR (insn, 2);
532 else
533 c_test = XSTR (insn, 1);
535 if (pred_name[0] != 0)
537 pred = lookup_predicate (pred_name);
538 if (!pred)
539 error_with_line (pattern_lineno, "unknown predicate '%s'",
540 pred_name);
542 else
543 pred = 0;
545 if (code == MATCH_OPERAND)
547 const char *constraints = XSTR (pattern, 2);
548 const char constraints0 = constraints[0];
550 if (!constraints_supported_in_insn_p (insn))
552 if (constraints0)
554 error_with_line (pattern_lineno,
555 "constraints not supported in %s",
556 rtx_name[GET_CODE (insn)]);
560 /* A MATCH_OPERAND that is a SET should have an output reload. */
561 else if (set && constraints0)
563 if (set_code == '+')
565 if (constraints0 == '+')
567 /* If we've only got an output reload for this operand,
568 we'd better have a matching input operand. */
569 else if (constraints0 == '='
570 && find_matching_operand (insn, XINT (pattern, 0)))
572 else
573 error_with_line (pattern_lineno,
574 "operand %d missing in-out reload",
575 XINT (pattern, 0));
577 else if (constraints0 != '=' && constraints0 != '+')
578 error_with_line (pattern_lineno,
579 "operand %d missing output reload",
580 XINT (pattern, 0));
583 /* For matching constraint in MATCH_OPERAND, the digit must be a
584 smaller number than the number of the operand that uses it in the
585 constraint. */
586 while (1)
588 while (constraints[0]
589 && (constraints[0] == ' ' || constraints[0] == ','))
590 constraints++;
591 if (!constraints[0])
592 break;
594 if (constraints[0] >= '0' && constraints[0] <= '9')
596 int val;
598 sscanf (constraints, "%d", &val);
599 if (val >= XINT (pattern, 0))
600 error_with_line (pattern_lineno,
601 "constraint digit %d is not smaller than"
602 " operand %d",
603 val, XINT (pattern, 0));
606 while (constraints[0] && constraints[0] != ',')
607 constraints++;
611 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
612 while not likely to occur at runtime, results in less efficient
613 code from insn-recog.c. */
614 if (set && pred && pred->allows_non_lvalue)
615 error_with_line (pattern_lineno,
616 "destination operand %d allows non-lvalue",
617 XINT (pattern, 0));
619 /* A modeless MATCH_OPERAND can be handy when we can check for
620 multiple modes in the c_test. In most other cases, it is a
621 mistake. Only DEFINE_INSN is eligible, since SPLIT and
622 PEEP2 can FAIL within the output pattern. Exclude special
623 predicates, which check the mode themselves. Also exclude
624 predicates that allow only constants. Exclude the SET_DEST
625 of a call instruction, as that is a common idiom. */
627 if (GET_MODE (pattern) == VOIDmode
628 && code == MATCH_OPERAND
629 && GET_CODE (insn) == DEFINE_INSN
630 && pred
631 && !pred->special
632 && pred->allows_non_const
633 && strstr (c_test, "operands") == NULL
634 && ! (set
635 && GET_CODE (set) == SET
636 && GET_CODE (SET_SRC (set)) == CALL))
637 message_with_line (pattern_lineno,
638 "warning: operand %d missing mode?",
639 XINT (pattern, 0));
640 return;
643 case SET:
645 machine_mode dmode, smode;
646 rtx dest, src;
648 dest = SET_DEST (pattern);
649 src = SET_SRC (pattern);
651 /* STRICT_LOW_PART is a wrapper. Its argument is the real
652 destination, and it's mode should match the source. */
653 if (GET_CODE (dest) == STRICT_LOW_PART)
654 dest = XEXP (dest, 0);
656 /* Find the referent for a DUP. */
658 if (GET_CODE (dest) == MATCH_DUP
659 || GET_CODE (dest) == MATCH_OP_DUP
660 || GET_CODE (dest) == MATCH_PAR_DUP)
661 dest = find_operand (insn, XINT (dest, 0), NULL);
663 if (GET_CODE (src) == MATCH_DUP
664 || GET_CODE (src) == MATCH_OP_DUP
665 || GET_CODE (src) == MATCH_PAR_DUP)
666 src = find_operand (insn, XINT (src, 0), NULL);
668 dmode = GET_MODE (dest);
669 smode = GET_MODE (src);
671 /* The mode of an ADDRESS_OPERAND is the mode of the memory
672 reference, not the mode of the address. */
673 if (GET_CODE (src) == MATCH_OPERAND
674 && ! strcmp (XSTR (src, 1), "address_operand"))
677 /* The operands of a SET must have the same mode unless one
678 is VOIDmode. */
679 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
680 error_with_line (pattern_lineno,
681 "mode mismatch in set: %smode vs %smode",
682 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
684 /* If only one of the operands is VOIDmode, and PC or CC0 is
685 not involved, it's probably a mistake. */
686 else if (dmode != smode
687 && GET_CODE (dest) != PC
688 && GET_CODE (dest) != CC0
689 && GET_CODE (src) != PC
690 && GET_CODE (src) != CC0
691 && !CONST_INT_P (src)
692 && !CONST_WIDE_INT_P (src)
693 && GET_CODE (src) != CALL)
695 const char *which;
696 which = (dmode == VOIDmode ? "destination" : "source");
697 message_with_line (pattern_lineno,
698 "warning: %s missing a mode?", which);
701 if (dest != SET_DEST (pattern))
702 validate_pattern (dest, insn, pattern, '=');
703 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
704 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
705 return;
708 case CLOBBER:
709 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
710 return;
712 case ZERO_EXTRACT:
713 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
714 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
715 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
716 return;
718 case STRICT_LOW_PART:
719 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
720 return;
722 case LABEL_REF:
723 if (GET_MODE (LABEL_REF_LABEL (pattern)) != VOIDmode)
724 error_with_line (pattern_lineno,
725 "operand to label_ref %smode not VOIDmode",
726 GET_MODE_NAME (GET_MODE (LABEL_REF_LABEL (pattern))));
727 break;
729 default:
730 break;
733 fmt = GET_RTX_FORMAT (code);
734 len = GET_RTX_LENGTH (code);
735 for (i = 0; i < len; i++)
737 switch (fmt[i])
739 case 'e': case 'u':
740 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
741 break;
743 case 'E':
744 for (j = 0; j < XVECLEN (pattern, i); j++)
745 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
746 break;
748 case 'i': case 'r': case 'w': case '0': case 's':
749 break;
751 default:
752 gcc_unreachable ();
757 /* Simple list structure for items of type T, for use when being part
758 of a list is an inherent property of T. T must have members equivalent
759 to "T *prev, *next;" and a function "void set_parent (list_head <T> *)"
760 to set the parent list. */
761 template <typename T>
762 struct list_head
764 /* A range of linked items. */
765 struct range
767 range (T *);
768 range (T *, T *);
770 T *start, *end;
771 void set_parent (list_head *);
774 list_head ();
775 range release ();
776 void push_back (range);
777 range remove (range);
778 void replace (range, range);
779 T *singleton () const;
781 T *first, *last;
784 /* Create a range [START_IN, START_IN]. */
786 template <typename T>
787 list_head <T>::range::range (T *start_in) : start (start_in), end (start_in) {}
789 /* Create a range [START_IN, END_IN], linked by next and prev fields. */
791 template <typename T>
792 list_head <T>::range::range (T *start_in, T *end_in)
793 : start (start_in), end (end_in) {}
795 template <typename T>
796 void
797 list_head <T>::range::set_parent (list_head <T> *owner)
799 for (T *item = start; item != end; item = item->next)
800 item->set_parent (owner);
801 end->set_parent (owner);
804 template <typename T>
805 list_head <T>::list_head () : first (0), last (0) {}
807 /* Add R to the end of the list. */
809 template <typename T>
810 void
811 list_head <T>::push_back (range r)
813 if (last)
814 last->next = r.start;
815 else
816 first = r.start;
817 r.start->prev = last;
818 last = r.end;
819 r.set_parent (this);
822 /* Remove R from the list. R remains valid and can be inserted into
823 other lists. */
825 template <typename T>
826 typename list_head <T>::range
827 list_head <T>::remove (range r)
829 if (r.start->prev)
830 r.start->prev->next = r.end->next;
831 else
832 first = r.end->next;
833 if (r.end->next)
834 r.end->next->prev = r.start->prev;
835 else
836 last = r.start->prev;
837 r.start->prev = 0;
838 r.end->next = 0;
839 r.set_parent (0);
840 return r;
843 /* Replace OLDR with NEWR. OLDR remains valid and can be inserted into
844 other lists. */
846 template <typename T>
847 void
848 list_head <T>::replace (range oldr, range newr)
850 newr.start->prev = oldr.start->prev;
851 newr.end->next = oldr.end->next;
853 oldr.start->prev = 0;
854 oldr.end->next = 0;
855 oldr.set_parent (0);
857 if (newr.start->prev)
858 newr.start->prev->next = newr.start;
859 else
860 first = newr.start;
861 if (newr.end->next)
862 newr.end->next->prev = newr.end;
863 else
864 last = newr.end;
865 newr.set_parent (this);
868 /* Empty the list and return the previous contents as a range that can
869 be inserted into other lists. */
871 template <typename T>
872 typename list_head <T>::range
873 list_head <T>::release ()
875 range r (first, last);
876 first = 0;
877 last = 0;
878 r.set_parent (0);
879 return r;
882 /* If the list contains a single item, return that item, otherwise return
883 null. */
885 template <typename T>
887 list_head <T>::singleton () const
889 return first == last ? first : 0;
892 struct state;
894 /* Describes a possible successful return from a routine. */
895 struct acceptance_type
897 /* The type of routine we're returning from. */
898 routine_type type : 16;
900 /* True if this structure only really represents a partial match,
901 and if we must call a subroutine of type TYPE to complete the match.
902 In this case we'll call the subroutine and, if it succeeds, return
903 whatever the subroutine returned.
905 False if this structure presents a full match. */
906 unsigned int partial_p : 1;
908 union
910 /* If PARTIAL_P, this is the number of the subroutine to call. */
911 int subroutine_id;
913 /* Valid if !PARTIAL_P. */
914 struct
916 /* The identifier of the matching pattern. For SUBPATTERNs this
917 value belongs to an ad-hoc routine-specific enum. For the
918 others it's the number of an .md file pattern. */
919 int code;
920 union
922 /* For RECOG, the number of clobbers that must be added to the
923 pattern in order for it to match CODE. */
924 int num_clobbers;
926 /* For PEEPHOLE2, the number of additional instructions that were
927 included in the optimization. */
928 int match_len;
929 } u;
930 } full;
931 } u;
934 bool
935 operator == (const acceptance_type &a, const acceptance_type &b)
937 if (a.partial_p != b.partial_p)
938 return false;
939 if (a.partial_p)
940 return a.u.subroutine_id == b.u.subroutine_id;
941 else
942 return a.u.full.code == b.u.full.code;
945 bool
946 operator != (const acceptance_type &a, const acceptance_type &b)
948 return !operator == (a, b);
951 /* Represents a parameter to a pattern routine. */
952 struct parameter
954 /* The C type of parameter. */
955 enum type_enum {
956 /* Represents an invalid parameter. */
957 UNSET,
959 /* A machine_mode parameter. */
960 MODE,
962 /* An rtx_code parameter. */
963 CODE,
965 /* An int parameter. */
966 INT,
968 /* An unsigned int parameter. */
969 UINT,
971 /* A HOST_WIDE_INT parameter. */
972 WIDE_INT
975 parameter ();
976 parameter (type_enum, bool, uint64_t);
978 /* The type of the parameter. */
979 type_enum type;
981 /* True if the value passed is variable, false if it is constant. */
982 bool is_param;
984 /* If IS_PARAM, this is the number of the variable passed, for an "i%d"
985 format string. If !IS_PARAM, this is the constant value passed. */
986 uint64_t value;
989 parameter::parameter ()
990 : type (UNSET), is_param (false), value (0) {}
992 parameter::parameter (type_enum type_in, bool is_param_in, uint64_t value_in)
993 : type (type_in), is_param (is_param_in), value (value_in) {}
995 bool
996 operator == (const parameter &param1, const parameter &param2)
998 return (param1.type == param2.type
999 && param1.is_param == param2.is_param
1000 && param1.value == param2.value);
1003 bool
1004 operator != (const parameter &param1, const parameter &param2)
1006 return !operator == (param1, param2);
1009 /* Represents a routine that matches a partial rtx pattern, returning
1010 an ad-hoc enum value on success and -1 on failure. The routine can
1011 be used by any subroutine type. The match can be parameterized by
1012 things like mode, code and UNSPEC number. */
1013 struct pattern_routine
1015 /* The state that implements the pattern. */
1016 state *s;
1018 /* The deepest root position from which S can access all the rtxes it needs.
1019 This is NULL if the pattern doesn't need an rtx input, usually because
1020 all matching is done on operands[] instead. */
1021 position *pos;
1023 /* A unique identifier for the routine. */
1024 unsigned int pattern_id;
1026 /* True if the routine takes pnum_clobbers as argument. */
1027 bool pnum_clobbers_p;
1029 /* True if the routine takes the enclosing instruction as argument. */
1030 bool insn_p;
1032 /* The types of the other parameters to the routine, if any. */
1033 auto_vec <parameter::type_enum, MAX_PATTERN_PARAMS> param_types;
1036 /* All defined patterns. */
1037 static vec <pattern_routine *> patterns;
1039 /* Represents one use of a pattern routine. */
1040 struct pattern_use
1042 /* The pattern routine to use. */
1043 pattern_routine *routine;
1045 /* The values to pass as parameters. This vector has the same length
1046 as ROUTINE->PARAM_TYPES. */
1047 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
1050 /* Represents a test performed by a decision. */
1051 struct rtx_test
1053 rtx_test ();
1055 /* The types of test that can be performed. Most of them take as input
1056 an rtx X. Some also take as input a transition label LABEL; the others
1057 are booleans for which the transition label is always "true".
1059 The order of the enum isn't important. */
1060 enum kind_enum {
1061 /* Check GET_CODE (X) == LABEL. */
1062 CODE,
1064 /* Check GET_MODE (X) == LABEL. */
1065 MODE,
1067 /* Check REGNO (X) == LABEL. */
1068 REGNO_FIELD,
1070 /* Check XINT (X, u.opno) == LABEL. */
1071 INT_FIELD,
1073 /* Check XWINT (X, u.opno) == LABEL. */
1074 WIDE_INT_FIELD,
1076 /* Check XVECLEN (X, 0) == LABEL. */
1077 VECLEN,
1079 /* Check peep2_current_count >= u.min_len. */
1080 PEEP2_COUNT,
1082 /* Check XVECLEN (X, 0) >= u.min_len. */
1083 VECLEN_GE,
1085 /* Check whether X is a cached const_int with value u.integer. */
1086 SAVED_CONST_INT,
1088 /* Check u.predicate.data (X, u.predicate.mode). */
1089 PREDICATE,
1091 /* Check rtx_equal_p (X, operands[u.opno]). */
1092 DUPLICATE,
1094 /* Check whether X matches pattern u.pattern. */
1095 PATTERN,
1097 /* Check whether pnum_clobbers is nonnull (RECOG only). */
1098 HAVE_NUM_CLOBBERS,
1100 /* Check whether general C test u.string holds. In general the condition
1101 needs access to "insn" and the full operand list. */
1102 C_TEST,
1104 /* Execute operands[u.opno] = X. (Always succeeds.) */
1105 SET_OP,
1107 /* Accept u.acceptance. Always succeeds for SUBPATTERN, RECOG and SPLIT.
1108 May fail for PEEPHOLE2 if the define_peephole2 C code executes FAIL. */
1109 ACCEPT
1112 /* The position of rtx X in the above description, relative to the
1113 incoming instruction "insn". The position is null if the test
1114 doesn't take an X as input. */
1115 position *pos;
1117 /* Which element of operands[] already contains POS, or -1 if no element
1118 is known to hold POS. */
1119 int pos_operand;
1121 /* The type of test and its parameters, as described above. */
1122 kind_enum kind;
1123 union
1125 int opno;
1126 int min_len;
1127 struct
1129 bool is_param;
1130 int value;
1131 } integer;
1132 struct
1134 const struct pred_data *data;
1135 /* True if the mode is taken from a machine_mode parameter
1136 to the routine rather than a constant machine_mode. If true,
1137 MODE is the number of the parameter (for an "i%d" format string),
1138 otherwise it is the mode itself. */
1139 bool mode_is_param;
1140 unsigned int mode;
1141 } predicate;
1142 pattern_use *pattern;
1143 const char *string;
1144 acceptance_type acceptance;
1145 } u;
1147 static rtx_test code (position *);
1148 static rtx_test mode (position *);
1149 static rtx_test regno_field (position *);
1150 static rtx_test int_field (position *, int);
1151 static rtx_test wide_int_field (position *, int);
1152 static rtx_test veclen (position *);
1153 static rtx_test peep2_count (int);
1154 static rtx_test veclen_ge (position *, int);
1155 static rtx_test predicate (position *, const pred_data *, machine_mode);
1156 static rtx_test duplicate (position *, int);
1157 static rtx_test pattern (position *, pattern_use *);
1158 static rtx_test have_num_clobbers ();
1159 static rtx_test c_test (const char *);
1160 static rtx_test set_op (position *, int);
1161 static rtx_test accept (const acceptance_type &);
1163 bool terminal_p () const;
1164 bool single_outcome_p () const;
1166 private:
1167 rtx_test (position *, kind_enum);
1170 rtx_test::rtx_test () {}
1172 rtx_test::rtx_test (position *pos_in, kind_enum kind_in)
1173 : pos (pos_in), pos_operand (-1), kind (kind_in) {}
1175 rtx_test
1176 rtx_test::code (position *pos)
1178 return rtx_test (pos, rtx_test::CODE);
1181 rtx_test
1182 rtx_test::mode (position *pos)
1184 return rtx_test (pos, rtx_test::MODE);
1187 rtx_test
1188 rtx_test::regno_field (position *pos)
1190 rtx_test res (pos, rtx_test::REGNO_FIELD);
1191 return res;
1194 rtx_test
1195 rtx_test::int_field (position *pos, int opno)
1197 rtx_test res (pos, rtx_test::INT_FIELD);
1198 res.u.opno = opno;
1199 return res;
1202 rtx_test
1203 rtx_test::wide_int_field (position *pos, int opno)
1205 rtx_test res (pos, rtx_test::WIDE_INT_FIELD);
1206 res.u.opno = opno;
1207 return res;
1210 rtx_test
1211 rtx_test::veclen (position *pos)
1213 return rtx_test (pos, rtx_test::VECLEN);
1216 rtx_test
1217 rtx_test::peep2_count (int min_len)
1219 rtx_test res (0, rtx_test::PEEP2_COUNT);
1220 res.u.min_len = min_len;
1221 return res;
1224 rtx_test
1225 rtx_test::veclen_ge (position *pos, int min_len)
1227 rtx_test res (pos, rtx_test::VECLEN_GE);
1228 res.u.min_len = min_len;
1229 return res;
1232 rtx_test
1233 rtx_test::predicate (position *pos, const struct pred_data *data,
1234 machine_mode mode)
1236 rtx_test res (pos, rtx_test::PREDICATE);
1237 res.u.predicate.data = data;
1238 res.u.predicate.mode_is_param = false;
1239 res.u.predicate.mode = mode;
1240 return res;
1243 rtx_test
1244 rtx_test::duplicate (position *pos, int opno)
1246 rtx_test res (pos, rtx_test::DUPLICATE);
1247 res.u.opno = opno;
1248 return res;
1251 rtx_test
1252 rtx_test::pattern (position *pos, pattern_use *pattern)
1254 rtx_test res (pos, rtx_test::PATTERN);
1255 res.u.pattern = pattern;
1256 return res;
1259 rtx_test
1260 rtx_test::have_num_clobbers ()
1262 return rtx_test (0, rtx_test::HAVE_NUM_CLOBBERS);
1265 rtx_test
1266 rtx_test::c_test (const char *string)
1268 rtx_test res (0, rtx_test::C_TEST);
1269 res.u.string = string;
1270 return res;
1273 rtx_test
1274 rtx_test::set_op (position *pos, int opno)
1276 rtx_test res (pos, rtx_test::SET_OP);
1277 res.u.opno = opno;
1278 return res;
1281 rtx_test
1282 rtx_test::accept (const acceptance_type &acceptance)
1284 rtx_test res (0, rtx_test::ACCEPT);
1285 res.u.acceptance = acceptance;
1286 return res;
1289 /* Return true if the test represents an unconditionally successful match. */
1291 bool
1292 rtx_test::terminal_p () const
1294 return kind == rtx_test::ACCEPT && u.acceptance.type != PEEPHOLE2;
1297 /* Return true if the test is a boolean that is always true. */
1299 bool
1300 rtx_test::single_outcome_p () const
1302 return terminal_p () || kind == rtx_test::SET_OP;
1305 bool
1306 operator == (const rtx_test &a, const rtx_test &b)
1308 if (a.pos != b.pos || a.kind != b.kind)
1309 return false;
1310 switch (a.kind)
1312 case rtx_test::CODE:
1313 case rtx_test::MODE:
1314 case rtx_test::REGNO_FIELD:
1315 case rtx_test::VECLEN:
1316 case rtx_test::HAVE_NUM_CLOBBERS:
1317 return true;
1319 case rtx_test::PEEP2_COUNT:
1320 case rtx_test::VECLEN_GE:
1321 return a.u.min_len == b.u.min_len;
1323 case rtx_test::INT_FIELD:
1324 case rtx_test::WIDE_INT_FIELD:
1325 case rtx_test::DUPLICATE:
1326 case rtx_test::SET_OP:
1327 return a.u.opno == b.u.opno;
1329 case rtx_test::SAVED_CONST_INT:
1330 return (a.u.integer.is_param == b.u.integer.is_param
1331 && a.u.integer.value == b.u.integer.value);
1333 case rtx_test::PREDICATE:
1334 return (a.u.predicate.data == b.u.predicate.data
1335 && a.u.predicate.mode_is_param == b.u.predicate.mode_is_param
1336 && a.u.predicate.mode == b.u.predicate.mode);
1338 case rtx_test::PATTERN:
1339 return (a.u.pattern->routine == b.u.pattern->routine
1340 && a.u.pattern->params == b.u.pattern->params);
1342 case rtx_test::C_TEST:
1343 return strcmp (a.u.string, b.u.string) == 0;
1345 case rtx_test::ACCEPT:
1346 return a.u.acceptance == b.u.acceptance;
1348 gcc_unreachable ();
1351 bool
1352 operator != (const rtx_test &a, const rtx_test &b)
1354 return !operator == (a, b);
1357 /* A simple set of transition labels. Most transitions have a singleton
1358 label, so try to make that case as efficient as possible. */
1359 struct int_set : public auto_vec <uint64_t, 1>
1361 typedef uint64_t *iterator;
1363 int_set ();
1364 int_set (uint64_t);
1365 int_set (const int_set &);
1367 int_set &operator = (const int_set &);
1369 iterator begin ();
1370 iterator end ();
1373 int_set::int_set () {}
1375 int_set::int_set (uint64_t label)
1377 safe_push (label);
1380 int_set::int_set (const int_set &other)
1382 safe_splice (other);
1385 int_set &
1386 int_set::operator = (const int_set &other)
1388 truncate (0);
1389 safe_splice (other);
1390 return *this;
1393 int_set::iterator
1394 int_set::begin ()
1396 return address ();
1399 int_set::iterator
1400 int_set::end ()
1402 return address () + length ();
1405 bool
1406 operator == (const int_set &a, const int_set &b)
1408 if (a.length () != b.length ())
1409 return false;
1410 for (unsigned int i = 0; i < a.length (); ++i)
1411 if (a[i] != b[i])
1412 return false;
1413 return true;
1416 bool
1417 operator != (const int_set &a, const int_set &b)
1419 return !operator == (a, b);
1422 struct decision;
1424 /* Represents a transition between states, dependent on the result of
1425 a test T. */
1426 struct transition
1428 transition (const int_set &, state *, bool);
1430 void set_parent (list_head <transition> *);
1432 /* Links to other transitions for T. Always null for boolean tests. */
1433 transition *prev, *next;
1435 /* The transition should be taken when T has one of these values.
1436 E.g. for rtx_test::CODE this is a set of codes, while for booleans like
1437 rtx_test::PREDICATE it is always a singleton "true". The labels are
1438 sorted in ascending order. */
1439 int_set labels;
1441 /* The source decision. */
1442 decision *from;
1444 /* The target state. */
1445 state *to;
1447 /* True if TO would function correctly even if TEST wasn't performed.
1448 E.g. it isn't necessary to check whether GET_MODE (x1) is SImode
1449 before calling register_operand (x1, SImode), since register_operand
1450 performs its own mode check. However, checking GET_MODE can be a cheap
1451 way of disambiguating SImode and DImode register operands. */
1452 bool optional;
1454 /* True if LABELS contains parameter numbers rather than constants.
1455 E.g. if this is true for a rtx_test::CODE, the label is the number
1456 of an rtx_code parameter rather than an rtx_code itself.
1457 LABELS is always a singleton when this variable is true. */
1458 bool is_param;
1461 /* Represents a test and the action that should be taken on the result.
1462 If a transition exists for the test outcome, the machine switches
1463 to the transition's target state. If no suitable transition exists,
1464 the machine either falls through to the next decision or, if there are no
1465 more decisions to try, fails the match. */
1466 struct decision : list_head <transition>
1468 decision (const rtx_test &);
1470 void set_parent (list_head <decision> *s);
1471 bool if_statement_p (uint64_t * = 0) const;
1473 /* The state to which this decision belongs. */
1474 state *s;
1476 /* Links to other decisions in the same state. */
1477 decision *prev, *next;
1479 /* The test to perform. */
1480 rtx_test test;
1483 /* Represents one machine state. For each state the machine tries a list
1484 of decisions, in order, and acts on the first match. It fails without
1485 further backtracking if no decisions match. */
1486 struct state : list_head <decision>
1488 void set_parent (list_head <state> *) {}
1491 transition::transition (const int_set &labels_in, state *to_in,
1492 bool optional_in)
1493 : prev (0), next (0), labels (labels_in), from (0), to (to_in),
1494 optional (optional_in), is_param (false) {}
1496 /* Set the source decision of the transition. */
1498 void
1499 transition::set_parent (list_head <transition> *from_in)
1501 from = static_cast <decision *> (from_in);
1504 decision::decision (const rtx_test &test_in)
1505 : prev (0), next (0), test (test_in) {}
1507 /* Set the state to which this decision belongs. */
1509 void
1510 decision::set_parent (list_head <decision> *s_in)
1512 s = static_cast <state *> (s_in);
1515 /* Return true if the decision has a single transition with a single label.
1516 If so, return the label in *LABEL if nonnull. */
1518 inline bool
1519 decision::if_statement_p (uint64_t *label) const
1521 if (singleton () && first->labels.length () == 1)
1523 if (label)
1524 *label = first->labels[0];
1525 return true;
1527 return false;
1530 /* Add to FROM a decision that performs TEST and has a single transition
1531 TRANS. */
1533 static void
1534 add_decision (state *from, const rtx_test &test, transition *trans)
1536 decision *d = new decision (test);
1537 from->push_back (d);
1538 d->push_back (trans);
1541 /* Add a transition from FROM to a new, empty state that is taken
1542 when TEST == LABELS. OPTIONAL says whether the new transition
1543 should be optional. Return the new state. */
1545 static state *
1546 add_decision (state *from, const rtx_test &test, int_set labels, bool optional)
1548 state *to = new state;
1549 add_decision (from, test, new transition (labels, to, optional));
1550 return to;
1553 /* Insert a decision before decisions R to make them dependent on
1554 TEST == LABELS. OPTIONAL says whether the new transition should be
1555 optional. */
1557 static decision *
1558 insert_decision_before (state::range r, const rtx_test &test,
1559 const int_set &labels, bool optional)
1561 decision *newd = new decision (test);
1562 state *news = new state;
1563 newd->push_back (new transition (labels, news, optional));
1564 r.start->s->replace (r, newd);
1565 news->push_back (r);
1566 return newd;
1569 /* Remove any optional transitions from S that turned out not to be useful. */
1571 static void
1572 collapse_optional_decisions (state *s)
1574 decision *d = s->first;
1575 while (d)
1577 decision *next = d->next;
1578 for (transition *trans = d->first; trans; trans = trans->next)
1579 collapse_optional_decisions (trans->to);
1580 /* A decision with a single optional transition doesn't help
1581 partition the potential matches and so is unlikely to be
1582 worthwhile. In particular, if the decision that performs the
1583 test is the last in the state, the best it could do is reject
1584 an invalid pattern slightly earlier. If instead the decision
1585 is not the last in the state, the condition it tests could hold
1586 even for the later decisions in the state. The best it can do
1587 is save work in some cases where only the later decisions can
1588 succeed.
1590 In both cases the optional transition would add extra work to
1591 successful matches when the tested condition holds. */
1592 if (transition *trans = d->singleton ())
1593 if (trans->optional)
1594 s->replace (d, trans->to->release ());
1595 d = next;
1599 /* Try to squash several separate tests into simpler ones. */
1601 static void
1602 simplify_tests (state *s)
1604 for (decision *d = s->first; d; d = d->next)
1606 uint64_t label;
1607 /* Convert checks for GET_CODE (x) == CONST_INT and XWINT (x, 0) == N
1608 into checks for const_int_rtx[N'], if N is suitably small. */
1609 if (d->test.kind == rtx_test::CODE
1610 && d->if_statement_p (&label)
1611 && label == CONST_INT)
1612 if (decision *second = d->first->to->singleton ())
1613 if (d->test.pos == second->test.pos
1614 && second->test.kind == rtx_test::WIDE_INT_FIELD
1615 && second->test.u.opno == 0
1616 && second->if_statement_p (&label)
1617 && IN_RANGE (int64_t (label),
1618 -MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT))
1620 d->test.kind = rtx_test::SAVED_CONST_INT;
1621 d->test.u.integer.is_param = false;
1622 d->test.u.integer.value = label;
1623 d->replace (d->first, second->release ());
1624 d->first->labels[0] = true;
1626 /* If we have a CODE test followed by a PREDICATE test, rely on
1627 the predicate to test the code.
1629 This case exists for match_operators. We initially treat the
1630 CODE test for a match_operator as non-optional so that we can
1631 safely move down to its operands. It may turn out that all
1632 paths that reach that code test require the same predicate
1633 to be true. cse_tests will then put the predicate test in
1634 series with the code test. */
1635 if (d->test.kind == rtx_test::CODE)
1636 if (transition *trans = d->singleton ())
1638 state *s = trans->to;
1639 while (decision *d2 = s->singleton ())
1641 if (d->test.pos != d2->test.pos)
1642 break;
1643 transition *trans2 = d2->singleton ();
1644 if (!trans2)
1645 break;
1646 if (d2->test.kind == rtx_test::PREDICATE)
1648 d->test = d2->test;
1649 trans->labels = int_set (true);
1650 s->replace (d2, trans2->to->release ());
1651 break;
1653 s = trans2->to;
1656 for (transition *trans = d->first; trans; trans = trans->next)
1657 simplify_tests (trans->to);
1661 /* Return true if all successful returns passing through D require the
1662 condition tested by COMMON to be true.
1664 When returning true, add all transitions like COMMON in D to WHERE.
1665 WHERE may contain a partial result on failure. */
1667 static bool
1668 common_test_p (decision *d, transition *common, vec <transition *> *where)
1670 if (d->test.kind == rtx_test::ACCEPT)
1671 /* We found a successful return that didn't require COMMON. */
1672 return false;
1673 if (d->test == common->from->test)
1675 transition *trans = d->singleton ();
1676 if (!trans
1677 || trans->optional != common->optional
1678 || trans->labels != common->labels)
1679 return false;
1680 where->safe_push (trans);
1681 return true;
1683 for (transition *trans = d->first; trans; trans = trans->next)
1684 for (decision *subd = trans->to->first; subd; subd = subd->next)
1685 if (!common_test_p (subd, common, where))
1686 return false;
1687 return true;
1690 /* Indicates that we have tested GET_CODE (X) for a particular rtx X. */
1691 const unsigned char TESTED_CODE = 1;
1693 /* Indicates that we have tested XVECLEN (X, 0) for a particular rtx X. */
1694 const unsigned char TESTED_VECLEN = 2;
1696 /* Represents a set of conditions that are known to hold. */
1697 struct known_conditions
1699 /* A mask of TESTED_ values for each position, indexed by the position's
1700 id field. */
1701 auto_vec <unsigned char> position_tests;
1703 /* Index N says whether operands[N] has been set. */
1704 auto_vec <bool> set_operands;
1706 /* A guranteed lower bound on the value of peep2_current_count. */
1707 int peep2_count;
1710 /* Return true if TEST can safely be performed at D, where
1711 the conditions in KC hold. TEST is known to occur along the
1712 first path from D (i.e. always following the first transition
1713 of the first decision). Any intervening tests can be used as
1714 negative proof that hoisting isn't safe, but only KC can be used
1715 as positive proof. */
1717 static bool
1718 safe_to_hoist_p (decision *d, const rtx_test &test, known_conditions *kc)
1720 switch (test.kind)
1722 case rtx_test::C_TEST:
1723 /* In general, C tests require everything else to have been
1724 verified and all operands to have been set up. */
1725 return false;
1727 case rtx_test::ACCEPT:
1728 /* Don't accept something before all conditions have been tested. */
1729 return false;
1731 case rtx_test::PREDICATE:
1732 /* Don't move a predicate over a test for VECLEN_GE, since the
1733 predicate used in a match_parallel can legitimately expect the
1734 length to be checked first. */
1735 for (decision *subd = d;
1736 subd->test != test;
1737 subd = subd->first->to->first)
1738 if (subd->test.pos == test.pos
1739 && subd->test.kind == rtx_test::VECLEN_GE)
1740 return false;
1741 goto any_rtx;
1743 case rtx_test::DUPLICATE:
1744 /* Don't test for a match_dup until the associated operand has
1745 been set. */
1746 if (!kc->set_operands[test.u.opno])
1747 return false;
1748 goto any_rtx;
1750 case rtx_test::CODE:
1751 case rtx_test::MODE:
1752 case rtx_test::SAVED_CONST_INT:
1753 case rtx_test::SET_OP:
1754 any_rtx:
1755 /* Check whether it is safe to access the rtx under test. */
1756 switch (test.pos->type)
1758 case POS_PEEP2_INSN:
1759 return test.pos->arg < kc->peep2_count;
1761 case POS_XEXP:
1762 return kc->position_tests[test.pos->base->id] & TESTED_CODE;
1764 case POS_XVECEXP0:
1765 return kc->position_tests[test.pos->base->id] & TESTED_VECLEN;
1767 gcc_unreachable ();
1769 case rtx_test::REGNO_FIELD:
1770 case rtx_test::INT_FIELD:
1771 case rtx_test::WIDE_INT_FIELD:
1772 case rtx_test::VECLEN:
1773 case rtx_test::VECLEN_GE:
1774 /* These tests access a specific part of an rtx, so are only safe
1775 once we know what the rtx is. */
1776 return kc->position_tests[test.pos->id] & TESTED_CODE;
1778 case rtx_test::PEEP2_COUNT:
1779 case rtx_test::HAVE_NUM_CLOBBERS:
1780 /* These tests can be performed anywhere. */
1781 return true;
1783 case rtx_test::PATTERN:
1784 gcc_unreachable ();
1786 gcc_unreachable ();
1789 /* Look for a transition that is taken by all successful returns from a range
1790 of decisions starting at OUTER and that would be better performed by
1791 OUTER's state instead. On success, store all instances of that transition
1792 in WHERE and return the last decision in the range. The range could
1793 just be OUTER, or it could include later decisions as well.
1795 WITH_POSITION_P is true if only tests with position POS should be tried,
1796 false if any test should be tried. WORTHWHILE_SINGLE_P is true if the
1797 result is useful even when the range contains just a single decision
1798 with a single transition. KC are the conditions that are known to
1799 hold at OUTER. */
1801 static decision *
1802 find_common_test (decision *outer, bool with_position_p,
1803 position *pos, bool worthwhile_single_p,
1804 known_conditions *kc, vec <transition *> *where)
1806 /* After this, WORTHWHILE_SINGLE_P indicates whether a range that contains
1807 just a single decision is useful, regardless of the number of
1808 transitions it has. */
1809 if (!outer->singleton ())
1810 worthwhile_single_p = true;
1811 /* Quick exit if we don't have enough decisions to form a worthwhile
1812 range. */
1813 if (!worthwhile_single_p && !outer->next)
1814 return 0;
1815 /* Follow the first chain down, as one example of a path that needs
1816 to contain the common test. */
1817 for (decision *d = outer; d; d = d->first->to->first)
1819 transition *trans = d->singleton ();
1820 if (trans
1821 && (!with_position_p || d->test.pos == pos)
1822 && safe_to_hoist_p (outer, d->test, kc))
1824 if (common_test_p (outer, trans, where))
1826 if (!outer->next)
1827 /* We checked above whether the move is worthwhile. */
1828 return outer;
1829 /* See how many decisions in OUTER's chain could reuse
1830 the same test. */
1831 decision *outer_end = outer;
1834 unsigned int length = where->length ();
1835 if (!common_test_p (outer_end->next, trans, where))
1837 where->truncate (length);
1838 break;
1840 outer_end = outer_end->next;
1842 while (outer_end->next);
1843 /* It is worth moving TRANS if it can be shared by more than
1844 one decision. */
1845 if (outer_end != outer || worthwhile_single_p)
1846 return outer_end;
1848 where->truncate (0);
1851 return 0;
1854 /* Try to promote common subtests in S to a single, shared decision.
1855 Also try to bunch tests for the same position together. POS is the
1856 position of the rtx tested before reaching S. KC are the conditions
1857 that are known to hold on entry to S. */
1859 static void
1860 cse_tests (position *pos, state *s, known_conditions *kc)
1862 for (decision *d = s->first; d; d = d->next)
1864 auto_vec <transition *, 16> where;
1865 if (d->test.pos)
1867 /* Try to find conditions that don't depend on a particular rtx,
1868 such as pnum_clobbers != NULL or peep2_current_count >= X.
1869 It's usually better to check these conditions as soon as
1870 possible, so the change is worthwhile even if there is
1871 only one copy of the test. */
1872 decision *endd = find_common_test (d, true, 0, true, kc, &where);
1873 if (!endd && d->test.pos != pos)
1874 /* Try to find other conditions related to position POS
1875 before moving to the new position. Again, this is
1876 worthwhile even if there is only one copy of the test,
1877 since it means that fewer position variables are live
1878 at a given time. */
1879 endd = find_common_test (d, true, pos, true, kc, &where);
1880 if (!endd)
1881 /* Try to find any condition that is used more than once. */
1882 endd = find_common_test (d, false, 0, false, kc, &where);
1883 if (endd)
1885 transition *common = where[0];
1886 /* Replace [D, ENDD] with a test like COMMON. We'll recurse
1887 on the common test and see the original D again next time. */
1888 d = insert_decision_before (state::range (d, endd),
1889 common->from->test,
1890 common->labels,
1891 common->optional);
1892 /* Remove the old tests. */
1893 while (!where.is_empty ())
1895 transition *trans = where.pop ();
1896 trans->from->s->replace (trans->from, trans->to->release ());
1901 /* Make sure that safe_to_hoist_p isn't being overly conservative.
1902 It should realize that D's test is safe in the current
1903 environment. */
1904 gcc_assert (d->test.kind == rtx_test::C_TEST
1905 || d->test.kind == rtx_test::ACCEPT
1906 || safe_to_hoist_p (d, d->test, kc));
1908 /* D won't be changed any further by the current optimization.
1909 Recurse with the state temporarily updated to include D. */
1910 int prev = 0;
1911 switch (d->test.kind)
1913 case rtx_test::CODE:
1914 prev = kc->position_tests[d->test.pos->id];
1915 kc->position_tests[d->test.pos->id] |= TESTED_CODE;
1916 break;
1918 case rtx_test::VECLEN:
1919 case rtx_test::VECLEN_GE:
1920 prev = kc->position_tests[d->test.pos->id];
1921 kc->position_tests[d->test.pos->id] |= TESTED_VECLEN;
1922 break;
1924 case rtx_test::SET_OP:
1925 prev = kc->set_operands[d->test.u.opno];
1926 gcc_assert (!prev);
1927 kc->set_operands[d->test.u.opno] = true;
1928 break;
1930 case rtx_test::PEEP2_COUNT:
1931 prev = kc->peep2_count;
1932 kc->peep2_count = MAX (prev, d->test.u.min_len);
1933 break;
1935 default:
1936 break;
1938 for (transition *trans = d->first; trans; trans = trans->next)
1939 cse_tests (d->test.pos ? d->test.pos : pos, trans->to, kc);
1940 switch (d->test.kind)
1942 case rtx_test::CODE:
1943 case rtx_test::VECLEN:
1944 case rtx_test::VECLEN_GE:
1945 kc->position_tests[d->test.pos->id] = prev;
1946 break;
1948 case rtx_test::SET_OP:
1949 kc->set_operands[d->test.u.opno] = prev;
1950 break;
1952 case rtx_test::PEEP2_COUNT:
1953 kc->peep2_count = prev;
1954 break;
1956 default:
1957 break;
1962 /* Return the type of value that can be used to parameterize test KIND,
1963 or parameter::UNSET if none. */
1965 parameter::type_enum
1966 transition_parameter_type (rtx_test::kind_enum kind)
1968 switch (kind)
1970 case rtx_test::CODE:
1971 return parameter::CODE;
1973 case rtx_test::MODE:
1974 return parameter::MODE;
1976 case rtx_test::REGNO_FIELD:
1977 return parameter::UINT;
1979 case rtx_test::INT_FIELD:
1980 case rtx_test::VECLEN:
1981 case rtx_test::PATTERN:
1982 return parameter::INT;
1984 case rtx_test::WIDE_INT_FIELD:
1985 return parameter::WIDE_INT;
1987 case rtx_test::PEEP2_COUNT:
1988 case rtx_test::VECLEN_GE:
1989 case rtx_test::SAVED_CONST_INT:
1990 case rtx_test::PREDICATE:
1991 case rtx_test::DUPLICATE:
1992 case rtx_test::HAVE_NUM_CLOBBERS:
1993 case rtx_test::C_TEST:
1994 case rtx_test::SET_OP:
1995 case rtx_test::ACCEPT:
1996 return parameter::UNSET;
1998 gcc_unreachable ();
2001 /* Initialize the pos_operand fields of each state reachable from S.
2002 If OPERAND_POS[ID] >= 0, the position with id ID is stored in
2003 operands[OPERAND_POS[ID]] on entry to S. */
2005 static void
2006 find_operand_positions (state *s, vec <int> &operand_pos)
2008 for (decision *d = s->first; d; d = d->next)
2010 int this_operand = (d->test.pos ? operand_pos[d->test.pos->id] : -1);
2011 if (this_operand >= 0)
2012 d->test.pos_operand = this_operand;
2013 if (d->test.kind == rtx_test::SET_OP)
2014 operand_pos[d->test.pos->id] = d->test.u.opno;
2015 for (transition *trans = d->first; trans; trans = trans->next)
2016 find_operand_positions (trans->to, operand_pos);
2017 if (d->test.kind == rtx_test::SET_OP)
2018 operand_pos[d->test.pos->id] = this_operand;
2022 /* Statistics about a matching routine. */
2023 struct stats
2025 stats ();
2027 /* The total number of decisions in the routine, excluding trivial
2028 ones that never fail. */
2029 unsigned int num_decisions;
2031 /* The number of non-trivial decisions on the longest path through
2032 the routine, and the return value that contributes most to that
2033 long path. */
2034 unsigned int longest_path;
2035 int longest_path_code;
2037 /* The maximum number of times that a single call to the routine
2038 can backtrack, and the value returned at the end of that path.
2039 "Backtracking" here means failing one decision in state and
2040 going onto to the next. */
2041 unsigned int longest_backtrack;
2042 int longest_backtrack_code;
2045 stats::stats ()
2046 : num_decisions (0), longest_path (0), longest_path_code (-1),
2047 longest_backtrack (0), longest_backtrack_code (-1) {}
2049 /* Return statistics about S. */
2051 static stats
2052 get_stats (state *s)
2054 stats for_s;
2055 unsigned int longest_path = 0;
2056 for (decision *d = s->first; d; d = d->next)
2058 /* Work out the statistics for D. */
2059 stats for_d;
2060 for (transition *trans = d->first; trans; trans = trans->next)
2062 stats for_trans = get_stats (trans->to);
2063 for_d.num_decisions += for_trans.num_decisions;
2064 /* Each transition is mutually-exclusive, so just pick the
2065 longest of the individual paths. */
2066 if (for_d.longest_path <= for_trans.longest_path)
2068 for_d.longest_path = for_trans.longest_path;
2069 for_d.longest_path_code = for_trans.longest_path_code;
2071 /* Likewise for backtracking. */
2072 if (for_d.longest_backtrack <= for_trans.longest_backtrack)
2074 for_d.longest_backtrack = for_trans.longest_backtrack;
2075 for_d.longest_backtrack_code = for_trans.longest_backtrack_code;
2079 /* Account for D's test in its statistics. */
2080 if (!d->test.single_outcome_p ())
2082 for_d.num_decisions += 1;
2083 for_d.longest_path += 1;
2085 if (d->test.kind == rtx_test::ACCEPT)
2087 for_d.longest_path_code = d->test.u.acceptance.u.full.code;
2088 for_d.longest_backtrack_code = d->test.u.acceptance.u.full.code;
2091 /* Keep a running count of the number of backtracks. */
2092 if (d->prev)
2093 for_s.longest_backtrack += 1;
2095 /* Accumulate D's statistics into S's. */
2096 for_s.num_decisions += for_d.num_decisions;
2097 for_s.longest_path += for_d.longest_path;
2098 for_s.longest_backtrack += for_d.longest_backtrack;
2100 /* Use the code from the decision with the longest individual path,
2101 since that's more likely to be useful if trying to make the
2102 path shorter. In the event of a tie, pick the later decision,
2103 since that's closer to the end of the path. */
2104 if (longest_path <= for_d.longest_path)
2106 longest_path = for_d.longest_path;
2107 for_s.longest_path_code = for_d.longest_path_code;
2110 /* Later decisions in a state are necessarily in a longer backtrack
2111 than earlier decisions. */
2112 for_s.longest_backtrack_code = for_d.longest_backtrack_code;
2114 return for_s;
2117 /* Optimize ROOT. Use TYPE to describe ROOT in status messages. */
2119 static void
2120 optimize_subroutine_group (const char *type, state *root)
2122 /* Remove optional transitions that turned out not to be worthwhile. */
2123 if (collapse_optional_decisions_p)
2124 collapse_optional_decisions (root);
2126 /* Try to remove duplicated tests and to rearrange tests into a more
2127 logical order. */
2128 if (cse_tests_p)
2130 known_conditions kc;
2131 kc.position_tests.safe_grow_cleared (num_positions);
2132 kc.set_operands.safe_grow_cleared (num_operands);
2133 kc.peep2_count = 1;
2134 cse_tests (&root_pos, root, &kc);
2137 /* Try to simplify two or more tests into one. */
2138 if (simplify_tests_p)
2139 simplify_tests (root);
2141 /* Try to use operands[] instead of xN variables. */
2142 if (use_operand_variables_p)
2144 auto_vec <int> operand_pos (num_positions);
2145 for (unsigned int i = 0; i < num_positions; ++i)
2146 operand_pos.quick_push (-1);
2147 find_operand_positions (root, operand_pos);
2150 /* Print a summary of the new state. */
2151 stats st = get_stats (root);
2152 fprintf (stderr, "Statistics for %s:\n", type);
2153 fprintf (stderr, " Number of decisions: %6d\n", st.num_decisions);
2154 fprintf (stderr, " longest path: %6d (code: %6d)\n",
2155 st.longest_path, st.longest_path_code);
2156 fprintf (stderr, " longest backtrack: %6d (code: %6d)\n",
2157 st.longest_backtrack, st.longest_backtrack_code);
2160 struct merge_pattern_info;
2162 /* Represents a transition from one pattern to another. */
2163 struct merge_pattern_transition
2165 merge_pattern_transition (merge_pattern_info *);
2167 /* The target pattern. */
2168 merge_pattern_info *to;
2170 /* The parameters that the source pattern passes to the target pattern.
2171 "parameter (TYPE, true, I)" represents parameter I of the source
2172 pattern. */
2173 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2176 merge_pattern_transition::merge_pattern_transition (merge_pattern_info *to_in)
2177 : to (to_in)
2181 /* Represents a pattern that can might match several states. The pattern
2182 may replace parts of the test with a parameter value. It may also
2183 replace transition labels with parameters. */
2184 struct merge_pattern_info
2186 merge_pattern_info (unsigned int);
2188 /* If PARAM_TEST_P, the state's singleton test should be generalized
2189 to use the runtime value of PARAMS[PARAM_TEST]. */
2190 unsigned int param_test : 8;
2192 /* If PARAM_TRANSITION_P, the state's single transition label should
2193 be replaced by the runtime value of PARAMS[PARAM_TRANSITION]. */
2194 unsigned int param_transition : 8;
2196 /* True if we have decided to generalize the root decision's test,
2197 as per PARAM_TEST. */
2198 unsigned int param_test_p : 1;
2200 /* Likewise for the root decision's transition, as per PARAM_TRANSITION. */
2201 unsigned int param_transition_p : 1;
2203 /* True if the contents of the structure are completely filled in. */
2204 unsigned int complete_p : 1;
2206 /* The number of pseudo-statements in the pattern. Used to decide
2207 whether it's big enough to break out into a subroutine. */
2208 unsigned int num_statements;
2210 /* The number of states that use this pattern. */
2211 unsigned int num_users;
2213 /* The number of distinct success values that the pattern returns. */
2214 unsigned int num_results;
2216 /* This array has one element for each runtime parameter to the pattern.
2217 PARAMS[I] gives the default value of parameter I, which is always
2218 constant.
2220 These default parameters are used in cases where we match the
2221 pattern against some state S1, then add more parameters while
2222 matching against some state S2. S1 is then left passing fewer
2223 parameters than S2. The array gives us enough informatino to
2224 construct a full parameter list for S1 (see update_parameters).
2226 If we decide to create a subroutine for this pattern,
2227 PARAMS[I].type determines the C type of parameter I. */
2228 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2230 /* All states that match this pattern must have the same number of
2231 transitions. TRANSITIONS[I] describes the subpattern for transition
2232 number I; it is null if transition I represents a successful return
2233 from the pattern. */
2234 auto_vec <merge_pattern_transition *, 1> transitions;
2236 /* The routine associated with the pattern, or null if we haven't generated
2237 one yet. */
2238 pattern_routine *routine;
2241 merge_pattern_info::merge_pattern_info (unsigned int num_transitions)
2242 : param_test (0),
2243 param_transition (0),
2244 param_test_p (false),
2245 param_transition_p (false),
2246 complete_p (false),
2247 num_statements (0),
2248 num_users (0),
2249 num_results (0),
2250 routine (0)
2252 transitions.safe_grow_cleared (num_transitions);
2255 /* Describes one way of matching a particular state to a particular
2256 pattern. */
2257 struct merge_state_result
2259 merge_state_result (merge_pattern_info *, position *, merge_state_result *);
2261 /* A pattern that matches the state. */
2262 merge_pattern_info *pattern;
2264 /* If we decide to use this match and create a subroutine for PATTERN,
2265 the state should pass the rtx at position ROOT to the pattern's
2266 rtx parameter. A null root means that the pattern doesn't need
2267 an rtx parameter; all the rtxes it matches come from elsewhere. */
2268 position *root;
2270 /* The parameters that should be passed to PATTERN for this state.
2271 If the array is shorter than PATTERN->params, the missing entries
2272 should be taken from the corresponding element of PATTERN->params. */
2273 auto_vec <parameter, MAX_PATTERN_PARAMS> params;
2275 /* An earlier match for the same state, or null if none. Patterns
2276 matched by earlier entries are smaller than PATTERN. */
2277 merge_state_result *prev;
2280 merge_state_result::merge_state_result (merge_pattern_info *pattern_in,
2281 position *root_in,
2282 merge_state_result *prev_in)
2283 : pattern (pattern_in), root (root_in), prev (prev_in)
2286 /* Information about a state, used while trying to match it against
2287 a pattern. */
2288 struct merge_state_info
2290 merge_state_info (state *);
2292 /* The state itself. */
2293 state *s;
2295 /* Index I gives information about the target of transition I. */
2296 merge_state_info *to_states;
2298 /* The number of transitions in S. */
2299 unsigned int num_transitions;
2301 /* True if the state has been deleted in favor of a call to a
2302 pattern routine. */
2303 bool merged_p;
2305 /* The previous state that might be a merge candidate for S, or null
2306 if no previous states could be merged with S. */
2307 merge_state_info *prev_same_test;
2309 /* A list of pattern matches for this state. */
2310 merge_state_result *res;
2313 merge_state_info::merge_state_info (state *s_in)
2314 : s (s_in),
2315 to_states (0),
2316 num_transitions (0),
2317 merged_p (false),
2318 prev_same_test (0),
2319 res (0) {}
2321 /* True if PAT would be useful as a subroutine. */
2323 static bool
2324 useful_pattern_p (merge_pattern_info *pat)
2326 return pat->num_statements >= MIN_COMBINE_COST;
2329 /* PAT2 is a subpattern of PAT1. Return true if PAT2 should be inlined
2330 into PAT1's C routine. */
2332 static bool
2333 same_pattern_p (merge_pattern_info *pat1, merge_pattern_info *pat2)
2335 return pat1->num_users == pat2->num_users || !useful_pattern_p (pat2);
2338 /* PAT was previously matched against SINFO based on tentative matches
2339 for the target states of SINFO's state. Return true if the match
2340 still holds; that is, if the target states of SINFO's state still
2341 match the corresponding transitions of PAT. */
2343 static bool
2344 valid_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2346 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2347 if (merge_pattern_transition *ptrans = pat->transitions[j])
2349 merge_state_result *to_res = sinfo->to_states[j].res;
2350 if (!to_res || to_res->pattern != ptrans->to)
2351 return false;
2353 return true;
2356 /* Remove any matches that are no longer valid from the head of SINFO's
2357 list of matches. */
2359 static void
2360 prune_invalid_results (merge_state_info *sinfo)
2362 while (sinfo->res && !valid_result_p (sinfo->res->pattern, sinfo))
2364 sinfo->res = sinfo->res->prev;
2365 gcc_assert (sinfo->res);
2369 /* Return true if PAT represents the biggest posssible match for SINFO;
2370 that is, if the next action of SINFO's state on return from PAT will
2371 be something that cannot be merged with any other state. */
2373 static bool
2374 complete_result_p (merge_pattern_info *pat, merge_state_info *sinfo)
2376 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
2377 if (sinfo->to_states[j].res && !pat->transitions[j])
2378 return false;
2379 return true;
2382 /* Update TO for any parameters that have been added to FROM since TO
2383 was last set. The extra parameters in FROM will be constants or
2384 instructions to duplicate earlier parameters. */
2386 static void
2387 update_parameters (vec <parameter> &to, const vec <parameter> &from)
2389 for (unsigned int i = to.length (); i < from.length (); ++i)
2390 to.quick_push (from[i]);
2393 /* Return true if A and B can be tested by a single test. If the test
2394 can be parameterised, store the parameter value for A in *PARAMA and
2395 the parameter value for B in *PARAMB, otherwise leave PARAMA and
2396 PARAMB alone. */
2398 static bool
2399 compatible_tests_p (const rtx_test &a, const rtx_test &b,
2400 parameter *parama, parameter *paramb)
2402 if (a.kind != b.kind)
2403 return false;
2404 switch (a.kind)
2406 case rtx_test::PREDICATE:
2407 if (a.u.predicate.data != b.u.predicate.data)
2408 return false;
2409 *parama = parameter (parameter::MODE, false, a.u.predicate.mode);
2410 *paramb = parameter (parameter::MODE, false, b.u.predicate.mode);
2411 return true;
2413 case rtx_test::SAVED_CONST_INT:
2414 *parama = parameter (parameter::INT, false, a.u.integer.value);
2415 *paramb = parameter (parameter::INT, false, b.u.integer.value);
2416 return true;
2418 default:
2419 return a == b;
2423 /* PARAMS is an array of the parameters that a state is going to pass
2424 to a pattern routine. It is still incomplete; index I has a kind of
2425 parameter::UNSET if we don't yet know what the state will pass
2426 as parameter I. Try to make parameter ID equal VALUE, returning
2427 true on success. */
2429 static bool
2430 set_parameter (vec <parameter> &params, unsigned int id,
2431 const parameter &value)
2433 if (params[id].type == parameter::UNSET)
2435 if (force_unique_params_p)
2436 for (unsigned int i = 0; i < params.length (); ++i)
2437 if (params[i] == value)
2438 return false;
2439 params[id] = value;
2440 return true;
2442 return params[id] == value;
2445 /* PARAMS2 is the "params" array for a pattern and PARAMS1 is the
2446 set of parameters that a particular state is going to pass to
2447 that pattern.
2449 Try to extend PARAMS1 and PARAMS2 so that there is a parameter
2450 that is equal to PARAM1 for the state and has a default value of
2451 PARAM2. Parameters beginning at START were added as part of the
2452 same match and so may be reused. */
2454 static bool
2455 add_parameter (vec <parameter> &params1, vec <parameter> &params2,
2456 const parameter &param1, const parameter &param2,
2457 unsigned int start, unsigned int *res)
2459 gcc_assert (params1.length () == params2.length ());
2460 gcc_assert (!param1.is_param && !param2.is_param);
2462 for (unsigned int i = start; i < params2.length (); ++i)
2463 if (params1[i] == param1 && params2[i] == param2)
2465 *res = i;
2466 return true;
2469 if (force_unique_params_p)
2470 for (unsigned int i = 0; i < params2.length (); ++i)
2471 if (params1[i] == param1 || params2[i] == param2)
2472 return false;
2474 if (params2.length () >= MAX_PATTERN_PARAMS)
2475 return false;
2477 *res = params2.length ();
2478 params1.quick_push (param1);
2479 params2.quick_push (param2);
2480 return true;
2483 /* If *ROOTA is nonnull, return true if the same sequence of steps are
2484 required to reach A from *ROOTA as to reach B from ROOTB. If *ROOTA
2485 is null, update it if necessary in order to make the condition hold. */
2487 static bool
2488 merge_relative_positions (position **roota, position *a,
2489 position *rootb, position *b)
2491 if (!relative_patterns_p)
2493 if (a != b)
2494 return false;
2495 if (!*roota)
2497 *roota = rootb;
2498 return true;
2500 return *roota == rootb;
2502 /* If B does not belong to the same instruction as ROOTB, we don't
2503 start with ROOTB but instead start with a call to peep2_next_insn.
2504 In that case the sequences for B and A are identical iff B and A
2505 are themselves identical. */
2506 if (rootb->insn_id != b->insn_id)
2507 return a == b;
2508 while (rootb != b)
2510 if (!a || b->type != a->type || b->arg != a->arg)
2511 return false;
2512 b = b->base;
2513 a = a->base;
2515 if (!*roota)
2516 *roota = a;
2517 return *roota == a;
2520 /* A hasher of states that treats two states as "equal" if they might be
2521 merged (but trying to be more discriminating than "return true"). */
2522 struct test_pattern_hasher : typed_noop_remove <merge_state_info>
2524 typedef merge_state_info *value_type;
2525 typedef merge_state_info *compare_type;
2526 static inline hashval_t hash (const value_type &);
2527 static inline bool equal (const value_type &, const compare_type &);
2530 hashval_t
2531 test_pattern_hasher::hash (merge_state_info *const &sinfo)
2533 inchash::hash h;
2534 decision *d = sinfo->s->singleton ();
2535 h.add_int (d->test.pos_operand + 1);
2536 if (!relative_patterns_p)
2537 h.add_int (d->test.pos ? d->test.pos->id + 1 : 0);
2538 h.add_int (d->test.kind);
2539 h.add_int (sinfo->num_transitions);
2540 return h.end ();
2543 bool
2544 test_pattern_hasher::equal (merge_state_info *const &sinfo1,
2545 merge_state_info *const &sinfo2)
2547 decision *d1 = sinfo1->s->singleton ();
2548 decision *d2 = sinfo2->s->singleton ();
2549 gcc_assert (d1 && d2);
2551 parameter new_param1, new_param2;
2552 return (d1->test.pos_operand == d2->test.pos_operand
2553 && (relative_patterns_p || d1->test.pos == d2->test.pos)
2554 && compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2)
2555 && sinfo1->num_transitions == sinfo2->num_transitions);
2558 /* Try to make the state described by SINFO1 use the same pattern as the
2559 state described by SINFO2. Return true on success.
2561 SINFO1 and SINFO2 are known to have the same hash value. */
2563 static bool
2564 merge_patterns (merge_state_info *sinfo1, merge_state_info *sinfo2)
2566 merge_state_result *res2 = sinfo2->res;
2567 merge_pattern_info *pat = res2->pattern;
2569 /* Write to temporary arrays while matching, in case we have to abort
2570 half way through. */
2571 auto_vec <parameter, MAX_PATTERN_PARAMS> params1;
2572 auto_vec <parameter, MAX_PATTERN_PARAMS> params2;
2573 params1.quick_grow_cleared (pat->params.length ());
2574 params2.splice (pat->params);
2575 unsigned int start_param = params2.length ();
2577 /* An array for recording changes to PAT->transitions[?].params.
2578 All changes involve replacing a constant parameter with some
2579 PAT->params[N], where N is the second element of the pending_param. */
2580 typedef std::pair <parameter *, unsigned int> pending_param;
2581 auto_vec <pending_param, 32> pending_params;
2583 decision *d1 = sinfo1->s->singleton ();
2584 decision *d2 = sinfo2->s->singleton ();
2585 gcc_assert (d1 && d2);
2587 /* If D2 tests a position, SINFO1's root relative to D1 is the same
2588 as SINFO2's root relative to D2. */
2589 position *root1 = 0;
2590 position *root2 = res2->root;
2591 if (d2->test.pos_operand < 0
2592 && d1->test.pos
2593 && !merge_relative_positions (&root1, d1->test.pos,
2594 root2, d2->test.pos))
2595 return false;
2597 /* Check whether the patterns have the same shape. */
2598 unsigned int num_transitions = sinfo1->num_transitions;
2599 gcc_assert (num_transitions == sinfo2->num_transitions);
2600 for (unsigned int i = 0; i < num_transitions; ++i)
2601 if (merge_pattern_transition *ptrans = pat->transitions[i])
2603 merge_state_result *to1_res = sinfo1->to_states[i].res;
2604 merge_state_result *to2_res = sinfo2->to_states[i].res;
2605 merge_pattern_info *to_pat = ptrans->to;
2606 gcc_assert (to2_res && to2_res->pattern == to_pat);
2607 if (!to1_res || to1_res->pattern != to_pat)
2608 return false;
2609 if (to2_res->root
2610 && !merge_relative_positions (&root1, to1_res->root,
2611 root2, to2_res->root))
2612 return false;
2613 /* Match the parameters that TO1_RES passes to TO_PAT with the
2614 parameters that PAT passes to TO_PAT. */
2615 update_parameters (to1_res->params, to_pat->params);
2616 for (unsigned int j = 0; j < to1_res->params.length (); ++j)
2618 const parameter &param1 = to1_res->params[j];
2619 const parameter &param2 = ptrans->params[j];
2620 gcc_assert (!param1.is_param);
2621 if (param2.is_param)
2623 if (!set_parameter (params1, param2.value, param1))
2624 return false;
2626 else if (param1 != param2)
2628 unsigned int id;
2629 if (!add_parameter (params1, params2,
2630 param1, param2, start_param, &id))
2631 return false;
2632 /* Record that PAT should now pass parameter ID to TO_PAT,
2633 instead of the current contents of *PARAM2. We only
2634 make the change if the rest of the match succeeds. */
2635 pending_params.safe_push
2636 (pending_param (&ptrans->params[j], id));
2641 unsigned int param_test = pat->param_test;
2642 unsigned int param_transition = pat->param_transition;
2643 bool param_test_p = pat->param_test_p;
2644 bool param_transition_p = pat->param_transition_p;
2646 /* If the tests don't match exactly, try to parameterize them. */
2647 parameter new_param1, new_param2;
2648 if (!compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2))
2649 gcc_unreachable ();
2650 if (new_param1.type != parameter::UNSET)
2652 /* If the test has not already been parameterized, all existing
2653 matches use constant NEW_PARAM2. */
2654 if (param_test_p)
2656 if (!set_parameter (params1, param_test, new_param1))
2657 return false;
2659 else if (new_param1 != new_param2)
2661 if (!add_parameter (params1, params2, new_param1, new_param2,
2662 start_param, &param_test))
2663 return false;
2664 param_test_p = true;
2668 /* Match the transitions. */
2669 transition *trans1 = d1->first;
2670 transition *trans2 = d2->first;
2671 for (unsigned int i = 0; i < num_transitions; ++i)
2673 if (param_transition_p || trans1->labels != trans2->labels)
2675 /* We can only generalize a single transition with a single
2676 label. */
2677 if (num_transitions != 1
2678 || trans1->labels.length () != 1
2679 || trans2->labels.length () != 1)
2680 return false;
2682 /* Although we can match wide-int fields, in practice it leads
2683 to some odd results for const_vectors. We end up
2684 parameterizing the first N const_ints of the vector
2685 and then (once we reach the maximum number of parameters)
2686 we go on to match the other elements exactly. */
2687 if (d1->test.kind == rtx_test::WIDE_INT_FIELD)
2688 return false;
2690 /* See whether the label has a generalizable type. */
2691 parameter::type_enum param_type
2692 = transition_parameter_type (d1->test.kind);
2693 if (param_type == parameter::UNSET)
2694 return false;
2696 /* Match the labels using parameters. */
2697 new_param1 = parameter (param_type, false, trans1->labels[0]);
2698 if (param_transition_p)
2700 if (!set_parameter (params1, param_transition, new_param1))
2701 return false;
2703 else
2705 new_param2 = parameter (param_type, false, trans2->labels[0]);
2706 if (!add_parameter (params1, params2, new_param1, new_param2,
2707 start_param, &param_transition))
2708 return false;
2709 param_transition_p = true;
2712 trans1 = trans1->next;
2713 trans2 = trans2->next;
2716 /* Set any unset parameters to their default values. This occurs if some
2717 other state needed something to be parameterized in order to match SINFO2,
2718 but SINFO1 on its own does not. */
2719 for (unsigned int i = 0; i < params1.length (); ++i)
2720 if (params1[i].type == parameter::UNSET)
2721 params1[i] = params2[i];
2723 /* The match was successful. Commit all pending changes to PAT. */
2724 update_parameters (pat->params, params2);
2726 pending_param *pp;
2727 unsigned int i;
2728 FOR_EACH_VEC_ELT (pending_params, i, pp)
2729 *pp->first = parameter (pp->first->type, true, pp->second);
2731 pat->param_test = param_test;
2732 pat->param_transition = param_transition;
2733 pat->param_test_p = param_test_p;
2734 pat->param_transition_p = param_transition_p;
2736 /* Record the match of SINFO1. */
2737 merge_state_result *new_res1 = new merge_state_result (pat, root1,
2738 sinfo1->res);
2739 new_res1->params.splice (params1);
2740 sinfo1->res = new_res1;
2741 return true;
2744 /* The number of states that were removed by calling pattern routines. */
2745 static unsigned int pattern_use_states;
2747 /* The number of states used while defining pattern routines. */
2748 static unsigned int pattern_def_states;
2750 /* Information used while constructing a use or definition of a pattern
2751 routine. */
2752 struct create_pattern_info
2754 /* The routine itself. */
2755 pattern_routine *routine;
2757 /* The first unclaimed return value for this particular use or definition.
2758 We walk the substates of uses and definitions in the same order
2759 so each return value always refers to the same position within
2760 the pattern. */
2761 unsigned int next_result;
2764 static void populate_pattern_routine (create_pattern_info *,
2765 merge_state_info *, state *,
2766 const vec <parameter> &);
2768 /* SINFO matches a pattern for which we've decided to create a C routine.
2769 Return a decision that performs a call to the pattern routine,
2770 but leave the caller to add the transitions to it. Initialize CPI
2771 for this purpose. Also create a definition for the pattern routine,
2772 if it doesn't already have one.
2774 PARAMS are the parameters that SINFO passes to its pattern. */
2776 static decision *
2777 init_pattern_use (create_pattern_info *cpi, merge_state_info *sinfo,
2778 const vec <parameter> &params)
2780 state *s = sinfo->s;
2781 merge_state_result *res = sinfo->res;
2782 merge_pattern_info *pat = res->pattern;
2783 cpi->routine = pat->routine;
2784 if (!cpi->routine)
2786 /* We haven't defined the pattern routine yet, so create
2787 a definition now. */
2788 pattern_routine *routine = new pattern_routine;
2789 pat->routine = routine;
2790 cpi->routine = routine;
2791 routine->s = new state;
2792 routine->insn_p = false;
2793 routine->pnum_clobbers_p = false;
2795 /* Create an "idempotent" mapping of parameter I to parameter I.
2796 Also record the C type of each parameter to the routine. */
2797 auto_vec <parameter, MAX_PATTERN_PARAMS> def_params;
2798 for (unsigned int i = 0; i < pat->params.length (); ++i)
2800 def_params.quick_push (parameter (pat->params[i].type, true, i));
2801 routine->param_types.quick_push (pat->params[i].type);
2804 /* Any of the states that match the pattern could be used to
2805 create the routine definition. We might as well use SINFO
2806 since it's already to hand. This means that all positions
2807 in the definition will be relative to RES->root. */
2808 routine->pos = res->root;
2809 cpi->next_result = 0;
2810 populate_pattern_routine (cpi, sinfo, routine->s, def_params);
2811 gcc_assert (cpi->next_result == pat->num_results);
2813 /* Add the routine to the global list, after the subroutines
2814 that it calls. */
2815 routine->pattern_id = patterns.length ();
2816 patterns.safe_push (routine);
2819 /* Create a decision to call the routine, passing PARAMS to it. */
2820 pattern_use *use = new pattern_use;
2821 use->routine = pat->routine;
2822 use->params.splice (params);
2823 decision *d = new decision (rtx_test::pattern (res->root, use));
2825 /* If the original decision could use an element of operands[] instead
2826 of an rtx variable, try to transfer it to the new decision. */
2827 if (s->first->test.pos && res->root == s->first->test.pos)
2828 d->test.pos_operand = s->first->test.pos_operand;
2830 cpi->next_result = 0;
2831 return d;
2834 /* Make S return the next unclaimed pattern routine result for CPI. */
2836 static void
2837 add_pattern_acceptance (create_pattern_info *cpi, state *s)
2839 acceptance_type acceptance;
2840 acceptance.type = SUBPATTERN;
2841 acceptance.partial_p = false;
2842 acceptance.u.full.code = cpi->next_result;
2843 add_decision (s, rtx_test::accept (acceptance), true, false);
2844 cpi->next_result += 1;
2847 /* Initialize new empty state NEWS so that it implements SINFO's pattern
2848 (here referred to as "P"). P may be the top level of a pattern routine
2849 or a subpattern that should be inlined into its parent pattern's routine
2850 (as per same_pattern_p). The choice of SINFO for a top-level pattern is
2851 arbitrary; it could be any of the states that use P. The choice for
2852 subpatterns follows the choice for the parent pattern.
2854 PARAMS gives the value of each parameter to P in terms of the parameters
2855 to the top-level pattern. If P itself is the top level pattern, PARAMS[I]
2856 is always "parameter (TYPE, true, I)". */
2858 static void
2859 populate_pattern_routine (create_pattern_info *cpi, merge_state_info *sinfo,
2860 state *news, const vec <parameter> &params)
2862 pattern_def_states += 1;
2864 decision *d = sinfo->s->singleton ();
2865 merge_pattern_info *pat = sinfo->res->pattern;
2866 pattern_routine *routine = cpi->routine;
2868 /* Create a copy of D's test for the pattern routine and generalize it
2869 as appropriate. */
2870 decision *newd = new decision (d->test);
2871 gcc_assert (newd->test.pos_operand >= 0
2872 || !newd->test.pos
2873 || common_position (newd->test.pos,
2874 routine->pos) == routine->pos);
2875 if (pat->param_test_p)
2877 const parameter &param = params[pat->param_test];
2878 switch (newd->test.kind)
2880 case rtx_test::PREDICATE:
2881 newd->test.u.predicate.mode_is_param = param.is_param;
2882 newd->test.u.predicate.mode = param.value;
2883 break;
2885 case rtx_test::SAVED_CONST_INT:
2886 newd->test.u.integer.is_param = param.is_param;
2887 newd->test.u.integer.value = param.value;
2888 break;
2890 default:
2891 gcc_unreachable ();
2892 break;
2895 if (d->test.kind == rtx_test::C_TEST)
2896 routine->insn_p = true;
2897 else if (d->test.kind == rtx_test::HAVE_NUM_CLOBBERS)
2898 routine->pnum_clobbers_p = true;
2899 news->push_back (newd);
2901 /* Fill in the transitions of NEWD. */
2902 unsigned int i = 0;
2903 for (transition *trans = d->first; trans; trans = trans->next)
2905 /* Create a new state to act as the target of the new transition. */
2906 state *to_news = new state;
2907 if (merge_pattern_transition *ptrans = pat->transitions[i])
2909 /* The pattern hasn't finished matching yet. Get the target
2910 pattern and the corresponding target state of SINFO. */
2911 merge_pattern_info *to_pat = ptrans->to;
2912 merge_state_info *to = sinfo->to_states + i;
2913 gcc_assert (to->res->pattern == to_pat);
2914 gcc_assert (ptrans->params.length () == to_pat->params.length ());
2916 /* Express the parameters to TO_PAT in terms of the parameters
2917 to the top-level pattern. */
2918 auto_vec <parameter, MAX_PATTERN_PARAMS> to_params;
2919 for (unsigned int j = 0; j < ptrans->params.length (); ++j)
2921 const parameter &param = ptrans->params[j];
2922 to_params.quick_push (param.is_param
2923 ? params[param.value]
2924 : param);
2927 if (same_pattern_p (pat, to_pat))
2928 /* TO_PAT is part of the current routine, so just recurse. */
2929 populate_pattern_routine (cpi, to, to_news, to_params);
2930 else
2932 /* TO_PAT should be matched by calling a separate routine. */
2933 create_pattern_info sub_cpi;
2934 decision *subd = init_pattern_use (&sub_cpi, to, to_params);
2935 routine->insn_p |= sub_cpi.routine->insn_p;
2936 routine->pnum_clobbers_p |= sub_cpi.routine->pnum_clobbers_p;
2938 /* Add the pattern routine call to the new target state. */
2939 to_news->push_back (subd);
2941 /* Add a transition for each successful call result. */
2942 for (unsigned int j = 0; j < to_pat->num_results; ++j)
2944 state *res = new state;
2945 add_pattern_acceptance (cpi, res);
2946 subd->push_back (new transition (j, res, false));
2950 else
2951 /* This transition corresponds to a successful match. */
2952 add_pattern_acceptance (cpi, to_news);
2954 /* Create the transition itself, generalizing as necessary. */
2955 transition *new_trans = new transition (trans->labels, to_news,
2956 trans->optional);
2957 if (pat->param_transition_p)
2959 const parameter &param = params[pat->param_transition];
2960 new_trans->is_param = param.is_param;
2961 new_trans->labels[0] = param.value;
2963 newd->push_back (new_trans);
2964 i += 1;
2968 /* USE is a decision that calls a pattern routine and SINFO is part of the
2969 original state tree that the call is supposed to replace. Add the
2970 transitions for SINFO and its substates to USE. */
2972 static void
2973 populate_pattern_use (create_pattern_info *cpi, decision *use,
2974 merge_state_info *sinfo)
2976 pattern_use_states += 1;
2977 gcc_assert (!sinfo->merged_p);
2978 sinfo->merged_p = true;
2979 merge_state_result *res = sinfo->res;
2980 merge_pattern_info *pat = res->pattern;
2981 decision *d = sinfo->s->singleton ();
2982 unsigned int i = 0;
2983 for (transition *trans = d->first; trans; trans = trans->next)
2985 if (pat->transitions[i])
2986 /* The target state is also part of the pattern. */
2987 populate_pattern_use (cpi, use, sinfo->to_states + i);
2988 else
2990 /* The transition corresponds to a successful return from the
2991 pattern routine. */
2992 use->push_back (new transition (cpi->next_result, trans->to, false));
2993 cpi->next_result += 1;
2995 i += 1;
2999 /* We have decided to replace SINFO's state with a call to a pattern
3000 routine. Make the change, creating a definition of the pattern routine
3001 if it doesn't have one already. */
3003 static void
3004 use_pattern (merge_state_info *sinfo)
3006 merge_state_result *res = sinfo->res;
3007 merge_pattern_info *pat = res->pattern;
3008 state *s = sinfo->s;
3010 /* The pattern may have acquired new parameters after it was matched
3011 against SINFO. Update the parameters that SINFO passes accordingly. */
3012 update_parameters (res->params, pat->params);
3014 create_pattern_info cpi;
3015 decision *d = init_pattern_use (&cpi, sinfo, res->params);
3016 populate_pattern_use (&cpi, d, sinfo);
3017 s->release ();
3018 s->push_back (d);
3021 /* Look through the state trees in STATES for common patterns and
3022 split them into subroutines. */
3024 static void
3025 split_out_patterns (vec <merge_state_info> &states)
3027 unsigned int first_transition = states.length ();
3028 hash_table <test_pattern_hasher> hashtab (128);
3029 /* Stage 1: Create an order in which parent states come before their child
3030 states and in which sibling states are at consecutive locations.
3031 Having consecutive sibling states allows merge_state_info to have
3032 a single to_states pointer. */
3033 for (unsigned int i = 0; i < states.length (); ++i)
3034 for (decision *d = states[i].s->first; d; d = d->next)
3035 for (transition *trans = d->first; trans; trans = trans->next)
3037 states.safe_push (trans->to);
3038 states[i].num_transitions += 1;
3040 /* Stage 2: Now that the addresses are stable, set up the to_states
3041 pointers. Look for states that might be merged and enter them
3042 into the hash table. */
3043 for (unsigned int i = 0; i < states.length (); ++i)
3045 merge_state_info *sinfo = &states[i];
3046 if (sinfo->num_transitions)
3048 sinfo->to_states = &states[first_transition];
3049 first_transition += sinfo->num_transitions;
3051 /* For simplicity, we only try to merge states that have a single
3052 decision. This is in any case the best we can do for peephole2,
3053 since whether a peephole2 ACCEPT succeeds or not depends on the
3054 specific peephole2 pattern (which is unique to each ACCEPT
3055 and so couldn't be shared between states). */
3056 if (decision *d = sinfo->s->singleton ())
3057 /* ACCEPT states are unique, so don't even try to merge them. */
3058 if (d->test.kind != rtx_test::ACCEPT
3059 && (pattern_have_num_clobbers_p
3060 || d->test.kind != rtx_test::HAVE_NUM_CLOBBERS)
3061 && (pattern_c_test_p
3062 || d->test.kind != rtx_test::C_TEST))
3064 merge_state_info **slot = hashtab.find_slot (sinfo, INSERT);
3065 sinfo->prev_same_test = *slot;
3066 *slot = sinfo;
3069 /* Stage 3: Walk backwards through the list of states and try to merge
3070 them. This is a greedy, bottom-up match; parent nodes can only start
3071 a new leaf pattern if they fail to match when combined with all child
3072 nodes that have matching patterns.
3074 For each state we keep a list of potential matches, with each
3075 potential match being larger (and deeper) than the next match in
3076 the list. The final element in the list is a leaf pattern that
3077 matches just a single state.
3079 Each candidate pattern created in this loop is unique -- it won't
3080 have been seen by an earlier iteration. We try to match each pattern
3081 with every state that appears earlier in STATES.
3083 Because the patterns created in the loop are unique, any state
3084 that already has a match must have a final potential match that
3085 is different from any new leaf pattern. Therefore, when matching
3086 leaf patterns, we need only consider states whose list of matches
3087 is empty.
3089 The non-leaf patterns that we try are as deep as possible
3090 and are an extension of the state's previous best candidate match (PB).
3091 We need only consider states whose current potential match is also PB;
3092 any states that don't match as much as PB cannnot match the new pattern,
3093 while any states that already match more than PB must be different from
3094 the new pattern. */
3095 for (unsigned int i2 = states.length (); i2-- > 0; )
3097 merge_state_info *sinfo2 = &states[i2];
3099 /* Enforce the bottom-upness of the match: remove matches with later
3100 states if SINFO2's child states ended up finding a better match. */
3101 prune_invalid_results (sinfo2);
3103 /* Do nothing if the state doesn't match a later one and if there are
3104 no earlier states it could match. */
3105 if (!sinfo2->res && !sinfo2->prev_same_test)
3106 continue;
3108 merge_state_result *res2 = sinfo2->res;
3109 decision *d2 = sinfo2->s->singleton ();
3110 position *root2 = (d2->test.pos_operand < 0 ? d2->test.pos : 0);
3111 unsigned int num_transitions = sinfo2->num_transitions;
3113 /* If RES2 is null then SINFO2's test in isolation has not been seen
3114 before. First try matching that on its own. */
3115 if (!res2)
3117 merge_pattern_info *new_pat
3118 = new merge_pattern_info (num_transitions);
3119 merge_state_result *new_res2
3120 = new merge_state_result (new_pat, root2, res2);
3121 sinfo2->res = new_res2;
3123 new_pat->num_statements = !d2->test.single_outcome_p ();
3124 new_pat->num_results = num_transitions;
3125 bool matched_p = false;
3126 /* Look for states that don't currently match anything but
3127 can be made to match SINFO2 on its own. */
3128 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3129 sinfo1 = sinfo1->prev_same_test)
3130 if (!sinfo1->res && merge_patterns (sinfo1, sinfo2))
3131 matched_p = true;
3132 if (!matched_p)
3134 /* No other states match. */
3135 sinfo2->res = res2;
3136 delete new_pat;
3137 delete new_res2;
3138 continue;
3140 else
3141 res2 = new_res2;
3144 /* Keep the existing pattern if it's as good as anything we'd
3145 create for SINFO2. */
3146 if (complete_result_p (res2->pattern, sinfo2))
3148 res2->pattern->num_users += 1;
3149 continue;
3152 /* Create a new pattern for SINFO2. */
3153 merge_pattern_info *new_pat = new merge_pattern_info (num_transitions);
3154 merge_state_result *new_res2
3155 = new merge_state_result (new_pat, root2, res2);
3156 sinfo2->res = new_res2;
3158 /* Fill in details about the pattern. */
3159 new_pat->num_statements = !d2->test.single_outcome_p ();
3160 new_pat->num_results = 0;
3161 for (unsigned int j = 0; j < num_transitions; ++j)
3162 if (merge_state_result *to_res = sinfo2->to_states[j].res)
3164 /* Count the target state as part of this pattern.
3165 First update the root position so that it can reach
3166 the target state's root. */
3167 if (to_res->root)
3169 if (new_res2->root)
3170 new_res2->root = common_position (new_res2->root,
3171 to_res->root);
3172 else
3173 new_res2->root = to_res->root;
3175 merge_pattern_info *to_pat = to_res->pattern;
3176 merge_pattern_transition *ptrans
3177 = new merge_pattern_transition (to_pat);
3179 /* TO_PAT may have acquired more parameters when matching
3180 states earlier in STATES than TO_RES's, but the list is
3181 now final. Make sure that TO_RES is up to date. */
3182 update_parameters (to_res->params, to_pat->params);
3184 /* Start out by assuming that every user of NEW_PAT will
3185 want to pass the same (constant) parameters as TO_RES. */
3186 update_parameters (ptrans->params, to_res->params);
3188 new_pat->transitions[j] = ptrans;
3189 new_pat->num_statements += to_pat->num_statements;
3190 new_pat->num_results += to_pat->num_results;
3192 else
3193 /* The target state doesn't match anything and so is not part
3194 of the pattern. */
3195 new_pat->num_results += 1;
3197 /* See if any earlier states that match RES2's pattern also match
3198 NEW_PAT. */
3199 bool matched_p = false;
3200 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1;
3201 sinfo1 = sinfo1->prev_same_test)
3203 prune_invalid_results (sinfo1);
3204 if (sinfo1->res
3205 && sinfo1->res->pattern == res2->pattern
3206 && merge_patterns (sinfo1, sinfo2))
3207 matched_p = true;
3209 if (!matched_p)
3211 /* Nothing else matches NEW_PAT, so go back to the previous
3212 pattern (possibly just a single-state one). */
3213 sinfo2->res = res2;
3214 delete new_pat;
3215 delete new_res2;
3217 /* Assume that SINFO2 will use RES. At this point we don't know
3218 whether earlier states that match the same pattern will use
3219 that match or a different one. */
3220 sinfo2->res->pattern->num_users += 1;
3222 /* Step 4: Finalize the choice of pattern for each state, ignoring
3223 patterns that were only used once. Update each pattern's size
3224 so that it doesn't include subpatterns that are going to be split
3225 out into subroutines. */
3226 for (unsigned int i = 0; i < states.length (); ++i)
3228 merge_state_info *sinfo = &states[i];
3229 merge_state_result *res = sinfo->res;
3230 /* Wind past patterns that are only used by SINFO. */
3231 while (res && res->pattern->num_users == 1)
3233 res = res->prev;
3234 sinfo->res = res;
3235 if (res)
3236 res->pattern->num_users += 1;
3238 if (!res)
3239 continue;
3241 /* We have a shared pattern and are now committed to the match. */
3242 merge_pattern_info *pat = res->pattern;
3243 gcc_assert (valid_result_p (pat, sinfo));
3245 if (!pat->complete_p)
3247 /* Look for subpatterns that are going to be split out and remove
3248 them from the number of statements. */
3249 for (unsigned int j = 0; j < sinfo->num_transitions; ++j)
3250 if (merge_pattern_transition *ptrans = pat->transitions[j])
3252 merge_pattern_info *to_pat = ptrans->to;
3253 if (!same_pattern_p (pat, to_pat))
3254 pat->num_statements -= to_pat->num_statements;
3256 pat->complete_p = true;
3259 /* Step 5: Split out the patterns. */
3260 for (unsigned int i = 0; i < states.length (); ++i)
3262 merge_state_info *sinfo = &states[i];
3263 merge_state_result *res = sinfo->res;
3264 if (!sinfo->merged_p && res && useful_pattern_p (res->pattern))
3265 use_pattern (sinfo);
3267 fprintf (stderr, "Shared %d out of %d states by creating %d new states,"
3268 " saving %d\n",
3269 pattern_use_states, states.length (), pattern_def_states,
3270 pattern_use_states - pattern_def_states);
3273 /* Information about a state tree that we're considering splitting into a
3274 subroutine. */
3275 struct state_size
3277 /* The number of pseudo-statements in the state tree. */
3278 unsigned int num_statements;
3280 /* The approximate number of nested "if" and "switch" statements that
3281 would be required if control could fall through to a later state. */
3282 unsigned int depth;
3285 /* Pairs a transition with information about its target state. */
3286 typedef std::pair <transition *, state_size> subroutine_candidate;
3288 /* Sort two subroutine_candidates so that the one with the largest
3289 number of statements comes last. */
3291 static int
3292 subroutine_candidate_cmp (const void *a, const void *b)
3294 return int (((const subroutine_candidate *) a)->second.num_statements
3295 - ((const subroutine_candidate *) b)->second.num_statements);
3298 /* Turn S into a subroutine of type TYPE and add it to PROCS. Return a new
3299 state that performs a subroutine call to S. */
3301 static state *
3302 create_subroutine (routine_type type, state *s, vec <state *> &procs)
3304 procs.safe_push (s);
3305 acceptance_type acceptance;
3306 acceptance.type = type;
3307 acceptance.partial_p = true;
3308 acceptance.u.subroutine_id = procs.length ();
3309 state *news = new state;
3310 add_decision (news, rtx_test::accept (acceptance), true, false);
3311 return news;
3314 /* Walk state tree S, of type TYPE, and look for subtrees that would be
3315 better split into subroutines. Accumulate all such subroutines in PROCS.
3316 Return the size of the new state tree (excluding subroutines). */
3318 static state_size
3319 find_subroutines (routine_type type, state *s, vec <state *> &procs)
3321 auto_vec <subroutine_candidate, 16> candidates;
3322 state_size size;
3323 size.num_statements = 0;
3324 size.depth = 0;
3325 for (decision *d = s->first; d; d = d->next)
3327 if (!d->test.single_outcome_p ())
3328 size.num_statements += 1;
3329 for (transition *trans = d->first; trans; trans = trans->next)
3331 /* Keep chains of simple decisions together if we know that no
3332 change of position is required. We'll output this chain as a
3333 single "if" statement, so it counts as a single nesting level. */
3334 if (d->test.pos && d->if_statement_p ())
3335 for (;;)
3337 decision *newd = trans->to->singleton ();
3338 if (!newd
3339 || (newd->test.pos
3340 && newd->test.pos_operand < 0
3341 && newd->test.pos != d->test.pos)
3342 || !newd->if_statement_p ())
3343 break;
3344 if (!newd->test.single_outcome_p ())
3345 size.num_statements += 1;
3346 trans = newd->singleton ();
3347 if (newd->test.kind == rtx_test::SET_OP
3348 || newd->test.kind == rtx_test::ACCEPT)
3349 break;
3351 /* The target of TRANS is a subroutine candidate. First recurse
3352 on it to see how big it is after subroutines have been
3353 split out. */
3354 state_size to_size = find_subroutines (type, trans->to, procs);
3355 if (d->next && to_size.depth > MAX_DEPTH)
3356 /* Keeping the target state in the same routine would lead
3357 to an excessive nesting of "if" and "switch" statements.
3358 Split it out into a subroutine so that it can use
3359 inverted tests that return early on failure. */
3360 trans->to = create_subroutine (type, trans->to, procs);
3361 else
3363 size.num_statements += to_size.num_statements;
3364 if (to_size.num_statements < MIN_NUM_STATEMENTS)
3365 /* The target state is too small to be worth splitting.
3366 Keep it in the same routine as S. */
3367 size.depth = MAX (size.depth, to_size.depth);
3368 else
3369 /* Assume for now that we'll keep the target state in the
3370 same routine as S, but record it as a subroutine candidate
3371 if S grows too big. */
3372 candidates.safe_push (subroutine_candidate (trans, to_size));
3376 if (size.num_statements > MAX_NUM_STATEMENTS)
3378 /* S is too big. Sort the subroutine candidates so that bigger ones
3379 are nearer the end. */
3380 candidates.qsort (subroutine_candidate_cmp);
3381 while (!candidates.is_empty ()
3382 && size.num_statements > MAX_NUM_STATEMENTS)
3384 /* Peel off a candidate and force it into a subroutine. */
3385 subroutine_candidate cand = candidates.pop ();
3386 size.num_statements -= cand.second.num_statements;
3387 cand.first->to = create_subroutine (type, cand.first->to, procs);
3390 /* Update the depth for subroutine candidates that we decided not to
3391 split out. */
3392 for (unsigned int i = 0; i < candidates.length (); ++i)
3393 size.depth = MAX (size.depth, candidates[i].second.depth);
3394 size.depth += 1;
3395 return size;
3398 /* Return true if, for all X, PRED (X, MODE) implies that X has mode MODE. */
3400 static bool
3401 safe_predicate_mode (const struct pred_data *pred, machine_mode mode)
3403 /* Scalar integer constants have VOIDmode. */
3404 if (GET_MODE_CLASS (mode) == MODE_INT
3405 && (pred->codes[CONST_INT]
3406 || pred->codes[CONST_DOUBLE]
3407 || pred->codes[CONST_WIDE_INT]))
3408 return false;
3410 return !pred->special && mode != VOIDmode;
3413 /* Fill CODES with the set of codes that could be matched by PRED. */
3415 static void
3416 get_predicate_codes (const struct pred_data *pred, int_set *codes)
3418 for (int i = 0; i < NUM_TRUE_RTX_CODE; ++i)
3419 if (!pred || pred->codes[i])
3420 codes->safe_push (i);
3423 /* Return true if the first path through D1 tests the same thing as D2. */
3425 static bool
3426 has_same_test_p (decision *d1, decision *d2)
3430 if (d1->test == d2->test)
3431 return true;
3432 d1 = d1->first->to->first;
3434 while (d1);
3435 return false;
3438 /* Return true if D1 and D2 cannot match the same rtx. All states reachable
3439 from D2 have single decisions and all those decisions have single
3440 transitions. */
3442 static bool
3443 mutually_exclusive_p (decision *d1, decision *d2)
3445 /* If one path through D1 fails to test the same thing as D2, assume
3446 that D2's test could be true for D1 and look for a later, more useful,
3447 test. This isn't as expensive as it looks in practice. */
3448 while (!has_same_test_p (d1, d2))
3450 d2 = d2->singleton ()->to->singleton ();
3451 if (!d2)
3452 return false;
3454 if (d1->test == d2->test)
3456 /* Look for any transitions from D1 that have the same labels as
3457 the transition from D2. */
3458 transition *trans2 = d2->singleton ();
3459 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3461 int_set::iterator i1 = trans1->labels.begin ();
3462 int_set::iterator end1 = trans1->labels.end ();
3463 int_set::iterator i2 = trans2->labels.begin ();
3464 int_set::iterator end2 = trans2->labels.end ();
3465 while (i1 != end1 && i2 != end2)
3466 if (*i1 < *i2)
3467 ++i1;
3468 else if (*i2 < *i1)
3469 ++i2;
3470 else
3472 /* TRANS1 has some labels in common with TRANS2. Assume
3473 that D1 and D2 could match the same rtx if the target
3474 of TRANS1 could match the same rtx as D2. */
3475 for (decision *subd1 = trans1->to->first;
3476 subd1; subd1 = subd1->next)
3477 if (!mutually_exclusive_p (subd1, d2))
3478 return false;
3479 break;
3482 return true;
3484 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3485 for (decision *subd1 = trans1->to->first; subd1; subd1 = subd1->next)
3486 if (!mutually_exclusive_p (subd1, d2))
3487 return false;
3488 return true;
3491 /* Try to merge S2's decision into D1, given that they have the same test.
3492 Fail only if EXCLUDE is nonnull and the new transition would have the
3493 same labels as *EXCLUDE. When returning true, set *NEXT_S1, *NEXT_S2
3494 and *NEXT_EXCLUDE as for merge_into_state_1, or set *NEXT_S2 to null
3495 if the merge is complete. */
3497 static bool
3498 merge_into_decision (decision *d1, state *s2, const int_set *exclude,
3499 state **next_s1, state **next_s2,
3500 const int_set **next_exclude)
3502 decision *d2 = s2->singleton ();
3503 transition *trans2 = d2->singleton ();
3505 /* Get a list of the transitions that intersect TRANS2. */
3506 auto_vec <transition *, 32> intersecting;
3507 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next)
3509 int_set::iterator i1 = trans1->labels.begin ();
3510 int_set::iterator end1 = trans1->labels.end ();
3511 int_set::iterator i2 = trans2->labels.begin ();
3512 int_set::iterator end2 = trans2->labels.end ();
3513 bool trans1_is_subset = true;
3514 bool trans2_is_subset = true;
3515 bool intersect_p = false;
3516 while (i1 != end1 && i2 != end2)
3517 if (*i1 < *i2)
3519 trans1_is_subset = false;
3520 ++i1;
3522 else if (*i2 < *i1)
3524 trans2_is_subset = false;
3525 ++i2;
3527 else
3529 intersect_p = true;
3530 ++i1;
3531 ++i2;
3533 if (i1 != end1)
3534 trans1_is_subset = false;
3535 if (i2 != end2)
3536 trans2_is_subset = false;
3537 if (trans1_is_subset && trans2_is_subset)
3539 /* There's already a transition that matches exactly.
3540 Merge the target states. */
3541 trans1->optional &= trans2->optional;
3542 *next_s1 = trans1->to;
3543 *next_s2 = trans2->to;
3544 *next_exclude = 0;
3545 return true;
3547 if (trans2_is_subset)
3549 /* TRANS1 has all the labels that TRANS2 needs. Merge S2 into
3550 the target of TRANS1, but (to avoid infinite recursion)
3551 make sure that we don't end up creating another transition
3552 like TRANS1. */
3553 *next_s1 = trans1->to;
3554 *next_s2 = s2;
3555 *next_exclude = &trans1->labels;
3556 return true;
3558 if (intersect_p)
3559 intersecting.safe_push (trans1);
3562 if (intersecting.is_empty ())
3564 /* No existing labels intersect the new ones. We can just add
3565 TRANS2 itself. */
3566 d1->push_back (d2->release ());
3567 *next_s1 = 0;
3568 *next_s2 = 0;
3569 *next_exclude = 0;
3570 return true;
3573 /* Take the union of the labels in INTERSECTING and TRANS2. Store the
3574 result in COMBINED and use NEXT as a temporary. */
3575 int_set tmp1 = trans2->labels, tmp2;
3576 int_set *combined = &tmp1, *next = &tmp2;
3577 for (unsigned int i = 0; i < intersecting.length (); ++i)
3579 transition *trans1 = intersecting[i];
3580 next->truncate (0);
3581 next->safe_grow (trans1->labels.length () + combined->length ());
3582 int_set::iterator end
3583 = std::set_union (trans1->labels.begin (), trans1->labels.end (),
3584 combined->begin (), combined->end (),
3585 next->begin ());
3586 next->truncate (end - next->begin ());
3587 std::swap (next, combined);
3590 /* Stop now if we've been told not to create a transition with these
3591 labels. */
3592 if (exclude && *combined == *exclude)
3593 return false;
3595 /* Get the transition that should carry the new labels. */
3596 transition *new_trans = intersecting[0];
3597 if (intersecting.length () == 1)
3599 /* We're merging with one existing transition whose labels are a
3600 subset of those required. If both transitions are optional,
3601 we can just expand the set of labels so that it's suitable
3602 for both transitions. It isn't worth preserving the original
3603 transitions since we know that they can't be merged; we would
3604 need to backtrack to S2 if TRANS1->to fails. In contrast,
3605 we might be able to merge the targets of the transitions
3606 without any backtracking.
3608 If instead the existing transition is not optional, ensure that
3609 all target decisions are suitably protected. Some decisions
3610 might already have a more specific requirement than NEW_TRANS,
3611 in which case there's no point testing NEW_TRANS as well. E.g. this
3612 would have happened if a test for an (eq ...) rtx had been
3613 added to a decision that tested whether the code is suitable
3614 for comparison_operator. The original comparison_operator
3615 transition would have been non-optional and the (eq ...) test
3616 would be performed by a second decision in the target of that
3617 transition.
3619 The remaining case -- keeping the original optional transition
3620 when adding a non-optional TRANS2 -- is a wash. Preserving
3621 the optional transition only helps if we later merge another
3622 state S3 that is mutually exclusive with S2 and whose labels
3623 belong to *COMBINED - TRANS1->labels. We can then test the
3624 original NEW_TRANS and S3 in the same decision. We keep the
3625 optional transition around for that case, but it occurs very
3626 rarely. */
3627 gcc_assert (new_trans->labels != *combined);
3628 if (!new_trans->optional || !trans2->optional)
3630 decision *start = 0;
3631 for (decision *end = new_trans->to->first; end; end = end->next)
3633 if (!start && end->test != d1->test)
3634 /* END belongs to a range of decisions that need to be
3635 protected by NEW_TRANS. */
3636 start = end;
3637 if (start && (!end->next || end->next->test == d1->test))
3639 /* Protect [START, END] with NEW_TRANS. The decisions
3640 move to NEW_S and NEW_D becomes part of NEW_TRANS->to. */
3641 state *new_s = new state;
3642 decision *new_d = new decision (d1->test);
3643 new_d->push_back (new transition (new_trans->labels, new_s,
3644 new_trans->optional));
3645 state::range r (start, end);
3646 new_trans->to->replace (r, new_d);
3647 new_s->push_back (r);
3649 /* Continue with an empty range. */
3650 start = 0;
3652 /* Continue from the decision after NEW_D. */
3653 end = new_d;
3657 new_trans->optional = true;
3658 new_trans->labels = *combined;
3660 else
3662 /* We're merging more than one existing transition together.
3663 Those transitions are successfully dividing the matching space
3664 and so we want to preserve them, even if they're optional.
3666 Create a new transition with the union set of labels and make
3667 it go to a state that has the original transitions. */
3668 decision *new_d = new decision (d1->test);
3669 for (unsigned int i = 0; i < intersecting.length (); ++i)
3670 new_d->push_back (d1->remove (intersecting[i]));
3672 state *new_s = new state;
3673 new_s->push_back (new_d);
3675 new_trans = new transition (*combined, new_s, true);
3676 d1->push_back (new_trans);
3679 /* We now have an optional transition with labels *COMBINED. Decide
3680 whether we can use it as TRANS2 or whether we need to merge S2
3681 into the target of NEW_TRANS. */
3682 gcc_assert (new_trans->optional);
3683 if (new_trans->labels == trans2->labels)
3685 /* NEW_TRANS matches TRANS2. Just merge the target states. */
3686 new_trans->optional = trans2->optional;
3687 *next_s1 = new_trans->to;
3688 *next_s2 = trans2->to;
3689 *next_exclude = 0;
3691 else
3693 /* Try to merge TRANS2 into the target of the overlapping transition,
3694 but (to prevent infinite recursion or excessive redundancy) without
3695 creating another transition of the same type. */
3696 *next_s1 = new_trans->to;
3697 *next_s2 = s2;
3698 *next_exclude = &new_trans->labels;
3700 return true;
3703 /* Make progress in merging S2 into S1, given that each state in S2
3704 has a single decision. If EXCLUDE is nonnull, avoid creating a new
3705 transition with the same test as S2's decision and with the labels
3706 in *EXCLUDE.
3708 Return true if there is still work to do. When returning true,
3709 set *NEXT_S1, *NEXT_S2 and *NEXT_EXCLUDE to the values that
3710 S1, S2 and EXCLUDE should have next time round.
3712 If S1 and S2 both match a particular rtx, give priority to S1. */
3714 static bool
3715 merge_into_state_1 (state *s1, state *s2, const int_set *exclude,
3716 state **next_s1, state **next_s2,
3717 const int_set **next_exclude)
3719 decision *d2 = s2->singleton ();
3720 if (decision *d1 = s1->last)
3722 if (d1->test.terminal_p ())
3723 /* D1 is an unconditional return, so S2 can never match. This can
3724 sometimes be a bug in the .md description, but might also happen
3725 if genconditions forces some conditions to true for certain
3726 configurations. */
3727 return false;
3729 /* Go backwards through the decisions in S1, stopping once we find one
3730 that could match the same thing as S2. */
3731 while (d1->prev && mutually_exclusive_p (d1, d2))
3732 d1 = d1->prev;
3734 /* Search forwards from that point, merging D2 into the first
3735 decision we can. */
3736 for (; d1; d1 = d1->next)
3738 /* If S2 performs some optional tests before testing the same thing
3739 as D1, those tests do not help to distinguish D1 and S2, so it's
3740 better to drop them. Search through such optional decisions
3741 until we find something that tests the same thing as D1. */
3742 state *sub_s2 = s2;
3743 for (;;)
3745 decision *sub_d2 = sub_s2->singleton ();
3746 if (d1->test == sub_d2->test)
3748 /* Only apply EXCLUDE if we're testing the same thing
3749 as D2. */
3750 const int_set *sub_exclude = (d2 == sub_d2 ? exclude : 0);
3752 /* Try to merge SUB_S2 into D1. This can only fail if
3753 it would involve creating a new transition with
3754 labels SUB_EXCLUDE. */
3755 if (merge_into_decision (d1, sub_s2, sub_exclude,
3756 next_s1, next_s2, next_exclude))
3757 return *next_s2 != 0;
3759 /* Can't merge with D1; try a later decision. */
3760 break;
3762 transition *sub_trans2 = sub_d2->singleton ();
3763 if (!sub_trans2->optional)
3764 /* Can't merge with D1; try a later decision. */
3765 break;
3766 sub_s2 = sub_trans2->to;
3771 /* We can't merge D2 with any existing decision. Just add it to the end. */
3772 s1->push_back (s2->release ());
3773 return false;
3776 /* Merge S2 into S1. If they both match a particular rtx, give
3777 priority to S1. Each state in S2 has a single decision. */
3779 static void
3780 merge_into_state (state *s1, state *s2)
3782 const int_set *exclude = 0;
3783 while (s2 && merge_into_state_1 (s1, s2, exclude, &s1, &s2, &exclude))
3784 continue;
3787 /* Pairs a pattern that needs to be matched with the rtx position at
3788 which the pattern should occur. */
3789 struct pattern_pos {
3790 pattern_pos () {}
3791 pattern_pos (rtx, position *);
3793 rtx pattern;
3794 position *pos;
3797 pattern_pos::pattern_pos (rtx pattern_in, position *pos_in)
3798 : pattern (pattern_in), pos (pos_in)
3801 /* Compare entries according to their depth-first order. There shouldn't
3802 be two entries at the same position. */
3804 bool
3805 operator < (const pattern_pos &e1, const pattern_pos &e2)
3807 int diff = compare_positions (e1.pos, e2.pos);
3808 gcc_assert (diff != 0 || e1.pattern == e2.pattern);
3809 return diff < 0;
3812 /* Return the name of the predicate matched by MATCH_RTX. */
3814 static const char *
3815 predicate_name (rtx match_rtx)
3817 if (GET_CODE (match_rtx) == MATCH_SCRATCH)
3818 return "scratch_operand";
3819 else
3820 return XSTR (match_rtx, 1);
3823 /* Add new decisions to S that check whether the rtx at position POS
3824 matches PATTERN. Return the state that is reached in that case.
3825 TOP_PATTERN is the overall pattern, as passed to match_pattern_1. */
3827 static state *
3828 match_pattern_2 (state *s, rtx top_pattern, position *pos, rtx pattern)
3830 auto_vec <pattern_pos, 32> worklist;
3831 auto_vec <pattern_pos, 32> pred_and_mode_tests;
3832 auto_vec <pattern_pos, 32> dup_tests;
3834 worklist.safe_push (pattern_pos (pattern, pos));
3835 while (!worklist.is_empty ())
3837 pattern_pos next = worklist.pop ();
3838 pattern = next.pattern;
3839 pos = next.pos;
3840 unsigned int reverse_s = worklist.length ();
3842 enum rtx_code code = GET_CODE (pattern);
3843 switch (code)
3845 case MATCH_OP_DUP:
3846 case MATCH_DUP:
3847 case MATCH_PAR_DUP:
3848 /* Add a test that the rtx matches the earlier one, but only
3849 after the structure and predicates have been checked. */
3850 dup_tests.safe_push (pattern_pos (pattern, pos));
3852 /* Use the same code check as the original operand. */
3853 pattern = find_operand (top_pattern, XINT (pattern, 0), NULL_RTX);
3854 /* Fall through. */
3856 case MATCH_PARALLEL:
3857 case MATCH_OPERAND:
3858 case MATCH_SCRATCH:
3859 case MATCH_OPERATOR:
3861 const char *pred_name = predicate_name (pattern);
3862 const struct pred_data *pred = 0;
3863 if (pred_name[0] != 0)
3865 pred = lookup_predicate (pred_name);
3866 /* Only report errors once per rtx. */
3867 if (code == GET_CODE (pattern))
3869 if (!pred)
3870 error_with_line (pattern_lineno,
3871 "unknown predicate '%s'"
3872 " in '%s' expression",
3873 pred_name, GET_RTX_NAME (code));
3874 else if (code == MATCH_PARALLEL
3875 && pred->singleton != PARALLEL)
3876 error_with_line (pattern_lineno,
3877 "predicate '%s' used in match_parallel"
3878 " does not allow only PARALLEL",
3879 pred->name);
3883 if (code == MATCH_PARALLEL || code == MATCH_PAR_DUP)
3885 /* Check that we have a parallel with enough elements. */
3886 s = add_decision (s, rtx_test::code (pos), PARALLEL, false);
3887 int min_len = XVECLEN (pattern, 2);
3888 s = add_decision (s, rtx_test::veclen_ge (pos, min_len),
3889 true, false);
3891 else
3893 /* Check that the rtx has one of codes accepted by the
3894 predicate. This is necessary when matching suboperands
3895 of a MATCH_OPERATOR or MATCH_OP_DUP, since we can't
3896 call XEXP (X, N) without checking that X has at least
3897 N+1 operands. */
3898 int_set codes;
3899 get_predicate_codes (pred, &codes);
3900 bool need_codes = (pred
3901 && (code == MATCH_OPERATOR
3902 || code == MATCH_OP_DUP));
3903 s = add_decision (s, rtx_test::code (pos), codes, !need_codes);
3906 /* Postpone the predicate check until we've checked the rest
3907 of the rtx structure. */
3908 if (code == GET_CODE (pattern))
3909 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3911 /* If we need to match suboperands, add them to the worklist. */
3912 if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
3914 position **subpos_ptr;
3915 enum position_type pos_type;
3916 int i;
3917 if (code == MATCH_OPERATOR || code == MATCH_OP_DUP)
3919 pos_type = POS_XEXP;
3920 subpos_ptr = &pos->xexps;
3921 i = (code == MATCH_OPERATOR ? 2 : 1);
3923 else
3925 pos_type = POS_XVECEXP0;
3926 subpos_ptr = &pos->xvecexp0s;
3927 i = 2;
3929 for (int j = 0; j < XVECLEN (pattern, i); ++j)
3931 position *subpos = next_position (subpos_ptr, pos,
3932 pos_type, j);
3933 worklist.safe_push (pattern_pos (XVECEXP (pattern, i, j),
3934 subpos));
3935 subpos_ptr = &subpos->next;
3938 break;
3941 default:
3943 /* Check that the rtx has the right code. */
3944 s = add_decision (s, rtx_test::code (pos), code, false);
3946 /* Queue a test for the mode if one is specified. */
3947 if (GET_MODE (pattern) != VOIDmode)
3948 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos));
3950 /* Push subrtxes onto the worklist. Match nonrtx operands now. */
3951 const char *fmt = GET_RTX_FORMAT (code);
3952 position **subpos_ptr = &pos->xexps;
3953 for (size_t i = 0; fmt[i]; ++i)
3955 position *subpos = next_position (subpos_ptr, pos,
3956 POS_XEXP, i);
3957 switch (fmt[i])
3959 case 'e': case 'u':
3960 worklist.safe_push (pattern_pos (XEXP (pattern, i),
3961 subpos));
3962 break;
3964 case 'E':
3966 /* Make sure the vector has the right number of
3967 elements. */
3968 int length = XVECLEN (pattern, i);
3969 s = add_decision (s, rtx_test::veclen (pos),
3970 length, false);
3972 position **subpos2_ptr = &pos->xvecexp0s;
3973 for (int j = 0; j < length; j++)
3975 position *subpos2 = next_position (subpos2_ptr, pos,
3976 POS_XVECEXP0, j);
3977 rtx x = XVECEXP (pattern, i, j);
3978 worklist.safe_push (pattern_pos (x, subpos2));
3979 subpos2_ptr = &subpos2->next;
3981 break;
3984 case 'i':
3985 /* Make sure that XINT (X, I) has the right value. */
3986 s = add_decision (s, rtx_test::int_field (pos, i),
3987 XINT (pattern, i), false);
3988 break;
3990 case 'r':
3991 /* Make sure that REGNO (X) has the right value. */
3992 gcc_assert (i == 0);
3993 s = add_decision (s, rtx_test::regno_field (pos),
3994 REGNO (pattern), false);
3995 break;
3997 case 'w':
3998 /* Make sure that XWINT (X, I) has the right value. */
3999 s = add_decision (s, rtx_test::wide_int_field (pos, i),
4000 XWINT (pattern, 0), false);
4001 break;
4003 case '0':
4004 break;
4006 default:
4007 gcc_unreachable ();
4009 subpos_ptr = &subpos->next;
4012 break;
4014 /* Operands are pushed onto the worklist so that later indices are
4015 nearer the top. That's what we want for SETs, since a SET_SRC
4016 is a better discriminator than a SET_DEST. In other cases it's
4017 usually better to match earlier indices first. This is especially
4018 true of PARALLELs, where the first element tends to be the most
4019 individual. It's also true for commutative operators, where the
4020 canonicalization rules say that the more complex operand should
4021 come first. */
4022 if (code != SET && worklist.length () > reverse_s)
4023 std::reverse (&worklist[0] + reverse_s,
4024 &worklist[0] + worklist.length ());
4027 /* Sort the predicate and mode tests so that they're in depth-first order.
4028 The main goal of this is to put SET_SRC match_operands after SET_DEST
4029 match_operands and after mode checks for the enclosing SET_SRC operators
4030 (such as the mode of a PLUS in an addition instruction). The latter
4031 two types of test can determine the mode exactly, whereas a SET_SRC
4032 match_operand often has to cope with the possibility of the operand
4033 being a modeless constant integer. E.g. something that matches
4034 register_operand (x, SImode) never matches register_operand (x, DImode),
4035 but a const_int that matches immediate_operand (x, SImode) also matches
4036 immediate_operand (x, DImode). The register_operand cases can therefore
4037 be distinguished by a switch on the mode, but the immediate_operand
4038 cases can't. */
4039 if (pred_and_mode_tests.length () > 1)
4040 std::sort (&pred_and_mode_tests[0],
4041 &pred_and_mode_tests[0] + pred_and_mode_tests.length ());
4043 /* Add the mode and predicate tests. */
4044 pattern_pos *e;
4045 unsigned int i;
4046 FOR_EACH_VEC_ELT (pred_and_mode_tests, i, e)
4048 switch (GET_CODE (e->pattern))
4050 case MATCH_PARALLEL:
4051 case MATCH_OPERAND:
4052 case MATCH_SCRATCH:
4053 case MATCH_OPERATOR:
4055 int opno = XINT (e->pattern, 0);
4056 num_operands = MAX (num_operands, opno + 1);
4057 const char *pred_name = predicate_name (e->pattern);
4058 if (pred_name[0])
4060 const struct pred_data *pred = lookup_predicate (pred_name);
4061 /* Check the mode first, to distinguish things like SImode
4062 and DImode register_operands, as described above. */
4063 machine_mode mode = GET_MODE (e->pattern);
4064 if (safe_predicate_mode (pred, mode))
4065 s = add_decision (s, rtx_test::mode (e->pos), mode, true);
4067 /* Assign to operands[] first, so that the rtx usually doesn't
4068 need to be live across the call to the predicate.
4070 This shouldn't cause a problem with dirtying the page,
4071 since we fully expect to assign to operands[] at some point,
4072 and since the caller usually writes to other parts of
4073 recog_data anyway. */
4074 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4075 true, false);
4076 s = add_decision (s, rtx_test::predicate (e->pos, pred, mode),
4077 true, false);
4079 else
4080 /* Historically we've ignored the mode when there's no
4081 predicate. Just set up operands[] unconditionally. */
4082 s = add_decision (s, rtx_test::set_op (e->pos, opno),
4083 true, false);
4084 break;
4087 default:
4088 s = add_decision (s, rtx_test::mode (e->pos),
4089 GET_MODE (e->pattern), false);
4090 break;
4094 /* Finally add rtx_equal_p checks for duplicated operands. */
4095 FOR_EACH_VEC_ELT (dup_tests, i, e)
4096 s = add_decision (s, rtx_test::duplicate (e->pos, XINT (e->pattern, 0)),
4097 true, false);
4098 return s;
4101 /* Add new decisions to S that make it return ACCEPTANCE if:
4103 (1) the rtx doesn't match anything already matched by S
4104 (2) the rtx matches TOP_PATTERN and
4105 (3) C_TEST is true.
4107 For peephole2, TOP_PATTERN is a SEQUENCE of the instruction patterns
4108 to match, otherwise it is a single instruction pattern. */
4110 static void
4111 match_pattern_1 (state *s, rtx top_pattern, const char *c_test,
4112 acceptance_type acceptance)
4114 if (acceptance.type == PEEPHOLE2)
4116 /* Match each individual instruction. */
4117 position **subpos_ptr = &peep2_insn_pos_list;
4118 int count = 0;
4119 for (int i = 0; i < XVECLEN (top_pattern, 0); ++i)
4121 rtx x = XVECEXP (top_pattern, 0, i);
4122 position *subpos = next_position (subpos_ptr, &root_pos,
4123 POS_PEEP2_INSN, count);
4124 if (count > 0)
4125 s = add_decision (s, rtx_test::peep2_count (count + 1),
4126 true, false);
4127 s = match_pattern_2 (s, top_pattern, subpos, x);
4128 subpos_ptr = &subpos->next;
4129 count += 1;
4131 acceptance.u.full.u.match_len = count - 1;
4133 else
4135 /* Make the rtx itself. */
4136 s = match_pattern_2 (s, top_pattern, &root_pos, top_pattern);
4138 /* If the match is only valid when extra clobbers are added,
4139 make sure we're able to pass that information to the caller. */
4140 if (acceptance.type == RECOG && acceptance.u.full.u.num_clobbers)
4141 s = add_decision (s, rtx_test::have_num_clobbers (), true, false);
4144 /* Make sure that the C test is true. */
4145 if (maybe_eval_c_test (c_test) != 1)
4146 s = add_decision (s, rtx_test::c_test (c_test), true, false);
4148 /* Accept the pattern. */
4149 add_decision (s, rtx_test::accept (acceptance), true, false);
4152 /* Like match_pattern_1, but (if merge_states_p) try to merge the
4153 decisions with what's already in S, to reduce the amount of
4154 backtracking. */
4156 static void
4157 match_pattern (state *s, rtx top_pattern, const char *c_test,
4158 acceptance_type acceptance)
4160 if (merge_states_p)
4162 state root;
4163 /* Add the decisions to a fresh state and then merge the full tree
4164 into the existing one. */
4165 match_pattern_1 (&root, top_pattern, c_test, acceptance);
4166 merge_into_state (s, &root);
4168 else
4169 match_pattern_1 (s, top_pattern, c_test, acceptance);
4172 /* Begin the output file. */
4174 static void
4175 write_header (void)
4177 puts ("\
4178 /* Generated automatically by the program `genrecog' from the target\n\
4179 machine description file. */\n\
4181 #include \"config.h\"\n\
4182 #include \"system.h\"\n\
4183 #include \"coretypes.h\"\n\
4184 #include \"tm.h\"\n\
4185 #include \"rtl.h\"\n\
4186 #include \"tm_p.h\"\n\
4187 #include \"hashtab.h\"\n\
4188 #include \"hash-set.h\"\n\
4189 #include \"vec.h\"\n\
4190 #include \"machmode.h\"\n\
4191 #include \"hard-reg-set.h\"\n\
4192 #include \"input.h\"\n\
4193 #include \"function.h\"\n\
4194 #include \"insn-config.h\"\n\
4195 #include \"recog.h\"\n\
4196 #include \"output.h\"\n\
4197 #include \"flags.h\"\n\
4198 #include \"hard-reg-set.h\"\n\
4199 #include \"predict.h\"\n\
4200 #include \"basic-block.h\"\n\
4201 #include \"resource.h\"\n\
4202 #include \"diagnostic-core.h\"\n\
4203 #include \"reload.h\"\n\
4204 #include \"regs.h\"\n\
4205 #include \"tm-constrs.h\"\n\
4206 #include \"predict.h\"\n\
4207 \n");
4209 puts ("\n\
4210 /* `recog' contains a decision tree that recognizes whether the rtx\n\
4211 X0 is a valid instruction.\n\
4213 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
4214 returns a nonnegative number which is the insn code number for the\n\
4215 pattern that matched. This is the same as the order in the machine\n\
4216 description of the entry that matched. This number can be used as an\n\
4217 index into `insn_data' and other tables.\n");
4218 puts ("\
4219 The third parameter to recog is an optional pointer to an int. If\n\
4220 present, recog will accept a pattern if it matches except for missing\n\
4221 CLOBBER expressions at the end. In that case, the value pointed to by\n\
4222 the optional pointer will be set to the number of CLOBBERs that need\n\
4223 to be added (it should be initialized to zero by the caller). If it");
4224 puts ("\
4225 is set nonzero, the caller should allocate a PARALLEL of the\n\
4226 appropriate size, copy the initial entries, and call add_clobbers\n\
4227 (found in insn-emit.c) to fill in the CLOBBERs.\n\
4230 puts ("\n\
4231 The function split_insns returns 0 if the rtl could not\n\
4232 be split or the split rtl as an INSN list if it can be.\n\
4234 The function peephole2_insns returns 0 if the rtl could not\n\
4235 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
4236 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
4237 */\n\n");
4240 /* Return the C type of a parameter with type TYPE. */
4242 static const char *
4243 parameter_type_string (parameter::type_enum type)
4245 switch (type)
4247 case parameter::UNSET:
4248 break;
4250 case parameter::CODE:
4251 return "rtx_code";
4253 case parameter::MODE:
4254 return "machine_mode";
4256 case parameter::INT:
4257 return "int";
4259 case parameter::UINT:
4260 return "unsigned int";
4262 case parameter::WIDE_INT:
4263 return "HOST_WIDE_INT";
4265 gcc_unreachable ();
4268 /* Return true if ACCEPTANCE requires only a single C statement even in
4269 a backtracking context. */
4271 static bool
4272 single_statement_p (const acceptance_type &acceptance)
4274 if (acceptance.partial_p)
4275 /* We need to handle failures of the subroutine. */
4276 return false;
4277 switch (acceptance.type)
4279 case SUBPATTERN:
4280 case SPLIT:
4281 return true;
4283 case RECOG:
4284 /* False if we need to assign to pnum_clobbers. */
4285 return acceptance.u.full.u.num_clobbers == 0;
4287 case PEEPHOLE2:
4288 /* We need to assign to pmatch_len_ and handle null returns from the
4289 peephole2 routine. */
4290 return false;
4292 gcc_unreachable ();
4295 /* Return the C failure value for a routine of type TYPE. */
4297 static const char *
4298 get_failure_return (routine_type type)
4300 switch (type)
4302 case SUBPATTERN:
4303 case RECOG:
4304 return "-1";
4306 case SPLIT:
4307 case PEEPHOLE2:
4308 return "NULL";
4310 gcc_unreachable ();
4313 /* Indicates whether a block of code always returns or whether it can fall
4314 through. */
4316 enum exit_state {
4317 ES_RETURNED,
4318 ES_FALLTHROUGH
4321 /* Information used while writing out code. */
4323 struct output_state
4325 /* The type of routine that we're generating. */
4326 routine_type type;
4328 /* Maps position ids to xN variable numbers. The entry is only valid if
4329 it is less than the length of VAR_TO_ID, but this holds for every position
4330 tested by a state when writing out that state. */
4331 auto_vec <unsigned int> id_to_var;
4333 /* Maps xN variable numbers to position ids. */
4334 auto_vec <unsigned int> var_to_id;
4336 /* Index N is true if variable xN has already been set. */
4337 auto_vec <bool> seen_vars;
4340 /* Return true if D is a call to a pattern routine and if there is some X
4341 such that the transition for pattern result N goes to a successful return
4342 with code X+N. When returning true, set *BASE_OUT to this X and *COUNT_OUT
4343 to the number of return values. (We know that every PATTERN decision has
4344 a transition for every successful return.) */
4346 static bool
4347 terminal_pattern_p (decision *d, unsigned int *base_out,
4348 unsigned int *count_out)
4350 if (d->test.kind != rtx_test::PATTERN)
4351 return false;
4352 unsigned int base = 0;
4353 unsigned int count = 0;
4354 for (transition *trans = d->first; trans; trans = trans->next)
4356 if (trans->is_param || trans->labels.length () != 1)
4357 return false;
4358 decision *subd = trans->to->singleton ();
4359 if (!subd || subd->test.kind != rtx_test::ACCEPT)
4360 return false;
4361 unsigned int this_base = (subd->test.u.acceptance.u.full.code
4362 - trans->labels[0]);
4363 if (trans == d->first)
4364 base = this_base;
4365 else if (base != this_base)
4366 return false;
4367 count += 1;
4369 *base_out = base;
4370 *count_out = count;
4371 return true;
4374 /* Return true if TEST doesn't test an rtx or if the rtx it tests is
4375 already available in state OS. */
4377 static bool
4378 test_position_available_p (output_state *os, const rtx_test &test)
4380 return (!test.pos
4381 || test.pos_operand >= 0
4382 || os->seen_vars[os->id_to_var[test.pos->id]]);
4385 /* Like printf, but print INDENT spaces at the beginning. */
4387 static void ATTRIBUTE_PRINTF_2
4388 printf_indent (unsigned int indent, const char *format, ...)
4390 va_list ap;
4391 va_start (ap, format);
4392 printf ("%*s", indent, "");
4393 vprintf (format, ap);
4394 va_end (ap);
4397 /* Emit code to initialize the variable associated with POS, if it isn't
4398 already valid in state OS. Indent each line by INDENT spaces. Update
4399 OS with the new state. */
4401 static void
4402 change_state (output_state *os, position *pos, unsigned int indent)
4404 unsigned int var = os->id_to_var[pos->id];
4405 gcc_assert (var < os->var_to_id.length () && os->var_to_id[var] == pos->id);
4406 if (os->seen_vars[var])
4407 return;
4408 switch (pos->type)
4410 case POS_PEEP2_INSN:
4411 printf_indent (indent, "x%d = PATTERN (peep2_next_insn (%d));\n",
4412 var, pos->arg);
4413 break;
4415 case POS_XEXP:
4416 change_state (os, pos->base, indent);
4417 printf_indent (indent, "x%d = XEXP (x%d, %d);\n",
4418 var, os->id_to_var[pos->base->id], pos->arg);
4419 break;
4421 case POS_XVECEXP0:
4422 change_state (os, pos->base, indent);
4423 printf_indent (indent, "x%d = XVECEXP (x%d, 0, %d);\n",
4424 var, os->id_to_var[pos->base->id], pos->arg);
4425 break;
4427 os->seen_vars[var] = true;
4430 /* Print the enumerator constant for CODE -- the upcase version of
4431 the name. */
4433 static void
4434 print_code (enum rtx_code code)
4436 const char *p;
4437 for (p = GET_RTX_NAME (code); *p; p++)
4438 putchar (TOUPPER (*p));
4441 /* Emit a uint64_t as an integer constant expression. We need to take
4442 special care to avoid "decimal constant is so large that it is unsigned"
4443 warnings in the resulting code. */
4445 static void
4446 print_host_wide_int (uint64_t val)
4448 uint64_t min = uint64_t (1) << (HOST_BITS_PER_WIDE_INT - 1);
4449 if (val == min)
4450 printf ("(" HOST_WIDE_INT_PRINT_DEC_C " - 1)", val + 1);
4451 else
4452 printf (HOST_WIDE_INT_PRINT_DEC_C, val);
4455 /* Print the C expression for actual parameter PARAM. */
4457 static void
4458 print_parameter_value (const parameter &param)
4460 if (param.is_param)
4461 printf ("i%d", (int) param.value + 1);
4462 else
4463 switch (param.type)
4465 case parameter::UNSET:
4466 gcc_unreachable ();
4467 break;
4469 case parameter::CODE:
4470 print_code ((enum rtx_code) param.value);
4471 break;
4473 case parameter::MODE:
4474 printf ("%smode", GET_MODE_NAME ((machine_mode) param.value));
4475 break;
4477 case parameter::INT:
4478 printf ("%d", (int) param.value);
4479 break;
4481 case parameter::UINT:
4482 printf ("%u", (unsigned int) param.value);
4483 break;
4485 case parameter::WIDE_INT:
4486 print_host_wide_int (param.value);
4487 break;
4491 /* Print the C expression for the rtx tested by TEST. */
4493 static void
4494 print_test_rtx (output_state *os, const rtx_test &test)
4496 if (test.pos_operand >= 0)
4497 printf ("operands[%d]", test.pos_operand);
4498 else
4499 printf ("x%d", os->id_to_var[test.pos->id]);
4502 /* Print the C expression for non-boolean test TEST. */
4504 static void
4505 print_nonbool_test (output_state *os, const rtx_test &test)
4507 switch (test.kind)
4509 case rtx_test::CODE:
4510 printf ("GET_CODE (");
4511 print_test_rtx (os, test);
4512 printf (")");
4513 break;
4515 case rtx_test::MODE:
4516 printf ("GET_MODE (");
4517 print_test_rtx (os, test);
4518 printf (")");
4519 break;
4521 case rtx_test::VECLEN:
4522 printf ("XVECLEN (");
4523 print_test_rtx (os, test);
4524 printf (", 0)");
4525 break;
4527 case rtx_test::INT_FIELD:
4528 printf ("XINT (");
4529 print_test_rtx (os, test);
4530 printf (", %d)", test.u.opno);
4531 break;
4533 case rtx_test::REGNO_FIELD:
4534 printf ("REGNO (");
4535 print_test_rtx (os, test);
4536 printf (")");
4537 break;
4539 case rtx_test::WIDE_INT_FIELD:
4540 printf ("XWINT (");
4541 print_test_rtx (os, test);
4542 printf (", %d)", test.u.opno);
4543 break;
4545 case rtx_test::PATTERN:
4547 pattern_routine *routine = test.u.pattern->routine;
4548 printf ("pattern%d (", routine->pattern_id);
4549 const char *sep = "";
4550 if (test.pos)
4552 print_test_rtx (os, test);
4553 sep = ", ";
4555 if (routine->insn_p)
4557 printf ("%sinsn", sep);
4558 sep = ", ";
4560 if (routine->pnum_clobbers_p)
4562 printf ("%spnum_clobbers", sep);
4563 sep = ", ";
4565 for (unsigned int i = 0; i < test.u.pattern->params.length (); ++i)
4567 fputs (sep, stdout);
4568 print_parameter_value (test.u.pattern->params[i]);
4569 sep = ", ";
4571 printf (")");
4572 break;
4575 case rtx_test::PEEP2_COUNT:
4576 case rtx_test::VECLEN_GE:
4577 case rtx_test::SAVED_CONST_INT:
4578 case rtx_test::DUPLICATE:
4579 case rtx_test::PREDICATE:
4580 case rtx_test::SET_OP:
4581 case rtx_test::HAVE_NUM_CLOBBERS:
4582 case rtx_test::C_TEST:
4583 case rtx_test::ACCEPT:
4584 gcc_unreachable ();
4588 /* IS_PARAM and LABEL are taken from a transition whose source
4589 decision performs TEST. Print the C code for the label. */
4591 static void
4592 print_label_value (const rtx_test &test, bool is_param, uint64_t value)
4594 print_parameter_value (parameter (transition_parameter_type (test.kind),
4595 is_param, value));
4598 /* If IS_PARAM, print code to compare TEST with the C variable i<VALUE+1>.
4599 If !IS_PARAM, print code to compare TEST with the C constant VALUE.
4600 Test for inequality if INVERT_P, otherwise test for equality. */
4602 static void
4603 print_test (output_state *os, const rtx_test &test, bool is_param,
4604 uint64_t value, bool invert_p)
4606 switch (test.kind)
4608 /* Handle the non-boolean TESTs. */
4609 case rtx_test::CODE:
4610 case rtx_test::MODE:
4611 case rtx_test::VECLEN:
4612 case rtx_test::REGNO_FIELD:
4613 case rtx_test::INT_FIELD:
4614 case rtx_test::WIDE_INT_FIELD:
4615 case rtx_test::PATTERN:
4616 print_nonbool_test (os, test);
4617 printf (" %s ", invert_p ? "!=" : "==");
4618 print_label_value (test, is_param, value);
4619 break;
4621 case rtx_test::SAVED_CONST_INT:
4622 gcc_assert (!is_param && value == 1);
4623 print_test_rtx (os, test);
4624 printf (" %s const_int_rtx[MAX_SAVED_CONST_INT + ",
4625 invert_p ? "!=" : "==");
4626 print_parameter_value (parameter (parameter::INT,
4627 test.u.integer.is_param,
4628 test.u.integer.value));
4629 printf ("]");
4630 break;
4632 case rtx_test::PEEP2_COUNT:
4633 gcc_assert (!is_param && value == 1);
4634 printf ("peep2_current_count %s %d", invert_p ? "<" : ">=",
4635 test.u.min_len);
4636 break;
4638 case rtx_test::VECLEN_GE:
4639 gcc_assert (!is_param && value == 1);
4640 printf ("XVECLEN (");
4641 print_test_rtx (os, test);
4642 printf (", 0) %s %d", invert_p ? "<" : ">=", test.u.min_len);
4643 break;
4645 case rtx_test::PREDICATE:
4646 gcc_assert (!is_param && value == 1);
4647 printf ("%s%s (", invert_p ? "!" : "", test.u.predicate.data->name);
4648 print_test_rtx (os, test);
4649 printf (", ");
4650 print_parameter_value (parameter (parameter::MODE,
4651 test.u.predicate.mode_is_param,
4652 test.u.predicate.mode));
4653 printf (")");
4654 break;
4656 case rtx_test::DUPLICATE:
4657 gcc_assert (!is_param && value == 1);
4658 printf ("%srtx_equal_p (", invert_p ? "!" : "");
4659 print_test_rtx (os, test);
4660 printf (", operands[%d])", test.u.opno);
4661 break;
4663 case rtx_test::HAVE_NUM_CLOBBERS:
4664 gcc_assert (!is_param && value == 1);
4665 printf ("pnum_clobbers %s NULL", invert_p ? "==" : "!=");
4666 break;
4668 case rtx_test::C_TEST:
4669 gcc_assert (!is_param && value == 1);
4670 if (invert_p)
4671 printf ("!");
4672 print_c_condition (test.u.string);
4673 break;
4675 case rtx_test::ACCEPT:
4676 case rtx_test::SET_OP:
4677 gcc_unreachable ();
4681 static exit_state print_decision (output_state *, decision *,
4682 unsigned int, bool);
4684 /* Print code to perform S, indent each line by INDENT spaces.
4685 IS_FINAL is true if there are no fallback decisions to test on failure;
4686 if the state fails then the entire routine fails. */
4688 static exit_state
4689 print_state (output_state *os, state *s, unsigned int indent, bool is_final)
4691 exit_state es = ES_FALLTHROUGH;
4692 for (decision *d = s->first; d; d = d->next)
4693 es = print_decision (os, d, indent, is_final && !d->next);
4694 if (es != ES_RETURNED && is_final)
4696 printf_indent (indent, "return %s;\n", get_failure_return (os->type));
4697 es = ES_RETURNED;
4699 return es;
4702 /* Print the code for subroutine call ACCEPTANCE (for which partial_p
4703 is known to be true). Return the C condition that indicates a successful
4704 match. */
4706 static const char *
4707 print_subroutine_call (const acceptance_type &acceptance)
4709 switch (acceptance.type)
4711 case SUBPATTERN:
4712 gcc_unreachable ();
4714 case RECOG:
4715 printf ("recog_%d (x1, insn, pnum_clobbers)",
4716 acceptance.u.subroutine_id);
4717 return ">= 0";
4719 case SPLIT:
4720 printf ("split_%d (x1, insn)", acceptance.u.subroutine_id);
4721 return "!= NULL_RTX";
4723 case PEEPHOLE2:
4724 printf ("peephole2_%d (x1, insn, pmatch_len_)",
4725 acceptance.u.subroutine_id);
4726 return "!= NULL_RTX";
4728 gcc_unreachable ();
4731 /* Print code for the successful match described by ACCEPTANCE.
4732 INDENT and IS_FINAL are as for print_state. */
4734 static exit_state
4735 print_acceptance (const acceptance_type &acceptance, unsigned int indent,
4736 bool is_final)
4738 if (acceptance.partial_p)
4740 /* Defer the rest of the match to a subroutine. */
4741 if (is_final)
4743 printf_indent (indent, "return ");
4744 print_subroutine_call (acceptance);
4745 printf (";\n");
4746 return ES_RETURNED;
4748 else
4750 printf_indent (indent, "res = ");
4751 const char *res_test = print_subroutine_call (acceptance);
4752 printf (";\n");
4753 printf_indent (indent, "if (res %s)\n", res_test);
4754 printf_indent (indent + 2, "return res;\n");
4755 return ES_FALLTHROUGH;
4758 switch (acceptance.type)
4760 case SUBPATTERN:
4761 printf_indent (indent, "return %d;\n", acceptance.u.full.code);
4762 return ES_RETURNED;
4764 case RECOG:
4765 if (acceptance.u.full.u.num_clobbers != 0)
4766 printf_indent (indent, "*pnum_clobbers = %d;\n",
4767 acceptance.u.full.u.num_clobbers);
4768 printf_indent (indent, "return %d; /* %s */\n", acceptance.u.full.code,
4769 get_insn_name (acceptance.u.full.code));
4770 return ES_RETURNED;
4772 case SPLIT:
4773 printf_indent (indent, "return gen_split_%d (insn, operands);\n",
4774 acceptance.u.full.code);
4775 return ES_RETURNED;
4777 case PEEPHOLE2:
4778 printf_indent (indent, "*pmatch_len_ = %d;\n",
4779 acceptance.u.full.u.match_len);
4780 if (is_final)
4782 printf_indent (indent, "return gen_peephole2_%d (insn, operands);\n",
4783 acceptance.u.full.code);
4784 return ES_RETURNED;
4786 else
4788 printf_indent (indent, "res = gen_peephole2_%d (insn, operands);\n",
4789 acceptance.u.full.code);
4790 printf_indent (indent, "if (res != NULL_RTX)\n");
4791 printf_indent (indent + 2, "return res;\n");
4792 return ES_FALLTHROUGH;
4795 gcc_unreachable ();
4798 /* Print code to perform D. INDENT and IS_FINAL are as for print_state. */
4800 static exit_state
4801 print_decision (output_state *os, decision *d, unsigned int indent,
4802 bool is_final)
4804 uint64_t label;
4805 unsigned int base, count;
4807 /* Make sure the rtx under test is available either in operands[] or
4808 in an xN variable. */
4809 if (d->test.pos && d->test.pos_operand < 0)
4810 change_state (os, d->test.pos, indent);
4812 /* Look for cases where a pattern routine P1 calls another pattern routine
4813 P2 and where P1 returns X + BASE whenever P2 returns X. If IS_FINAL
4814 is true and BASE is zero we can simply use:
4816 return patternN (...);
4818 Otherwise we can use:
4820 res = patternN (...);
4821 if (res >= 0)
4822 return res + BASE;
4824 However, if BASE is nonzero and patternN only returns 0 or -1,
4825 the usual "return BASE;" is better than "return res + BASE;".
4826 If BASE is zero, "return res;" should be better than "return 0;",
4827 since no assignment to the return register is required. */
4828 if (os->type == SUBPATTERN
4829 && terminal_pattern_p (d, &base, &count)
4830 && (base == 0 || count > 1))
4832 if (is_final && base == 0)
4834 printf_indent (indent, "return ");
4835 print_nonbool_test (os, d->test);
4836 printf ("; /* [-1, %d] */\n", count - 1);
4837 return ES_RETURNED;
4839 else
4841 printf_indent (indent, "res = ");
4842 print_nonbool_test (os, d->test);
4843 printf (";\n");
4844 printf_indent (indent, "if (res >= 0)\n");
4845 printf_indent (indent + 2, "return res");
4846 if (base != 0)
4847 printf (" + %d", base);
4848 printf ("; /* [%d, %d] */\n", base, base + count - 1);
4849 return ES_FALLTHROUGH;
4852 else if (d->test.kind == rtx_test::ACCEPT)
4853 return print_acceptance (d->test.u.acceptance, indent, is_final);
4854 else if (d->test.kind == rtx_test::SET_OP)
4856 printf_indent (indent, "operands[%d] = ", d->test.u.opno);
4857 print_test_rtx (os, d->test);
4858 printf (";\n");
4859 return print_state (os, d->singleton ()->to, indent, is_final);
4861 /* Handle decisions with a single transition and a single transition
4862 label. */
4863 else if (d->if_statement_p (&label))
4865 transition *trans = d->singleton ();
4866 if (mark_optional_transitions_p && trans->optional)
4867 printf_indent (indent, "/* OPTIONAL IF */\n");
4869 /* Print the condition associated with TRANS. Invert it if IS_FINAL,
4870 so that we return immediately on failure and fall through on
4871 success. */
4872 printf_indent (indent, "if (");
4873 print_test (os, d->test, trans->is_param, label, is_final);
4875 /* Look for following states that would be handled by this code
4876 on recursion. If they don't need any preparatory statements,
4877 include them in the current "if" statement rather than creating
4878 a new one. */
4879 for (;;)
4881 d = trans->to->singleton ();
4882 if (!d
4883 || d->test.kind == rtx_test::ACCEPT
4884 || d->test.kind == rtx_test::SET_OP
4885 || !d->if_statement_p (&label)
4886 || !test_position_available_p (os, d->test))
4887 break;
4888 trans = d->first;
4889 printf ("\n");
4890 if (mark_optional_transitions_p && trans->optional)
4891 printf_indent (indent + 4, "/* OPTIONAL IF */\n");
4892 printf_indent (indent + 4, "%s ", is_final ? "||" : "&&");
4893 print_test (os, d->test, trans->is_param, label, is_final);
4895 printf (")\n");
4897 /* Print the conditional code with INDENT + 2 and the fallthrough
4898 code with indent INDENT. */
4899 state *to = trans->to;
4900 if (is_final)
4902 /* We inverted the condition above, so return failure in the
4903 "if" body and fall through to the target of the transition. */
4904 printf_indent (indent + 2, "return %s;\n",
4905 get_failure_return (os->type));
4906 return print_state (os, to, indent, is_final);
4908 else if (to->singleton ()
4909 && to->first->test.kind == rtx_test::ACCEPT
4910 && single_statement_p (to->first->test.u.acceptance))
4912 /* The target of the transition is a simple "return" statement.
4913 It doesn't need any braces and doesn't fall through. */
4914 if (print_acceptance (to->first->test.u.acceptance,
4915 indent + 2, true) != ES_RETURNED)
4916 gcc_unreachable ();
4917 return ES_FALLTHROUGH;
4919 else
4921 /* The general case. Output code for the target of the transition
4922 in braces. This will not invalidate any of the xN variables
4923 that are already valid, but we mustn't rely on any that are
4924 set by the "if" body. */
4925 auto_vec <bool, 32> old_seen;
4926 old_seen.safe_splice (os->seen_vars);
4928 printf_indent (indent + 2, "{\n");
4929 print_state (os, trans->to, indent + 4, is_final);
4930 printf_indent (indent + 2, "}\n");
4932 os->seen_vars.truncate (0);
4933 os->seen_vars.splice (old_seen);
4934 return ES_FALLTHROUGH;
4937 else
4939 /* Output the decision as a switch statement. */
4940 printf_indent (indent, "switch (");
4941 print_nonbool_test (os, d->test);
4942 printf (")\n");
4944 /* Each case statement starts with the same set of valid variables.
4945 These are also the only variables will be valid on fallthrough. */
4946 auto_vec <bool, 32> old_seen;
4947 old_seen.safe_splice (os->seen_vars);
4949 printf_indent (indent + 2, "{\n");
4950 for (transition *trans = d->first; trans; trans = trans->next)
4952 gcc_assert (!trans->is_param);
4953 if (mark_optional_transitions_p && trans->optional)
4954 printf_indent (indent + 2, "/* OPTIONAL CASE */\n");
4955 for (int_set::iterator j = trans->labels.begin ();
4956 j != trans->labels.end (); ++j)
4958 printf_indent (indent + 2, "case ");
4959 print_label_value (d->test, trans->is_param, *j);
4960 printf (":\n");
4962 if (print_state (os, trans->to, indent + 4, is_final))
4964 /* The state can fall through. Add an explicit break. */
4965 gcc_assert (!is_final);
4966 printf_indent (indent + 4, "break;\n");
4968 printf ("\n");
4970 /* Restore the original set of valid variables. */
4971 os->seen_vars.truncate (0);
4972 os->seen_vars.splice (old_seen);
4974 /* Add a default case. */
4975 printf_indent (indent + 2, "default:\n");
4976 if (is_final)
4977 printf_indent (indent + 4, "return %s;\n",
4978 get_failure_return (os->type));
4979 else
4980 printf_indent (indent + 4, "break;\n");
4981 printf_indent (indent + 2, "}\n");
4982 return is_final ? ES_RETURNED : ES_FALLTHROUGH;
4986 /* Make sure that OS has a position variable for POS. ROOT_P is true if
4987 POS is the root position for the routine. */
4989 static void
4990 assign_position_var (output_state *os, position *pos, bool root_p)
4992 unsigned int idx = os->id_to_var[pos->id];
4993 if (idx < os->var_to_id.length () && os->var_to_id[idx] == pos->id)
4994 return;
4995 if (!root_p && pos->type != POS_PEEP2_INSN)
4996 assign_position_var (os, pos->base, false);
4997 os->id_to_var[pos->id] = os->var_to_id.length ();
4998 os->var_to_id.safe_push (pos->id);
5001 /* Make sure that OS has the position variables required by S. */
5003 static void
5004 assign_position_vars (output_state *os, state *s)
5006 for (decision *d = s->first; d; d = d->next)
5008 /* Positions associated with operands can be read from the
5009 operands[] array. */
5010 if (d->test.pos && d->test.pos_operand < 0)
5011 assign_position_var (os, d->test.pos, false);
5012 for (transition *trans = d->first; trans; trans = trans->next)
5013 assign_position_vars (os, trans->to);
5017 /* Print the open brace and variable definitions for a routine that
5018 implements S. ROOT is the deepest rtx from which S can access all
5019 relevant parts of the first instruction it matches. Initialize OS
5020 so that every relevant position has an rtx variable xN and so that
5021 only ROOT's variable has a valid value. */
5023 static void
5024 print_subroutine_start (output_state *os, state *s, position *root)
5026 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED"
5027 " = &recog_data.operand[0];\n");
5028 os->var_to_id.truncate (0);
5029 os->seen_vars.truncate (0);
5030 if (root)
5032 /* Create a fake entry for position 0 so that an id_to_var of 0
5033 is always invalid. This also makes the xN variables naturally
5034 1-based rather than 0-based. */
5035 os->var_to_id.safe_push (num_positions);
5037 /* Associate ROOT with x1. */
5038 assign_position_var (os, root, true);
5040 /* Assign xN variables to all other relevant positions. */
5041 assign_position_vars (os, s);
5043 /* Output the variable declarations (except for ROOT's, which is
5044 passed in as a parameter). */
5045 unsigned int num_vars = os->var_to_id.length ();
5046 if (num_vars > 2)
5048 for (unsigned int i = 2; i < num_vars; ++i)
5049 /* Print 8 rtx variables to a line. */
5050 printf ("%s x%d",
5051 i == 2 ? " rtx" : (i - 2) % 8 == 0 ? ";\n rtx" : ",", i);
5052 printf (";\n");
5055 /* Say that x1 is valid and the rest aren't. */
5056 os->seen_vars.safe_grow_cleared (num_vars);
5057 os->seen_vars[1] = true;
5059 if (os->type == SUBPATTERN || os->type == RECOG)
5060 printf (" int res ATTRIBUTE_UNUSED;\n");
5061 else
5062 printf (" rtx_insn *res ATTRIBUTE_UNUSED;\n");
5065 /* Output the definition of pattern routine ROUTINE. */
5067 static void
5068 print_pattern (output_state *os, pattern_routine *routine)
5070 printf ("\nstatic int\npattern%d (", routine->pattern_id);
5071 const char *sep = "";
5072 /* Add the top-level rtx parameter, if any. */
5073 if (routine->pos)
5075 printf ("%srtx x1", sep);
5076 sep = ", ";
5078 /* Add the optional parameters. */
5079 if (routine->insn_p)
5081 /* We can't easily tell whether a C condition actually reads INSN,
5082 so add an ATTRIBUTE_UNUSED just in case. */
5083 printf ("%srtx_insn *insn ATTRIBUTE_UNUSED", sep);
5084 sep = ", ";
5086 if (routine->pnum_clobbers_p)
5088 printf ("%sint *pnum_clobbers", sep);
5089 sep = ", ";
5091 /* Add the "i" parameters. */
5092 for (unsigned int i = 0; i < routine->param_types.length (); ++i)
5094 printf ("%s%s i%d", sep,
5095 parameter_type_string (routine->param_types[i]), i + 1);
5096 sep = ", ";
5098 printf (")\n");
5099 os->type = SUBPATTERN;
5100 print_subroutine_start (os, routine->s, routine->pos);
5101 print_state (os, routine->s, 2, true);
5102 printf ("}\n");
5105 /* Output a routine of type TYPE that implements S. PROC_ID is the
5106 number of the subroutine associated with S, or 0 if S is the main
5107 routine. */
5109 static void
5110 print_subroutine (output_state *os, state *s, int proc_id)
5112 /* For now, the top-level "recog" takes a plain "rtx", and performs a
5113 checked cast to "rtx_insn *" for use throughout the rest of the
5114 function and the code it calls. */
5115 const char *insn_param
5116 = proc_id > 0 ? "rtx_insn *insn" : "rtx uncast_insn";
5117 printf ("\n");
5118 switch (os->type)
5120 case SUBPATTERN:
5121 gcc_unreachable ();
5123 case RECOG:
5124 if (proc_id)
5125 printf ("static int\nrecog_%d", proc_id);
5126 else
5127 printf ("int\nrecog");
5128 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5129 "\t%s ATTRIBUTE_UNUSED,\n"
5130 "\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", insn_param);
5131 break;
5133 case SPLIT:
5134 if (proc_id)
5135 printf ("static rtx_insn *\nsplit_%d", proc_id);
5136 else
5137 printf ("rtx_insn *\nsplit_insns");
5138 printf (" (rtx x1 ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n");
5139 break;
5141 case PEEPHOLE2:
5142 if (proc_id)
5143 printf ("static rtx_insn *\npeephole2_%d", proc_id);
5144 else
5145 printf ("rtx_insn *\npeephole2_insns");
5146 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n"
5147 "\trtx_insn *insn ATTRIBUTE_UNUSED,\n"
5148 "\tint *pmatch_len_ ATTRIBUTE_UNUSED)\n");
5149 break;
5151 print_subroutine_start (os, s, &root_pos);
5152 if (proc_id == 0)
5154 printf (" recog_data.insn = NULL;\n");
5155 if (os->type == RECOG)
5157 printf (" rtx_insn *insn ATTRIBUTE_UNUSED;\n");
5158 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 for the list of rtxes in a define_peephole2. */
5190 static rtx
5191 get_peephole2_pattern (rtvec vec)
5193 int i, j;
5194 rtx pattern = rtx_alloc (SEQUENCE);
5195 XVEC (pattern, 0) = rtvec_alloc (GET_NUM_ELEM (vec));
5196 for (i = j = 0; i < GET_NUM_ELEM (vec); i++)
5198 rtx x = RTVEC_ELT (vec, i);
5199 /* Ignore scratch register requirements. */
5200 if (GET_CODE (x) != MATCH_SCRATCH && GET_CODE (x) != MATCH_DUP)
5202 XVECEXP (pattern, 0, j) = x;
5203 j++;
5206 XVECLEN (pattern, 0) = j;
5207 if (j == 0)
5208 error_with_line (pattern_lineno, "empty define_peephole2");
5209 return pattern;
5212 /* Return true if *PATTERN_PTR is a PARALLEL in which at least one trailing
5213 rtx can be added automatically by add_clobbers. If so, update
5214 *ACCEPTANCE_PTR so that its num_clobbers field contains the number
5215 of such trailing rtxes and update *PATTERN_PTR so that it contains
5216 the pattern without those rtxes. */
5218 static bool
5219 remove_clobbers (acceptance_type *acceptance_ptr, rtx *pattern_ptr)
5221 int i;
5222 rtx new_pattern;
5224 /* Find the last non-clobber in the parallel. */
5225 rtx pattern = *pattern_ptr;
5226 for (i = XVECLEN (pattern, 0); i > 0; i--)
5228 rtx x = XVECEXP (pattern, 0, i - 1);
5229 if (GET_CODE (x) != CLOBBER
5230 || (!REG_P (XEXP (x, 0))
5231 && GET_CODE (XEXP (x, 0)) != MATCH_SCRATCH))
5232 break;
5235 if (i == XVECLEN (pattern, 0))
5236 return false;
5238 /* Build a similar insn without the clobbers. */
5239 if (i == 1)
5240 new_pattern = XVECEXP (pattern, 0, 0);
5241 else
5243 new_pattern = rtx_alloc (PARALLEL);
5244 XVEC (new_pattern, 0) = rtvec_alloc (i);
5245 for (int j = 0; j < i; ++j)
5246 XVECEXP (new_pattern, 0, j) = XVECEXP (pattern, 0, j);
5249 /* Recognize it. */
5250 acceptance_ptr->u.full.u.num_clobbers = XVECLEN (pattern, 0) - i;
5251 *pattern_ptr = new_pattern;
5252 return true;
5256 main (int argc, char **argv)
5258 rtx desc;
5259 state insn_root, split_root, peephole2_root;
5261 progname = "genrecog";
5263 if (!init_rtx_reader_args (argc, argv))
5264 return (FATAL_EXIT_CODE);
5266 next_insn_code = 0;
5268 write_header ();
5270 /* Read the machine description. */
5272 while (1)
5274 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
5275 if (desc == NULL)
5276 break;
5278 acceptance_type acceptance;
5279 acceptance.partial_p = false;
5280 acceptance.u.full.code = next_insn_code;
5282 rtx pattern;
5283 switch (GET_CODE (desc))
5285 case DEFINE_INSN:
5287 /* Match the instruction in the original .md form. */
5288 acceptance.type = RECOG;
5289 acceptance.u.full.u.num_clobbers = 0;
5290 pattern = add_implicit_parallel (XVEC (desc, 1));
5291 validate_pattern (pattern, desc, NULL_RTX, 0);
5292 match_pattern (&insn_root, pattern, XSTR (desc, 2), acceptance);
5294 /* If the pattern is a PARALLEL with trailing CLOBBERs,
5295 allow recog_for_combine to match without the clobbers. */
5296 if (GET_CODE (pattern) == PARALLEL
5297 && remove_clobbers (&acceptance, &pattern))
5298 match_pattern (&insn_root, pattern, XSTR (desc, 2), acceptance);
5299 break;
5302 case DEFINE_SPLIT:
5303 acceptance.type = SPLIT;
5304 pattern = add_implicit_parallel (XVEC (desc, 0));
5305 validate_pattern (pattern, desc, NULL_RTX, 0);
5306 match_pattern (&split_root, pattern, XSTR (desc, 1), acceptance);
5308 /* Declare the gen_split routine that we'll call if the
5309 pattern matches. The definition comes from insn-emit.c. */
5310 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
5311 next_insn_code);
5312 break;
5314 case DEFINE_PEEPHOLE2:
5315 acceptance.type = PEEPHOLE2;
5316 pattern = get_peephole2_pattern (XVEC (desc, 0));
5317 validate_pattern (pattern, desc, NULL_RTX, 0);
5318 match_pattern (&peephole2_root, pattern, XSTR (desc, 1), acceptance);
5320 /* Declare the gen_peephole2 routine that we'll call if the
5321 pattern matches. The definition comes from insn-emit.c. */
5322 printf ("extern rtx_insn *gen_peephole2_%d (rtx_insn *, rtx *);\n",
5323 next_insn_code);
5324 break;
5326 default:
5327 /* do nothing */;
5331 if (have_error)
5332 return FATAL_EXIT_CODE;
5334 puts ("\n\n");
5336 /* Optimize each routine in turn. */
5337 optimize_subroutine_group ("recog", &insn_root);
5338 optimize_subroutine_group ("split_insns", &split_root);
5339 optimize_subroutine_group ("peephole2_insns", &peephole2_root);
5341 output_state os;
5342 os.id_to_var.safe_grow_cleared (num_positions);
5344 if (use_pattern_routines_p)
5346 /* Look for common patterns and split them out into subroutines. */
5347 auto_vec <merge_state_info> states;
5348 states.safe_push (&insn_root);
5349 states.safe_push (&split_root);
5350 states.safe_push (&peephole2_root);
5351 split_out_patterns (states);
5353 /* Print out the routines that we just created. */
5354 unsigned int i;
5355 pattern_routine *routine;
5356 FOR_EACH_VEC_ELT (patterns, i, routine)
5357 print_pattern (&os, routine);
5360 /* Print out the matching routines. */
5361 print_subroutine_group (&os, RECOG, &insn_root);
5362 print_subroutine_group (&os, SPLIT, &split_root);
5363 print_subroutine_group (&os, PEEPHOLE2, &peephole2_root);
5365 fflush (stdout);
5366 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);