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