Fix oinitialisation of frame pointer in interrupt functions.
[official-gcc.git] / gcc / genrecog.c
blob00f425bc82268411f5b27ed244fe2a21a55f7139
1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
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.
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.
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. */
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.
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).
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
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
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.
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.
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. */
53 #include "hconfig.h"
54 #include "system.h"
55 #include "rtl.h"
56 #include "errors.h"
57 #include "gensupport.h"
60 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
61 printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
63 /* Holds an array of names indexed by insn_code_number. */
64 static char **insn_name_ptr = 0;
65 static int insn_name_ptr_size = 0;
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. */
71 struct decision_head
73 struct decision *first;
74 struct decision *last;
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. */
81 struct decision_test
83 /* A linked list through the tests attached to a node. */
84 struct decision_test *next;
86 /* These types are roughly in the order in which we'd like to test them. */
87 enum decision_type
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;
95 union
97 enum machine_mode mode; /* Machine mode of node. */
98 RTX_CODE code; /* Code to test. */
100 struct
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;
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. */
113 struct {
114 int code_number; /* Insn number matched. */
115 int lineno; /* Line number of the insn. */
116 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
117 } insn;
118 } u;
121 /* Data structure for decision tree for recognizing legitimate insns. */
123 struct decision
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. */
131 const char *position; /* String denoting position in pattern. */
133 struct decision_test *tests; /* The tests for this node. */
135 int number; /* Node number, used for labels */
136 int subroutine_number; /* Number of subroutine this node starts */
137 int need_label; /* Label needs to be output. */
140 #define SUBROUTINE_THRESHOLD 100
142 static int next_subroutine_number;
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. */
148 enum routine_type {
149 RECOG, SPLIT, PEEPHOLE2
152 #define IS_SPLIT(X) ((X) != RECOG)
154 /* Next available node number for tree nodes. */
156 static int next_number;
158 /* Next number to use as an insn_code. */
160 static int next_insn_code;
162 /* Similar, but counts all expressions in the MD file; used for
163 error messages. */
165 static int next_index;
167 /* Record the highest depth we ever have so we know how many variables to
168 allocate in each subroutine we make. */
170 static int max_depth;
172 /* The line number of the start of the pattern currently being processed. */
173 static int pattern_lineno;
175 /* Count of errors. */
176 static int error_count;
178 /* This table contains a list of the rtl codes that can possibly match a
179 predicate defined in recog.c. The function `maybe_both_true' uses it to
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. */
184 static struct pred_table
186 const char *name;
187 RTX_CODE codes[NUM_RTX_CODE];
188 } preds[] = {
189 {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
190 LABEL_REF, SUBREG, REG, MEM}},
191 #ifdef PREDICATE_CODES
192 PREDICATE_CODES
193 #endif
194 {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
195 LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
196 {"register_operand", {SUBREG, REG}},
197 {"pmode_register_operand", {SUBREG, REG}},
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}},
210 {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
211 UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
212 UNLT, LTGT}},
213 {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
214 LABEL_REF, SUBREG, REG, MEM}}
217 #define NUM_KNOWN_PREDS ARRAY_SIZE (preds)
219 static const char * special_mode_pred_table[] = {
220 #ifdef SPECIAL_MODE_PREDICATES
221 SPECIAL_MODE_PREDICATES
222 #endif
223 "pmode_register_operand"
226 #define NUM_SPECIAL_MODE_PREDS ARRAY_SIZE (special_mode_pred_table)
228 static struct decision *new_decision
229 PARAMS ((const char *, struct decision_head *));
230 static struct decision_test *new_decision_test
231 PARAMS ((enum decision_type, struct decision_test ***));
232 static rtx find_operand
233 PARAMS ((rtx, int));
234 static rtx find_matching_operand
235 PARAMS ((rtx, int));
236 static void validate_pattern
237 PARAMS ((rtx, rtx, rtx, int));
238 static struct decision *add_to_sequence
239 PARAMS ((rtx, struct decision_head *, const char *, enum routine_type, int));
241 static int maybe_both_true_2
242 PARAMS ((struct decision_test *, struct decision_test *));
243 static int maybe_both_true_1
244 PARAMS ((struct decision_test *, struct decision_test *));
245 static int maybe_both_true
246 PARAMS ((struct decision *, struct decision *, int));
248 static int nodes_identical_1
249 PARAMS ((struct decision_test *, struct decision_test *));
250 static int nodes_identical
251 PARAMS ((struct decision *, struct decision *));
252 static void merge_accept_insn
253 PARAMS ((struct decision *, struct decision *));
254 static void merge_trees
255 PARAMS ((struct decision_head *, struct decision_head *));
257 static void factor_tests
258 PARAMS ((struct decision_head *));
259 static void simplify_tests
260 PARAMS ((struct decision_head *));
261 static int break_out_subroutines
262 PARAMS ((struct decision_head *, int));
263 static void find_afterward
264 PARAMS ((struct decision_head *, struct decision *));
266 static void change_state
267 PARAMS ((const char *, const char *, struct decision *, const char *));
268 static void print_code
269 PARAMS ((enum rtx_code));
270 static void write_afterward
271 PARAMS ((struct decision *, struct decision *, const char *));
272 static struct decision *write_switch
273 PARAMS ((struct decision *, int));
274 static void write_cond
275 PARAMS ((struct decision_test *, int, enum routine_type));
276 static void write_action
277 PARAMS ((struct decision *, struct decision_test *, int, int,
278 struct decision *, enum routine_type));
279 static int is_unconditional
280 PARAMS ((struct decision_test *, enum routine_type));
281 static int write_node
282 PARAMS ((struct decision *, int, enum routine_type));
283 static void write_tree_1
284 PARAMS ((struct decision_head *, int, enum routine_type));
285 static void write_tree
286 PARAMS ((struct decision_head *, const char *, enum routine_type, int));
287 static void write_subroutine
288 PARAMS ((struct decision_head *, enum routine_type));
289 static void write_subroutines
290 PARAMS ((struct decision_head *, enum routine_type));
291 static void write_header
292 PARAMS ((void));
294 static struct decision_head make_insn_sequence
295 PARAMS ((rtx, enum routine_type));
296 static void process_tree
297 PARAMS ((struct decision_head *, enum routine_type));
299 static void record_insn_name
300 PARAMS ((int, const char *));
302 static void debug_decision_0
303 PARAMS ((struct decision *, int, int));
304 static void debug_decision_1
305 PARAMS ((struct decision *, int));
306 static void debug_decision_2
307 PARAMS ((struct decision_test *));
308 extern void debug_decision
309 PARAMS ((struct decision *));
310 extern void debug_decision_list
311 PARAMS ((struct decision *));
313 /* Create a new node in sequence after LAST. */
315 static struct decision *
316 new_decision (position, last)
317 const char *position;
318 struct decision_head *last;
320 register struct decision *new
321 = (struct decision *) xmalloc (sizeof (struct decision));
323 memset (new, 0, sizeof (*new));
324 new->success = *last;
325 new->position = xstrdup (position);
326 new->number = next_number++;
328 last->first = last->last = new;
329 return new;
332 /* Create a new test and link it in at PLACE. */
334 static struct decision_test *
335 new_decision_test (type, pplace)
336 enum decision_type type;
337 struct decision_test ***pplace;
339 struct decision_test **place = *pplace;
340 struct decision_test *test;
342 test = (struct decision_test *) xmalloc (sizeof (*test));
343 test->next = *place;
344 test->type = type;
345 *place = test;
347 place = &test->next;
348 *pplace = place;
350 return test;
353 /* Search for and return operand N. */
355 static rtx
356 find_operand (pattern, n)
357 rtx pattern;
358 int n;
360 const char *fmt;
361 RTX_CODE code;
362 int i, j, len;
363 rtx r;
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;
374 fmt = GET_RTX_FORMAT (code);
375 len = GET_RTX_LENGTH (code);
376 for (i = 0; i < len; i++)
378 switch (fmt[i])
380 case 'e': case 'u':
381 if ((r = find_operand (XEXP (pattern, i), n)) != NULL_RTX)
382 return r;
383 break;
385 case 'V':
386 if (! XVEC (pattern, i))
387 break;
388 /* FALLTHRU */
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;
396 case 'i': case 'w': case '0': case 's':
397 break;
399 default:
400 abort ();
404 return NULL;
407 /* Search for and return operand M, such that it has a matching
408 constraint for operand N. */
410 static rtx
411 find_matching_operand (pattern, n)
412 rtx pattern;
413 int n;
415 const char *fmt;
416 RTX_CODE code;
417 int i, j, len;
418 rtx r;
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;
427 fmt = GET_RTX_FORMAT (code);
428 len = GET_RTX_LENGTH (code);
429 for (i = 0; i < len; i++)
431 switch (fmt[i])
433 case 'e': case 'u':
434 if ((r = find_matching_operand (XEXP (pattern, i), n)))
435 return r;
436 break;
438 case 'V':
439 if (! XVEC (pattern, i))
440 break;
441 /* FALLTHRU */
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;
449 case 'i': case 'w': case '0': case 's':
450 break;
452 default:
453 abort ();
457 return NULL;
461 /* Check for various errors in patterns. SET is nonnull for a destination,
462 and is the complete set pattern. SET_CODE is '=' for normal sets, and
463 '+' within a context that requires in-out constraints. */
465 static void
466 validate_pattern (pattern, insn, set, set_code)
467 rtx pattern;
468 rtx insn;
469 rtx set;
470 int set_code;
472 const char *fmt;
473 RTX_CODE code;
474 size_t i, len;
475 int j;
477 code = GET_CODE (pattern);
478 switch (code)
480 case MATCH_SCRATCH:
481 return;
483 case MATCH_INSN:
484 case MATCH_OPERAND:
485 case MATCH_OPERATOR:
487 const char *pred_name = XSTR (pattern, 1);
488 int allows_non_lvalue = 1, allows_non_const = 1;
489 int special_mode_pred = 0;
490 const char *c_test;
492 if (GET_CODE (insn) == DEFINE_INSN)
493 c_test = XSTR (insn, 2);
494 else
495 c_test = XSTR (insn, 1);
497 if (pred_name[0] != 0)
499 for (i = 0; i < NUM_KNOWN_PREDS; i++)
500 if (! strcmp (preds[i].name, pred_name))
501 break;
503 if (i < NUM_KNOWN_PREDS)
505 int j;
507 allows_non_lvalue = allows_non_const = 0;
508 for (j = 0; preds[i].codes[j] != 0; j++)
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;
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;
529 else
531 #ifdef PREDICATE_CODES
532 /* If the port has a list of the predicates it uses but
533 omits one, warn. */
534 message_with_line (pattern_lineno,
535 "warning: `%s' not in PREDICATE_CODES",
536 pred_name);
537 #endif
540 for (i = 0; i < NUM_SPECIAL_MODE_PREDS; ++i)
541 if (strcmp (pred_name, special_mode_pred_table[i]) == 0)
543 special_mode_pred = 1;
544 break;
548 /* A MATCH_OPERAND that is a SET should have an output reload. */
549 if (set && code == MATCH_OPERAND
550 && XSTR (pattern, 2)[0] != '\0')
552 if (set_code == '+')
554 if (XSTR (pattern, 2)[0] == '+')
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)))
561 else
563 message_with_line (pattern_lineno,
564 "operand %d missing in-out reload",
565 XINT (pattern, 0));
566 error_count++;
569 else if (XSTR (pattern, 2)[0] != '='
570 && XSTR (pattern, 2)[0] != '+')
572 message_with_line (pattern_lineno,
573 "operand %d missing output reload",
574 XINT (pattern, 0));
575 error_count++;
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. */
582 if (set
583 && pred_name[0] != '\0'
584 && allows_non_lvalue)
586 message_with_line (pattern_lineno,
587 "warning: destination operand %d allows non-lvalue",
588 XINT (pattern, 0));
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
594 and PEEP2 can FAIL within the output pattern. Exclude
595 address_operand, since its mode is related to the mode of
596 the memory not the operand. Exclude the SET_DEST of a call
597 instruction, as that is a common idiom. */
599 if (GET_MODE (pattern) == VOIDmode
600 && code == MATCH_OPERAND
601 && GET_CODE (insn) == DEFINE_INSN
602 && allows_non_const
603 && ! special_mode_pred
604 && pred_name[0] != '\0'
605 && strcmp (pred_name, "address_operand") != 0
606 && strstr (c_test, "operands") == NULL
607 && ! (set
608 && GET_CODE (set) == SET
609 && GET_CODE (SET_SRC (set)) == CALL))
611 message_with_line (pattern_lineno,
612 "warning: operand %d missing mode?",
613 XINT (pattern, 0));
615 return;
618 case SET:
620 enum machine_mode dmode, smode;
621 rtx dest, src;
623 dest = SET_DEST (pattern);
624 src = SET_SRC (pattern);
626 /* Find the referant for a DUP. */
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));
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));
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);
643 dmode = GET_MODE (dest);
644 smode = GET_MODE (src);
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"))
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)
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++;
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
667 && GET_CODE (src) != PC
668 && GET_CODE (src) != CC0
669 && GET_CODE (src) != CONST_INT)
671 const char *which;
672 which = (dmode == VOIDmode ? "destination" : "source");
673 message_with_line (pattern_lineno,
674 "warning: %s missing a mode?", which);
677 if (dest != SET_DEST (pattern))
678 validate_pattern (dest, insn, pattern, '=');
679 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
680 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
681 return;
684 case CLOBBER:
685 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
686 return;
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;
694 case STRICT_LOW_PART:
695 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
696 return;
698 case LABEL_REF:
699 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
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++;
706 break;
708 default:
709 break;
712 fmt = GET_RTX_FORMAT (code);
713 len = GET_RTX_LENGTH (code);
714 for (i = 0; i < len; i++)
716 switch (fmt[i])
718 case 'e': case 'u':
719 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
720 break;
722 case 'E':
723 for (j = 0; j < XVECLEN (pattern, i); j++)
724 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
725 break;
727 case 'i': case 'w': case '0': case 's':
728 break;
730 default:
731 abort ();
736 /* Create a chain of nodes to verify that an rtl expression matches
737 PATTERN.
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).
742 POSITION is the string representing the current position in the insn.
744 INSN_TYPE is the type of insn for which we are emitting code.
746 A pointer to the final node in the chain is returned. */
748 static struct decision *
749 add_to_sequence (pattern, last, position, insn_type, top)
750 rtx pattern;
751 struct decision_head *last;
752 const char *position;
753 enum routine_type insn_type;
754 int top;
756 RTX_CODE code;
757 struct decision *this, *sub;
758 struct decision_test *test;
759 struct decision_test **place;
760 char *subpos;
761 register size_t i;
762 register const char *fmt;
763 int depth = strlen (position);
764 int len;
765 enum machine_mode mode;
767 if (depth > max_depth)
768 max_depth = depth;
770 subpos = (char *) xmalloc (depth + 2);
771 strcpy (subpos, position);
772 subpos[depth + 1] = 0;
774 sub = this = new_decision (position, last);
775 place = &this->tests;
777 restart:
778 mode = GET_MODE (pattern);
779 code = GET_CODE (pattern);
781 switch (code)
783 case PARALLEL:
784 /* Toplevel peephole pattern. */
785 if (insn_type == PEEPHOLE2 && top)
787 /* We don't need the node we just created -- unlink it. */
788 last->first = last->last = NULL;
790 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
792 /* Which insn we're looking at is represented by A-Z. We don't
793 ever use 'A', however; it is always implied. */
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;
800 goto ret;
803 /* Else nothing special. */
804 break;
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 */
816 case MATCH_OPERAND:
817 case MATCH_SCRATCH:
818 case MATCH_OPERATOR:
819 case MATCH_INSN:
821 const char *pred_name;
822 RTX_CODE was_code = code;
823 int allows_const_int = 1;
825 if (code == MATCH_SCRATCH)
827 pred_name = "scratch_operand";
828 code = UNKNOWN;
830 else
832 pred_name = XSTR (pattern, 1);
833 if (code == MATCH_PARALLEL)
834 code = PARALLEL;
835 else
836 code = UNKNOWN;
839 if (pred_name[0] != 0)
841 test = new_decision_test (DT_pred, &place);
842 test->u.pred.name = pred_name;
843 test->u.pred.mode = mode;
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.
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. */
856 for (i = 0; i < NUM_KNOWN_PREDS; i++)
857 if (! strcmp (preds[i].name, pred_name))
858 break;
860 if (i < NUM_KNOWN_PREDS)
862 int j;
864 test->u.pred.index = i;
866 if (preds[i].codes[1] == 0 && code == UNKNOWN)
867 code = preds[i].codes[0];
869 allows_const_int = 0;
870 for (j = 0; preds[i].codes[j] != 0; j++)
871 if (preds[i].codes[j] == CONST_INT)
873 allows_const_int = 1;
874 break;
877 else
878 test->u.pred.index = -1;
881 /* Can't enforce a mode if we allow const_int. */
882 if (allows_const_int)
883 mode = VOIDmode;
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);
889 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
891 char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
892 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
894 subpos[depth] = i + base;
895 sub = add_to_sequence (XVECEXP (pattern, 2, i),
896 &sub->success, subpos, insn_type, 0);
899 goto fini;
902 case MATCH_OP_DUP:
903 code = UNKNOWN;
905 test = new_decision_test (DT_dup, &place);
906 test->u.dup = XINT (pattern, 0);
908 test = new_decision_test (DT_accept_op, &place);
909 test->u.opno = XINT (pattern, 0);
911 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
913 subpos[depth] = i + '0';
914 sub = add_to_sequence (XVECEXP (pattern, 1, i),
915 &sub->success, subpos, insn_type, 0);
917 goto fini;
919 case MATCH_DUP:
920 case MATCH_PAR_DUP:
921 code = UNKNOWN;
923 test = new_decision_test (DT_dup, &place);
924 test->u.dup = XINT (pattern, 0);
925 goto fini;
927 case ADDRESS:
928 pattern = XEXP (pattern, 0);
929 goto restart;
931 default:
932 break;
935 fmt = GET_RTX_FORMAT (code);
936 len = GET_RTX_LENGTH (code);
938 /* Do tests against the current node first. */
939 for (i = 0; i < (size_t) len; i++)
941 if (fmt[i] == 'i')
943 if (i == 0)
945 test = new_decision_test (DT_elt_zero_int, &place);
946 test->u.intval = XINT (pattern, i);
948 else if (i == 1)
950 test = new_decision_test (DT_elt_one_int, &place);
951 test->u.intval = XINT (pattern, i);
953 else
954 abort ();
956 else if (fmt[i] == 'w')
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;
964 if (i != 0)
965 abort ();
967 test = new_decision_test (type, &place);
968 test->u.intval = XWINT (pattern, i);
970 else if (fmt[i] == 'E')
972 if (i != 0)
973 abort ();
975 test = new_decision_test (DT_veclen, &place);
976 test->u.veclen = XVECLEN (pattern, i);
980 /* Now test our sub-patterns. */
981 for (i = 0; i < (size_t) len; i++)
983 switch (fmt[i])
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;
991 case 'E':
993 register int j;
994 for (j = 0; j < XVECLEN (pattern, i); j++)
996 subpos[depth] = 'a' + j;
997 sub = add_to_sequence (XVECEXP (pattern, i, j),
998 &sub->success, subpos, insn_type, 0);
1000 break;
1003 case 'i': case 'w':
1004 /* Handled above. */
1005 break;
1006 case '0':
1007 break;
1009 default:
1010 abort ();
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)
1019 place = &this->tests;
1020 test = new_decision_test (DT_code, &place);
1021 test->u.code = code;
1024 if (mode != VOIDmode)
1026 place = &this->tests;
1027 test = new_decision_test (DT_mode, &place);
1028 test->u.mode = mode;
1031 /* If we didn't insert any tests or accept nodes, hork. */
1032 if (this->tests == NULL)
1033 abort ();
1035 ret:
1036 free (subpos);
1037 return sub;
1040 /* A subroutine of maybe_both_true; examines only one test.
1041 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1043 static int
1044 maybe_both_true_2 (d1, d2)
1045 struct decision_test *d1, *d2;
1047 if (d1->type == d2->type)
1049 switch (d1->type)
1051 case DT_mode:
1052 return d1->u.mode == d2->u.mode;
1054 case DT_code:
1055 return d1->u.code == d2->u.code;
1057 case DT_veclen:
1058 return d1->u.veclen == d2->u.veclen;
1060 case DT_elt_zero_int:
1061 case DT_elt_one_int:
1062 case DT_elt_zero_wide:
1063 case DT_elt_zero_wide_safe:
1064 return d1->u.intval == d2->u.intval;
1066 default:
1067 break;
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. */
1075 if (d1->type == DT_pred || d2->type == DT_pred)
1077 if (d2->type == DT_pred)
1079 struct decision_test *tmp;
1080 tmp = d1, d1 = d2, d2 = tmp;
1083 /* If D2 tests a mode, see if it matches D1. */
1084 if (d1->u.pred.mode != VOIDmode)
1086 if (d2->type == DT_mode)
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)
1094 return 0;
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. */
1102 if (d1->u.pred.index >= 0)
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)
1108 const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
1109 while (*c != 0)
1111 if (*c == d2->u.code)
1112 break;
1113 ++c;
1115 if (*c == 0)
1116 return 0;
1119 /* Otherwise see if the predicates have any codes in common. */
1120 else if (d2->type == DT_pred && d2->u.pred.index >= 0)
1122 const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
1123 int common = 0;
1125 while (*c1 != 0 && !common)
1127 const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
1128 while (*c2 != 0 && !common)
1130 common = (*c1 == *c2);
1131 ++c2;
1133 ++c1;
1136 if (!common)
1137 return 0;
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;
1148 return -1;
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". */
1154 static int
1155 maybe_both_true_1 (d1, d2)
1156 struct decision_test *d1, *d2;
1158 struct decision_test *t1, *t2;
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;
1165 /* Eliminate pairs of tests while they can exactly match. */
1166 while (d1 && d2 && d1->type == d2->type)
1168 if (maybe_both_true_2 (d1, d2) == 0)
1169 return 0;
1170 d1 = d1->next, d2 = d2->next;
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;
1179 return -1;
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
1184 can match both or just that we couldn't prove there wasn't such an RTL).
1186 TOPLEVEL is non-zero if we are to only look at the top level and not
1187 recursively descend. */
1189 static int
1190 maybe_both_true (d1, d2, toplevel)
1191 struct decision *d1, *d2;
1192 int toplevel;
1194 struct decision *p1, *p2;
1195 int cmp;
1197 /* Don't compare strings on the different positions in insn. Doing so
1198 is incorrect and results in false matches from constructs like
1200 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1201 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1203 [(set (match_operand:HI "register_operand" "r")
1204 (match_operand:HI "register_operand" "r"))]
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.
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. */
1215 cmp = strcmp (d1->position, d2->position);
1216 if (cmp != 0)
1218 if (toplevel)
1219 abort();
1221 /* If the d2->position was lexically lower, swap. */
1222 if (cmp > 0)
1223 p1 = d1, d1 = d2, d2 = p1;
1225 if (d1->success.first == 0)
1226 return 1;
1227 for (p1 = d1->success.first; p1; p1 = p1->next)
1228 if (maybe_both_true (p1, d2, 0))
1229 return 1;
1231 return 0;
1234 /* Test the current level. */
1235 cmp = maybe_both_true_1 (d1->tests, d2->tests);
1236 if (cmp >= 0)
1237 return cmp;
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. */
1245 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
1246 return 1;
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;
1253 return 0;
1256 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
1258 static int
1259 nodes_identical_1 (d1, d2)
1260 struct decision_test *d1, *d2;
1262 switch (d1->type)
1264 case DT_mode:
1265 return d1->u.mode == d2->u.mode;
1267 case DT_code:
1268 return d1->u.code == d2->u.code;
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);
1274 case DT_c_test:
1275 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1277 case DT_veclen:
1278 case DT_veclen_ge:
1279 return d1->u.veclen == d2->u.veclen;
1281 case DT_dup:
1282 return d1->u.dup == d2->u.dup;
1284 case DT_elt_zero_int:
1285 case DT_elt_one_int:
1286 case DT_elt_zero_wide:
1287 case DT_elt_zero_wide_safe:
1288 return d1->u.intval == d2->u.intval;
1290 case DT_accept_op:
1291 return d1->u.opno == d2->u.opno;
1293 case DT_accept_insn:
1294 /* Differences will be handled in merge_accept_insn. */
1295 return 1;
1297 default:
1298 abort ();
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. */
1306 static int
1307 nodes_identical (d1, d2)
1308 struct decision *d1, *d2;
1310 struct decision_test *t1, *t2;
1312 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1314 if (t1->type != t2->type)
1315 return 0;
1316 if (! nodes_identical_1 (t1, t2))
1317 return 0;
1320 /* For success, they should now both be null. */
1321 if (t1 != t2)
1322 return 0;
1324 /* Check that their subnodes are at the same position, as any one set
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. */
1328 if (d1->success.first
1329 && d2->success.first
1330 && strcmp (d1->success.first->position, d2->success.first->position))
1331 return 0;
1333 return 1;
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. */
1343 static void
1344 merge_accept_insn (oldd, addd)
1345 struct decision *oldd, *addd;
1347 struct decision_test *old, *add;
1349 for (old = oldd->tests; old; old = old->next)
1350 if (old->type == DT_accept_insn)
1351 break;
1352 if (old == NULL)
1353 return;
1355 for (add = addd->tests; add; add = add->next)
1356 if (add->type == DT_accept_insn)
1357 break;
1358 if (add == NULL)
1359 return;
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. */
1364 if (old->u.insn.num_clobbers_to_add == 0
1365 && add->u.insn.num_clobbers_to_add > 0)
1367 /* Nothing to do here. */
1369 else if (old->u.insn.num_clobbers_to_add > 0
1370 && add->u.insn.num_clobbers_to_add == 0)
1372 /* In this case, replace OLD with ADD. */
1373 old->u.insn = add->u.insn;
1375 else
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++;
1386 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1388 static void
1389 merge_trees (oldh, addh)
1390 struct decision_head *oldh, *addh;
1392 struct decision *next, *add;
1394 if (addh->first == 0)
1395 return;
1396 if (oldh->first == 0)
1398 *oldh = *addh;
1399 return;
1402 /* Trying to merge bits at different positions isn't possible. */
1403 if (strcmp (oldh->first->position, addh->first->position))
1404 abort ();
1406 for (add = addh->first; add ; add = next)
1408 struct decision *old, *insert_before = NULL;
1410 next = add->next;
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.
1419 Scan starting from the end of OLDH until we reach a point
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.
1426 If we have no match, place NEW after the closest match we found. */
1428 for (old = oldh->last; old; old = old->prev)
1430 if (nodes_identical (old, add))
1432 merge_accept_insn (old, add);
1433 merge_trees (&old->success, &add->success);
1434 goto merged_nodes;
1437 if (maybe_both_true (old, add, 0))
1438 break;
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. */
1444 if ((int) add->tests->type < (int) old->tests->type)
1445 insert_before = old;
1448 if (insert_before == NULL)
1450 add->next = NULL;
1451 add->prev = oldh->last;
1452 oldh->last->next = add;
1453 oldh->last = add;
1455 else
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;
1465 merged_nodes:;
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. */
1473 static void
1474 factor_tests (head)
1475 struct decision_head *head;
1477 struct decision *first, *next;
1479 for (first = head->first; first && first->next; first = next)
1481 enum decision_type type;
1482 struct decision *new, *old_last;
1484 type = first->tests->type;
1485 next = first->next;
1487 /* Want at least two compatible sequential nodes. */
1488 if (next->tests->type != type)
1489 continue;
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
1498 && type != DT_elt_zero_wide_safe)
1499 continue;
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)
1505 new = new_decision (first->position, &first->success);
1506 new->tests = first->tests->next;
1507 first->tests->next = NULL;
1510 /* Crop the node tree off after our first test. */
1511 first->next = NULL;
1512 old_last = head->last;
1513 head->last = first;
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. */
1519 struct decision_head h;
1521 if (next->tests->next != NULL)
1523 new = new_decision (next->position, &next->success);
1524 new->tests = next->tests->next;
1525 next->tests->next = NULL;
1527 new = next;
1528 next = next->next;
1529 new->next = NULL;
1530 h.first = h.last = new;
1532 merge_trees (head, &h);
1534 while (next && next->tests->type == type);
1536 /* After we run out of compatible tests, graft the remaining nodes
1537 back onto the tree. */
1538 if (next)
1540 next->prev = head->last;
1541 head->last->next = next;
1542 head->last = old_last;
1546 /* Recurse. */
1547 for (first = head->first; first; first = first->next)
1548 factor_tests (&first->success);
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:
1556 If we have mode tests or code tests that are redundant with
1557 predicates, remove them. */
1559 static void
1560 simplify_tests (head)
1561 struct decision_head *head;
1563 struct decision *tree;
1565 for (tree = head->first; tree; tree = tree->next)
1567 struct decision_test *a, *b;
1569 a = tree->tests;
1570 b = a->next;
1571 if (b == NULL)
1572 continue;
1574 /* Find a predicate node. */
1575 while (b && b->type != DT_pred)
1576 b = b->next;
1577 if (b)
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;
1588 /* Recurse. */
1589 for (tree = head->first; tree; tree = tree->next)
1590 simplify_tests (&tree->success);
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
1595 that is generated. */
1597 static int
1598 break_out_subroutines (head, initial)
1599 struct decision_head *head;
1600 int initial;
1602 int size = 0;
1603 struct decision *sub;
1605 for (sub = head->first; sub; sub = sub->next)
1606 size += 1 + break_out_subroutines (&sub->success, 0);
1608 if (size > SUBROUTINE_THRESHOLD && ! initial)
1610 head->first->subroutine_number = ++next_subroutine_number;
1611 size = 1;
1613 return size;
1616 /* For each node p, find the next alternative that might be true
1617 when p is true. */
1619 static void
1620 find_afterward (head, real_afterward)
1621 struct decision_head *head;
1622 struct decision *real_afterward;
1624 struct decision *p, *q, *afterward;
1626 /* We can't propogate alternatives across subroutine boundaries.
1627 This is not incorrect, merely a minor optimization loss. */
1629 p = head->first;
1630 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1632 for ( ; p ; p = p->next)
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;
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;
1648 /* Recurse. */
1649 for (p = head->first; p ; p = p->next)
1650 if (p->success.first)
1651 find_afterward (&p->success, p->afterward);
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;
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.
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. */
1668 static void
1669 change_state (oldpos, newpos, afterward, indent)
1670 const char *oldpos;
1671 const char *newpos;
1672 struct decision *afterward;
1673 const char *indent;
1675 int odepth = strlen (oldpos);
1676 int ndepth = strlen (newpos);
1677 int depth;
1678 int old_has_insn, new_has_insn;
1680 /* Pop up as many levels as necessary. */
1681 for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1682 continue;
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;
1688 for (new_has_insn = ndepth - 1; new_has_insn >= 0; --new_has_insn)
1689 if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1690 break;
1692 /* Go down to desired level. */
1693 while (depth < ndepth)
1695 /* It's a different insn from the first one. */
1696 if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1698 /* We can only fail if we're moving down the tree. */
1699 if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1701 printf ("%stem = peep2_next_insn (%d);\n",
1702 indent, newpos[depth] - 'A');
1704 else
1706 printf ("%stem = peep2_next_insn (%d);\n",
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);
1711 else
1712 printf ("%s goto ret0;\n", indent);
1714 printf ("%sx%d = PATTERN (tem);\n", indent, depth + 1);
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;
1726 /* Print the enumerator constant for CODE -- the upcase version of
1727 the name. */
1729 static void
1730 print_code (code)
1731 enum rtx_code code;
1733 register const char *p;
1734 for (p = GET_RTX_NAME (code); *p; p++)
1735 putchar (TOUPPER (*p));
1738 /* Emit code to cross an afterward link -- change state and branch. */
1740 static void
1741 write_afterward (start, afterward, indent)
1742 struct decision *start;
1743 struct decision *afterward;
1744 const char *indent;
1746 if (!afterward || start->subroutine_number > 0)
1747 printf("%sgoto ret0;\n", indent);
1748 else
1750 change_state (start->position, afterward->position, NULL, indent);
1751 printf ("%sgoto L%d;\n", indent, afterward->number);
1755 /* Emit a switch statement, if possible, for an initial sequence of
1756 nodes at START. Return the first node yet untested. */
1758 static struct decision *
1759 write_switch (start, depth)
1760 struct decision *start;
1761 int depth;
1763 struct decision *p = start;
1764 enum decision_type type = p->tests->type;
1765 struct decision *needs_label = NULL;
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. */
1770 if (!p->next
1771 || p->tests->next
1772 || p->next->tests->type != type
1773 || p->next->tests->next
1774 || nodes_identical_1 (p->tests, p->next->tests))
1775 return p;
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)
1781 char codemap[NUM_RTX_CODE];
1782 struct decision *ret;
1783 RTX_CODE code;
1785 memset (codemap, 0, sizeof(codemap));
1787 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1788 code = p->tests->u.code;
1791 if (p != start && p->need_label && needs_label == NULL)
1792 needs_label = p;
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;
1799 codemap[code] = 1;
1800 p = p->next;
1802 while (p
1803 && ! p->tests->next
1804 && p->tests->type == DT_code
1805 && ! codemap[code = p->tests->u.code]);
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. */
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. */
1817 if (needs_label != NULL)
1818 ret = needs_label;
1819 else
1820 ret = p;
1822 while (p && p->tests->type == DT_pred
1823 && p->tests->u.pred.index >= 0)
1825 const RTX_CODE *c;
1827 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1828 if (codemap[(int) *c] != 0)
1829 goto pred_done;
1831 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1833 printf (" case ");
1834 print_code (*c);
1835 printf (":\n");
1836 codemap[(int) *c] = 1;
1839 printf (" goto L%d;\n", p->number);
1840 p->need_label = 1;
1841 p = p->next;
1844 pred_done:
1845 /* Make the default case skip the predicates we managed to match. */
1847 printf (" default:\n");
1848 if (p != ret)
1850 if (p)
1852 printf (" goto L%d;\n", p->number);
1853 p->need_label = 1;
1855 else
1856 write_afterward (start, start->afterward, " ");
1858 else
1859 printf (" break;\n");
1860 printf (" }\n");
1862 return ret;
1864 else if (type == DT_mode
1865 || type == DT_veclen
1866 || type == DT_elt_zero_int
1867 || type == DT_elt_one_int
1868 || type == DT_elt_zero_wide_safe)
1870 printf (" switch (");
1871 switch (type)
1873 case DT_mode:
1874 printf ("GET_MODE (x%d)", depth);
1875 break;
1876 case DT_veclen:
1877 printf ("XVECLEN (x%d, 0)", depth);
1878 break;
1879 case DT_elt_zero_int:
1880 printf ("XINT (x%d, 0)", depth);
1881 break;
1882 case DT_elt_one_int:
1883 printf ("XINT (x%d, 1)", depth);
1884 break;
1885 case DT_elt_zero_wide_safe:
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);
1889 break;
1890 default:
1891 abort ();
1893 printf (")\n {\n");
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;
1905 if (p != start && p->need_label && needs_label == NULL)
1906 needs_label = p;
1908 printf (" case ");
1909 switch (type)
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:
1920 case DT_elt_zero_wide_safe:
1921 printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1922 break;
1923 default:
1924 abort ();
1926 printf (":\n goto L%d;\n", p->success.first->number);
1927 p->success.first->need_label = 1;
1929 p = p->next;
1931 while (p && p->tests->type == type && !p->tests->next);
1933 case_done:
1934 printf (" default:\n break;\n }\n");
1936 return needs_label != NULL ? needs_label : p;
1938 else
1940 /* None of the other tests are ameanable. */
1941 return p;
1945 /* Emit code for one test. */
1947 static void
1948 write_cond (p, depth, subroutine_type)
1949 struct decision_test *p;
1950 int depth;
1951 enum routine_type subroutine_type;
1953 switch (p->type)
1955 case DT_mode:
1956 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1957 break;
1959 case DT_code:
1960 printf ("GET_CODE (x%d) == ", depth);
1961 print_code (p->u.code);
1962 break;
1964 case DT_veclen:
1965 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1966 break;
1968 case DT_elt_zero_int:
1969 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1970 break;
1972 case DT_elt_one_int:
1973 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1974 break;
1976 case DT_elt_zero_wide:
1977 case DT_elt_zero_wide_safe:
1978 printf ("XWINT (x%d, 0) == ", depth);
1979 printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1980 break;
1982 case DT_veclen_ge:
1983 printf ("XVECLEN (x%d, 0) >= %d", depth, p->u.veclen);
1984 break;
1986 case DT_dup:
1987 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1988 break;
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;
1995 case DT_c_test:
1996 printf ("(%s)", p->u.c_test);
1997 break;
1999 case DT_accept_insn:
2000 switch (subroutine_type)
2002 case RECOG:
2003 if (p->u.insn.num_clobbers_to_add == 0)
2004 abort ();
2005 printf ("pnum_clobbers != NULL");
2006 break;
2008 default:
2009 abort ();
2011 break;
2013 default:
2014 abort ();
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. */
2022 static void
2023 write_action (p, test, depth, uncond, success, subroutine_type)
2024 struct decision *p;
2025 struct decision_test *test;
2026 int depth, uncond;
2027 struct decision *success;
2028 enum routine_type subroutine_type;
2030 const char *indent;
2031 int want_close = 0;
2033 if (uncond)
2034 indent = " ";
2035 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
2037 fputs (" {\n", stdout);
2038 indent = " ";
2039 want_close = 1;
2041 else
2042 indent = " ";
2044 if (test->type == DT_accept_op)
2046 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
2048 /* Only allow DT_accept_insn to follow. */
2049 if (test->next)
2051 test = test->next;
2052 if (test->type != DT_accept_insn)
2053 abort ();
2057 /* Sanity check that we're now at the end of the list of tests. */
2058 if (test->next)
2059 abort ();
2061 if (test->type == DT_accept_insn)
2063 switch (subroutine_type)
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;
2072 case SPLIT:
2073 printf ("%sreturn gen_split_%d (operands);\n",
2074 indent, test->u.insn.code_number);
2075 break;
2077 case PEEPHOLE2:
2079 int match_len = 0, i;
2081 for (i = strlen (p->position) - 1; i >= 0; --i)
2082 if (p->position[i] >= 'A' && p->position[i] <= 'Z')
2084 match_len = p->position[i] - 'A';
2085 break;
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);
2092 break;
2094 default:
2095 abort ();
2098 else
2100 printf("%sgoto L%d;\n", indent, success->number);
2101 success->need_label = 1;
2104 if (want_close)
2105 fputs (" }\n", stdout);
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. */
2113 static int
2114 is_unconditional (t, subroutine_type)
2115 struct decision_test *t;
2116 enum routine_type subroutine_type;
2118 if (t->type == DT_accept_op)
2119 return 1;
2121 if (t->type == DT_accept_insn)
2123 switch (subroutine_type)
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 ();
2136 return 0;
2139 /* Emit code for one node -- the conditional and the accompanying action.
2140 Return true if there is no fallthru path. */
2142 static int
2143 write_node (p, depth, subroutine_type)
2144 struct decision *p;
2145 int depth;
2146 enum routine_type subroutine_type;
2148 struct decision_test *test, *last_test;
2149 int uncond;
2151 last_test = test = p->tests;
2152 uncond = is_unconditional (test, subroutine_type);
2153 if (uncond == 0)
2155 printf (" if (");
2156 write_cond (test, depth, subroutine_type);
2158 while ((test = test->next) != NULL)
2160 int uncond2;
2162 last_test = test;
2163 uncond2 = is_unconditional (test, subroutine_type);
2164 if (uncond2 != 0)
2165 break;
2167 printf ("\n && ");
2168 write_cond (test, depth, subroutine_type);
2171 printf (")\n");
2174 write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
2176 return uncond > 0;
2179 /* Emit code for all of the sibling nodes of HEAD. */
2181 static void
2182 write_tree_1 (head, depth, subroutine_type)
2183 struct decision_head *head;
2184 int depth;
2185 enum routine_type subroutine_type;
2187 struct decision *p, *next;
2188 int uncond = 0;
2190 for (p = head->first; p ; p = next)
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);
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
2202 /* Failed -- fall back and write one node. */
2203 uncond = write_node (p, depth, subroutine_type);
2204 next = p->next;
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, " ");
2214 /* Write out the decision tree starting at HEAD. PREVPOS is the
2215 position at the node that branched to this node. */
2217 static void
2218 write_tree (head, prevpos, type, initial)
2219 struct decision_head *head;
2220 const char *prevpos;
2221 enum routine_type type;
2222 int initial;
2224 register struct decision *p = head->first;
2226 putchar ('\n');
2227 if (p->need_label)
2228 OUTPUT_LABEL (" ", p->number);
2230 if (! initial && p->subroutine_number > 0)
2232 static const char * const name_prefix[] = {
2233 "recog", "split", "peephole2"
2236 static const char * const call_suffix[] = {
2237 ", pnum_clobbers", "", ", _pmatch_len"
2240 /* This node has been broken out into a separate subroutine.
2241 Call it, test the result, and branch accordingly. */
2243 if (p->afterward)
2245 printf (" tem = %s_%d (x0, insn%s);\n",
2246 name_prefix[type], p->subroutine_number, call_suffix[type]);
2247 if (IS_SPLIT (type))
2248 printf (" if (tem != 0)\n return tem;\n");
2249 else
2250 printf (" if (tem >= 0)\n return tem;\n");
2252 change_state (p->position, p->afterward->position, NULL, " ");
2253 printf (" goto L%d;\n", p->afterward->number);
2255 else
2257 printf (" return %s_%d (x0, insn%s);\n",
2258 name_prefix[type], p->subroutine_number, call_suffix[type]);
2261 else
2263 int depth = strlen (p->position);
2265 change_state (prevpos, p->position, head->last->afterward, " ");
2266 write_tree_1 (head, depth, type);
2268 for (p = head->first; p; p = p->next)
2269 if (p->success.first)
2270 write_tree (&p->success, p->position, type, 0);
2274 /* Write out a subroutine of type TYPE to do comparisons starting at
2275 node TREE. */
2277 static void
2278 write_subroutine (head, type)
2279 struct decision_head *head;
2280 enum routine_type type;
2282 int subfunction = head->first ? head->first->subroutine_number : 0;
2283 const char *s_or_e;
2284 char extension[32];
2285 int i;
2287 s_or_e = subfunction ? "static " : "";
2289 if (subfunction)
2290 sprintf (extension, "_%d", subfunction);
2291 else if (type == RECOG)
2292 extension[0] = '\0';
2293 else
2294 strcpy (extension, "_insns");
2296 switch (type)
2298 case RECOG:
2299 printf ("%sint recog%s PARAMS ((rtx, rtx, int *));\n", s_or_e, extension);
2300 printf ("%sint\n\
2301 recog%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:
2307 printf ("%srtx split%s PARAMS ((rtx, rtx));\n", s_or_e, extension);
2308 printf ("%srtx\n\
2309 split%s (x0, insn)\n\
2310 register rtx x0;\n\
2311 rtx insn ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2312 break;
2313 case PEEPHOLE2:
2314 printf ("%srtx peephole2%s PARAMS ((rtx, rtx, int *));\n",
2315 s_or_e, extension);
2316 printf ("%srtx\n\
2317 peephole2%s (x0, insn, _pmatch_len)\n\
2318 register rtx x0;\n\
2319 rtx insn ATTRIBUTE_UNUSED;\n\
2320 int *_pmatch_len ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2321 break;
2324 printf ("{\n register rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2325 for (i = 1; i <= max_depth; i++)
2326 printf (" register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2328 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2330 if (!subfunction)
2331 printf (" recog_data.insn = NULL_RTX;\n");
2333 if (head->first)
2334 write_tree (head, "", type, 1);
2335 else
2336 printf (" goto ret0;\n");
2338 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2341 /* In break_out_subroutines, we discovered the boundaries for the
2342 subroutines, but did not write them out. Do so now. */
2344 static void
2345 write_subroutines (head, type)
2346 struct decision_head *head;
2347 enum routine_type type;
2349 struct decision *p;
2351 for (p = head->first; p ; p = p->next)
2352 if (p->success.first)
2353 write_subroutines (&p->success, type);
2355 if (head->first->subroutine_number > 0)
2356 write_subroutine (head, type);
2359 /* Begin the output file. */
2361 static void
2362 write_header ()
2364 puts ("\
2365 /* Generated automatically by the program `genrecog' from the target\n\
2366 machine description file. */\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\
2378 #include \"hard-reg-set.h\"\n\
2379 #include \"resource.h\"\n\
2380 #include \"toplev.h\"\n\
2381 \n");
2383 puts ("\n\
2384 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2385 X0 is a valid instruction.\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\
2391 index into `insn_data' and other tables.\n");
2392 puts ("\
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\
2397 to be added (it should be initialized to zero by the caller). If it");
2398 puts ("\
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\
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\
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");
2415 /* Construct and return a sequence of decisions
2416 that will recognize INSN.
2418 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2420 static struct decision_head
2421 make_insn_sequence (insn, type)
2422 rtx insn;
2423 enum routine_type type;
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;
2430 char c_test_pos[2];
2432 record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2434 c_test_pos[0] = '\0';
2435 if (type == PEEPHOLE2)
2437 int i, j;
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++)
2448 rtx tmp = XVECEXP (insn, 0, i);
2449 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2451 XVECEXP (x, 0, j) = tmp;
2452 j++;
2455 XVECLEN (x, 0) = j;
2457 c_test_pos[0] = 'A' + j - 1;
2458 c_test_pos[1] = '\0';
2460 else if (XVECLEN (insn, type == RECOG) == 1)
2461 x = XVECEXP (insn, type == RECOG, 0);
2462 else
2464 x = rtx_alloc (PARALLEL);
2465 XVEC (x, 0) = XVEC (insn, type == RECOG);
2466 PUT_MODE (x, VOIDmode);
2469 validate_pattern (x, insn, NULL_RTX, 0);
2471 memset(&head, 0, sizeof(head));
2472 last = add_to_sequence (x, &head, "", type, 1);
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;
2479 if (c_test[0])
2481 /* Need a new node if we have another test to add. */
2482 if (test->type == DT_accept_op)
2484 last = new_decision (c_test_pos, &last->success);
2485 place = &last->tests;
2487 test = new_decision_test (DT_c_test, &place);
2488 test->u.c_test = c_test;
2491 test = new_decision_test (DT_accept_insn, &place);
2492 test->u.insn.code_number = next_insn_code;
2493 test->u.insn.lineno = pattern_lineno;
2494 test->u.insn.num_clobbers_to_add = 0;
2496 switch (type)
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. */
2503 if (GET_CODE (x) == PARALLEL)
2505 int i;
2507 /* Find the last non-clobber in the parallel. */
2508 for (i = XVECLEN (x, 0); i > 0; i--)
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;
2517 if (i != XVECLEN (x, 0))
2519 rtx new;
2520 struct decision_head clobber_head;
2522 /* Build a similar insn without the clobbers. */
2523 if (i == 1)
2524 new = XVECEXP (x, 0, 0);
2525 else
2527 int j;
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);
2535 /* Recognize it. */
2536 memset (&clobber_head, 0, sizeof(clobber_head));
2537 last = add_to_sequence (new, &clobber_head, "", type, 1);
2539 /* Find the end of the test chain on the last node. */
2540 for (test = last->tests; test->next; test = test->next)
2541 continue;
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)
2548 last = new_decision ("", &last->success);
2549 place = &last->tests;
2552 if (c_test[0])
2554 test = new_decision_test (DT_c_test, &place);
2555 test->u.c_test = c_test;
2558 test = new_decision_test (DT_accept_insn, &place);
2559 test->u.insn.code_number = next_insn_code;
2560 test->u.insn.lineno = pattern_lineno;
2561 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2563 merge_trees (&head, &clobber_head);
2566 break;
2568 case SPLIT:
2569 /* Define the subroutine we will call below and emit in genemit. */
2570 printf ("extern rtx gen_split_%d PARAMS ((rtx *));\n", next_insn_code);
2571 break;
2573 case PEEPHOLE2:
2574 /* Define the subroutine we will call below and emit in genemit. */
2575 printf ("extern rtx gen_peephole2_%d PARAMS ((rtx, rtx *));\n",
2576 next_insn_code);
2577 break;
2580 return head;
2583 static void
2584 process_tree (head, subroutine_type)
2585 struct decision_head *head;
2586 enum routine_type subroutine_type;
2588 if (head->first == NULL)
2590 /* We can elide peephole2_insns, but not recog or split_insns. */
2591 if (subroutine_type == PEEPHOLE2)
2592 return;
2594 else
2596 factor_tests (head);
2598 next_subroutine_number = 0;
2599 break_out_subroutines (head, 1);
2600 find_afterward (head, NULL);
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);
2607 write_subroutines (head, subroutine_type);
2610 write_subroutine (head, subroutine_type);
2613 extern int main PARAMS ((int, char **));
2616 main (argc, argv)
2617 int argc;
2618 char **argv;
2620 rtx desc;
2621 struct decision_head recog_tree, split_tree, peephole2_tree, h;
2623 progname = "genrecog";
2625 memset (&recog_tree, 0, sizeof recog_tree);
2626 memset (&split_tree, 0, sizeof split_tree);
2627 memset (&peephole2_tree, 0, sizeof peephole2_tree);
2629 if (argc <= 1)
2630 fatal ("No input file name.");
2632 if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
2633 return (FATAL_EXIT_CODE);
2635 next_insn_code = 0;
2636 next_index = 0;
2638 write_header ();
2640 /* Read the machine description. */
2642 while (1)
2644 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2645 if (desc == NULL)
2646 break;
2648 if (GET_CODE (desc) == DEFINE_INSN)
2650 h = make_insn_sequence (desc, RECOG);
2651 merge_trees (&recog_tree, &h);
2653 else if (GET_CODE (desc) == DEFINE_SPLIT)
2655 h = make_insn_sequence (desc, SPLIT);
2656 merge_trees (&split_tree, &h);
2658 else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2660 h = make_insn_sequence (desc, PEEPHOLE2);
2661 merge_trees (&peephole2_tree, &h);
2664 next_index++;
2667 if (error_count)
2668 return FATAL_EXIT_CODE;
2670 puts ("\n\n");
2672 process_tree (&recog_tree, RECOG);
2673 process_tree (&split_tree, SPLIT);
2674 process_tree (&peephole2_tree, PEEPHOLE2);
2676 fflush (stdout);
2677 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2680 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
2681 const char *
2682 get_insn_name (code)
2683 int code;
2685 if (code < insn_name_ptr_size)
2686 return insn_name_ptr[code];
2687 else
2688 return NULL;
2691 static void
2692 record_insn_name (code, name)
2693 int code;
2694 const char *name;
2696 static const char *last_real_name = "insn";
2697 static int last_real_code = 0;
2698 char *new;
2700 if (insn_name_ptr_size <= code)
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;
2711 if (!name || name[0] == '\0')
2713 new = xmalloc (strlen (last_real_name) + 10);
2714 sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2716 else
2718 last_real_name = new = xstrdup (name);
2719 last_real_code = code;
2722 insn_name_ptr[code] = new;
2725 static void
2726 debug_decision_2 (test)
2727 struct decision_test *test;
2729 switch (test->type)
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;
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;
2754 case DT_veclen_ge:
2755 fprintf (stderr, "veclen>=%d", test->u.veclen);
2756 break;
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:
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);
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;
2780 default:
2781 abort ();
2785 static void
2786 debug_decision_1 (d, indent)
2787 struct decision *d;
2788 int indent;
2790 int i;
2791 struct decision_test *test;
2793 if (d == NULL)
2795 for (i = 0; i < indent; ++i)
2796 putc (' ', stderr);
2797 fputs ("(nil)\n", stderr);
2798 return;
2801 for (i = 0; i < indent; ++i)
2802 putc (' ', stderr);
2804 putc ('{', stderr);
2805 test = d->tests;
2806 if (test)
2808 debug_decision_2 (test);
2809 while ((test = test->next) != NULL)
2811 fputs (" + ", stderr);
2812 debug_decision_2 (test);
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));
2820 static void
2821 debug_decision_0 (d, indent, maxdepth)
2822 struct decision *d;
2823 int indent, maxdepth;
2825 struct decision *n;
2826 int i;
2828 if (maxdepth < 0)
2829 return;
2830 if (d == NULL)
2832 for (i = 0; i < indent; ++i)
2833 putc (' ', stderr);
2834 fputs ("(nil)\n", stderr);
2835 return;
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);
2843 void
2844 debug_decision (d)
2845 struct decision *d;
2847 debug_decision_0 (d, 0, 1000000);
2850 void
2851 debug_decision_list (d)
2852 struct decision *d;
2854 while (d)
2856 debug_decision_0 (d, 0, 0);
2857 d = d->next;