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