1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987, 88, 92-95, 97-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This program is used to produce insn-recog.c, which contains a
23 function called `recog' plus its subroutines. These functions
24 contain a decision tree that recognizes whether an rtx, the
25 argument given to recog, is a valid instruction.
27 recog returns -1 if the rtx is not valid. If the rtx is valid,
28 recog returns a nonnegative number which is the insn code number
29 for the pattern that matched. This is the same as the order in the
30 machine description of the entry that matched. This number can be
31 used as an index into various insn_* tables, such as insn_template,
32 insn_outfun, and insn_n_operands (found in insn-output.c).
34 The third argument to recog is an optional pointer to an int. If
35 present, recog will accept a pattern if it matches except for
36 missing CLOBBER expressions at the end. In that case, the value
37 pointed to by the optional pointer will be set to the number of
38 CLOBBERs that need to be added (it should be initialized to zero by
39 the caller). If it is set nonzero, the caller should allocate a
40 PARALLEL of the appropriate size, copy the initial entries, and
41 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
43 This program also generates the function `split_insns', which
44 returns 0 if the rtl could not be split, or it returns the split
47 This program also generates the function `peephole2_insns', which
48 returns 0 if the rtl could not be matched. If there was a match,
49 the new rtl is returned in a SEQUENCE, and LAST_INSN will point
50 to the last recognized insn in the old sequence. */
58 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
59 printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
61 static struct obstack obstack;
62 struct obstack *rtl_obstack = &obstack;
64 #define obstack_chunk_alloc xmalloc
65 #define obstack_chunk_free free
67 /* Holds an array of names indexed by insn_code_number. */
68 static char **insn_name_ptr = 0;
69 static int insn_name_ptr_size = 0;
71 /* A listhead of decision trees. The alternatives to a node are kept
72 in a doublely-linked list so we can easily add nodes to the proper
73 place when merging. */
77 struct decision *first;
78 struct decision *last;
81 /* A single test. The two accept types aren't tests per-se, but
82 their equality (or lack thereof) does affect tree merging so
83 it is convenient to keep them here. */
87 /* A linked list through the tests attached to a node. */
88 struct decision_test *next;
90 /* These types are roughly in the order in which we'd like to test them. */
92 DT_mode, DT_code, DT_veclen,
93 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide,
94 DT_dup, DT_pred, DT_c_test,
95 DT_accept_op, DT_accept_insn
100 enum machine_mode mode; /* Machine mode of node. */
101 RTX_CODE code; /* Code to test. */
105 const char *name; /* Predicate to call. */
106 int index; /* Index into `preds' or -1. */
107 enum machine_mode mode; /* Machine mode for node. */
110 const char *c_test; /* Additional test to perform. */
111 int veclen; /* Length of vector. */
112 int dup; /* Number of operand to compare against. */
113 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */
114 int opno; /* Operand number matched. */
117 int code_number; /* Insn number matched. */
118 int lineno; /* Line number of the insn. */
119 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
124 /* Data structure for decision tree for recognizing legitimate insns. */
128 struct decision_head success; /* Nodes to test on success. */
129 struct decision *next; /* Node to test on failure. */
130 struct decision *prev; /* Node whose failure tests us. */
131 struct decision *afterward; /* Node to test on success,
132 but failure of successor nodes. */
134 const char *position; /* String denoting position in pattern. */
136 struct decision_test *tests; /* The tests for this node. */
138 int number; /* Node number, used for labels */
139 int subroutine_number; /* Number of subroutine this node starts */
140 int need_label; /* Label needs to be output. */
143 #define SUBROUTINE_THRESHOLD 100
145 static int next_subroutine_number;
147 /* We can write three types of subroutines: One for insn recognition,
148 one to split insns, and one for peephole-type optimizations. This
149 defines which type is being written. */
152 RECOG, SPLIT, PEEPHOLE2
155 #define IS_SPLIT(X) ((X) != RECOG)
157 /* Next available node number for tree nodes. */
159 static int next_number;
161 /* Next number to use as an insn_code. */
163 static int next_insn_code;
165 /* Similar, but counts all expressions in the MD file; used for
168 static int next_index;
170 /* Record the highest depth we ever have so we know how many variables to
171 allocate in each subroutine we make. */
173 static int max_depth;
175 /* The line number of the start of the pattern currently being processed. */
176 static int pattern_lineno;
178 /* Count of errors. */
179 static int error_count;
181 /* This table contains a list of the rtl codes that can possibly match a
182 predicate defined in recog.c. The function `maybe_both_true' uses it to
183 deduce that there are no expressions that can be matches by certain pairs
184 of tree nodes. Also, if a predicate can match only one code, we can
185 hardwire that code into the node testing the predicate. */
187 static struct pred_table
190 RTX_CODE codes[NUM_RTX_CODE];
192 {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
193 LABEL_REF, SUBREG, REG, MEM}},
194 #ifdef PREDICATE_CODES
197 {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
198 LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
199 {"register_operand", {SUBREG, REG}},
200 {"scratch_operand", {SCRATCH, REG}},
201 {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
203 {"const_int_operand", {CONST_INT}},
204 {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
205 {"nonimmediate_operand", {SUBREG, REG, MEM}},
206 {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
207 LABEL_REF, SUBREG, REG}},
208 {"push_operand", {MEM}},
209 {"pop_operand", {MEM}},
210 {"memory_operand", {SUBREG, MEM}},
211 {"indirect_operand", {SUBREG, MEM}},
212 {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU}},
213 {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
214 LABEL_REF, SUBREG, REG, MEM}}
217 #define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
219 static struct decision *new_decision
220 PROTO((const char *, struct decision_head *));
221 static struct decision_test *new_decision_test
222 PROTO((enum decision_type, struct decision_test ***));
223 static void validate_pattern
225 static struct decision *add_to_sequence
226 PROTO((rtx, struct decision_head *, const char *, enum routine_type, int));
228 static int maybe_both_true_2
229 PROTO((struct decision_test *, struct decision_test *));
230 static int maybe_both_true_1
231 PROTO((struct decision_test *, struct decision_test *));
232 static int maybe_both_true
233 PROTO((struct decision *, struct decision *, int));
235 static int nodes_identical_1
236 PROTO((struct decision_test *, struct decision_test *));
237 static int nodes_identical
238 PROTO((struct decision *, struct decision *));
239 static void merge_accept_insn
240 PROTO((struct decision *, struct decision *));
241 static void merge_trees
242 PROTO((struct decision_head *, struct decision_head *));
244 static void factor_tests
245 PROTO((struct decision_head *));
246 static void simplify_tests
247 PROTO((struct decision_head *));
248 static int break_out_subroutines
249 PROTO((struct decision_head *, int));
250 static void find_afterward
251 PROTO((struct decision_head *, struct decision *));
253 static void change_state
254 PROTO((const char *, const char *, struct decision *, const char *));
255 static void print_code
256 PROTO((enum rtx_code));
257 static void write_afterward
258 PROTO((struct decision *, struct decision *, const char *));
259 static struct decision *write_switch
260 PROTO((struct decision *, int));
261 static void write_cond
262 PROTO((struct decision_test *, int, enum routine_type));
263 static void write_action
264 PROTO((struct decision_test *, int, int, struct decision *,
266 static int is_unconditional
267 PROTO((struct decision_test *, enum routine_type));
268 static int write_node
269 PROTO((struct decision *, int, enum routine_type));
270 static void write_tree_1
271 PROTO((struct decision_head *, int, enum routine_type));
272 static void write_tree
273 PROTO((struct decision_head *, const char *, enum routine_type, int));
274 static void write_subroutine
275 PROTO((struct decision_head *, enum routine_type));
276 static void write_subroutines
277 PROTO((struct decision_head *, enum routine_type));
278 static void write_header
281 static struct decision_head make_insn_sequence
282 PROTO((rtx, enum routine_type));
283 static void process_tree
284 PROTO((struct decision_head *, enum routine_type));
286 static void record_insn_name
287 PROTO((int, const char *));
289 static void debug_decision_1
290 PROTO((struct decision *, int));
291 static void debug_decision_2
292 PROTO((struct decision_test *));
293 extern void debug_decision
294 PROTO((struct decision *));
297 message_with_line VPROTO ((int lineno, const char *msg, ...))
299 #ifndef ANSI_PROTOTYPES
307 #ifndef ANSI_PROTOTYPES
308 lineno = va_arg (ap, int);
309 msg = va_arg (ap, const char *);
312 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
313 vfprintf (stderr, msg, ap);
314 fputc ('\n', stderr);
319 /* Create a new node in sequence after LAST. */
321 static struct decision *
322 new_decision (position, last)
323 const char *position;
324 struct decision_head *last;
326 register struct decision *new
327 = (struct decision *) xmalloc (sizeof (struct decision));
329 memset (new, 0, sizeof (*new));
330 new->success = *last;
331 new->position = xstrdup (position);
332 new->number = next_number++;
334 last->first = last->last = new;
338 /* Create a new test and link it in at PLACE. */
340 static struct decision_test *
341 new_decision_test (type, pplace)
342 enum decision_type type;
343 struct decision_test ***pplace;
345 struct decision_test **place = *pplace;
346 struct decision_test *test;
348 test = (struct decision_test *) xmalloc (sizeof (*test));
359 /* Check for various errors in patterns. */
362 validate_pattern (pattern, set_dest)
370 code = GET_CODE (pattern);
379 const char *pred_name = XSTR (pattern, 1);
381 if (pred_name[0] != 0)
383 /* See if we know about this predicate and save its number. If
384 we do, and it only accepts one code, note that fact. The
385 predicate `const_int_operand' only tests for a CONST_INT, so
386 if we do so we can avoid calling it at all.
388 Finally, if we know that the predicate does not allow
389 CONST_INT, we know that the only way the predicate can match
390 is if the modes match (here we use the kludge of relying on
391 the fact that "address_operand" accepts CONST_INT; otherwise,
392 it would have to be a special case), so we can test the mode
393 (but we need not). This fact should considerably simplify the
396 for (i = 0; i < (int) NUM_KNOWN_PREDS; i++)
397 if (! strcmp (preds[i].name, pred_name))
400 if (i < (int) NUM_KNOWN_PREDS)
402 int j, allows_const_int;
404 allows_const_int = 0;
405 for (j = 0; preds[i].codes[j] != 0; j++)
406 if (preds[i].codes[j] == CONST_INT)
408 allows_const_int = 1;
412 if (allows_const_int && set_dest)
414 message_with_line (pattern_lineno,
415 "warning: `%s' accepts const_int,",
417 message_with_line (pattern_lineno,
418 " and used as destination of a set");
423 #ifdef PREDICATE_CODES
424 /* If the port has a list of the predicates it uses but
426 message_with_line (pattern_lineno, "warning: `%s' not in PREDICATE_CODES", pred_name);
435 /* The operands of a SET must have the same mode unless one
437 if (GET_MODE (SET_SRC (pattern)) != VOIDmode
438 && GET_MODE (SET_DEST (pattern)) != VOIDmode
439 && GET_MODE (SET_SRC (pattern)) != GET_MODE (SET_DEST (pattern))
440 /* The mode of an ADDRESS_OPERAND is the mode of the memory
441 reference, not the mode of the address. */
442 && ! (GET_CODE (SET_SRC (pattern)) == MATCH_OPERAND
443 && ! strcmp (XSTR (SET_SRC (pattern), 1), "address_operand")))
445 message_with_line (pattern_lineno,
446 "mode mismatch in set: %smode vs %smode",
447 GET_MODE_NAME (GET_MODE (SET_DEST (pattern))),
448 GET_MODE_NAME (GET_MODE (SET_SRC (pattern))));
452 validate_pattern (SET_DEST (pattern), 1);
453 validate_pattern (SET_SRC (pattern), 0);
457 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
459 message_with_line (pattern_lineno,
460 "operand to label_ref %smode not VOIDmode",
461 GET_MODE_NAME (GET_MODE (XEXP (pattern, 0))));
470 fmt = GET_RTX_FORMAT (code);
471 len = GET_RTX_LENGTH (code);
472 for (i = 0; i < len; i++)
477 validate_pattern (XEXP (pattern, i), 0);
481 for (j = 0; j < XVECLEN (pattern, i); j++)
482 validate_pattern (XVECEXP (pattern, i, j), 0);
485 case 'i': case 'w': case '0': case 's':
495 /* Create a chain of nodes to verify that an rtl expression matches
498 LAST is a pointer to the listhead in the previous node in the chain (or
499 in the calling function, for the first node).
501 POSITION is the string representing the current position in the insn.
503 INSN_TYPE is the type of insn for which we are emitting code.
505 A pointer to the final node in the chain is returned. */
507 static struct decision *
508 add_to_sequence (pattern, last, position, insn_type, top)
510 struct decision_head *last;
511 const char *position;
512 enum routine_type insn_type;
516 struct decision *this, *sub;
517 struct decision_test *test;
518 struct decision_test **place;
521 register const char *fmt;
522 int depth = strlen (position);
524 enum machine_mode mode;
526 if (depth > max_depth)
529 subpos = (char *) alloca (depth + 2);
530 strcpy (subpos, position);
531 subpos[depth + 1] = 0;
533 sub = this = new_decision (position, last);
534 place = &this->tests;
537 mode = GET_MODE (pattern);
538 code = GET_CODE (pattern);
543 /* Toplevel peephole pattern. */
544 if (insn_type == PEEPHOLE2 && top)
546 /* We don't need the node we just created -- unlink it. */
547 last->first = last->last = NULL;
549 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
551 /* Which insn we're looking at is represented by A-Z. We don't
552 ever use 'A', however; it is always implied. */
554 subpos[depth] = (i > 0 ? 'A' + i : 0);
555 sub = add_to_sequence (XVECEXP (pattern, 0, i),
556 last, subpos, insn_type, 0);
557 last = &sub->success;
562 /* Else nothing special. */
571 const char *pred_name;
572 RTX_CODE was_code = code;
573 int allows_const_int = 1;
575 if (code == MATCH_SCRATCH)
577 pred_name = "scratch_operand";
582 pred_name = XSTR (pattern, 1);
583 if (code == MATCH_PARALLEL)
589 /* We know exactly what const_int_operand matches -- any CONST_INT. */
590 if (strcmp ("const_int_operand", pred_name) == 0)
595 else if (pred_name[0] != 0)
597 test = new_decision_test (DT_pred, &place);
598 test->u.pred.name = pred_name;
599 test->u.pred.mode = mode;
601 /* See if we know about this predicate and save its number. If
602 we do, and it only accepts one code, note that fact. The
603 predicate `const_int_operand' only tests for a CONST_INT, so
604 if we do so we can avoid calling it at all.
606 Finally, if we know that the predicate does not allow
607 CONST_INT, we know that the only way the predicate can match
608 is if the modes match (here we use the kludge of relying on
609 the fact that "address_operand" accepts CONST_INT; otherwise,
610 it would have to be a special case), so we can test the mode
611 (but we need not). This fact should considerably simplify the
614 for (i = 0; i < NUM_KNOWN_PREDS; i++)
615 if (! strcmp (preds[i].name, pred_name))
618 if (i < NUM_KNOWN_PREDS)
622 test->u.pred.index = i;
624 if (preds[i].codes[1] == 0 && code == UNKNOWN)
625 code = preds[i].codes[0];
627 allows_const_int = 0;
628 for (j = 0; preds[i].codes[j] != 0; j++)
629 if (preds[i].codes[j] == CONST_INT)
631 allows_const_int = 1;
636 test->u.pred.index = -1;
639 /* Can't enforce a mode if we allow const_int. */
640 if (allows_const_int)
643 /* Accept the operand, ie. record it in `operands'. */
644 test = new_decision_test (DT_accept_op, &place);
645 test->u.opno = XINT (pattern, 0);
647 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
649 char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
650 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
652 subpos[depth] = i + base;
653 sub = add_to_sequence (XVECEXP (pattern, 2, i),
654 &sub->success, subpos, insn_type, 0);
663 test = new_decision_test (DT_dup, &place);
664 test->u.dup = XINT (pattern, 0);
666 test = new_decision_test (DT_accept_op, &place);
667 test->u.opno = XINT (pattern, 0);
669 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
671 subpos[depth] = i + '0';
672 sub = add_to_sequence (XVECEXP (pattern, 1, i),
673 &sub->success, subpos, insn_type, 0);
681 test = new_decision_test (DT_dup, &place);
682 test->u.dup = XINT (pattern, 0);
686 pattern = XEXP (pattern, 0);
693 fmt = GET_RTX_FORMAT (code);
694 len = GET_RTX_LENGTH (code);
696 /* Do tests against the current node first. */
697 for (i = 0; i < (size_t) len; i++)
703 test = new_decision_test (DT_elt_zero_int, &place);
704 test->u.intval = XINT (pattern, i);
708 test = new_decision_test (DT_elt_one_int, &place);
709 test->u.intval = XINT (pattern, i);
714 else if (fmt[i] == 'w')
719 test = new_decision_test (DT_elt_zero_wide, &place);
720 test->u.intval = XWINT (pattern, i);
722 else if (fmt[i] == 'E')
727 test = new_decision_test (DT_veclen, &place);
728 test->u.veclen = XVECLEN (pattern, i);
732 /* Now test our sub-patterns. */
733 for (i = 0; i < (size_t) len; i++)
738 subpos[depth] = '0' + i;
739 sub = add_to_sequence (XEXP (pattern, i), &sub->success,
740 subpos, insn_type, 0);
746 for (j = 0; j < XVECLEN (pattern, i); j++)
748 subpos[depth] = 'a' + j;
749 sub = add_to_sequence (XVECEXP (pattern, i, j),
750 &sub->success, subpos, insn_type, 0);
767 /* Insert nodes testing mode and code, if they're still relevant,
768 before any of the nodes we may have added above. */
771 place = &this->tests;
772 test = new_decision_test (DT_code, &place);
776 if (mode != VOIDmode)
778 place = &this->tests;
779 test = new_decision_test (DT_mode, &place);
783 /* If we didn't insert any tests or accept nodes, hork. */
784 if (this->tests == NULL)
790 /* A subroutine of maybe_both_true; examines only one test.
791 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
794 maybe_both_true_2 (d1, d2)
795 struct decision_test *d1, *d2;
797 if (d1->type == d2->type)
802 return d1->u.mode == d2->u.mode;
805 return d1->u.code == d2->u.code;
808 return d1->u.veclen == d2->u.veclen;
810 case DT_elt_zero_int:
812 case DT_elt_zero_wide:
813 return d1->u.intval == d2->u.intval;
820 /* If either has a predicate that we know something about, set
821 things up so that D1 is the one that always has a known
822 predicate. Then see if they have any codes in common. */
824 if (d1->type == DT_pred || d2->type == DT_pred)
826 if (d2->type == DT_pred)
828 struct decision_test *tmp;
829 tmp = d1, d1 = d2, d2 = tmp;
832 /* If D2 tests a mode, see if it matches D1. */
833 if (d1->u.pred.mode != VOIDmode)
835 if (d2->type == DT_mode)
837 if (d1->u.pred.mode != d2->u.mode)
840 /* Don't check two predicate modes here, because if both predicates
841 accept CONST_INT, then both can still be true even if the modes
842 are different. If they don't accept CONST_INT, there will be a
843 separate DT_mode that will make maybe_both_true_1 return 0. */
846 if (d1->u.pred.index >= 0)
848 /* If D2 tests a code, see if it is in the list of valid
849 codes for D1's predicate. */
850 if (d2->type == DT_code)
852 const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
855 if (*c == d2->u.code)
863 /* Otherwise see if the predicates have any codes in common. */
864 else if (d2->type == DT_pred && d2->u.pred.index >= 0)
866 const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
869 while (*c1 != 0 && !common)
871 const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
872 while (*c2 != 0 && !common)
874 common = (*c1 == *c2);
889 /* A subroutine of maybe_both_true; examines all the tests for a given node.
890 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
893 maybe_both_true_1 (d1, d2)
894 struct decision_test *d1, *d2;
896 struct decision_test *t1, *t2;
898 /* A match_operand with no predicate can match anything. Recognize
899 this by the existance of a lone DT_accept_op test. */
900 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
903 /* Eliminate pairs of tests while they can exactly match. */
904 while (d1 && d2 && d1->type == d2->type)
906 if (maybe_both_true_2 (d1, d2) == 0)
908 d1 = d1->next, d2 = d2->next;
911 /* After that, consider all pairs. */
912 for (t1 = d1; t1 ; t1 = t1->next)
913 for (t2 = d2; t2 ; t2 = t2->next)
914 if (maybe_both_true_2 (t1, t2) == 0)
920 /* Return 0 if we can prove that there is no RTL that can match both
921 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
922 can match both or just that we couldn't prove there wasn't such an RTL).
924 TOPLEVEL is non-zero if we are to only look at the top level and not
925 recursively descend. */
928 maybe_both_true (d1, d2, toplevel)
929 struct decision *d1, *d2;
932 struct decision *p1, *p2;
935 /* Don't compare strings on the different positions in insn. Doing so
936 is incorrect and results in false matches from constructs like
938 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
939 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
941 [(set (match_operand:HI "register_operand" "r")
942 (match_operand:HI "register_operand" "r"))]
944 If we are presented with such, we are recursing through the remainder
945 of a node's success nodes (from the loop at the end of this function).
946 Skip forward until we come to a position that matches.
948 Due to the way position strings are constructed, we know that iterating
949 forward from the lexically lower position (e.g. "00") will run into
950 the lexically higher position (e.g. "1") and not the other way around.
951 This saves a bit of effort. */
953 cmp = strcmp (d1->position, d2->position);
959 /* If the d2->position was lexically lower, swap. */
961 p1 = d1, d1 = d2, d2 = p1;
963 if (d1->success.first == 0)
965 for (p1 = d1->success.first; p1; p1 = p1->next)
966 if (maybe_both_true (p1, d2, 0))
972 /* Test the current level. */
973 cmp = maybe_both_true_1 (d1->tests, d2->tests);
977 /* We can't prove that D1 and D2 cannot both be true. If we are only
978 to check the top level, return 1. Otherwise, see if we can prove
979 that all choices in both successors are mutually exclusive. If
980 either does not have any successors, we can't prove they can't both
983 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
986 for (p1 = d1->success.first; p1; p1 = p1->next)
987 for (p2 = d2->success.first; p2; p2 = p2->next)
988 if (maybe_both_true (p1, p2, 0))
994 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
997 nodes_identical_1 (d1, d2)
998 struct decision_test *d1, *d2;
1003 return d1->u.mode == d2->u.mode;
1006 return d1->u.code == d2->u.code;
1009 return (d1->u.pred.mode == d2->u.pred.mode
1010 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1013 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1016 return d1->u.veclen == d2->u.veclen;
1019 return d1->u.dup == d2->u.dup;
1021 case DT_elt_zero_int:
1022 case DT_elt_one_int:
1023 case DT_elt_zero_wide:
1024 return d1->u.intval == d2->u.intval;
1027 return d1->u.opno == d2->u.opno;
1029 case DT_accept_insn:
1030 /* Differences will be handled in merge_accept_insn. */
1038 /* True iff the two nodes are identical (on one level only). Due
1039 to the way these lists are constructed, we shouldn't have to
1040 consider different orderings on the tests. */
1043 nodes_identical (d1, d2)
1044 struct decision *d1, *d2;
1046 struct decision_test *t1, *t2;
1048 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1050 if (t1->type != t2->type)
1052 if (! nodes_identical_1 (t1, t2))
1056 /* For success, they should now both be null. */
1060 /* A subroutine of merge_trees; given two nodes that have been declared
1061 identical, cope with two insn accept states. If they differ in the
1062 number of clobbers, then the conflict was created by make_insn_sequence
1063 and we can drop the with-clobbers version on the floor. If both
1064 nodes have no additional clobbers, we have found an ambiguity in the
1065 source machine description. */
1068 merge_accept_insn (oldd, addd)
1069 struct decision *oldd, *addd;
1071 struct decision_test *old, *add;
1073 for (old = oldd->tests; old; old = old->next)
1074 if (old->type == DT_accept_insn)
1079 for (add = addd->tests; add; add = add->next)
1080 if (add->type == DT_accept_insn)
1085 /* If one node is for a normal insn and the second is for the base
1086 insn with clobbers stripped off, the second node should be ignored. */
1088 if (old->u.insn.num_clobbers_to_add == 0
1089 && add->u.insn.num_clobbers_to_add > 0)
1091 /* Nothing to do here. */
1093 else if (old->u.insn.num_clobbers_to_add > 0
1094 && add->u.insn.num_clobbers_to_add == 0)
1096 /* In this case, replace OLD with ADD. */
1097 old->u.insn = add->u.insn;
1101 message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1102 get_insn_name (add->u.insn.code_number),
1103 get_insn_name (old->u.insn.code_number));
1104 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1105 get_insn_name (old->u.insn.code_number));
1110 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1113 merge_trees (oldh, addh)
1114 struct decision_head *oldh, *addh;
1116 struct decision *next, *add;
1118 if (addh->first == 0)
1120 if (oldh->first == 0)
1126 /* Trying to merge bits at different positions isn't possible. */
1127 if (strcmp (oldh->first->position, addh->first->position))
1130 for (add = addh->first; add ; add = next)
1132 struct decision *old, *insert_before = NULL;
1136 /* The semantics of pattern matching state that the tests are
1137 done in the order given in the MD file so that if an insn
1138 matches two patterns, the first one will be used. However,
1139 in practice, most, if not all, patterns are unambiguous so
1140 that their order is independent. In that case, we can merge
1141 identical tests and group all similar modes and codes together.
1143 Scan starting from the end of OLDH until we reach a point
1144 where we reach the head of the list or where we pass a
1145 pattern that could also be true if NEW is true. If we find
1146 an identical pattern, we can merge them. Also, record the
1147 last node that tests the same code and mode and the last one
1148 that tests just the same mode.
1150 If we have no match, place NEW after the closest match we found. */
1152 for (old = oldh->last; old; old = old->prev)
1154 if (nodes_identical (old, add))
1156 merge_accept_insn (old, add);
1157 merge_trees (&old->success, &add->success);
1161 if (maybe_both_true (old, add, 0))
1164 /* Insert the nodes in DT test type order, which is roughly
1165 how expensive/important the test is. Given that the tests
1166 are also ordered within the list, examining the first is
1168 if (add->tests->type < old->tests->type)
1169 insert_before = old;
1172 if (insert_before == NULL)
1175 add->prev = oldh->last;
1176 oldh->last->next = add;
1181 if ((add->prev = insert_before->prev) != NULL)
1182 add->prev->next = add;
1185 add->next = insert_before;
1186 insert_before->prev = add;
1193 /* Walk the tree looking for sub-nodes that perform common tests.
1194 Factor out the common test into a new node. This enables us
1195 (depending on the test type) to emit switch statements later. */
1199 struct decision_head *head;
1201 struct decision *first, *next;
1203 for (first = head->first; first && first->next; first = next)
1205 enum decision_type type;
1206 struct decision *new, *old_last;
1208 type = first->tests->type;
1211 /* Want at least two compatible sequential nodes. */
1212 if (next->tests->type != type)
1215 /* Don't want all node types, just those we can turn into
1216 switch statements. */
1219 && type != DT_veclen
1220 && type != DT_elt_zero_int
1221 && type != DT_elt_one_int
1222 && type != DT_elt_zero_wide)
1225 /* If we'd been performing more than one test, create a new node
1226 below our first test. */
1227 if (first->tests->next != NULL)
1229 new = new_decision (first->position, &first->success);
1230 new->tests = first->tests->next;
1231 first->tests->next = NULL;
1234 /* Crop the node tree off after our first test. */
1236 old_last = head->last;
1239 /* For each compatible test, adjust to perform only one test in
1240 the top level node, then merge the node back into the tree. */
1243 struct decision_head h;
1245 if (next->tests->next != NULL)
1247 new = new_decision (next->position, &next->success);
1248 new->tests = next->tests->next;
1249 next->tests->next = NULL;
1254 h.first = h.last = new;
1256 merge_trees (head, &h);
1258 while (next && next->tests->type == type);
1260 /* After we run out of compatible tests, graft the remaining nodes
1261 back onto the tree. */
1264 next->prev = head->last;
1265 head->last->next = next;
1266 head->last = old_last;
1271 for (first = head->first; first; first = first->next)
1272 factor_tests (&first->success);
1275 /* After factoring, try to simplify the tests on any one node.
1276 Tests that are useful for switch statements are recognizable
1277 by having only a single test on a node -- we'll be manipulating
1278 nodes with multiple tests:
1280 If we have mode tests or code tests that are redundant with
1281 predicates, remove them. */
1284 simplify_tests (head)
1285 struct decision_head *head;
1287 struct decision *tree;
1289 for (tree = head->first; tree; tree = tree->next)
1291 struct decision_test *a, *b;
1298 /* Find a predicate node. */
1299 while (b && b->type != DT_pred)
1303 /* Due to how these tests are constructed, we don't even need
1304 to check that the mode and code are compatible -- they were
1305 generated from the predicate in the first place. */
1306 while (a->type == DT_mode || a->type == DT_code)
1313 for (tree = head->first; tree; tree = tree->next)
1314 simplify_tests (&tree->success);
1317 /* Count the number of subnodes of HEAD. If the number is high enough,
1318 make the first node in HEAD start a separate subroutine in the C code
1319 that is generated. */
1322 break_out_subroutines (head, initial)
1323 struct decision_head *head;
1327 struct decision *sub;
1329 for (sub = head->first; sub; sub = sub->next)
1330 size += 1 + break_out_subroutines (&sub->success, 0);
1332 if (size > SUBROUTINE_THRESHOLD && ! initial)
1334 head->first->subroutine_number = ++next_subroutine_number;
1340 /* For each node p, find the next alternative that might be true
1344 find_afterward (head, real_afterward)
1345 struct decision_head *head;
1346 struct decision *real_afterward;
1348 struct decision *p, *q, *afterward;
1350 /* We can't propogate alternatives across subroutine boundaries.
1351 This is not incorrect, merely a minor optimization loss. */
1354 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1356 for ( ; p ; p = p->next)
1358 /* Find the next node that might be true if this one fails. */
1359 for (q = p->next; q ; q = q->next)
1360 if (maybe_both_true (p, q, 1))
1363 /* If we reached the end of the list without finding one,
1364 use the incoming afterward position. */
1373 for (p = head->first; p ; p = p->next)
1374 if (p->success.first)
1375 find_afterward (&p->success, p->afterward);
1377 /* When we are generating a subroutine, record the real afterward
1378 position in the first node where write_tree can find it, and we
1379 can do the right thing at the subroutine call site. */
1381 if (p->subroutine_number > 0)
1382 p->afterward = real_afterward;
1385 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1386 actions are necessary to move to NEWPOS. If we fail to move to the
1387 new state, branch to node AFTERWARD if non-zero, otherwise return.
1389 Failure to move to the new state can only occur if we are trying to
1390 match multiple insns and we try to step past the end of the stream. */
1393 change_state (oldpos, newpos, afterward, indent)
1396 struct decision *afterward;
1399 int odepth = strlen (oldpos);
1400 int ndepth = strlen (newpos);
1402 int old_has_insn, new_has_insn;
1404 /* Pop up as many levels as necessary. */
1405 for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1408 /* Hunt for the last [A-Z] in both strings. */
1409 for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1410 if (oldpos[old_has_insn] >= 'A' && oldpos[old_has_insn] <= 'Z')
1412 for (new_has_insn = odepth - 1; new_has_insn >= 0; --new_has_insn)
1413 if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1416 /* Make sure to reset the _last_insn pointer when popping back up. */
1417 if (old_has_insn >= 0 && new_has_insn < 0)
1418 printf ("%s_last_insn = insn;\n", indent);
1420 /* Go down to desired level. */
1421 while (depth < ndepth)
1423 /* It's a different insn from the first one. */
1424 if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1426 /* We can only fail if we're moving down the tree. */
1427 if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1429 printf ("%s_last_insn = recog_next_insn (insn, %d);\n",
1430 indent, newpos[depth] - 'A');
1434 printf ("%stem = recog_next_insn (insn, %d);\n",
1435 indent, newpos[depth] - 'A');
1436 printf ("%sif (tem == NULL_RTX)\n", indent);
1438 printf ("%s goto L%d;\n", indent, afterward->number);
1440 printf ("%s goto ret0;\n", indent);
1441 printf ("%s_last_insn = tem;\n", indent);
1443 printf ("%sx%d = PATTERN (_last_insn);\n", indent, depth + 1);
1445 else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1446 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1447 indent, depth + 1, depth, newpos[depth] - 'a');
1449 printf ("%sx%d = XEXP (x%d, %c);\n",
1450 indent, depth + 1, depth, newpos[depth]);
1455 /* Print the enumerator constant for CODE -- the upcase version of
1462 register const char *p;
1463 for (p = GET_RTX_NAME (code); *p; p++)
1464 putchar (TOUPPER (*p));
1467 /* Emit code to cross an afterward link -- change state and branch. */
1470 write_afterward (start, afterward, indent)
1471 struct decision *start;
1472 struct decision *afterward;
1475 if (!afterward || start->subroutine_number > 0)
1476 printf("%sgoto ret0;\n", indent);
1479 change_state (start->position, afterward->position, NULL, indent);
1480 printf ("%sgoto L%d;\n", indent, afterward->number);
1484 /* Emit a switch statement, if possible, for an initial sequence of
1485 nodes at START. Return the first node yet untested. */
1487 static struct decision *
1488 write_switch (start, depth)
1489 struct decision *start;
1492 struct decision *p = start;
1493 enum decision_type type = p->tests->type;
1495 /* If we have two or more nodes in sequence that test the same one
1496 thing, we may be able to use a switch statement. */
1500 || p->next->tests->type != type
1501 || p->next->tests->next)
1504 /* DT_code is special in that we can do interesting things with
1505 known predicates at the same time. */
1506 if (type == DT_code)
1508 char codemap[NUM_RTX_CODE];
1509 struct decision *ret;
1511 memset (codemap, 0, sizeof(codemap));
1513 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1516 RTX_CODE code = p->tests->u.code;
1519 printf (":\n goto L%d;\n", p->success.first->number);
1520 p->success.first->need_label = 1;
1525 while (p && p->tests->type == DT_code && !p->tests->next);
1527 /* If P is testing a predicate that we know about and we haven't
1528 seen any of the codes that are valid for the predicate, we can
1529 write a series of "case" statement, one for each possible code.
1530 Since we are already in a switch, these redundant tests are very
1531 cheap and will reduce the number of predicates called. */
1533 /* Note that while we write out cases for these predicates here,
1534 we don't actually write the test here, as it gets kinda messy.
1535 It is trivial to leave this to later by telling our caller that
1536 we only processed the CODE tests. */
1539 while (p && p->tests->type == DT_pred
1540 && p->tests->u.pred.index >= 0)
1544 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1545 if (codemap[(int) *c] != 0)
1548 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1553 codemap[(int) *c] = 1;
1556 printf (" goto L%d;\n", p->number);
1562 /* Make the default case skip the predicates we managed to match. */
1564 printf (" default:\n");
1569 printf (" goto L%d;\n", p->number);
1573 write_afterward (start, start->afterward, " ");
1576 printf (" break;\n");
1581 else if (type == DT_mode
1582 || type == DT_veclen
1583 || type == DT_elt_zero_int
1584 || type == DT_elt_one_int
1585 || type == DT_elt_zero_wide)
1589 printf (" switch (");
1593 str = "GET_MODE (x%d)";
1596 str = "XVECLEN (x%d, 0)";
1598 case DT_elt_zero_int:
1599 str = "XINT (x%d, 0)";
1601 case DT_elt_one_int:
1602 str = "XINT (x%d, 1)";
1604 case DT_elt_zero_wide:
1605 str = "XWINT (x%d, 0)";
1610 printf (str, depth);
1619 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1622 printf ("%d", p->tests->u.veclen);
1624 case DT_elt_zero_int:
1625 case DT_elt_one_int:
1626 case DT_elt_zero_wide:
1627 printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1632 printf (":\n goto L%d;\n", p->success.first->number);
1633 p->success.first->need_label = 1;
1637 while (p && p->tests->type == type && !p->tests->next);
1639 printf (" default:\n break;\n }\n");
1645 /* None of the other tests are ameanable. */
1650 /* Emit code for one test. */
1653 write_cond (p, depth, subroutine_type)
1654 struct decision_test *p;
1656 enum routine_type subroutine_type;
1661 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1665 printf ("GET_CODE (x%d) == ", depth);
1666 print_code (p->u.code);
1670 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1673 case DT_elt_zero_int:
1674 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1677 case DT_elt_one_int:
1678 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1681 case DT_elt_zero_wide:
1682 printf ("XWINT (x%d, 0) == ", depth);
1683 printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1687 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1691 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1692 GET_MODE_NAME (p->u.pred.mode));
1696 printf ("(%s)", p->u.c_test);
1699 case DT_accept_insn:
1700 switch (subroutine_type)
1703 if (p->u.insn.num_clobbers_to_add == 0)
1705 printf ("pnum_clobbers != NULL");
1718 /* Emit code for one action. The previous tests have succeeded;
1719 TEST is the last of the chain. In the normal case we simply
1720 perform a state change. For the `accept' tests we must do more work. */
1723 write_action (test, depth, uncond, success, subroutine_type)
1724 struct decision_test *test;
1726 struct decision *success;
1727 enum routine_type subroutine_type;
1734 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1736 fputs (" {\n", stdout);
1743 if (test->type == DT_accept_op)
1745 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1747 /* Only allow DT_accept_insn to follow. */
1751 if (test->type != DT_accept_insn)
1756 /* Sanity check that we're now at the end of the list of tests. */
1760 if (test->type == DT_accept_insn)
1762 switch (subroutine_type)
1765 if (test->u.insn.num_clobbers_to_add != 0)
1766 printf ("%s*pnum_clobbers = %d;\n",
1767 indent, test->u.insn.num_clobbers_to_add);
1768 printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1772 printf ("%sreturn gen_split_%d (operands);\n",
1773 indent, test->u.insn.code_number);
1777 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1778 indent, test->u.insn.code_number);
1779 printf ("%sif (tem != 0)\n%s goto ret1;\n", indent, indent);
1788 printf("%sgoto L%d;\n", indent, success->number);
1789 success->need_label = 1;
1793 fputs (" }\n", stdout);
1796 /* Return 1 if the test is always true and has no fallthru path. Return -1
1797 if the test does have a fallthru path, but requires that the condition be
1798 terminated. Otherwise return 0 for a normal test. */
1799 /* ??? is_unconditional is a stupid name for a tri-state function. */
1802 is_unconditional (t, subroutine_type)
1803 struct decision_test *t;
1804 enum routine_type subroutine_type;
1806 if (t->type == DT_accept_op)
1809 if (t->type == DT_accept_insn)
1811 switch (subroutine_type)
1814 return (t->u.insn.num_clobbers_to_add == 0);
1827 /* Emit code for one node -- the conditional and the accompanying action.
1828 Return true if there is no fallthru path. */
1831 write_node (p, depth, subroutine_type)
1834 enum routine_type subroutine_type;
1836 struct decision_test *test, *last_test;
1839 last_test = test = p->tests;
1840 uncond = is_unconditional (test, subroutine_type);
1844 write_cond (test, depth, subroutine_type);
1846 while ((test = test->next) != NULL)
1851 uncond2 = is_unconditional (test, subroutine_type);
1856 write_cond (test, depth, subroutine_type);
1862 write_action (last_test, depth, uncond, p->success.first, subroutine_type);
1867 /* Emit code for all of the sibling nodes of HEAD. */
1870 write_tree_1 (head, depth, subroutine_type)
1871 struct decision_head *head;
1873 enum routine_type subroutine_type;
1875 struct decision *p, *next;
1878 for (p = head->first; p ; p = next)
1880 /* The label for the first element was printed in write_tree. */
1881 if (p != head->first && p->need_label)
1882 OUTPUT_LABEL (" ", p->number);
1884 /* Attempt to write a switch statement for a whole sequence. */
1885 next = write_switch (p, depth);
1890 /* Failed -- fall back and write one node. */
1891 uncond = write_node (p, depth, subroutine_type);
1896 /* Finished with this chain. Close a fallthru path by branching
1897 to the afterward node. */
1899 write_afterward (head->last, head->last->afterward, " ");
1902 /* Write out the decision tree starting at HEAD. PREVPOS is the
1903 position at the node that branched to this node. */
1906 write_tree (head, prevpos, type, initial)
1907 struct decision_head *head;
1908 const char *prevpos;
1909 enum routine_type type;
1912 register struct decision *p = head->first;
1916 OUTPUT_LABEL (" ", p->number);
1918 if (! initial && p->subroutine_number > 0)
1920 static const char * const name_prefix[] = {
1921 "recog", "split", "peephole2"
1924 static const char * const call_suffix[] = {
1925 ", pnum_clobbers", "", ", _plast_insn"
1928 /* This node has been broken out into a separate subroutine.
1929 Call it, test the result, and branch accordingly. */
1933 printf (" tem = %s_%d (x0, insn%s);\n",
1934 name_prefix[type], p->subroutine_number, call_suffix[type]);
1935 if (IS_SPLIT (type))
1936 printf (" if (tem != 0)\n return tem;\n");
1938 printf (" if (tem >= 0)\n return tem;\n");
1940 change_state (p->position, p->afterward->position, NULL, " ");
1941 printf (" goto L%d;\n", p->afterward->number);
1945 printf (" return %s_%d (x0, insn%s);\n",
1946 name_prefix[type], p->subroutine_number, call_suffix[type]);
1951 int depth = strlen (p->position);
1953 change_state (prevpos, p->position, head->last->afterward, " ");
1954 write_tree_1 (head, depth, type);
1956 for (p = head->first; p; p = p->next)
1957 if (p->success.first)
1958 write_tree (&p->success, p->position, type, 0);
1962 /* Write out a subroutine of type TYPE to do comparisons starting at
1966 write_subroutine (head, type)
1967 struct decision_head *head;
1968 enum routine_type type;
1970 static const char * const proto_pattern[] = {
1971 "%sint recog%s PROTO ((rtx, rtx, int *));\n",
1972 "%srtx split%s PROTO ((rtx, rtx));\n",
1973 "%srtx peephole2%s PROTO ((rtx, rtx, rtx *));\n"
1976 static const char * const decl_pattern[] = {
1978 recog%s (x0, insn, pnum_clobbers)\n\
1980 rtx insn ATTRIBUTE_UNUSED;\n\
1981 int *pnum_clobbers ATTRIBUTE_UNUSED;\n",
1984 split%s (x0, insn)\n\
1986 rtx insn ATTRIBUTE_UNUSED;\n",
1989 peephole2%s (x0, insn, _plast_insn)\n\
1991 rtx insn ATTRIBUTE_UNUSED;\n\
1992 rtx *_plast_insn ATTRIBUTE_UNUSED;\n"
1995 int subfunction = head->first ? head->first->subroutine_number : 0;
2000 s_or_e = subfunction ? "static " : "";
2003 sprintf (extension, "_%d", subfunction);
2004 else if (type == RECOG)
2005 extension[0] = '\0';
2007 strcpy (extension, "_insns");
2009 printf (proto_pattern[type], s_or_e, extension);
2010 printf (decl_pattern[type], s_or_e, extension);
2012 printf ("{\n register rtx * const operands = &recog_data.operand[0];\n");
2013 for (i = 1; i <= max_depth; i++)
2014 printf (" register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2016 if (type == PEEPHOLE2)
2017 printf (" register rtx _last_insn = insn;\n");
2018 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2021 write_tree (head, "", type, 1);
2023 printf (" goto ret0;\n");
2025 if (type == PEEPHOLE2)
2026 printf (" ret1:\n *_plast_insn = _last_insn;\n return tem;\n");
2027 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2030 /* In break_out_subroutines, we discovered the boundaries for the
2031 subroutines, but did not write them out. Do so now. */
2034 write_subroutines (head, type)
2035 struct decision_head *head;
2036 enum routine_type type;
2040 for (p = head->first; p ; p = p->next)
2041 if (p->success.first)
2042 write_subroutines (&p->success, type);
2044 if (head->first->subroutine_number > 0)
2045 write_subroutine (head, type);
2048 /* Begin the output file. */
2054 /* Generated automatically by the program `genrecog' from the target\n\
2055 machine description file. */\n\
2057 #include \"config.h\"\n\
2058 #include \"system.h\"\n\
2059 #include \"rtl.h\"\n\
2060 #include \"tm_p.h\"\n\
2061 #include \"function.h\"\n\
2062 #include \"insn-config.h\"\n\
2063 #include \"recog.h\"\n\
2064 #include \"real.h\"\n\
2065 #include \"output.h\"\n\
2066 #include \"flags.h\"\n\
2067 #include \"hard-reg-set.h\"\n\
2068 #include \"resource.h\"\n\
2072 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2073 X0 is a valid instruction.\n\
2075 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2076 returns a nonnegative number which is the insn code number for the\n\
2077 pattern that matched. This is the same as the order in the machine\n\
2078 description of the entry that matched. This number can be used as an\n\
2079 index into `insn_data' and other tables.\n\
2081 The third argument to recog is an optional pointer to an int. If\n\
2082 present, recog will accept a pattern if it matches except for missing\n\
2083 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2084 the optional pointer will be set to the number of CLOBBERs that need\n\
2085 to be added (it should be initialized to zero by the caller). If it\n\
2086 is set nonzero, the caller should allocate a PARALLEL of the\n\
2087 appropriate size, copy the initial entries, and call add_clobbers\n\
2088 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2092 The function split_insns returns 0 if the rtl could not\n\
2093 be split or the split rtl in a SEQUENCE if it can be.\n\
2095 The function peephole2_insns returns 0 if the rtl could not\n\
2096 be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2097 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2102 /* Construct and return a sequence of decisions
2103 that will recognize INSN.
2105 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2107 static struct decision_head
2108 make_insn_sequence (insn, type)
2110 enum routine_type type;
2113 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2114 struct decision *last;
2115 struct decision_test *test, **place;
2116 struct decision_head head;
2118 record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2120 if (type == PEEPHOLE2)
2124 /* peephole2 gets special treatment:
2125 - X always gets an outer parallel even if it's only one entry
2126 - we remove all traces of outer-level match_scratch and match_dup
2127 expressions here. */
2128 x = rtx_alloc (PARALLEL);
2129 PUT_MODE (x, VOIDmode);
2130 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2131 for (i = j = 0; i < XVECLEN (insn, 0); i++)
2133 rtx tmp = XVECEXP (insn, 0, i);
2134 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2136 XVECEXP (x, 0, j) = tmp;
2142 else if (XVECLEN (insn, type == RECOG) == 1)
2143 x = XVECEXP (insn, type == RECOG, 0);
2146 x = rtx_alloc (PARALLEL);
2147 XVEC (x, 0) = XVEC (insn, type == RECOG);
2148 PUT_MODE (x, VOIDmode);
2151 validate_pattern (x, 0);
2153 memset(&head, 0, sizeof(head));
2154 last = add_to_sequence (x, &head, "", type, 1);
2156 /* Find the end of the test chain on the last node. */
2157 for (test = last->tests; test->next; test = test->next)
2159 place = &test->next;
2163 /* Need a new node if we have another test to add. */
2164 if (test->type == DT_accept_op)
2166 last = new_decision ("", &last->success);
2167 place = &last->tests;
2169 test = new_decision_test (DT_c_test, &place);
2170 test->u.c_test = c_test;
2173 test = new_decision_test (DT_accept_insn, &place);
2174 test->u.insn.code_number = next_insn_code;
2175 test->u.insn.lineno = pattern_lineno;
2176 test->u.insn.num_clobbers_to_add = 0;
2181 /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2182 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2183 If so, set up to recognize the pattern without these CLOBBERs. */
2185 if (GET_CODE (x) == PARALLEL)
2189 /* Find the last non-clobber in the parallel. */
2190 for (i = XVECLEN (x, 0); i > 0; i--)
2192 rtx y = XVECEXP (x, 0, i - 1);
2193 if (GET_CODE (y) != CLOBBER
2194 || (GET_CODE (XEXP (y, 0)) != REG
2195 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2199 if (i != XVECLEN (x, 0))
2202 struct decision_head clobber_head;
2204 /* Build a similar insn without the clobbers. */
2206 new = XVECEXP (x, 0, 0);
2211 new = rtx_alloc (PARALLEL);
2212 XVEC (new, 0) = rtvec_alloc (i);
2213 for (j = i - 1; j >= 0; j--)
2214 XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2218 memset (&clobber_head, 0, sizeof(clobber_head));
2219 last = add_to_sequence (new, &clobber_head, "", type, 1);
2221 /* Find the end of the test chain on the last node. */
2222 for (test = last->tests; test->next; test = test->next)
2225 /* We definitely have a new test to add -- create a new
2227 place = &test->next;
2228 if (test->type == DT_accept_op)
2230 last = new_decision ("", &last->success);
2231 place = &last->tests;
2236 test = new_decision_test (DT_c_test, &place);
2237 test->u.c_test = c_test;
2240 test = new_decision_test (DT_accept_insn, &place);
2241 test->u.insn.code_number = next_insn_code;
2242 test->u.insn.lineno = pattern_lineno;
2243 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2245 merge_trees (&head, &clobber_head);
2251 /* Define the subroutine we will call below and emit in genemit. */
2252 printf ("extern rtx gen_split_%d PROTO ((rtx *));\n", next_insn_code);
2256 /* Define the subroutine we will call below and emit in genemit. */
2257 printf ("extern rtx gen_peephole2_%d PROTO ((rtx, rtx *));\n",
2267 process_tree (head, subroutine_type)
2268 struct decision_head *head;
2269 enum routine_type subroutine_type;
2271 if (head->first == NULL)
2273 /* We can elide peephole2_insns, but not recog or split_insns. */
2274 if (subroutine_type == PEEPHOLE2)
2279 factor_tests (head);
2281 next_subroutine_number = 0;
2282 break_out_subroutines (head, 1);
2283 find_afterward (head, NULL);
2285 /* We run this after find_afterward, because find_afterward needs
2286 the redundant DT_mode tests on predicates to determine whether
2287 two tests can both be true or not. */
2288 simplify_tests(head);
2290 write_subroutines (head, subroutine_type);
2293 write_subroutine (head, subroutine_type);
2302 struct decision_head recog_tree, split_tree, peephole2_tree, h;
2306 progname = "genrecog";
2307 obstack_init (rtl_obstack);
2309 memset (&recog_tree, 0, sizeof recog_tree);
2310 memset (&split_tree, 0, sizeof split_tree);
2311 memset (&peephole2_tree, 0, sizeof peephole2_tree);
2314 fatal ("No input file name.");
2316 infile = fopen (argv[1], "r");
2320 return FATAL_EXIT_CODE;
2322 read_rtx_filename = argv[1];
2329 /* Read the machine description. */
2333 c = read_skip_spaces (infile);
2337 pattern_lineno = read_rtx_lineno;
2339 desc = read_rtx (infile);
2340 if (GET_CODE (desc) == DEFINE_INSN)
2342 h = make_insn_sequence (desc, RECOG);
2343 merge_trees (&recog_tree, &h);
2345 else if (GET_CODE (desc) == DEFINE_SPLIT)
2347 h = make_insn_sequence (desc, SPLIT);
2348 merge_trees (&split_tree, &h);
2350 else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2352 h = make_insn_sequence (desc, PEEPHOLE2);
2353 merge_trees (&peephole2_tree, &h);
2356 if (GET_CODE (desc) == DEFINE_PEEPHOLE
2357 || GET_CODE (desc) == DEFINE_EXPAND)
2363 return FATAL_EXIT_CODE;
2367 process_tree (&recog_tree, RECOG);
2368 process_tree (&split_tree, SPLIT);
2369 process_tree (&peephole2_tree, PEEPHOLE2);
2372 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2375 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
2377 get_insn_name (code)
2380 if (code < insn_name_ptr_size)
2381 return insn_name_ptr[code];
2387 record_insn_name (code, name)
2391 static const char *last_real_name = "insn";
2392 static int last_real_code = 0;
2395 if (insn_name_ptr_size <= code)
2398 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2400 (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2401 memset (insn_name_ptr + insn_name_ptr_size, 0,
2402 sizeof(char *) * (new_size - insn_name_ptr_size));
2403 insn_name_ptr_size = new_size;
2406 if (!name || name[0] == '\0')
2408 new = xmalloc (strlen (last_real_name) + 10);
2409 sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2413 last_real_name = new = xstrdup (name);
2414 last_real_code = code;
2417 insn_name_ptr[code] = new;
2424 register size_t len = strlen (input) + 1;
2425 register char *output = xmalloc (len);
2426 memcpy (output, input, len);
2431 xrealloc (old, size)
2437 ptr = (PTR) realloc (old, size);
2439 ptr = (PTR) malloc (size);
2441 fatal ("virtual memory exhausted");
2449 register PTR val = (PTR) malloc (size);
2452 fatal ("virtual memory exhausted");
2457 debug_decision_2 (test)
2458 struct decision_test *test;
2463 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2466 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2469 fprintf (stderr, "veclen=%d", test->u.veclen);
2471 case DT_elt_zero_int:
2472 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2474 case DT_elt_one_int:
2475 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2477 case DT_elt_zero_wide:
2478 fprintf (stderr, "elt0_w=");
2479 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2482 fprintf (stderr, "dup=%d", test->u.dup);
2485 fprintf (stderr, "pred=(%s,%s)",
2486 test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2491 strncpy (sub, test->u.c_test, sizeof(sub));
2492 memcpy (sub+16, "...", 4);
2493 fprintf (stderr, "c_test=\"%s\"", sub);
2497 fprintf (stderr, "A_op=%d", test->u.opno);
2499 case DT_accept_insn:
2500 fprintf (stderr, "A_insn=(%d,%d)",
2501 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2510 debug_decision_1 (d, indent)
2515 struct decision_test *test;
2519 for (i = 0; i < indent; ++i)
2521 fputs ("(nil)\n", stderr);
2525 for (i = 0; i < indent; ++i)
2532 debug_decision_2 (test);
2533 while ((test = test->next) != NULL)
2535 fputs (" + ", stderr);
2536 debug_decision_2 (test);
2539 fprintf (stderr, "} %d n %d a %d\n", d->number,
2540 (d->next ? d->next->number : -1),
2541 (d->afterward ? d->afterward->number : -1));
2545 debug_decision_0 (d, indent, maxdepth)
2547 int indent, maxdepth;
2556 for (i = 0; i < indent; ++i)
2558 fputs ("(nil)\n", stderr);
2562 debug_decision_1 (d, indent);
2563 for (n = d->success.first; n ; n = n->next)
2564 debug_decision_0 (n, indent + 2, maxdepth - 1);
2571 debug_decision_0 (d, 0, 1000000);
2575 debug_decision_list (d)
2580 debug_decision_0 (d, 0, 0);