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