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