]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genrecog.c
dwarf2out.c (decl_ultimate_origin): Update comment.
[thirdparty/gcc.git] / gcc / genrecog.c
CommitLineData
ec65fa66 1/* Generate code from machine description to recognize rtl as insns.
23a5b65a 2 Copyright (C) 1987-2014 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
09051660 49 to the last recognized insn in the old sequence. */
ec65fa66 50
4977bab6 51#include "bconfig.h"
0b93b64e 52#include "system.h"
4977bab6
ZW
53#include "coretypes.h"
54#include "tm.h"
ec65fa66 55#include "rtl.h"
f8b6598e 56#include "errors.h"
10692477 57#include "read-md.h"
c88c0d42 58#include "gensupport.h"
ec65fa66 59
736b02fd 60#define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
c3284718 61 printf ("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
736b02fd 62
6a1a787e
RS
63/* Ways of obtaining an rtx to be tested. */
64enum position_type {
65 /* PATTERN (peep2_next_insn (ARG)). */
66 POS_PEEP2_INSN,
67
68 /* XEXP (BASE, ARG). */
69 POS_XEXP,
70
71 /* XVECEXP (BASE, 0, ARG). */
72 POS_XVECEXP0
73};
74
75/* The position of an rtx relative to X0. Each useful position is
76 represented by exactly one instance of this structure. */
77struct position
78{
79 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */
80 struct position *base;
81
82 /* A position with the same BASE and TYPE, but with the next value
83 of ARG. */
84 struct position *next;
85
86 /* A list of all POS_XEXP positions that use this one as their base,
87 chained by NEXT fields. The first entry represents XEXP (this, 0),
88 the second represents XEXP (this, 1), and so on. */
89 struct position *xexps;
90
91 /* A list of POS_XVECEXP0 positions that use this one as their base,
92 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0),
93 the second represents XVECEXP (this, 0, 1), and so on. */
94 struct position *xvecexp0s;
95
96 /* The type of position. */
97 enum position_type type;
98
99 /* The argument to TYPE (shown as ARG in the position_type comments). */
100 int arg;
101
102 /* The depth of this position, with 0 as the root. */
103 int depth;
104};
105
09051660 106/* A listhead of decision trees. The alternatives to a node are kept
fbe5a4a6 107 in a doubly-linked list so we can easily add nodes to the proper
09051660
RH
108 place when merging. */
109
110struct decision_head
111{
112 struct decision *first;
113 struct decision *last;
114};
5b7c7046 115
24b97832
ILT
116/* These types are roughly in the order in which we'd like to test them. */
117enum decision_type
118{
119 DT_num_insns,
120 DT_mode, DT_code, DT_veclen,
121 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide, DT_elt_zero_wide_safe,
122 DT_const_int,
123 DT_veclen_ge, DT_dup, DT_pred, DT_c_test,
124 DT_accept_op, DT_accept_insn
125};
126
09051660
RH
127/* A single test. The two accept types aren't tests per-se, but
128 their equality (or lack thereof) does affect tree merging so
129 it is convenient to keep them here. */
130
131struct decision_test
132{
133 /* A linked list through the tests attached to a node. */
134 struct decision_test *next;
135
24b97832 136 enum decision_type type;
09051660
RH
137
138 union
139 {
0cd6c85a 140 int num_insns; /* Number if insn in a define_peephole2. */
09051660
RH
141 enum machine_mode mode; /* Machine mode of node. */
142 RTX_CODE code; /* Code to test. */
e0689256 143
09051660
RH
144 struct
145 {
146 const char *name; /* Predicate to call. */
e543e219
ZW
147 const struct pred_data *data;
148 /* Optimization hints for this predicate. */
09051660
RH
149 enum machine_mode mode; /* Machine mode for node. */
150 } pred;
151
152 const char *c_test; /* Additional test to perform. */
153 int veclen; /* Length of vector. */
154 int dup; /* Number of operand to compare against. */
155 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */
156 int opno; /* Operand number matched. */
157
158 struct {
159 int code_number; /* Insn number matched. */
bcdaba58 160 int lineno; /* Line number of the insn. */
09051660
RH
161 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
162 } insn;
163 } u;
164};
e0689256 165
09051660 166/* Data structure for decision tree for recognizing legitimate insns. */
ec65fa66
RK
167
168struct decision
169{
09051660
RH
170 struct decision_head success; /* Nodes to test on success. */
171 struct decision *next; /* Node to test on failure. */
172 struct decision *prev; /* Node whose failure tests us. */
173 struct decision *afterward; /* Node to test on success,
174 but failure of successor nodes. */
175
6a1a787e 176 struct position *position; /* Position in pattern. */
09051660
RH
177
178 struct decision_test *tests; /* The tests for this node. */
179
e0689256 180 int number; /* Node number, used for labels */
e0689256 181 int subroutine_number; /* Number of subroutine this node starts */
09051660 182 int need_label; /* Label needs to be output. */
ec65fa66
RK
183};
184
09051660 185#define SUBROUTINE_THRESHOLD 100
ec65fa66
RK
186
187static int next_subroutine_number;
188
ede7cd44
RH
189/* We can write three types of subroutines: One for insn recognition,
190 one to split insns, and one for peephole-type optimizations. This
191 defines which type is being written. */
ec65fa66 192
09051660
RH
193enum routine_type {
194 RECOG, SPLIT, PEEPHOLE2
195};
ede7cd44 196
09051660 197#define IS_SPLIT(X) ((X) != RECOG)
ec65fa66 198
e0689256 199/* Next available node number for tree nodes. */
ec65fa66 200
e0689256 201static int next_number;
ec65fa66 202
e0689256 203/* Next number to use as an insn_code. */
ec65fa66 204
e0689256 205static int next_insn_code;
ec65fa66 206
e0689256
RK
207/* Record the highest depth we ever have so we know how many variables to
208 allocate in each subroutine we make. */
ec65fa66 209
e0689256 210static int max_depth;
bcdaba58
RH
211
212/* The line number of the start of the pattern currently being processed. */
213static int pattern_lineno;
6a1a787e
RS
214
215/* The root position (x0). */
216static struct position root_pos;
217
218/* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position,
219 since we are given that instruction's pattern as x0. */
220static struct position *peep2_insn_pos_list = &root_pos;
e543e219 221\f
09051660 222extern void debug_decision
3d7aafde 223 (struct decision *);
36f0e0a6 224extern void debug_decision_list
3d7aafde 225 (struct decision *);
ede7cd44 226\f
6a1a787e
RS
227/* Return a position with the given BASE, TYPE and ARG. NEXT_PTR
228 points to where the unique object that represents the position
229 should be stored. Create the object if it doesn't already exist,
230 otherwise reuse the object that is already there. */
231
232static struct position *
233next_position (struct position **next_ptr, struct position *base,
234 enum position_type type, int arg)
235{
236 struct position *pos;
237
238 pos = *next_ptr;
239 if (!pos)
240 {
241 pos = XCNEW (struct position);
242 pos->base = base;
243 pos->type = type;
244 pos->arg = arg;
245 pos->depth = base->depth + 1;
246 *next_ptr = pos;
247 }
248 return pos;
249}
250
251/* Compare positions POS1 and POS2 lexicographically. */
252
253static int
254compare_positions (struct position *pos1, struct position *pos2)
255{
256 int diff;
257
258 diff = pos1->depth - pos2->depth;
259 if (diff < 0)
260 do
261 pos2 = pos2->base;
262 while (pos1->depth != pos2->depth);
263 else if (diff > 0)
264 do
265 pos1 = pos1->base;
266 while (pos1->depth != pos2->depth);
267 while (pos1 != pos2)
268 {
269 diff = (int) pos1->type - (int) pos2->type;
270 if (diff == 0)
271 diff = pos1->arg - pos2->arg;
272 pos1 = pos1->base;
273 pos2 = pos2->base;
274 }
275 return diff;
276}
277
09051660 278/* Create a new node in sequence after LAST. */
e0689256 279
09051660 280static struct decision *
6a1a787e 281new_decision (struct position *pos, struct decision_head *last)
ec65fa66 282{
8ad97cfc 283 struct decision *new_decision = XCNEW (struct decision);
ec65fa66 284
8ad97cfc 285 new_decision->success = *last;
6a1a787e 286 new_decision->position = pos;
8ad97cfc 287 new_decision->number = next_number++;
ec65fa66 288
8ad97cfc
KG
289 last->first = last->last = new_decision;
290 return new_decision;
09051660 291}
e0689256 292
09051660 293/* Create a new test and link it in at PLACE. */
ec65fa66 294
09051660 295static struct decision_test *
3d7aafde 296new_decision_test (enum decision_type type, struct decision_test ***pplace)
09051660
RH
297{
298 struct decision_test **place = *pplace;
299 struct decision_test *test;
ec65fa66 300
5ed6ace5 301 test = XNEW (struct decision_test);
09051660
RH
302 test->next = *place;
303 test->type = type;
304 *place = test;
ede7cd44 305
09051660
RH
306 place = &test->next;
307 *pplace = place;
ec65fa66 308
09051660 309 return test;
e0689256 310}
09051660 311
076963eb 312/* Search for and return operand N, stop when reaching node STOP. */
8fe0ca0c
RH
313
314static rtx
076963eb 315find_operand (rtx pattern, int n, rtx stop)
8fe0ca0c
RH
316{
317 const char *fmt;
318 RTX_CODE code;
319 int i, j, len;
320 rtx r;
321
076963eb
JH
322 if (pattern == stop)
323 return stop;
324
8fe0ca0c
RH
325 code = GET_CODE (pattern);
326 if ((code == MATCH_SCRATCH
8fe0ca0c
RH
327 || code == MATCH_OPERAND
328 || code == MATCH_OPERATOR
329 || code == MATCH_PARALLEL)
330 && XINT (pattern, 0) == n)
331 return pattern;
332
333 fmt = GET_RTX_FORMAT (code);
334 len = GET_RTX_LENGTH (code);
335 for (i = 0; i < len; i++)
336 {
337 switch (fmt[i])
338 {
339 case 'e': case 'u':
076963eb 340 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
8fe0ca0c
RH
341 return r;
342 break;
343
c0ea284b
RH
344 case 'V':
345 if (! XVEC (pattern, i))
346 break;
5d3cc252 347 /* Fall through. */
c0ea284b 348
8fe0ca0c
RH
349 case 'E':
350 for (j = 0; j < XVECLEN (pattern, i); j++)
076963eb
JH
351 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
352 != NULL_RTX)
8fe0ca0c
RH
353 return r;
354 break;
355
356 case 'i': case 'w': case '0': case 's':
357 break;
358
359 default:
b2d59f6f 360 gcc_unreachable ();
8fe0ca0c
RH
361 }
362 }
363
364 return NULL;
365}
366
c0ea284b
RH
367/* Search for and return operand M, such that it has a matching
368 constraint for operand N. */
369
370static rtx
3d7aafde 371find_matching_operand (rtx pattern, int n)
c0ea284b
RH
372{
373 const char *fmt;
374 RTX_CODE code;
375 int i, j, len;
376 rtx r;
377
378 code = GET_CODE (pattern);
379 if (code == MATCH_OPERAND
380 && (XSTR (pattern, 2)[0] == '0' + n
381 || (XSTR (pattern, 2)[0] == '%'
382 && XSTR (pattern, 2)[1] == '0' + n)))
383 return pattern;
384
385 fmt = GET_RTX_FORMAT (code);
386 len = GET_RTX_LENGTH (code);
387 for (i = 0; i < len; i++)
388 {
389 switch (fmt[i])
390 {
391 case 'e': case 'u':
392 if ((r = find_matching_operand (XEXP (pattern, i), n)))
393 return r;
394 break;
395
396 case 'V':
397 if (! XVEC (pattern, i))
398 break;
5d3cc252 399 /* Fall through. */
c0ea284b
RH
400
401 case 'E':
402 for (j = 0; j < XVECLEN (pattern, i); j++)
403 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
404 return r;
405 break;
406
407 case 'i': case 'w': case '0': case 's':
408 break;
409
410 default:
b2d59f6f 411 gcc_unreachable ();
c0ea284b
RH
412 }
413 }
414
415 return NULL;
416}
417
5fd4bc96
JG
418/* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
419 don't use the MATCH_OPERAND constraint, only the predicate.
420 This is confusing to folks doing new ports, so help them
421 not make the mistake. */
422
423static bool
424constraints_supported_in_insn_p (rtx insn)
425{
426 return !(GET_CODE (insn) == DEFINE_EXPAND
427 || GET_CODE (insn) == DEFINE_SPLIT
428 || GET_CODE (insn) == DEFINE_PEEPHOLE2);
429}
c0ea284b 430
aece2740 431/* Check for various errors in patterns. SET is nonnull for a destination,
7297e9fc
RH
432 and is the complete set pattern. SET_CODE is '=' for normal sets, and
433 '+' within a context that requires in-out constraints. */
bcdaba58
RH
434
435static void
3d7aafde 436validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
bcdaba58
RH
437{
438 const char *fmt;
439 RTX_CODE code;
8fe0ca0c
RH
440 size_t i, len;
441 int j;
bcdaba58
RH
442
443 code = GET_CODE (pattern);
444 switch (code)
445 {
446 case MATCH_SCRATCH:
5fd4bc96
JG
447 {
448 const char constraints0 = XSTR (pattern, 1)[0];
449
450 if (!constraints_supported_in_insn_p (insn))
451 {
452 if (constraints0)
453 {
454 error_with_line (pattern_lineno,
455 "constraints not supported in %s",
456 rtx_name[GET_CODE (insn)]);
457 }
458 return;
459 }
460
461 /* If a MATCH_SCRATCH is used in a context requiring an write-only
462 or read/write register, validate that. */
463 if (set_code == '='
464 && constraints0 != '='
465 && constraints0 != '+')
466 {
467 error_with_line (pattern_lineno,
468 "operand %d missing output reload",
469 XINT (pattern, 0));
470 }
471 return;
472 }
076963eb
JH
473 case MATCH_DUP:
474 case MATCH_OP_DUP:
475 case MATCH_PAR_DUP:
476 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern)
bb933490
RS
477 error_with_line (pattern_lineno,
478 "operand %i duplicated before defined",
479 XINT (pattern, 0));
076963eb 480 break;
bcdaba58 481 case MATCH_OPERAND:
8fe0ca0c 482 case MATCH_OPERATOR:
bcdaba58
RH
483 {
484 const char *pred_name = XSTR (pattern, 1);
e543e219 485 const struct pred_data *pred;
8fe0ca0c
RH
486 const char *c_test;
487
488 if (GET_CODE (insn) == DEFINE_INSN)
489 c_test = XSTR (insn, 2);
490 else
491 c_test = XSTR (insn, 1);
bcdaba58
RH
492
493 if (pred_name[0] != 0)
494 {
e543e219
ZW
495 pred = lookup_predicate (pred_name);
496 if (!pred)
525f6ed7
RS
497 error_with_line (pattern_lineno, "unknown predicate '%s'",
498 pred_name);
8fe0ca0c 499 }
e543e219
ZW
500 else
501 pred = 0;
8fe0ca0c 502
0dab343a 503 if (code == MATCH_OPERAND)
aece2740 504 {
0dab343a
RH
505 const char constraints0 = XSTR (pattern, 2)[0];
506
5fd4bc96 507 if (!constraints_supported_in_insn_p (insn))
7297e9fc 508 {
0dab343a 509 if (constraints0)
5fd4bc96
JG
510 {
511 error_with_line (pattern_lineno,
512 "constraints not supported in %s",
513 rtx_name[GET_CODE (insn)]);
514 }
0dab343a 515 }
3d7aafde 516
0dab343a
RH
517 /* A MATCH_OPERAND that is a SET should have an output reload. */
518 else if (set && constraints0)
519 {
520 if (set_code == '+')
521 {
522 if (constraints0 == '+')
523 ;
524 /* If we've only got an output reload for this operand,
525 we'd better have a matching input operand. */
526 else if (constraints0 == '='
527 && find_matching_operand (insn, XINT (pattern, 0)))
528 ;
529 else
bb933490
RS
530 error_with_line (pattern_lineno,
531 "operand %d missing in-out reload",
c0ea284b 532 XINT (pattern, 0));
c0ea284b 533 }
bb933490
RS
534 else if (constraints0 != '=' && constraints0 != '+')
535 error_with_line (pattern_lineno,
536 "operand %d missing output reload",
537 XINT (pattern, 0));
7297e9fc 538 }
aece2740
RH
539 }
540
8fe0ca0c
RH
541 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
542 while not likely to occur at runtime, results in less efficient
543 code from insn-recog.c. */
e543e219 544 if (set && pred && pred->allows_non_lvalue)
525f6ed7
RS
545 error_with_line (pattern_lineno,
546 "destination operand %d allows non-lvalue",
547 XINT (pattern, 0));
8fe0ca0c 548
e543e219
ZW
549 /* A modeless MATCH_OPERAND can be handy when we can check for
550 multiple modes in the c_test. In most other cases, it is a
551 mistake. Only DEFINE_INSN is eligible, since SPLIT and
552 PEEP2 can FAIL within the output pattern. Exclude special
553 predicates, which check the mode themselves. Also exclude
554 predicates that allow only constants. Exclude the SET_DEST
555 of a call instruction, as that is a common idiom. */
8fe0ca0c
RH
556
557 if (GET_MODE (pattern) == VOIDmode
558 && code == MATCH_OPERAND
556ffcc5 559 && GET_CODE (insn) == DEFINE_INSN
e543e219
ZW
560 && pred
561 && !pred->special
562 && pred->allows_non_const
aece2740
RH
563 && strstr (c_test, "operands") == NULL
564 && ! (set
565 && GET_CODE (set) == SET
566 && GET_CODE (SET_SRC (set)) == CALL))
e543e219
ZW
567 message_with_line (pattern_lineno,
568 "warning: operand %d missing mode?",
569 XINT (pattern, 0));
bcdaba58
RH
570 return;
571 }
572
573 case SET:
8fe0ca0c
RH
574 {
575 enum machine_mode dmode, smode;
576 rtx dest, src;
577
578 dest = SET_DEST (pattern);
579 src = SET_SRC (pattern);
580
0dab343a
RH
581 /* STRICT_LOW_PART is a wrapper. Its argument is the real
582 destination, and it's mode should match the source. */
583 if (GET_CODE (dest) == STRICT_LOW_PART)
584 dest = XEXP (dest, 0);
585
d91edf86 586 /* Find the referent for a DUP. */
8fe0ca0c
RH
587
588 if (GET_CODE (dest) == MATCH_DUP
589 || GET_CODE (dest) == MATCH_OP_DUP
590 || GET_CODE (dest) == MATCH_PAR_DUP)
076963eb 591 dest = find_operand (insn, XINT (dest, 0), NULL);
8fe0ca0c
RH
592
593 if (GET_CODE (src) == MATCH_DUP
594 || GET_CODE (src) == MATCH_OP_DUP
595 || GET_CODE (src) == MATCH_PAR_DUP)
076963eb 596 src = find_operand (insn, XINT (src, 0), NULL);
8fe0ca0c 597
8fe0ca0c
RH
598 dmode = GET_MODE (dest);
599 smode = GET_MODE (src);
bcdaba58 600
8fe0ca0c
RH
601 /* The mode of an ADDRESS_OPERAND is the mode of the memory
602 reference, not the mode of the address. */
603 if (GET_CODE (src) == MATCH_OPERAND
604 && ! strcmp (XSTR (src, 1), "address_operand"))
605 ;
606
607 /* The operands of a SET must have the same mode unless one
608 is VOIDmode. */
609 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
bb933490
RS
610 error_with_line (pattern_lineno,
611 "mode mismatch in set: %smode vs %smode",
612 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
8fe0ca0c 613
5b7c7046 614 /* If only one of the operands is VOIDmode, and PC or CC0 is
8fe0ca0c
RH
615 not involved, it's probably a mistake. */
616 else if (dmode != smode
617 && GET_CODE (dest) != PC
618 && GET_CODE (dest) != CC0
aece2740
RH
619 && GET_CODE (src) != PC
620 && GET_CODE (src) != CC0
481683e1 621 && !CONST_INT_P (src)
807e902e 622 && !CONST_WIDE_INT_P (src)
23750d7f 623 && GET_CODE (src) != CALL)
8fe0ca0c
RH
624 {
625 const char *which;
626 which = (dmode == VOIDmode ? "destination" : "source");
627 message_with_line (pattern_lineno,
628 "warning: %s missing a mode?", which);
629 }
630
631 if (dest != SET_DEST (pattern))
7297e9fc
RH
632 validate_pattern (dest, insn, pattern, '=');
633 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
634 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
8fe0ca0c
RH
635 return;
636 }
637
638 case CLOBBER:
7297e9fc
RH
639 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
640 return;
641
642 case ZERO_EXTRACT:
643 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
644 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
645 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
646 return;
647
648 case STRICT_LOW_PART:
649 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
bcdaba58 650 return;
8fe0ca0c 651
bcdaba58 652 case LABEL_REF:
a827d9b1 653 if (GET_MODE (LABEL_REF_LABEL (pattern)) != VOIDmode)
bb933490
RS
654 error_with_line (pattern_lineno,
655 "operand to label_ref %smode not VOIDmode",
a827d9b1 656 GET_MODE_NAME (GET_MODE (LABEL_REF_LABEL (pattern))));
bcdaba58
RH
657 break;
658
659 default:
660 break;
661 }
662
663 fmt = GET_RTX_FORMAT (code);
664 len = GET_RTX_LENGTH (code);
665 for (i = 0; i < len; i++)
666 {
667 switch (fmt[i])
668 {
669 case 'e': case 'u':
7297e9fc 670 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
bcdaba58
RH
671 break;
672
673 case 'E':
674 for (j = 0; j < XVECLEN (pattern, i); j++)
7297e9fc 675 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
bcdaba58
RH
676 break;
677
678 case 'i': case 'w': case '0': case 's':
679 break;
680
681 default:
b2d59f6f 682 gcc_unreachable ();
bcdaba58
RH
683 }
684 }
bcdaba58
RH
685}
686
e0689256
RK
687/* Create a chain of nodes to verify that an rtl expression matches
688 PATTERN.
ec65fa66 689
e0689256
RK
690 LAST is a pointer to the listhead in the previous node in the chain (or
691 in the calling function, for the first node).
ec65fa66 692
6a1a787e 693 POSITION is the current position in the insn.
ec65fa66 694
ede7cd44
RH
695 INSN_TYPE is the type of insn for which we are emitting code.
696
e0689256 697 A pointer to the final node in the chain is returned. */
ec65fa66
RK
698
699static struct decision *
6a1a787e
RS
700add_to_sequence (rtx pattern, struct decision_head *last,
701 struct position *pos, enum routine_type insn_type, int top)
ec65fa66 702{
09051660 703 RTX_CODE code;
8ad97cfc 704 struct decision *this_decision, *sub;
09051660
RH
705 struct decision_test *test;
706 struct decision_test **place;
6a1a787e 707 struct position *subpos, **subpos_ptr;
b3694847
SS
708 size_t i;
709 const char *fmt;
ec65fa66 710 int len;
09051660 711 enum machine_mode mode;
6a1a787e 712 enum position_type pos_type;
ec65fa66 713
6a1a787e
RS
714 if (pos->depth > max_depth)
715 max_depth = pos->depth;
ec65fa66 716
6a1a787e 717 sub = this_decision = new_decision (pos, last);
8ad97cfc 718 place = &this_decision->tests;
ec65fa66 719
09051660
RH
720 mode = GET_MODE (pattern);
721 code = GET_CODE (pattern);
ec65fa66
RK
722
723 switch (code)
724 {
ede7cd44 725 case PARALLEL:
dc297297 726 /* Toplevel peephole pattern. */
ede7cd44
RH
727 if (insn_type == PEEPHOLE2 && top)
728 {
0cd6c85a
PB
729 int num_insns;
730
731 /* Check we have sufficient insns. This avoids complications
732 because we then know peep2_next_insn never fails. */
733 num_insns = XVECLEN (pattern, 0);
734 if (num_insns > 1)
735 {
736 test = new_decision_test (DT_num_insns, &place);
737 test->u.num_insns = num_insns;
738 last = &sub->success;
739 }
740 else
741 {
742 /* We don't need the node we just created -- unlink it. */
743 last->first = last->last = NULL;
744 }
ede7cd44 745
6a1a787e 746 subpos_ptr = &peep2_insn_pos_list;
ede7cd44
RH
747 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
748 {
6a1a787e
RS
749 subpos = next_position (subpos_ptr, &root_pos,
750 POS_PEEP2_INSN, i);
09051660
RH
751 sub = add_to_sequence (XVECEXP (pattern, 0, i),
752 last, subpos, insn_type, 0);
753 last = &sub->success;
6a1a787e 754 subpos_ptr = &subpos->next;
ede7cd44 755 }
b548dffb 756 goto ret;
ede7cd44 757 }
09051660
RH
758
759 /* Else nothing special. */
ede7cd44 760 break;
09051660 761
521b9224
RH
762 case MATCH_PARALLEL:
763 /* The explicit patterns within a match_parallel enforce a minimum
764 length on the vector. The match_parallel predicate may allow
765 for more elements. We do need to check for this minimum here
766 or the code generated to match the internals may reference data
767 beyond the end of the vector. */
768 test = new_decision_test (DT_veclen_ge, &place);
769 test->u.veclen = XVECLEN (pattern, 2);
5d3cc252 770 /* Fall through. */
521b9224 771
ec65fa66 772 case MATCH_OPERAND:
ec65fa66 773 case MATCH_SCRATCH:
ec65fa66 774 case MATCH_OPERATOR:
09051660 775 {
09051660 776 RTX_CODE was_code = code;
e543e219
ZW
777 const char *pred_name;
778 bool allows_const_int = true;
09051660
RH
779
780 if (code == MATCH_SCRATCH)
781 {
782 pred_name = "scratch_operand";
783 code = UNKNOWN;
784 }
785 else
786 {
787 pred_name = XSTR (pattern, 1);
788 if (code == MATCH_PARALLEL)
789 code = PARALLEL;
790 else
791 code = UNKNOWN;
792 }
793
29360e56 794 if (pred_name[0] != 0)
09051660 795 {
e543e219
ZW
796 const struct pred_data *pred;
797
09051660
RH
798 test = new_decision_test (DT_pred, &place);
799 test->u.pred.name = pred_name;
800 test->u.pred.mode = mode;
801
e543e219
ZW
802 /* See if we know about this predicate.
803 If we do, remember it for use below.
e0689256 804
e543e219
ZW
805 We can optimize the generated code a little if either
806 (a) the predicate only accepts one code, or (b) the
807e902e
KZ
807 predicate does not allow CONST_INT or CONST_WIDE_INT,
808 in which case it can match only if the modes match. */
e543e219
ZW
809 pred = lookup_predicate (pred_name);
810 if (pred)
9edd4689 811 {
e543e219 812 test->u.pred.data = pred;
807e902e
KZ
813 allows_const_int = (pred->codes[CONST_INT]
814 || pred->codes[CONST_WIDE_INT]);
47d2cee5
ZW
815 if (was_code == MATCH_PARALLEL
816 && pred->singleton != PARALLEL)
525f6ed7
RS
817 error_with_line (pattern_lineno,
818 "predicate '%s' used in match_parallel "
819 "does not allow only PARALLEL", pred->name);
47d2cee5
ZW
820 else
821 code = pred->singleton;
9edd4689 822 }
47d2cee5 823 else
525f6ed7
RS
824 error_with_line (pattern_lineno,
825 "unknown predicate '%s' in '%s' expression",
826 pred_name, GET_RTX_NAME (was_code));
09051660 827 }
ec1c89e6
RH
828
829 /* Can't enforce a mode if we allow const_int. */
830 if (allows_const_int)
831 mode = VOIDmode;
e0689256 832
89dbed81 833 /* Accept the operand, i.e. record it in `operands'. */
09051660
RH
834 test = new_decision_test (DT_accept_op, &place);
835 test->u.opno = XINT (pattern, 0);
e0689256 836
09051660
RH
837 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
838 {
6a1a787e
RS
839 if (was_code == MATCH_OPERATOR)
840 {
841 pos_type = POS_XEXP;
842 subpos_ptr = &pos->xexps;
843 }
844 else
845 {
846 pos_type = POS_XVECEXP0;
847 subpos_ptr = &pos->xvecexp0s;
848 }
09051660
RH
849 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
850 {
6a1a787e 851 subpos = next_position (subpos_ptr, pos, pos_type, i);
09051660
RH
852 sub = add_to_sequence (XVECEXP (pattern, 2, i),
853 &sub->success, subpos, insn_type, 0);
6a1a787e 854 subpos_ptr = &subpos->next;
09051660
RH
855 }
856 }
857 goto fini;
858 }
ec65fa66
RK
859
860 case MATCH_OP_DUP:
09051660
RH
861 code = UNKNOWN;
862
863 test = new_decision_test (DT_dup, &place);
864 test->u.dup = XINT (pattern, 0);
865
866 test = new_decision_test (DT_accept_op, &place);
867 test->u.opno = XINT (pattern, 0);
868
a601a53a 869 subpos_ptr = &pos->xexps;
e51712db 870 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
ec65fa66 871 {
a601a53a 872 subpos = next_position (subpos_ptr, pos, POS_XEXP, i);
09051660
RH
873 sub = add_to_sequence (XVECEXP (pattern, 1, i),
874 &sub->success, subpos, insn_type, 0);
6a1a787e 875 subpos_ptr = &subpos->next;
ec65fa66 876 }
09051660 877 goto fini;
ec65fa66
RK
878
879 case MATCH_DUP:
f582c9d5 880 case MATCH_PAR_DUP:
09051660
RH
881 code = UNKNOWN;
882
883 test = new_decision_test (DT_dup, &place);
884 test->u.dup = XINT (pattern, 0);
885 goto fini;
ec65fa66 886
76d31c63
JL
887 default:
888 break;
ec65fa66
RK
889 }
890
891 fmt = GET_RTX_FORMAT (code);
892 len = GET_RTX_LENGTH (code);
09051660
RH
893
894 /* Do tests against the current node first. */
e51712db 895 for (i = 0; i < (size_t) len; i++)
ec65fa66 896 {
09051660 897 if (fmt[i] == 'i')
ec65fa66 898 {
b2d59f6f 899 gcc_assert (i < 2);
5abc5de9 900
b2d59f6f 901 if (!i)
09051660
RH
902 {
903 test = new_decision_test (DT_elt_zero_int, &place);
904 test->u.intval = XINT (pattern, i);
905 }
b2d59f6f 906 else
09051660
RH
907 {
908 test = new_decision_test (DT_elt_one_int, &place);
909 test->u.intval = XINT (pattern, i);
910 }
ec65fa66 911 }
09051660 912 else if (fmt[i] == 'w')
3d678dca 913 {
070ef6f4
RK
914 /* If this value actually fits in an int, we can use a switch
915 statement here, so indicate that. */
916 enum decision_type type
917 = ((int) XWINT (pattern, i) == XWINT (pattern, i))
918 ? DT_elt_zero_wide_safe : DT_elt_zero_wide;
919
b2d59f6f 920 gcc_assert (!i);
09051660 921
070ef6f4 922 test = new_decision_test (type, &place);
09051660 923 test->u.intval = XWINT (pattern, i);
3d678dca 924 }
ec65fa66
RK
925 else if (fmt[i] == 'E')
926 {
b2d59f6f 927 gcc_assert (!i);
09051660
RH
928
929 test = new_decision_test (DT_veclen, &place);
930 test->u.veclen = XVECLEN (pattern, i);
931 }
932 }
933
934 /* Now test our sub-patterns. */
6a1a787e 935 subpos_ptr = &pos->xexps;
09051660
RH
936 for (i = 0; i < (size_t) len; i++)
937 {
6a1a787e 938 subpos = next_position (subpos_ptr, pos, POS_XEXP, i);
09051660
RH
939 switch (fmt[i])
940 {
941 case 'e': case 'u':
09051660
RH
942 sub = add_to_sequence (XEXP (pattern, i), &sub->success,
943 subpos, insn_type, 0);
944 break;
945
946 case 'E':
947 {
6a1a787e 948 struct position *subpos2, **subpos2_ptr;
b3694847 949 int j;
6a1a787e
RS
950
951 subpos2_ptr = &pos->xvecexp0s;
09051660
RH
952 for (j = 0; j < XVECLEN (pattern, i); j++)
953 {
6a1a787e 954 subpos2 = next_position (subpos2_ptr, pos, POS_XVECEXP0, j);
09051660 955 sub = add_to_sequence (XVECEXP (pattern, i, j),
6a1a787e
RS
956 &sub->success, subpos2, insn_type, 0);
957 subpos2_ptr = &subpos2->next;
09051660
RH
958 }
959 break;
960 }
961
962 case 'i': case 'w':
963 /* Handled above. */
964 break;
965 case '0':
966 break;
967
968 default:
b2d59f6f 969 gcc_unreachable ();
09051660 970 }
6a1a787e 971 subpos_ptr = &subpos->next;
09051660
RH
972 }
973
974 fini:
975 /* Insert nodes testing mode and code, if they're still relevant,
976 before any of the nodes we may have added above. */
977 if (code != UNKNOWN)
978 {
8ad97cfc 979 place = &this_decision->tests;
09051660
RH
980 test = new_decision_test (DT_code, &place);
981 test->u.code = code;
982 }
983
984 if (mode != VOIDmode)
985 {
8ad97cfc 986 place = &this_decision->tests;
09051660
RH
987 test = new_decision_test (DT_mode, &place);
988 test->u.mode = mode;
989 }
990
991 /* If we didn't insert any tests or accept nodes, hork. */
8ad97cfc 992 gcc_assert (this_decision->tests);
09051660 993
b548dffb 994 ret:
09051660
RH
995 return sub;
996}
997\f
998/* A subroutine of maybe_both_true; examines only one test.
999 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1000
1001static int
3d7aafde 1002maybe_both_true_2 (struct decision_test *d1, struct decision_test *d2)
09051660
RH
1003{
1004 if (d1->type == d2->type)
1005 {
1006 switch (d1->type)
1007 {
0cd6c85a
PB
1008 case DT_num_insns:
1009 if (d1->u.num_insns == d2->u.num_insns)
1010 return 1;
1011 else
1012 return -1;
1013
09051660 1014 case DT_mode:
f0e1f482 1015 return d1->u.mode == d2->u.mode;
09051660
RH
1016
1017 case DT_code:
1018 return d1->u.code == d2->u.code;
1019
1020 case DT_veclen:
1021 return d1->u.veclen == d2->u.veclen;
1022
1023 case DT_elt_zero_int:
1024 case DT_elt_one_int:
1025 case DT_elt_zero_wide:
070ef6f4 1026 case DT_elt_zero_wide_safe:
09051660
RH
1027 return d1->u.intval == d2->u.intval;
1028
1029 default:
1030 break;
1031 }
1032 }
1033
1034 /* If either has a predicate that we know something about, set
1035 things up so that D1 is the one that always has a known
1036 predicate. Then see if they have any codes in common. */
1037
1038 if (d1->type == DT_pred || d2->type == DT_pred)
1039 {
1040 if (d2->type == DT_pred)
1041 {
1042 struct decision_test *tmp;
1043 tmp = d1, d1 = d2, d2 = tmp;
1044 }
1045
1046 /* If D2 tests a mode, see if it matches D1. */
1047 if (d1->u.pred.mode != VOIDmode)
1048 {
1049 if (d2->type == DT_mode)
1050 {
f0e1f482 1051 if (d1->u.pred.mode != d2->u.mode
8f496bc2
HPN
1052 /* The mode of an address_operand predicate is the
1053 mode of the memory, not the operand. It can only
1054 be used for testing the predicate, so we must
1055 ignore it here. */
1056 && strcmp (d1->u.pred.name, "address_operand") != 0)
09051660
RH
1057 return 0;
1058 }
4dc320a5
RH
1059 /* Don't check two predicate modes here, because if both predicates
1060 accept CONST_INT, then both can still be true even if the modes
1061 are different. If they don't accept CONST_INT, there will be a
1062 separate DT_mode that will make maybe_both_true_1 return 0. */
09051660
RH
1063 }
1064
e543e219 1065 if (d1->u.pred.data)
09051660
RH
1066 {
1067 /* If D2 tests a code, see if it is in the list of valid
1068 codes for D1's predicate. */
1069 if (d2->type == DT_code)
1070 {
e543e219 1071 if (!d1->u.pred.data->codes[d2->u.code])
09051660
RH
1072 return 0;
1073 }
1074
1075 /* Otherwise see if the predicates have any codes in common. */
e543e219 1076 else if (d2->type == DT_pred && d2->u.pred.data)
ec65fa66 1077 {
e543e219 1078 bool common = false;
32e8bb8e 1079 int c;
09051660 1080
e543e219
ZW
1081 for (c = 0; c < NUM_RTX_CODE; c++)
1082 if (d1->u.pred.data->codes[c] && d2->u.pred.data->codes[c])
1083 {
1084 common = true;
1085 break;
1086 }
09051660
RH
1087
1088 if (!common)
1089 return 0;
ec65fa66
RK
1090 }
1091 }
ec65fa66 1092 }
09051660 1093
521b9224
RH
1094 /* Tests vs veclen may be known when strict equality is involved. */
1095 if (d1->type == DT_veclen && d2->type == DT_veclen_ge)
1096 return d1->u.veclen >= d2->u.veclen;
1097 if (d1->type == DT_veclen_ge && d2->type == DT_veclen)
1098 return d2->u.veclen >= d1->u.veclen;
1099
09051660 1100 return -1;
ec65fa66 1101}
09051660
RH
1102
1103/* A subroutine of maybe_both_true; examines all the tests for a given node.
1104 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1105
1106static int
3d7aafde 1107maybe_both_true_1 (struct decision_test *d1, struct decision_test *d2)
09051660
RH
1108{
1109 struct decision_test *t1, *t2;
1110
1111 /* A match_operand with no predicate can match anything. Recognize
ff7cc307 1112 this by the existence of a lone DT_accept_op test. */
09051660
RH
1113 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
1114 return 1;
1115
1116 /* Eliminate pairs of tests while they can exactly match. */
1117 while (d1 && d2 && d1->type == d2->type)
1118 {
1119 if (maybe_both_true_2 (d1, d2) == 0)
1120 return 0;
1121 d1 = d1->next, d2 = d2->next;
1122 }
1123
1124 /* After that, consider all pairs. */
1125 for (t1 = d1; t1 ; t1 = t1->next)
1126 for (t2 = d2; t2 ; t2 = t2->next)
1127 if (maybe_both_true_2 (t1, t2) == 0)
1128 return 0;
1129
1130 return -1;
1131}
1132
1133/* Return 0 if we can prove that there is no RTL that can match both
1134 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
e0689256 1135 can match both or just that we couldn't prove there wasn't such an RTL).
ec65fa66 1136
cc2902df 1137 TOPLEVEL is nonzero if we are to only look at the top level and not
e0689256 1138 recursively descend. */
ec65fa66 1139
e0689256 1140static int
3d7aafde
AJ
1141maybe_both_true (struct decision *d1, struct decision *d2,
1142 int toplevel)
ec65fa66 1143{
e0689256 1144 struct decision *p1, *p2;
00ec6daa
JH
1145 int cmp;
1146
1147 /* Don't compare strings on the different positions in insn. Doing so
1148 is incorrect and results in false matches from constructs like
1149
1150 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1151 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1152 vs
1153 [(set (match_operand:HI "register_operand" "r")
1154 (match_operand:HI "register_operand" "r"))]
1155
1156 If we are presented with such, we are recursing through the remainder
1157 of a node's success nodes (from the loop at the end of this function).
1158 Skip forward until we come to a position that matches.
1159
6a1a787e
RS
1160 Due to the way positions are constructed, we know that iterating
1161 forward from the lexically lower position will run into the lexically
1162 higher position and not the other way around. This saves a bit
1163 of effort. */
00ec6daa 1164
6a1a787e 1165 cmp = compare_positions (d1->position, d2->position);
00ec6daa
JH
1166 if (cmp != 0)
1167 {
b2d59f6f 1168 gcc_assert (!toplevel);
00ec6daa
JH
1169
1170 /* If the d2->position was lexically lower, swap. */
1171 if (cmp > 0)
ace91ff1 1172 p1 = d1, d1 = d2, d2 = p1;
00ec6daa
JH
1173
1174 if (d1->success.first == 0)
29360e56 1175 return 1;
00ec6daa 1176 for (p1 = d1->success.first; p1; p1 = p1->next)
09051660
RH
1177 if (maybe_both_true (p1, d2, 0))
1178 return 1;
00ec6daa 1179
09051660 1180 return 0;
00ec6daa 1181 }
e0689256 1182
09051660
RH
1183 /* Test the current level. */
1184 cmp = maybe_both_true_1 (d1->tests, d2->tests);
1185 if (cmp >= 0)
1186 return cmp;
1187
1188 /* We can't prove that D1 and D2 cannot both be true. If we are only
1189 to check the top level, return 1. Otherwise, see if we can prove
1190 that all choices in both successors are mutually exclusive. If
1191 either does not have any successors, we can't prove they can't both
1192 be true. */
1193
1194 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
e0689256
RK
1195 return 1;
1196
09051660
RH
1197 for (p1 = d1->success.first; p1; p1 = p1->next)
1198 for (p2 = d2->success.first; p2; p2 = p2->next)
1199 if (maybe_both_true (p1, p2, 0))
1200 return 1;
e0689256 1201
09051660
RH
1202 return 0;
1203}
ec65fa66 1204
09051660 1205/* A subroutine of nodes_identical. Examine two tests for equivalence. */
ec65fa66 1206
09051660 1207static int
3d7aafde 1208nodes_identical_1 (struct decision_test *d1, struct decision_test *d2)
09051660
RH
1209{
1210 switch (d1->type)
ec65fa66 1211 {
0cd6c85a
PB
1212 case DT_num_insns:
1213 return d1->u.num_insns == d2->u.num_insns;
1214
09051660
RH
1215 case DT_mode:
1216 return d1->u.mode == d2->u.mode;
e0689256 1217
09051660
RH
1218 case DT_code:
1219 return d1->u.code == d2->u.code;
e0689256 1220
09051660
RH
1221 case DT_pred:
1222 return (d1->u.pred.mode == d2->u.pred.mode
1223 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
e0689256 1224
09051660
RH
1225 case DT_c_test:
1226 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
e0689256 1227
09051660 1228 case DT_veclen:
521b9224 1229 case DT_veclen_ge:
09051660 1230 return d1->u.veclen == d2->u.veclen;
e0689256 1231
09051660
RH
1232 case DT_dup:
1233 return d1->u.dup == d2->u.dup;
e0689256 1234
09051660
RH
1235 case DT_elt_zero_int:
1236 case DT_elt_one_int:
1237 case DT_elt_zero_wide:
070ef6f4 1238 case DT_elt_zero_wide_safe:
09051660 1239 return d1->u.intval == d2->u.intval;
e0689256 1240
09051660
RH
1241 case DT_accept_op:
1242 return d1->u.opno == d2->u.opno;
1243
1244 case DT_accept_insn:
1245 /* Differences will be handled in merge_accept_insn. */
1246 return 1;
1247
1248 default:
b2d59f6f 1249 gcc_unreachable ();
ec65fa66 1250 }
09051660 1251}
ec65fa66 1252
09051660 1253/* True iff the two nodes are identical (on one level only). Due
5b7c7046 1254 to the way these lists are constructed, we shouldn't have to
09051660 1255 consider different orderings on the tests. */
ec65fa66 1256
09051660 1257static int
3d7aafde 1258nodes_identical (struct decision *d1, struct decision *d2)
09051660
RH
1259{
1260 struct decision_test *t1, *t2;
e0689256 1261
09051660
RH
1262 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1263 {
1264 if (t1->type != t2->type)
1265 return 0;
1266 if (! nodes_identical_1 (t1, t2))
e0689256 1267 return 0;
09051660 1268 }
e0689256 1269
09051660 1270 /* For success, they should now both be null. */
aece2740
RH
1271 if (t1 != t2)
1272 return 0;
1273
1274 /* Check that their subnodes are at the same position, as any one set
2cec75a1
RH
1275 of sibling decisions must be at the same position. Allowing this
1276 requires complications to find_afterward and when change_state is
1277 invoked. */
aece2740
RH
1278 if (d1->success.first
1279 && d2->success.first
6a1a787e 1280 && d1->success.first->position != d2->success.first->position)
aece2740
RH
1281 return 0;
1282
1283 return 1;
e0689256 1284}
e0689256 1285
09051660
RH
1286/* A subroutine of merge_trees; given two nodes that have been declared
1287 identical, cope with two insn accept states. If they differ in the
1288 number of clobbers, then the conflict was created by make_insn_sequence
5b7c7046 1289 and we can drop the with-clobbers version on the floor. If both
09051660
RH
1290 nodes have no additional clobbers, we have found an ambiguity in the
1291 source machine description. */
1292
1293static void
3d7aafde 1294merge_accept_insn (struct decision *oldd, struct decision *addd)
ec65fa66 1295{
09051660
RH
1296 struct decision_test *old, *add;
1297
1298 for (old = oldd->tests; old; old = old->next)
1299 if (old->type == DT_accept_insn)
1300 break;
1301 if (old == NULL)
1302 return;
e0689256 1303
09051660
RH
1304 for (add = addd->tests; add; add = add->next)
1305 if (add->type == DT_accept_insn)
1306 break;
1307 if (add == NULL)
1308 return;
e0689256 1309
09051660
RH
1310 /* If one node is for a normal insn and the second is for the base
1311 insn with clobbers stripped off, the second node should be ignored. */
e0689256 1312
09051660
RH
1313 if (old->u.insn.num_clobbers_to_add == 0
1314 && add->u.insn.num_clobbers_to_add > 0)
1315 {
1316 /* Nothing to do here. */
1317 }
1318 else if (old->u.insn.num_clobbers_to_add > 0
1319 && add->u.insn.num_clobbers_to_add == 0)
1320 {
1321 /* In this case, replace OLD with ADD. */
1322 old->u.insn = add->u.insn;
1323 }
1324 else
1325 {
bb933490
RS
1326 error_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1327 get_insn_name (add->u.insn.code_number),
1328 get_insn_name (old->u.insn.code_number));
bcdaba58
RH
1329 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1330 get_insn_name (old->u.insn.code_number));
09051660 1331 }
e0689256 1332}
e0689256 1333
09051660
RH
1334/* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1335
1336static void
3d7aafde 1337merge_trees (struct decision_head *oldh, struct decision_head *addh)
e0689256 1338{
09051660 1339 struct decision *next, *add;
e0689256 1340
09051660
RH
1341 if (addh->first == 0)
1342 return;
1343 if (oldh->first == 0)
1344 {
1345 *oldh = *addh;
1346 return;
1347 }
ec65fa66 1348
09051660 1349 /* Trying to merge bits at different positions isn't possible. */
6a1a787e 1350 gcc_assert (oldh->first->position == addh->first->position);
e0689256 1351
09051660 1352 for (add = addh->first; add ; add = next)
ec65fa66 1353 {
09051660 1354 struct decision *old, *insert_before = NULL;
e0689256
RK
1355
1356 next = add->next;
1357
09051660
RH
1358 /* The semantics of pattern matching state that the tests are
1359 done in the order given in the MD file so that if an insn
1360 matches two patterns, the first one will be used. However,
1361 in practice, most, if not all, patterns are unambiguous so
1362 that their order is independent. In that case, we can merge
1363 identical tests and group all similar modes and codes together.
e0689256
RK
1364
1365 Scan starting from the end of OLDH until we reach a point
09051660
RH
1366 where we reach the head of the list or where we pass a
1367 pattern that could also be true if NEW is true. If we find
1368 an identical pattern, we can merge them. Also, record the
1369 last node that tests the same code and mode and the last one
1370 that tests just the same mode.
e0689256
RK
1371
1372 If we have no match, place NEW after the closest match we found. */
5b7c7046 1373
09051660 1374 for (old = oldh->last; old; old = old->prev)
ec65fa66 1375 {
09051660 1376 if (nodes_identical (old, add))
e0689256 1377 {
09051660
RH
1378 merge_accept_insn (old, add);
1379 merge_trees (&old->success, &add->success);
1380 goto merged_nodes;
1381 }
e0689256 1382
09051660
RH
1383 if (maybe_both_true (old, add, 0))
1384 break;
e0689256 1385
09051660
RH
1386 /* Insert the nodes in DT test type order, which is roughly
1387 how expensive/important the test is. Given that the tests
1388 are also ordered within the list, examining the first is
1389 sufficient. */
dbbbbf3b 1390 if ((int) add->tests->type < (int) old->tests->type)
09051660
RH
1391 insert_before = old;
1392 }
de6a431b 1393
09051660
RH
1394 if (insert_before == NULL)
1395 {
1396 add->next = NULL;
1397 add->prev = oldh->last;
1398 oldh->last->next = add;
1399 oldh->last = add;
1400 }
1401 else
1402 {
1403 if ((add->prev = insert_before->prev) != NULL)
1404 add->prev->next = add;
1405 else
1406 oldh->first = add;
1407 add->next = insert_before;
1408 insert_before->prev = add;
1409 }
1410
1411 merged_nodes:;
1412 }
1413}
1414\f
5b7c7046 1415/* Walk the tree looking for sub-nodes that perform common tests.
09051660
RH
1416 Factor out the common test into a new node. This enables us
1417 (depending on the test type) to emit switch statements later. */
1418
1419static void
3d7aafde 1420factor_tests (struct decision_head *head)
09051660
RH
1421{
1422 struct decision *first, *next;
e0689256 1423
09051660
RH
1424 for (first = head->first; first && first->next; first = next)
1425 {
1426 enum decision_type type;
8ad97cfc 1427 struct decision *new_dec, *old_last;
e0689256 1428
09051660
RH
1429 type = first->tests->type;
1430 next = first->next;
e0689256 1431
09051660
RH
1432 /* Want at least two compatible sequential nodes. */
1433 if (next->tests->type != type)
1434 continue;
ec65fa66 1435
5b7c7046 1436 /* Don't want all node types, just those we can turn into
09051660
RH
1437 switch statements. */
1438 if (type != DT_mode
1439 && type != DT_code
1440 && type != DT_veclen
1441 && type != DT_elt_zero_int
1442 && type != DT_elt_one_int
070ef6f4 1443 && type != DT_elt_zero_wide_safe)
e0689256 1444 continue;
ec65fa66 1445
09051660
RH
1446 /* If we'd been performing more than one test, create a new node
1447 below our first test. */
1448 if (first->tests->next != NULL)
1449 {
8ad97cfc
KG
1450 new_dec = new_decision (first->position, &first->success);
1451 new_dec->tests = first->tests->next;
09051660
RH
1452 first->tests->next = NULL;
1453 }
5b7c7046 1454
09051660
RH
1455 /* Crop the node tree off after our first test. */
1456 first->next = NULL;
1457 old_last = head->last;
1458 head->last = first;
1459
1460 /* For each compatible test, adjust to perform only one test in
1461 the top level node, then merge the node back into the tree. */
1462 do
1463 {
1464 struct decision_head h;
1465
1466 if (next->tests->next != NULL)
1467 {
8ad97cfc
KG
1468 new_dec = new_decision (next->position, &next->success);
1469 new_dec->tests = next->tests->next;
09051660
RH
1470 next->tests->next = NULL;
1471 }
8ad97cfc 1472 new_dec = next;
09051660 1473 next = next->next;
8ad97cfc
KG
1474 new_dec->next = NULL;
1475 h.first = h.last = new_dec;
ec65fa66 1476
09051660
RH
1477 merge_trees (head, &h);
1478 }
1479 while (next && next->tests->type == type);
ec65fa66 1480
09051660
RH
1481 /* After we run out of compatible tests, graft the remaining nodes
1482 back onto the tree. */
1483 if (next)
e0689256 1484 {
09051660
RH
1485 next->prev = head->last;
1486 head->last->next = next;
1487 head->last = old_last;
e0689256 1488 }
09051660 1489 }
ec65fa66 1490
09051660
RH
1491 /* Recurse. */
1492 for (first = head->first; first; first = first->next)
1493 factor_tests (&first->success);
1494}
1495
1496/* After factoring, try to simplify the tests on any one node.
1497 Tests that are useful for switch statements are recognizable
1498 by having only a single test on a node -- we'll be manipulating
1499 nodes with multiple tests:
1500
1501 If we have mode tests or code tests that are redundant with
1502 predicates, remove them. */
1503
1504static void
3d7aafde 1505simplify_tests (struct decision_head *head)
09051660
RH
1506{
1507 struct decision *tree;
1508
1509 for (tree = head->first; tree; tree = tree->next)
1510 {
1511 struct decision_test *a, *b;
1512
1513 a = tree->tests;
1514 b = a->next;
1515 if (b == NULL)
1516 continue;
1517
1518 /* Find a predicate node. */
1519 while (b && b->type != DT_pred)
1520 b = b->next;
1521 if (b)
e0689256 1522 {
09051660
RH
1523 /* Due to how these tests are constructed, we don't even need
1524 to check that the mode and code are compatible -- they were
1525 generated from the predicate in the first place. */
1526 while (a->type == DT_mode || a->type == DT_code)
1527 a = a->next;
1528 tree->tests = a;
e0689256
RK
1529 }
1530 }
ec65fa66 1531
09051660
RH
1532 /* Recurse. */
1533 for (tree = head->first; tree; tree = tree->next)
1534 simplify_tests (&tree->success);
ec65fa66 1535}
09051660 1536
e0689256
RK
1537/* Count the number of subnodes of HEAD. If the number is high enough,
1538 make the first node in HEAD start a separate subroutine in the C code
09051660 1539 that is generated. */
ec65fa66
RK
1540
1541static int
3d7aafde 1542break_out_subroutines (struct decision_head *head, int initial)
ec65fa66
RK
1543{
1544 int size = 0;
87bd0490 1545 struct decision *sub;
e0689256 1546
09051660
RH
1547 for (sub = head->first; sub; sub = sub->next)
1548 size += 1 + break_out_subroutines (&sub->success, 0);
e0689256
RK
1549
1550 if (size > SUBROUTINE_THRESHOLD && ! initial)
ec65fa66 1551 {
09051660 1552 head->first->subroutine_number = ++next_subroutine_number;
ec65fa66
RK
1553 size = 1;
1554 }
1555 return size;
1556}
09051660
RH
1557
1558/* For each node p, find the next alternative that might be true
1559 when p is true. */
ec65fa66
RK
1560
1561static void
3d7aafde 1562find_afterward (struct decision_head *head, struct decision *real_afterward)
ec65fa66 1563{
09051660 1564 struct decision *p, *q, *afterward;
69277eec 1565
a1f300c0 1566 /* We can't propagate alternatives across subroutine boundaries.
09051660 1567 This is not incorrect, merely a minor optimization loss. */
ec65fa66 1568
09051660
RH
1569 p = head->first;
1570 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
e0689256 1571
09051660 1572 for ( ; p ; p = p->next)
e0689256 1573 {
09051660
RH
1574 /* Find the next node that might be true if this one fails. */
1575 for (q = p->next; q ; q = q->next)
1576 if (maybe_both_true (p, q, 1))
1577 break;
e0689256 1578
5b7c7046 1579 /* If we reached the end of the list without finding one,
09051660
RH
1580 use the incoming afterward position. */
1581 if (!q)
1582 q = afterward;
1583 p->afterward = q;
1584 if (q)
1585 q->need_label = 1;
e0689256
RK
1586 }
1587
09051660
RH
1588 /* Recurse. */
1589 for (p = head->first; p ; p = p->next)
1590 if (p->success.first)
1591 find_afterward (&p->success, p->afterward);
1592
1593 /* When we are generating a subroutine, record the real afterward
1594 position in the first node where write_tree can find it, and we
1595 can do the right thing at the subroutine call site. */
1596 p = head->first;
1597 if (p->subroutine_number > 0)
1598 p->afterward = real_afterward;
1599}
1600\f
1601/* Assuming that the state of argument is denoted by OLDPOS, take whatever
1602 actions are necessary to move to NEWPOS. If we fail to move to the
cc2902df 1603 new state, branch to node AFTERWARD if nonzero, otherwise return.
e0689256 1604
09051660 1605 Failure to move to the new state can only occur if we are trying to
dc297297 1606 match multiple insns and we try to step past the end of the stream. */
e0689256 1607
09051660 1608static void
6a1a787e
RS
1609change_state (struct position *oldpos, struct position *newpos,
1610 const char *indent)
09051660 1611{
6a1a787e
RS
1612 while (oldpos->depth > newpos->depth)
1613 oldpos = oldpos->base;
e0689256 1614
6a1a787e
RS
1615 if (oldpos != newpos)
1616 switch (newpos->type)
1617 {
1618 case POS_PEEP2_INSN:
1619 printf ("%stem = peep2_next_insn (%d);\n", indent, newpos->arg);
1620 printf ("%sx%d = PATTERN (tem);\n", indent, newpos->depth);
1621 break;
1622
1623 case POS_XEXP:
1624 change_state (oldpos, newpos->base, indent);
1625 printf ("%sx%d = XEXP (x%d, %d);\n",
1626 indent, newpos->depth, newpos->depth - 1, newpos->arg);
1627 break;
1628
1629 case POS_XVECEXP0:
1630 change_state (oldpos, newpos->base, indent);
09051660 1631 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
6a1a787e
RS
1632 indent, newpos->depth, newpos->depth - 1, newpos->arg);
1633 break;
09051660
RH
1634 }
1635}
1636\f
1637/* Print the enumerator constant for CODE -- the upcase version of
1638 the name. */
1639
1640static void
3d7aafde 1641print_code (enum rtx_code code)
09051660 1642{
b3694847 1643 const char *p;
09051660
RH
1644 for (p = GET_RTX_NAME (code); *p; p++)
1645 putchar (TOUPPER (*p));
1646}
ec65fa66 1647
09051660 1648/* Emit code to cross an afterward link -- change state and branch. */
ec65fa66 1649
09051660 1650static void
3d7aafde
AJ
1651write_afterward (struct decision *start, struct decision *afterward,
1652 const char *indent)
09051660
RH
1653{
1654 if (!afterward || start->subroutine_number > 0)
c3284718 1655 printf ("%sgoto ret0;\n", indent);
09051660
RH
1656 else
1657 {
0cd6c85a 1658 change_state (start->position, afterward->position, indent);
09051660
RH
1659 printf ("%sgoto L%d;\n", indent, afterward->number);
1660 }
1661}
e0689256 1662
4f2ca7f5
RH
1663/* Emit a HOST_WIDE_INT as an integer constant expression. We need to take
1664 special care to avoid "decimal constant is so large that it is unsigned"
1665 warnings in the resulting code. */
1666
1667static void
1668print_host_wide_int (HOST_WIDE_INT val)
1669{
1670 HOST_WIDE_INT min = (unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT-1);
1671 if (val == min)
1672 printf ("(" HOST_WIDE_INT_PRINT_DEC_C "-1)", val + 1);
1673 else
1674 printf (HOST_WIDE_INT_PRINT_DEC_C, val);
1675}
1676
5b7c7046 1677/* Emit a switch statement, if possible, for an initial sequence of
09051660 1678 nodes at START. Return the first node yet untested. */
e0689256 1679
09051660 1680static struct decision *
3d7aafde 1681write_switch (struct decision *start, int depth)
09051660
RH
1682{
1683 struct decision *p = start;
1684 enum decision_type type = p->tests->type;
1651ab85 1685 struct decision *needs_label = NULL;
ec65fa66 1686
09051660
RH
1687 /* If we have two or more nodes in sequence that test the same one
1688 thing, we may be able to use a switch statement. */
e0689256 1689
09051660
RH
1690 if (!p->next
1691 || p->tests->next
1692 || p->next->tests->type != type
2cec75a1
RH
1693 || p->next->tests->next
1694 || nodes_identical_1 (p->tests, p->next->tests))
09051660 1695 return p;
e0689256 1696
09051660
RH
1697 /* DT_code is special in that we can do interesting things with
1698 known predicates at the same time. */
1699 if (type == DT_code)
1700 {
1701 char codemap[NUM_RTX_CODE];
1702 struct decision *ret;
1e193337 1703 RTX_CODE code;
ec65fa66 1704
c3284718 1705 memset (codemap, 0, sizeof (codemap));
ec65fa66 1706
09051660 1707 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1e193337 1708 code = p->tests->u.code;
5b7c7046 1709 do
ec65fa66 1710 {
1651ab85
AO
1711 if (p != start && p->need_label && needs_label == NULL)
1712 needs_label = p;
1713
09051660
RH
1714 printf (" case ");
1715 print_code (code);
1716 printf (":\n goto L%d;\n", p->success.first->number);
1717 p->success.first->need_label = 1;
1718
1719 codemap[code] = 1;
1720 p = p->next;
1721 }
1e193337
RH
1722 while (p
1723 && ! p->tests->next
1724 && p->tests->type == DT_code
1725 && ! codemap[code = p->tests->u.code]);
09051660
RH
1726
1727 /* If P is testing a predicate that we know about and we haven't
1728 seen any of the codes that are valid for the predicate, we can
1729 write a series of "case" statement, one for each possible code.
1730 Since we are already in a switch, these redundant tests are very
1731 cheap and will reduce the number of predicates called. */
1732
1733 /* Note that while we write out cases for these predicates here,
1734 we don't actually write the test here, as it gets kinda messy.
1735 It is trivial to leave this to later by telling our caller that
1736 we only processed the CODE tests. */
1651ab85
AO
1737 if (needs_label != NULL)
1738 ret = needs_label;
1739 else
1740 ret = p;
09051660 1741
e543e219 1742 while (p && p->tests->type == DT_pred && p->tests->u.pred.data)
09051660 1743 {
e543e219 1744 const struct pred_data *data = p->tests->u.pred.data;
32e8bb8e
ILT
1745 int c;
1746
e543e219
ZW
1747 for (c = 0; c < NUM_RTX_CODE; c++)
1748 if (codemap[c] && data->codes[c])
09051660 1749 goto pred_done;
e0689256 1750
e543e219
ZW
1751 for (c = 0; c < NUM_RTX_CODE; c++)
1752 if (data->codes[c])
1753 {
1754 fputs (" case ", stdout);
32e8bb8e 1755 print_code ((enum rtx_code) c);
e543e219
ZW
1756 fputs (":\n", stdout);
1757 codemap[c] = 1;
1758 }
e0689256 1759
09051660
RH
1760 printf (" goto L%d;\n", p->number);
1761 p->need_label = 1;
1762 p = p->next;
ec65fa66
RK
1763 }
1764
09051660
RH
1765 pred_done:
1766 /* Make the default case skip the predicates we managed to match. */
e0689256 1767
09051660
RH
1768 printf (" default:\n");
1769 if (p != ret)
ec65fa66 1770 {
09051660 1771 if (p)
b030d598 1772 {
09051660
RH
1773 printf (" goto L%d;\n", p->number);
1774 p->need_label = 1;
b030d598 1775 }
e0689256 1776 else
09051660 1777 write_afterward (start, start->afterward, " ");
ec65fa66 1778 }
ec65fa66 1779 else
09051660
RH
1780 printf (" break;\n");
1781 printf (" }\n");
1782
1783 return ret;
1784 }
1785 else if (type == DT_mode
1786 || type == DT_veclen
1787 || type == DT_elt_zero_int
1788 || type == DT_elt_one_int
070ef6f4 1789 || type == DT_elt_zero_wide_safe)
09051660 1790 {
9591d210 1791 const char *indent = "";
9e9f3ede 1792
9591d210
JH
1793 /* We cast switch parameter to integer, so we must ensure that the value
1794 fits. */
1795 if (type == DT_elt_zero_wide_safe)
1796 {
1797 indent = " ";
c3284718
RS
1798 printf (" if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n",
1799 depth, depth);
9591d210
JH
1800 }
1801 printf ("%s switch (", indent);
09051660
RH
1802 switch (type)
1803 {
1804 case DT_mode:
c8d8ed65 1805 printf ("GET_MODE (x%d)", depth);
09051660
RH
1806 break;
1807 case DT_veclen:
c8d8ed65 1808 printf ("XVECLEN (x%d, 0)", depth);
09051660
RH
1809 break;
1810 case DT_elt_zero_int:
c8d8ed65 1811 printf ("XINT (x%d, 0)", depth);
09051660
RH
1812 break;
1813 case DT_elt_one_int:
c8d8ed65 1814 printf ("XINT (x%d, 1)", depth);
09051660 1815 break;
070ef6f4 1816 case DT_elt_zero_wide_safe:
c8d8ed65
RK
1817 /* Convert result of XWINT to int for portability since some C
1818 compilers won't do it and some will. */
1819 printf ("(int) XWINT (x%d, 0)", depth);
09051660
RH
1820 break;
1821 default:
b2d59f6f 1822 gcc_unreachable ();
09051660 1823 }
9591d210 1824 printf (")\n%s {\n", indent);
cba998bf 1825
09051660 1826 do
e0689256 1827 {
2cec75a1
RH
1828 /* Merge trees will not unify identical nodes if their
1829 sub-nodes are at different levels. Thus we must check
1830 for duplicate cases. */
1831 struct decision *q;
1832 for (q = start; q != p; q = q->next)
1833 if (nodes_identical_1 (p->tests, q->tests))
1834 goto case_done;
1835
1651ab85
AO
1836 if (p != start && p->need_label && needs_label == NULL)
1837 needs_label = p;
1838
9591d210 1839 printf ("%s case ", indent);
09051660 1840 switch (type)
cba998bf 1841 {
09051660
RH
1842 case DT_mode:
1843 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1844 break;
1845 case DT_veclen:
1846 printf ("%d", p->tests->u.veclen);
1847 break;
1848 case DT_elt_zero_int:
1849 case DT_elt_one_int:
1850 case DT_elt_zero_wide:
070ef6f4 1851 case DT_elt_zero_wide_safe:
4f2ca7f5 1852 print_host_wide_int (p->tests->u.intval);
09051660
RH
1853 break;
1854 default:
b2d59f6f 1855 gcc_unreachable ();
cba998bf 1856 }
9591d210 1857 printf (":\n%s goto L%d;\n", indent, p->success.first->number);
09051660 1858 p->success.first->need_label = 1;
cba998bf 1859
09051660 1860 p = p->next;
e0689256 1861 }
09051660 1862 while (p && p->tests->type == type && !p->tests->next);
2cec75a1
RH
1863
1864 case_done:
9591d210
JH
1865 printf ("%s default:\n%s break;\n%s }\n",
1866 indent, indent, indent);
ec65fa66 1867
1651ab85 1868 return needs_label != NULL ? needs_label : p;
09051660
RH
1869 }
1870 else
1871 {
fbe5a4a6 1872 /* None of the other tests are amenable. */
09051660
RH
1873 return p;
1874 }
1875}
ec65fa66 1876
09051660 1877/* Emit code for one test. */
e0689256 1878
09051660 1879static void
3d7aafde
AJ
1880write_cond (struct decision_test *p, int depth,
1881 enum routine_type subroutine_type)
09051660
RH
1882{
1883 switch (p->type)
1884 {
0cd6c85a
PB
1885 case DT_num_insns:
1886 printf ("peep2_current_count >= %d", p->u.num_insns);
1887 break;
1888
09051660
RH
1889 case DT_mode:
1890 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1891 break;
e0689256 1892
09051660
RH
1893 case DT_code:
1894 printf ("GET_CODE (x%d) == ", depth);
1895 print_code (p->u.code);
1896 break;
1897
1898 case DT_veclen:
1899 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1900 break;
1901
1902 case DT_elt_zero_int:
1903 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1904 break;
1905
1906 case DT_elt_one_int:
1907 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1908 break;
1909
1910 case DT_elt_zero_wide:
070ef6f4 1911 case DT_elt_zero_wide_safe:
09051660 1912 printf ("XWINT (x%d, 0) == ", depth);
4f2ca7f5 1913 print_host_wide_int (p->u.intval);
09051660
RH
1914 break;
1915
ccdc1703
KH
1916 case DT_const_int:
1917 printf ("x%d == const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
1918 depth, (int) p->u.intval);
1919 break;
1920
521b9224
RH
1921 case DT_veclen_ge:
1922 printf ("XVECLEN (x%d, 0) >= %d", depth, p->u.veclen);
1923 break;
1924
09051660
RH
1925 case DT_dup:
1926 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1927 break;
1928
1929 case DT_pred:
1930 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1931 GET_MODE_NAME (p->u.pred.mode));
1932 break;
1933
1934 case DT_c_test:
7445392c 1935 print_c_condition (p->u.c_test);
09051660
RH
1936 break;
1937
1938 case DT_accept_insn:
b2d59f6f
NS
1939 gcc_assert (subroutine_type == RECOG);
1940 gcc_assert (p->u.insn.num_clobbers_to_add);
1941 printf ("pnum_clobbers != NULL");
09051660 1942 break;
ec65fa66 1943
09051660 1944 default:
b2d59f6f 1945 gcc_unreachable ();
e0689256 1946 }
09051660 1947}
ec65fa66 1948
09051660
RH
1949/* Emit code for one action. The previous tests have succeeded;
1950 TEST is the last of the chain. In the normal case we simply
1951 perform a state change. For the `accept' tests we must do more work. */
ec65fa66 1952
09051660 1953static void
3d7aafde
AJ
1954write_action (struct decision *p, struct decision_test *test,
1955 int depth, int uncond, struct decision *success,
1956 enum routine_type subroutine_type)
09051660
RH
1957{
1958 const char *indent;
1959 int want_close = 0;
1960
1961 if (uncond)
1962 indent = " ";
1963 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
e0689256 1964 {
09051660
RH
1965 fputs (" {\n", stdout);
1966 indent = " ";
1967 want_close = 1;
e0689256 1968 }
09051660
RH
1969 else
1970 indent = " ";
ec65fa66 1971
09051660 1972 if (test->type == DT_accept_op)
e0689256 1973 {
c3284718 1974 printf ("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
09051660
RH
1975
1976 /* Only allow DT_accept_insn to follow. */
1977 if (test->next)
1978 {
1979 test = test->next;
b2d59f6f 1980 gcc_assert (test->type == DT_accept_insn);
09051660 1981 }
ec65fa66
RK
1982 }
1983
09051660 1984 /* Sanity check that we're now at the end of the list of tests. */
b2d59f6f 1985 gcc_assert (!test->next);
ec65fa66 1986
09051660 1987 if (test->type == DT_accept_insn)
ec65fa66 1988 {
09051660
RH
1989 switch (subroutine_type)
1990 {
1991 case RECOG:
1992 if (test->u.insn.num_clobbers_to_add != 0)
1993 printf ("%s*pnum_clobbers = %d;\n",
1994 indent, test->u.insn.num_clobbers_to_add);
1e9c8405
RS
1995 printf ("%sreturn %d; /* %s */\n", indent,
1996 test->u.insn.code_number,
0458fe77 1997 get_insn_name (test->u.insn.code_number));
09051660
RH
1998 break;
1999
2000 case SPLIT:
a406f566 2001 printf ("%sreturn gen_split_%d (insn, operands);\n",
09051660
RH
2002 indent, test->u.insn.code_number);
2003 break;
2004
2005 case PEEPHOLE2:
23280139 2006 {
6a1a787e
RS
2007 int match_len = 0;
2008 struct position *pos;
23280139 2009
6a1a787e
RS
2010 for (pos = p->position; pos; pos = pos->base)
2011 if (pos->type == POS_PEEP2_INSN)
23280139 2012 {
6a1a787e 2013 match_len = pos->arg;
23280139
RH
2014 break;
2015 }
2016 printf ("%s*_pmatch_len = %d;\n", indent, match_len);
2017 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
2018 indent, test->u.insn.code_number);
2019 printf ("%sif (tem != 0)\n%s return tem;\n", indent, indent);
2020 }
09051660
RH
2021 break;
2022
2023 default:
b2d59f6f 2024 gcc_unreachable ();
09051660 2025 }
ec65fa66
RK
2026 }
2027 else
09051660 2028 {
c3284718 2029 printf ("%sgoto L%d;\n", indent, success->number);
09051660
RH
2030 success->need_label = 1;
2031 }
ec65fa66 2032
09051660
RH
2033 if (want_close)
2034 fputs (" }\n", stdout);
ec65fa66
RK
2035}
2036
09051660
RH
2037/* Return 1 if the test is always true and has no fallthru path. Return -1
2038 if the test does have a fallthru path, but requires that the condition be
2039 terminated. Otherwise return 0 for a normal test. */
2040/* ??? is_unconditional is a stupid name for a tri-state function. */
2041
ec65fa66 2042static int
3d7aafde 2043is_unconditional (struct decision_test *t, enum routine_type subroutine_type)
ec65fa66 2044{
09051660
RH
2045 if (t->type == DT_accept_op)
2046 return 1;
ec65fa66 2047
09051660
RH
2048 if (t->type == DT_accept_insn)
2049 {
2050 switch (subroutine_type)
2051 {
2052 case RECOG:
2053 return (t->u.insn.num_clobbers_to_add == 0);
2054 case SPLIT:
2055 return 1;
2056 case PEEPHOLE2:
2057 return -1;
2058 default:
b2d59f6f 2059 gcc_unreachable ();
09051660
RH
2060 }
2061 }
ec65fa66 2062
09051660 2063 return 0;
ec65fa66
RK
2064}
2065
09051660
RH
2066/* Emit code for one node -- the conditional and the accompanying action.
2067 Return true if there is no fallthru path. */
2068
ec65fa66 2069static int
3d7aafde
AJ
2070write_node (struct decision *p, int depth,
2071 enum routine_type subroutine_type)
ec65fa66 2072{
09051660
RH
2073 struct decision_test *test, *last_test;
2074 int uncond;
ec65fa66 2075
ccdc1703
KH
2076 /* Scan the tests and simplify comparisons against small
2077 constants. */
2078 for (test = p->tests; test; test = test->next)
2079 {
2080 if (test->type == DT_code
2081 && test->u.code == CONST_INT
2082 && test->next
2083 && test->next->type == DT_elt_zero_wide_safe
2084 && -MAX_SAVED_CONST_INT <= test->next->u.intval
2085 && test->next->u.intval <= MAX_SAVED_CONST_INT)
2086 {
2087 test->type = DT_const_int;
2088 test->u.intval = test->next->u.intval;
2089 test->next = test->next->next;
2090 }
2091 }
2092
09051660
RH
2093 last_test = test = p->tests;
2094 uncond = is_unconditional (test, subroutine_type);
2095 if (uncond == 0)
2096 {
2097 printf (" if (");
2098 write_cond (test, depth, subroutine_type);
2099
2100 while ((test = test->next) != NULL)
2101 {
09051660 2102 last_test = test;
be2c39f8 2103 if (is_unconditional (test, subroutine_type))
09051660
RH
2104 break;
2105
2106 printf ("\n && ");
2107 write_cond (test, depth, subroutine_type);
2108 }
2109
2110 printf (")\n");
2111 }
2112
23280139 2113 write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
09051660
RH
2114
2115 return uncond > 0;
ec65fa66
RK
2116}
2117
09051660
RH
2118/* Emit code for all of the sibling nodes of HEAD. */
2119
ec65fa66 2120static void
3d7aafde
AJ
2121write_tree_1 (struct decision_head *head, int depth,
2122 enum routine_type subroutine_type)
ec65fa66 2123{
09051660
RH
2124 struct decision *p, *next;
2125 int uncond = 0;
e0689256 2126
09051660
RH
2127 for (p = head->first; p ; p = next)
2128 {
2129 /* The label for the first element was printed in write_tree. */
2130 if (p != head->first && p->need_label)
2131 OUTPUT_LABEL (" ", p->number);
2132
2133 /* Attempt to write a switch statement for a whole sequence. */
2134 next = write_switch (p, depth);
2135 if (p != next)
2136 uncond = 0;
2137 else
2138 {
2139 /* Failed -- fall back and write one node. */
2140 uncond = write_node (p, depth, subroutine_type);
2141 next = p->next;
2142 }
2143 }
e0689256 2144
09051660
RH
2145 /* Finished with this chain. Close a fallthru path by branching
2146 to the afterward node. */
2147 if (! uncond)
2148 write_afterward (head->last, head->last->afterward, " ");
2149}
e0689256 2150
09051660
RH
2151/* Write out the decision tree starting at HEAD. PREVPOS is the
2152 position at the node that branched to this node. */
e0689256
RK
2153
2154static void
6a1a787e 2155write_tree (struct decision_head *head, struct position *prevpos,
3d7aafde 2156 enum routine_type type, int initial)
e0689256 2157{
b3694847 2158 struct decision *p = head->first;
e0689256 2159
09051660
RH
2160 putchar ('\n');
2161 if (p->need_label)
2162 OUTPUT_LABEL (" ", p->number);
2163
2164 if (! initial && p->subroutine_number > 0)
e0689256 2165 {
09051660
RH
2166 static const char * const name_prefix[] = {
2167 "recog", "split", "peephole2"
2168 };
2169
2170 static const char * const call_suffix[] = {
23280139 2171 ", pnum_clobbers", "", ", _pmatch_len"
09051660 2172 };
e0689256 2173
09051660
RH
2174 /* This node has been broken out into a separate subroutine.
2175 Call it, test the result, and branch accordingly. */
2176
2177 if (p->afterward)
e0689256
RK
2178 {
2179 printf (" tem = %s_%d (x0, insn%s);\n",
09051660 2180 name_prefix[type], p->subroutine_number, call_suffix[type]);
ede7cd44 2181 if (IS_SPLIT (type))
09051660 2182 printf (" if (tem != 0)\n return tem;\n");
71bde1f3 2183 else
09051660
RH
2184 printf (" if (tem >= 0)\n return tem;\n");
2185
0cd6c85a 2186 change_state (p->position, p->afterward->position, " ");
09051660 2187 printf (" goto L%d;\n", p->afterward->number);
e0689256
RK
2188 }
2189 else
09051660
RH
2190 {
2191 printf (" return %s_%d (x0, insn%s);\n",
2192 name_prefix[type], p->subroutine_number, call_suffix[type]);
2193 }
e0689256 2194 }
09051660
RH
2195 else
2196 {
0cd6c85a 2197 change_state (prevpos, p->position, " ");
6a1a787e 2198 write_tree_1 (head, p->position->depth, type);
e0689256 2199
09051660
RH
2200 for (p = head->first; p; p = p->next)
2201 if (p->success.first)
2202 write_tree (&p->success, p->position, type, 0);
2203 }
e0689256
RK
2204}
2205
09051660
RH
2206/* Write out a subroutine of type TYPE to do comparisons starting at
2207 node TREE. */
ede7cd44 2208
09051660 2209static void
3d7aafde 2210write_subroutine (struct decision_head *head, enum routine_type type)
09051660 2211{
e8f9b13a 2212 int subfunction = head->first ? head->first->subroutine_number : 0;
09051660
RH
2213 const char *s_or_e;
2214 char extension[32];
2215 int i;
95770ca3 2216 const char *insn_param;
5b7c7046 2217
09051660 2218 s_or_e = subfunction ? "static " : "";
e0689256 2219
09051660
RH
2220 if (subfunction)
2221 sprintf (extension, "_%d", subfunction);
2222 else if (type == RECOG)
2223 extension[0] = '\0';
2224 else
2225 strcpy (extension, "_insns");
2226
95770ca3
DM
2227 /* For now, the top-level functions take a plain "rtx", and perform a
2228 checked cast to "rtx_insn *" for use throughout the rest of the
2229 function and the code it calls. */
2230 insn_param = subfunction ? "rtx_insn *insn" : "rtx uncast_insn";
2231
913d0833
KG
2232 switch (type)
2233 {
2234 case RECOG:
913d0833 2235 printf ("%sint\n\
95770ca3
DM
2236recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\t%s ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n",
2237 s_or_e, extension, insn_param);
913d0833
KG
2238 break;
2239 case SPLIT:
913d0833 2240 printf ("%srtx\n\
95770ca3
DM
2241split%s (rtx x0 ATTRIBUTE_UNUSED, %s ATTRIBUTE_UNUSED)\n",
2242 s_or_e, extension, insn_param);
913d0833
KG
2243 break;
2244 case PEEPHOLE2:
913d0833 2245 printf ("%srtx\n\
95770ca3
DM
2246peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\t%s ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n",
2247 s_or_e, extension, insn_param);
913d0833
KG
2248 break;
2249 }
09051660 2250
b3694847 2251 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
09051660 2252 for (i = 1; i <= max_depth; i++)
b3694847 2253 printf (" rtx x%d ATTRIBUTE_UNUSED;\n", i);
09051660 2254
09051660
RH
2255 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2256
d90ffc8d
JH
2257 if (!subfunction)
2258 printf (" recog_data.insn = NULL_RTX;\n");
2259
95770ca3
DM
2260 /* For now add the downcast to rtx_insn *, at the top of each top-level
2261 function. */
2262 if (!subfunction)
2263 {
2264 printf (" rtx_insn *insn ATTRIBUTE_UNUSED;\n");
2265 printf (" insn = safe_as_a <rtx_insn *> (uncast_insn);\n");
2266 }
2267
e8f9b13a 2268 if (head->first)
6a1a787e 2269 write_tree (head, &root_pos, type, 1);
e8f9b13a
RH
2270 else
2271 printf (" goto ret0;\n");
09051660 2272
09051660
RH
2273 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2274}
2275
2276/* In break_out_subroutines, we discovered the boundaries for the
2277 subroutines, but did not write them out. Do so now. */
e0689256 2278
ec65fa66 2279static void
3d7aafde 2280write_subroutines (struct decision_head *head, enum routine_type type)
ec65fa66 2281{
09051660 2282 struct decision *p;
ec65fa66 2283
09051660
RH
2284 for (p = head->first; p ; p = p->next)
2285 if (p->success.first)
2286 write_subroutines (&p->success, type);
ec65fa66 2287
09051660
RH
2288 if (head->first->subroutine_number > 0)
2289 write_subroutine (head, type);
2290}
ede7cd44 2291
09051660 2292/* Begin the output file. */
ede7cd44 2293
09051660 2294static void
3d7aafde 2295write_header (void)
09051660
RH
2296{
2297 puts ("\
2298/* Generated automatically by the program `genrecog' from the target\n\
2299 machine description file. */\n\
2300\n\
2301#include \"config.h\"\n\
2302#include \"system.h\"\n\
4977bab6
ZW
2303#include \"coretypes.h\"\n\
2304#include \"tm.h\"\n\
09051660
RH
2305#include \"rtl.h\"\n\
2306#include \"tm_p.h\"\n\
2307#include \"function.h\"\n\
2308#include \"insn-config.h\"\n\
2309#include \"recog.h\"\n\
09051660
RH
2310#include \"output.h\"\n\
2311#include \"flags.h\"\n\
b1afd7f4
KG
2312#include \"hard-reg-set.h\"\n\
2313#include \"resource.h\"\n\
79a3f089 2314#include \"diagnostic-core.h\"\n\
5b7c7046 2315#include \"reload.h\"\n\
5abc5de9 2316#include \"regs.h\"\n\
279bb624 2317#include \"tm-constrs.h\"\n\
09051660
RH
2318\n");
2319
2320 puts ("\n\
2321/* `recog' contains a decision tree that recognizes whether the rtx\n\
2322 X0 is a valid instruction.\n\
2323\n\
2324 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2325 returns a nonnegative number which is the insn code number for the\n\
2326 pattern that matched. This is the same as the order in the machine\n\
2327 description of the entry that matched. This number can be used as an\n\
3f6790bf
KG
2328 index into `insn_data' and other tables.\n");
2329 puts ("\
09051660
RH
2330 The third argument to recog is an optional pointer to an int. If\n\
2331 present, recog will accept a pattern if it matches except for missing\n\
2332 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2333 the optional pointer will be set to the number of CLOBBERs that need\n\
3f6790bf
KG
2334 to be added (it should be initialized to zero by the caller). If it");
2335 puts ("\
09051660
RH
2336 is set nonzero, the caller should allocate a PARALLEL of the\n\
2337 appropriate size, copy the initial entries, and call add_clobbers\n\
2338 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2339");
2340
2341 puts ("\n\
2342 The function split_insns returns 0 if the rtl could not\n\
2f937369 2343 be split or the split rtl as an INSN list if it can be.\n\
09051660
RH
2344\n\
2345 The function peephole2_insns returns 0 if the rtl could not\n\
2f937369 2346 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
09051660
RH
2347 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2348*/\n\n");
2349}
ec65fa66 2350
09051660
RH
2351\f
2352/* Construct and return a sequence of decisions
2353 that will recognize INSN.
ec65fa66 2354
09051660
RH
2355 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2356
2357static struct decision_head
3d7aafde 2358make_insn_sequence (rtx insn, enum routine_type type)
09051660
RH
2359{
2360 rtx x;
2361 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2199e5fa 2362 int truth = maybe_eval_c_test (c_test);
09051660
RH
2363 struct decision *last;
2364 struct decision_test *test, **place;
2365 struct decision_head head;
6a1a787e 2366 struct position *c_test_pos, **pos_ptr;
09051660 2367
2199e5fa 2368 /* We should never see an insn whose C test is false at compile time. */
b2d59f6f 2369 gcc_assert (truth);
2199e5fa 2370
6a1a787e 2371 c_test_pos = &root_pos;
09051660 2372 if (type == PEEPHOLE2)
ec65fa66 2373 {
09051660
RH
2374 int i, j;
2375
2376 /* peephole2 gets special treatment:
2377 - X always gets an outer parallel even if it's only one entry
2378 - we remove all traces of outer-level match_scratch and match_dup
2379 expressions here. */
2380 x = rtx_alloc (PARALLEL);
2381 PUT_MODE (x, VOIDmode);
2382 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
6a1a787e 2383 pos_ptr = &peep2_insn_pos_list;
09051660 2384 for (i = j = 0; i < XVECLEN (insn, 0); i++)
ede7cd44 2385 {
09051660
RH
2386 rtx tmp = XVECEXP (insn, 0, i);
2387 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2388 {
6a1a787e 2389 c_test_pos = next_position (pos_ptr, &root_pos,
02aec184 2390 POS_PEEP2_INSN, j);
09051660
RH
2391 XVECEXP (x, 0, j) = tmp;
2392 j++;
6a1a787e 2393 pos_ptr = &c_test_pos->next;
09051660
RH
2394 }
2395 }
2396 XVECLEN (x, 0) = j;
2397 }
2398 else if (XVECLEN (insn, type == RECOG) == 1)
2399 x = XVECEXP (insn, type == RECOG, 0);
2400 else
2401 {
2402 x = rtx_alloc (PARALLEL);
2403 XVEC (x, 0) = XVEC (insn, type == RECOG);
2404 PUT_MODE (x, VOIDmode);
2405 }
2406
7297e9fc 2407 validate_pattern (x, insn, NULL_RTX, 0);
bcdaba58 2408
c3284718 2409 memset (&head, 0, sizeof (head));
6a1a787e 2410 last = add_to_sequence (x, &head, &root_pos, type, 1);
09051660
RH
2411
2412 /* Find the end of the test chain on the last node. */
2413 for (test = last->tests; test->next; test = test->next)
2414 continue;
2415 place = &test->next;
2416
2199e5fa
ZW
2417 /* Skip the C test if it's known to be true at compile time. */
2418 if (truth == -1)
09051660
RH
2419 {
2420 /* Need a new node if we have another test to add. */
2421 if (test->type == DT_accept_op)
2422 {
4e9887c7 2423 last = new_decision (c_test_pos, &last->success);
09051660
RH
2424 place = &last->tests;
2425 }
2426 test = new_decision_test (DT_c_test, &place);
2427 test->u.c_test = c_test;
2428 }
2429
2430 test = new_decision_test (DT_accept_insn, &place);
2431 test->u.insn.code_number = next_insn_code;
bcdaba58 2432 test->u.insn.lineno = pattern_lineno;
09051660
RH
2433 test->u.insn.num_clobbers_to_add = 0;
2434
2435 switch (type)
2436 {
2437 case RECOG:
b20b352b 2438 /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends
09051660
RH
2439 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2440 If so, set up to recognize the pattern without these CLOBBERs. */
2441
2442 if (GET_CODE (x) == PARALLEL)
2443 {
2444 int i;
2445
2446 /* Find the last non-clobber in the parallel. */
2447 for (i = XVECLEN (x, 0); i > 0; i--)
ede7cd44 2448 {
09051660
RH
2449 rtx y = XVECEXP (x, 0, i - 1);
2450 if (GET_CODE (y) != CLOBBER
f8cfc6aa 2451 || (!REG_P (XEXP (y, 0))
09051660
RH
2452 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2453 break;
ede7cd44 2454 }
09051660
RH
2455
2456 if (i != XVECLEN (x, 0))
ede7cd44 2457 {
8ad97cfc 2458 rtx new_rtx;
09051660 2459 struct decision_head clobber_head;
ede7cd44 2460
09051660
RH
2461 /* Build a similar insn without the clobbers. */
2462 if (i == 1)
8ad97cfc 2463 new_rtx = XVECEXP (x, 0, 0);
ede7cd44 2464 else
09051660
RH
2465 {
2466 int j;
2467
8ad97cfc
KG
2468 new_rtx = rtx_alloc (PARALLEL);
2469 XVEC (new_rtx, 0) = rtvec_alloc (i);
09051660 2470 for (j = i - 1; j >= 0; j--)
8ad97cfc 2471 XVECEXP (new_rtx, 0, j) = XVECEXP (x, 0, j);
09051660
RH
2472 }
2473
2474 /* Recognize it. */
c3284718 2475 memset (&clobber_head, 0, sizeof (clobber_head));
6a1a787e
RS
2476 last = add_to_sequence (new_rtx, &clobber_head, &root_pos,
2477 type, 1);
ede7cd44 2478
09051660
RH
2479 /* Find the end of the test chain on the last node. */
2480 for (test = last->tests; test->next; test = test->next)
2481 continue;
2482
2483 /* We definitely have a new test to add -- create a new
2484 node if needed. */
2485 place = &test->next;
2486 if (test->type == DT_accept_op)
2487 {
6a1a787e 2488 last = new_decision (&root_pos, &last->success);
09051660
RH
2489 place = &last->tests;
2490 }
2491
2199e5fa
ZW
2492 /* Skip the C test if it's known to be true at compile
2493 time. */
2494 if (truth == -1)
09051660
RH
2495 {
2496 test = new_decision_test (DT_c_test, &place);
2497 test->u.c_test = c_test;
2498 }
2499
2500 test = new_decision_test (DT_accept_insn, &place);
2501 test->u.insn.code_number = next_insn_code;
bcdaba58 2502 test->u.insn.lineno = pattern_lineno;
09051660
RH
2503 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2504
2505 merge_trees (&head, &clobber_head);
ede7cd44 2506 }
ede7cd44 2507 }
09051660
RH
2508 break;
2509
2510 case SPLIT:
2511 /* Define the subroutine we will call below and emit in genemit. */
95770ca3 2512 printf ("extern rtx gen_split_%d (rtx_insn *, rtx *);\n", next_insn_code);
09051660
RH
2513 break;
2514
2515 case PEEPHOLE2:
2516 /* Define the subroutine we will call below and emit in genemit. */
95770ca3 2517 printf ("extern rtx gen_peephole2_%d (rtx_insn *, rtx *);\n",
09051660
RH
2518 next_insn_code);
2519 break;
ec65fa66 2520 }
e0689256 2521
09051660 2522 return head;
ec65fa66
RK
2523}
2524
09051660 2525static void
3d7aafde 2526process_tree (struct decision_head *head, enum routine_type subroutine_type)
ec65fa66 2527{
4dc320a5
RH
2528 if (head->first == NULL)
2529 {
2530 /* We can elide peephole2_insns, but not recog or split_insns. */
2531 if (subroutine_type == PEEPHOLE2)
2532 return;
2533 }
2534 else
e8f9b13a
RH
2535 {
2536 factor_tests (head);
ec65fa66 2537
e8f9b13a
RH
2538 next_subroutine_number = 0;
2539 break_out_subroutines (head, 1);
2540 find_afterward (head, NULL);
c1b59dce 2541
4dc320a5
RH
2542 /* We run this after find_afterward, because find_afterward needs
2543 the redundant DT_mode tests on predicates to determine whether
2544 two tests can both be true or not. */
c3284718 2545 simplify_tests (head);
4dc320a5 2546
e8f9b13a
RH
2547 write_subroutines (head, subroutine_type);
2548 }
4dc320a5 2549
09051660
RH
2550 write_subroutine (head, subroutine_type);
2551}
2552\f
3d7aafde 2553extern int main (int, char **);
36f0e0a6 2554
ec65fa66 2555int
3d7aafde 2556main (int argc, char **argv)
ec65fa66
RK
2557{
2558 rtx desc;
09051660 2559 struct decision_head recog_tree, split_tree, peephole2_tree, h;
ec65fa66 2560
f8b6598e 2561 progname = "genrecog";
09051660
RH
2562
2563 memset (&recog_tree, 0, sizeof recog_tree);
2564 memset (&split_tree, 0, sizeof split_tree);
2565 memset (&peephole2_tree, 0, sizeof peephole2_tree);
ec65fa66 2566
600ab3fc 2567 if (!init_rtx_reader_args (argc, argv))
c88c0d42 2568 return (FATAL_EXIT_CODE);
ec65fa66 2569
ec65fa66 2570 next_insn_code = 0;
ec65fa66 2571
09051660 2572 write_header ();
ec65fa66
RK
2573
2574 /* Read the machine description. */
2575
2576 while (1)
2577 {
c88c0d42
CP
2578 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2579 if (desc == NULL)
ec65fa66 2580 break;
ec65fa66 2581
e543e219 2582 switch (GET_CODE (desc))
09051660 2583 {
e543e219 2584 case DEFINE_INSN:
09051660
RH
2585 h = make_insn_sequence (desc, RECOG);
2586 merge_trees (&recog_tree, &h);
e543e219
ZW
2587 break;
2588
2589 case DEFINE_SPLIT:
09051660
RH
2590 h = make_insn_sequence (desc, SPLIT);
2591 merge_trees (&split_tree, &h);
e543e219
ZW
2592 break;
2593
2594 case DEFINE_PEEPHOLE2:
09051660
RH
2595 h = make_insn_sequence (desc, PEEPHOLE2);
2596 merge_trees (&peephole2_tree, &h);
5b7c7046 2597
e543e219
ZW
2598 default:
2599 /* do nothing */;
2600 }
ec65fa66
RK
2601 }
2602
bb933490 2603 if (have_error)
bcdaba58
RH
2604 return FATAL_EXIT_CODE;
2605
09051660 2606 puts ("\n\n");
ec65fa66 2607
09051660
RH
2608 process_tree (&recog_tree, RECOG);
2609 process_tree (&split_tree, SPLIT);
2610 process_tree (&peephole2_tree, PEEPHOLE2);
ede7cd44 2611
ec65fa66 2612 fflush (stdout);
c1b59dce 2613 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
ec65fa66 2614}
09051660 2615\f
09051660 2616static void
3d7aafde 2617debug_decision_2 (struct decision_test *test)
09051660
RH
2618{
2619 switch (test->type)
2620 {
0cd6c85a
PB
2621 case DT_num_insns:
2622 fprintf (stderr, "num_insns=%d", test->u.num_insns);
2623 break;
09051660
RH
2624 case DT_mode:
2625 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2626 break;
2627 case DT_code:
2628 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2629 break;
2630 case DT_veclen:
2631 fprintf (stderr, "veclen=%d", test->u.veclen);
2632 break;
2633 case DT_elt_zero_int:
2634 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2635 break;
2636 case DT_elt_one_int:
2637 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2638 break;
2639 case DT_elt_zero_wide:
90ff44cf 2640 fprintf (stderr, "elt0_w=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
09051660 2641 break;
070ef6f4 2642 case DT_elt_zero_wide_safe:
90ff44cf 2643 fprintf (stderr, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
070ef6f4 2644 break;
521b9224
RH
2645 case DT_veclen_ge:
2646 fprintf (stderr, "veclen>=%d", test->u.veclen);
2647 break;
09051660
RH
2648 case DT_dup:
2649 fprintf (stderr, "dup=%d", test->u.dup);
2650 break;
2651 case DT_pred:
2652 fprintf (stderr, "pred=(%s,%s)",
c3284718 2653 test->u.pred.name, GET_MODE_NAME (test->u.pred.mode));
09051660
RH
2654 break;
2655 case DT_c_test:
2656 {
2657 char sub[16+4];
c3284718 2658 strncpy (sub, test->u.c_test, sizeof (sub));
09051660
RH
2659 memcpy (sub+16, "...", 4);
2660 fprintf (stderr, "c_test=\"%s\"", sub);
2661 }
2662 break;
2663 case DT_accept_op:
2664 fprintf (stderr, "A_op=%d", test->u.opno);
2665 break;
2666 case DT_accept_insn:
5b7c7046 2667 fprintf (stderr, "A_insn=(%d,%d)",
09051660
RH
2668 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2669 break;
2670
2671 default:
b2d59f6f 2672 gcc_unreachable ();
09051660
RH
2673 }
2674}
2675
2676static void
3d7aafde 2677debug_decision_1 (struct decision *d, int indent)
09051660
RH
2678{
2679 int i;
2680 struct decision_test *test;
2681
2682 if (d == NULL)
2683 {
2684 for (i = 0; i < indent; ++i)
2685 putc (' ', stderr);
2686 fputs ("(nil)\n", stderr);
2687 return;
2688 }
2689
2690 for (i = 0; i < indent; ++i)
2691 putc (' ', stderr);
2692
2693 putc ('{', stderr);
2694 test = d->tests;
2695 if (test)
2696 {
2697 debug_decision_2 (test);
2698 while ((test = test->next) != NULL)
2699 {
2700 fputs (" + ", stderr);
2701 debug_decision_2 (test);
2702 }
2703 }
4dc320a5
RH
2704 fprintf (stderr, "} %d n %d a %d\n", d->number,
2705 (d->next ? d->next->number : -1),
2706 (d->afterward ? d->afterward->number : -1));
09051660
RH
2707}
2708
2709static void
3d7aafde 2710debug_decision_0 (struct decision *d, int indent, int maxdepth)
09051660
RH
2711{
2712 struct decision *n;
2713 int i;
2714
2715 if (maxdepth < 0)
2716 return;
2717 if (d == NULL)
2718 {
2719 for (i = 0; i < indent; ++i)
2720 putc (' ', stderr);
2721 fputs ("(nil)\n", stderr);
2722 return;
2723 }
2724
2725 debug_decision_1 (d, indent);
2726 for (n = d->success.first; n ; n = n->next)
2727 debug_decision_0 (n, indent + 2, maxdepth - 1);
2728}
2729
24e47c76 2730DEBUG_FUNCTION void
3d7aafde 2731debug_decision (struct decision *d)
09051660
RH
2732{
2733 debug_decision_0 (d, 0, 1000000);
2734}
ec1c89e6 2735
24e47c76 2736DEBUG_FUNCTION void
3d7aafde 2737debug_decision_list (struct decision *d)
ec1c89e6
RH
2738{
2739 while (d)
2740 {
2741 debug_decision_0 (d, 0, 0);
2742 d = d->next;
2743 }
2744}