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