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 else if (d2->type == DT_pred)
842 if (d2->u.pred.mode != VOIDmode
843 && d1->u.pred.mode != d2->u.pred.mode)
848 if (d1->u.pred.index >= 0)
850 /* If D2 tests a code, see if it is in the list of valid
851 codes for D1's predicate. */
852 if (d2->type == DT_code)
854 const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
857 if (*c == d2->u.code)
865 /* Otherwise see if the predicates have any codes in common. */
866 else if (d2->type == DT_pred && d2->u.pred.index >= 0)
868 const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
871 while (*c1 != 0 && !common)
873 const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
874 while (*c2 != 0 && !common)
876 common = (*c1 == *c2);
891 /* A subroutine of maybe_both_true; examines all the tests for a given node.
892 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
895 maybe_both_true_1 (d1, d2)
896 struct decision_test *d1, *d2;
898 struct decision_test *t1, *t2;
900 /* A match_operand with no predicate can match anything. Recognize
901 this by the existance of a lone DT_accept_op test. */
902 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
905 /* Eliminate pairs of tests while they can exactly match. */
906 while (d1 && d2 && d1->type == d2->type)
908 if (maybe_both_true_2 (d1, d2) == 0)
910 d1 = d1->next, d2 = d2->next;
913 /* After that, consider all pairs. */
914 for (t1 = d1; t1 ; t1 = t1->next)
915 for (t2 = d2; t2 ; t2 = t2->next)
916 if (maybe_both_true_2 (t1, t2) == 0)
922 /* Return 0 if we can prove that there is no RTL that can match both
923 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
924 can match both or just that we couldn't prove there wasn't such an RTL).
926 TOPLEVEL is non-zero if we are to only look at the top level and not
927 recursively descend. */
930 maybe_both_true (d1, d2, toplevel)
931 struct decision *d1, *d2;
934 struct decision *p1, *p2;
937 /* Don't compare strings on the different positions in insn. Doing so
938 is incorrect and results in false matches from constructs like
940 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
941 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
943 [(set (match_operand:HI "register_operand" "r")
944 (match_operand:HI "register_operand" "r"))]
946 If we are presented with such, we are recursing through the remainder
947 of a node's success nodes (from the loop at the end of this function).
948 Skip forward until we come to a position that matches.
950 Due to the way position strings are constructed, we know that iterating
951 forward from the lexically lower position (e.g. "00") will run into
952 the lexically higher position (e.g. "1") and not the other way around.
953 This saves a bit of effort. */
955 cmp = strcmp (d1->position, d2->position);
961 /* If the d2->position was lexically lower, swap. */
963 p1 = d1, d1 = d2, d2 = p1;
965 if (d1->success.first == 0)
967 for (p1 = d1->success.first; p1; p1 = p1->next)
968 if (maybe_both_true (p1, d2, 0))
974 /* Test the current level. */
975 cmp = maybe_both_true_1 (d1->tests, d2->tests);
979 /* We can't prove that D1 and D2 cannot both be true. If we are only
980 to check the top level, return 1. Otherwise, see if we can prove
981 that all choices in both successors are mutually exclusive. If
982 either does not have any successors, we can't prove they can't both
985 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
988 for (p1 = d1->success.first; p1; p1 = p1->next)
989 for (p2 = d2->success.first; p2; p2 = p2->next)
990 if (maybe_both_true (p1, p2, 0))
996 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
999 nodes_identical_1 (d1, d2)
1000 struct decision_test *d1, *d2;
1005 return d1->u.mode == d2->u.mode;
1008 return d1->u.code == d2->u.code;
1011 return (d1->u.pred.mode == d2->u.pred.mode
1012 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1015 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1018 return d1->u.veclen == d2->u.veclen;
1021 return d1->u.dup == d2->u.dup;
1023 case DT_elt_zero_int:
1024 case DT_elt_one_int:
1025 case DT_elt_zero_wide:
1026 return d1->u.intval == d2->u.intval;
1029 return d1->u.opno == d2->u.opno;
1031 case DT_accept_insn:
1032 /* Differences will be handled in merge_accept_insn. */
1040 /* True iff the two nodes are identical (on one level only). Due
1041 to the way these lists are constructed, we shouldn't have to
1042 consider different orderings on the tests. */
1045 nodes_identical (d1, d2)
1046 struct decision *d1, *d2;
1048 struct decision_test *t1, *t2;
1050 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1052 if (t1->type != t2->type)
1054 if (! nodes_identical_1 (t1, t2))
1058 /* For success, they should now both be null. */
1062 /* A subroutine of merge_trees; given two nodes that have been declared
1063 identical, cope with two insn accept states. If they differ in the
1064 number of clobbers, then the conflict was created by make_insn_sequence
1065 and we can drop the with-clobbers version on the floor. If both
1066 nodes have no additional clobbers, we have found an ambiguity in the
1067 source machine description. */
1070 merge_accept_insn (oldd, addd)
1071 struct decision *oldd, *addd;
1073 struct decision_test *old, *add;
1075 for (old = oldd->tests; old; old = old->next)
1076 if (old->type == DT_accept_insn)
1081 for (add = addd->tests; add; add = add->next)
1082 if (add->type == DT_accept_insn)
1087 /* If one node is for a normal insn and the second is for the base
1088 insn with clobbers stripped off, the second node should be ignored. */
1090 if (old->u.insn.num_clobbers_to_add == 0
1091 && add->u.insn.num_clobbers_to_add > 0)
1093 /* Nothing to do here. */
1095 else if (old->u.insn.num_clobbers_to_add > 0
1096 && add->u.insn.num_clobbers_to_add == 0)
1098 /* In this case, replace OLD with ADD. */
1099 old->u.insn = add->u.insn;
1103 message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1104 get_insn_name (add->u.insn.code_number),
1105 get_insn_name (old->u.insn.code_number));
1106 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1107 get_insn_name (old->u.insn.code_number));
1112 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1115 merge_trees (oldh, addh)
1116 struct decision_head *oldh, *addh;
1118 struct decision *next, *add;
1120 if (addh->first == 0)
1122 if (oldh->first == 0)
1128 /* Trying to merge bits at different positions isn't possible. */
1129 if (strcmp (oldh->first->position, addh->first->position))
1132 for (add = addh->first; add ; add = next)
1134 struct decision *old, *insert_before = NULL;
1138 /* The semantics of pattern matching state that the tests are
1139 done in the order given in the MD file so that if an insn
1140 matches two patterns, the first one will be used. However,
1141 in practice, most, if not all, patterns are unambiguous so
1142 that their order is independent. In that case, we can merge
1143 identical tests and group all similar modes and codes together.
1145 Scan starting from the end of OLDH until we reach a point
1146 where we reach the head of the list or where we pass a
1147 pattern that could also be true if NEW is true. If we find
1148 an identical pattern, we can merge them. Also, record the
1149 last node that tests the same code and mode and the last one
1150 that tests just the same mode.
1152 If we have no match, place NEW after the closest match we found. */
1154 for (old = oldh->last; old; old = old->prev)
1156 if (nodes_identical (old, add))
1158 merge_accept_insn (old, add);
1159 merge_trees (&old->success, &add->success);
1163 if (maybe_both_true (old, add, 0))
1166 /* Insert the nodes in DT test type order, which is roughly
1167 how expensive/important the test is. Given that the tests
1168 are also ordered within the list, examining the first is
1170 if (add->tests->type < old->tests->type)
1171 insert_before = old;
1174 if (insert_before == NULL)
1177 add->prev = oldh->last;
1178 oldh->last->next = add;
1183 if ((add->prev = insert_before->prev) != NULL)
1184 add->prev->next = add;
1187 add->next = insert_before;
1188 insert_before->prev = add;
1195 /* Walk the tree looking for sub-nodes that perform common tests.
1196 Factor out the common test into a new node. This enables us
1197 (depending on the test type) to emit switch statements later. */
1201 struct decision_head *head;
1203 struct decision *first, *next;
1205 for (first = head->first; first && first->next; first = next)
1207 enum decision_type type;
1208 struct decision *new, *old_last;
1210 type = first->tests->type;
1213 /* Want at least two compatible sequential nodes. */
1214 if (next->tests->type != type)
1217 /* Don't want all node types, just those we can turn into
1218 switch statements. */
1221 && type != DT_veclen
1222 && type != DT_elt_zero_int
1223 && type != DT_elt_one_int
1224 && type != DT_elt_zero_wide)
1227 /* If we'd been performing more than one test, create a new node
1228 below our first test. */
1229 if (first->tests->next != NULL)
1231 new = new_decision (first->position, &first->success);
1232 new->tests = first->tests->next;
1233 first->tests->next = NULL;
1236 /* Crop the node tree off after our first test. */
1238 old_last = head->last;
1241 /* For each compatible test, adjust to perform only one test in
1242 the top level node, then merge the node back into the tree. */
1245 struct decision_head h;
1247 if (next->tests->next != NULL)
1249 new = new_decision (next->position, &next->success);
1250 new->tests = next->tests->next;
1251 next->tests->next = NULL;
1256 h.first = h.last = new;
1258 merge_trees (head, &h);
1260 while (next && next->tests->type == type);
1262 /* After we run out of compatible tests, graft the remaining nodes
1263 back onto the tree. */
1266 next->prev = head->last;
1267 head->last->next = next;
1268 head->last = old_last;
1273 for (first = head->first; first; first = first->next)
1274 factor_tests (&first->success);
1277 /* After factoring, try to simplify the tests on any one node.
1278 Tests that are useful for switch statements are recognizable
1279 by having only a single test on a node -- we'll be manipulating
1280 nodes with multiple tests:
1282 If we have mode tests or code tests that are redundant with
1283 predicates, remove them. */
1286 simplify_tests (head)
1287 struct decision_head *head;
1289 struct decision *tree;
1291 for (tree = head->first; tree; tree = tree->next)
1293 struct decision_test *a, *b;
1300 /* Find a predicate node. */
1301 while (b && b->type != DT_pred)
1305 /* Due to how these tests are constructed, we don't even need
1306 to check that the mode and code are compatible -- they were
1307 generated from the predicate in the first place. */
1308 while (a->type == DT_mode || a->type == DT_code)
1315 for (tree = head->first; tree; tree = tree->next)
1316 simplify_tests (&tree->success);
1319 /* Count the number of subnodes of HEAD. If the number is high enough,
1320 make the first node in HEAD start a separate subroutine in the C code
1321 that is generated. */
1324 break_out_subroutines (head, initial)
1325 struct decision_head *head;
1329 struct decision *sub;
1331 for (sub = head->first; sub; sub = sub->next)
1332 size += 1 + break_out_subroutines (&sub->success, 0);
1334 if (size > SUBROUTINE_THRESHOLD && ! initial)
1336 head->first->subroutine_number = ++next_subroutine_number;
1342 /* For each node p, find the next alternative that might be true
1346 find_afterward (head, real_afterward)
1347 struct decision_head *head;
1348 struct decision *real_afterward;
1350 struct decision *p, *q, *afterward;
1352 /* We can't propogate alternatives across subroutine boundaries.
1353 This is not incorrect, merely a minor optimization loss. */
1356 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1358 for ( ; p ; p = p->next)
1360 /* Find the next node that might be true if this one fails. */
1361 for (q = p->next; q ; q = q->next)
1362 if (maybe_both_true (p, q, 1))
1365 /* If we reached the end of the list without finding one,
1366 use the incoming afterward position. */
1375 for (p = head->first; p ; p = p->next)
1376 if (p->success.first)
1377 find_afterward (&p->success, p->afterward);
1379 /* When we are generating a subroutine, record the real afterward
1380 position in the first node where write_tree can find it, and we
1381 can do the right thing at the subroutine call site. */
1383 if (p->subroutine_number > 0)
1384 p->afterward = real_afterward;
1387 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1388 actions are necessary to move to NEWPOS. If we fail to move to the
1389 new state, branch to node AFTERWARD if non-zero, otherwise return.
1391 Failure to move to the new state can only occur if we are trying to
1392 match multiple insns and we try to step past the end of the stream. */
1395 change_state (oldpos, newpos, afterward, indent)
1398 struct decision *afterward;
1401 int odepth = strlen (oldpos);
1402 int ndepth = strlen (newpos);
1404 int old_has_insn, new_has_insn;
1406 /* Pop up as many levels as necessary. */
1407 for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1410 /* Hunt for the last [A-Z] in both strings. */
1411 for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1412 if (oldpos[old_has_insn] >= 'A' && oldpos[old_has_insn] <= 'Z')
1414 for (new_has_insn = odepth - 1; new_has_insn >= 0; --new_has_insn)
1415 if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1418 /* Make sure to reset the _last_insn pointer when popping back up. */
1419 if (old_has_insn >= 0 && new_has_insn < 0)
1420 printf ("%s_last_insn = insn;\n", indent);
1422 /* Go down to desired level. */
1423 while (depth < ndepth)
1425 /* It's a different insn from the first one. */
1426 if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1428 /* We can only fail if we're moving down the tree. */
1429 if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1431 printf ("%s_last_insn = recog_next_insn (insn, %d);\n",
1432 indent, newpos[depth] - 'A');
1436 printf ("%stem = recog_next_insn (insn, %d);\n",
1437 indent, newpos[depth] - 'A');
1438 printf ("%sif (tem == NULL_RTX)\n", indent);
1440 printf ("%s goto L%d;\n", indent, afterward->number);
1442 printf ("%s goto ret0;\n", indent);
1443 printf ("%s_last_insn = tem;\n", indent);
1445 printf ("%sx%d = PATTERN (_last_insn);\n", indent, depth + 1);
1447 else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1448 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1449 indent, depth + 1, depth, newpos[depth] - 'a');
1451 printf ("%sx%d = XEXP (x%d, %c);\n",
1452 indent, depth + 1, depth, newpos[depth]);
1457 /* Print the enumerator constant for CODE -- the upcase version of
1464 register const char *p;
1465 for (p = GET_RTX_NAME (code); *p; p++)
1466 putchar (TOUPPER (*p));
1469 /* Emit code to cross an afterward link -- change state and branch. */
1472 write_afterward (start, afterward, indent)
1473 struct decision *start;
1474 struct decision *afterward;
1477 if (!afterward || start->subroutine_number > 0)
1478 printf("%sgoto ret0;\n", indent);
1481 change_state (start->position, afterward->position, NULL, indent);
1482 printf ("%sgoto L%d;\n", indent, afterward->number);
1486 /* Emit a switch statement, if possible, for an initial sequence of
1487 nodes at START. Return the first node yet untested. */
1489 static struct decision *
1490 write_switch (start, depth)
1491 struct decision *start;
1494 struct decision *p = start;
1495 enum decision_type type = p->tests->type;
1497 /* If we have two or more nodes in sequence that test the same one
1498 thing, we may be able to use a switch statement. */
1502 || p->next->tests->type != type
1503 || p->next->tests->next)
1506 /* DT_code is special in that we can do interesting things with
1507 known predicates at the same time. */
1508 if (type == DT_code)
1510 char codemap[NUM_RTX_CODE];
1511 struct decision *ret;
1513 memset (codemap, 0, sizeof(codemap));
1515 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1518 RTX_CODE code = p->tests->u.code;
1521 printf (":\n goto L%d;\n", p->success.first->number);
1522 p->success.first->need_label = 1;
1527 while (p && p->tests->type == DT_code && !p->tests->next);
1529 /* If P is testing a predicate that we know about and we haven't
1530 seen any of the codes that are valid for the predicate, we can
1531 write a series of "case" statement, one for each possible code.
1532 Since we are already in a switch, these redundant tests are very
1533 cheap and will reduce the number of predicates called. */
1535 /* Note that while we write out cases for these predicates here,
1536 we don't actually write the test here, as it gets kinda messy.
1537 It is trivial to leave this to later by telling our caller that
1538 we only processed the CODE tests. */
1541 while (p && p->tests->type == DT_pred
1542 && p->tests->u.pred.index >= 0)
1546 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1547 if (codemap[(int) *c] != 0)
1550 for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1555 codemap[(int) *c] = 1;
1558 printf (" goto L%d;\n", p->number);
1564 /* Make the default case skip the predicates we managed to match. */
1566 printf (" default:\n");
1571 printf (" goto L%d;\n", p->number);
1575 write_afterward (start, start->afterward, " ");
1578 printf (" break;\n");
1583 else if (type == DT_mode
1584 || type == DT_veclen
1585 || type == DT_elt_zero_int
1586 || type == DT_elt_one_int
1587 || type == DT_elt_zero_wide)
1591 printf (" switch (");
1595 str = "GET_MODE (x%d)";
1598 str = "XVECLEN (x%d, 0)";
1600 case DT_elt_zero_int:
1601 str = "XINT (x%d, 0)";
1603 case DT_elt_one_int:
1604 str = "XINT (x%d, 1)";
1606 case DT_elt_zero_wide:
1607 str = "XWINT (x%d, 0)";
1612 printf (str, depth);
1621 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1624 printf ("%d", p->tests->u.veclen);
1626 case DT_elt_zero_int:
1627 case DT_elt_one_int:
1628 case DT_elt_zero_wide:
1629 printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1634 printf (":\n goto L%d;\n", p->success.first->number);
1635 p->success.first->need_label = 1;
1639 while (p && p->tests->type == type && !p->tests->next);
1641 printf (" default:\n break;\n }\n");
1647 /* None of the other tests are ameanable. */
1652 /* Emit code for one test. */
1655 write_cond (p, depth, subroutine_type)
1656 struct decision_test *p;
1658 enum routine_type subroutine_type;
1663 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1667 printf ("GET_CODE (x%d) == ", depth);
1668 print_code (p->u.code);
1672 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1675 case DT_elt_zero_int:
1676 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1679 case DT_elt_one_int:
1680 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1683 case DT_elt_zero_wide:
1684 printf ("XWINT (x%d, 0) == ", depth);
1685 printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1689 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1693 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1694 GET_MODE_NAME (p->u.pred.mode));
1698 printf ("(%s)", p->u.c_test);
1701 case DT_accept_insn:
1702 switch (subroutine_type)
1705 if (p->u.insn.num_clobbers_to_add == 0)
1707 printf ("pnum_clobbers != NULL");
1720 /* Emit code for one action. The previous tests have succeeded;
1721 TEST is the last of the chain. In the normal case we simply
1722 perform a state change. For the `accept' tests we must do more work. */
1725 write_action (test, depth, uncond, success, subroutine_type)
1726 struct decision_test *test;
1728 struct decision *success;
1729 enum routine_type subroutine_type;
1736 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1738 fputs (" {\n", stdout);
1745 if (test->type == DT_accept_op)
1747 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1749 /* Only allow DT_accept_insn to follow. */
1753 if (test->type != DT_accept_insn)
1758 /* Sanity check that we're now at the end of the list of tests. */
1762 if (test->type == DT_accept_insn)
1764 switch (subroutine_type)
1767 if (test->u.insn.num_clobbers_to_add != 0)
1768 printf ("%s*pnum_clobbers = %d;\n",
1769 indent, test->u.insn.num_clobbers_to_add);
1770 printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1774 printf ("%sreturn gen_split_%d (operands);\n",
1775 indent, test->u.insn.code_number);
1779 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1780 indent, test->u.insn.code_number);
1781 printf ("%sif (tem != 0)\n%s goto ret1;\n", indent, indent);
1790 printf("%sgoto L%d;\n", indent, success->number);
1791 success->need_label = 1;
1795 fputs (" }\n", stdout);
1798 /* Return 1 if the test is always true and has no fallthru path. Return -1
1799 if the test does have a fallthru path, but requires that the condition be
1800 terminated. Otherwise return 0 for a normal test. */
1801 /* ??? is_unconditional is a stupid name for a tri-state function. */
1804 is_unconditional (t, subroutine_type)
1805 struct decision_test *t;
1806 enum routine_type subroutine_type;
1808 if (t->type == DT_accept_op)
1811 if (t->type == DT_accept_insn)
1813 switch (subroutine_type)
1816 return (t->u.insn.num_clobbers_to_add == 0);
1829 /* Emit code for one node -- the conditional and the accompanying action.
1830 Return true if there is no fallthru path. */
1833 write_node (p, depth, subroutine_type)
1836 enum routine_type subroutine_type;
1838 struct decision_test *test, *last_test;
1841 last_test = test = p->tests;
1842 uncond = is_unconditional (test, subroutine_type);
1846 write_cond (test, depth, subroutine_type);
1848 while ((test = test->next) != NULL)
1853 uncond2 = is_unconditional (test, subroutine_type);
1858 write_cond (test, depth, subroutine_type);
1864 write_action (last_test, depth, uncond, p->success.first, subroutine_type);
1869 /* Emit code for all of the sibling nodes of HEAD. */
1872 write_tree_1 (head, depth, subroutine_type)
1873 struct decision_head *head;
1875 enum routine_type subroutine_type;
1877 struct decision *p, *next;
1880 for (p = head->first; p ; p = next)
1882 /* The label for the first element was printed in write_tree. */
1883 if (p != head->first && p->need_label)
1884 OUTPUT_LABEL (" ", p->number);
1886 /* Attempt to write a switch statement for a whole sequence. */
1887 next = write_switch (p, depth);
1892 /* Failed -- fall back and write one node. */
1893 uncond = write_node (p, depth, subroutine_type);
1898 /* Finished with this chain. Close a fallthru path by branching
1899 to the afterward node. */
1901 write_afterward (head->last, head->last->afterward, " ");
1904 /* Write out the decision tree starting at HEAD. PREVPOS is the
1905 position at the node that branched to this node. */
1908 write_tree (head, prevpos, type, initial)
1909 struct decision_head *head;
1910 const char *prevpos;
1911 enum routine_type type;
1914 register struct decision *p = head->first;
1918 OUTPUT_LABEL (" ", p->number);
1920 if (! initial && p->subroutine_number > 0)
1922 static const char * const name_prefix[] = {
1923 "recog", "split", "peephole2"
1926 static const char * const call_suffix[] = {
1927 ", pnum_clobbers", "", ", _plast_insn"
1930 /* This node has been broken out into a separate subroutine.
1931 Call it, test the result, and branch accordingly. */
1935 printf (" tem = %s_%d (x0, insn%s);\n",
1936 name_prefix[type], p->subroutine_number, call_suffix[type]);
1937 if (IS_SPLIT (type))
1938 printf (" if (tem != 0)\n return tem;\n");
1940 printf (" if (tem >= 0)\n return tem;\n");
1942 change_state (p->position, p->afterward->position, NULL, " ");
1943 printf (" goto L%d;\n", p->afterward->number);
1947 printf (" return %s_%d (x0, insn%s);\n",
1948 name_prefix[type], p->subroutine_number, call_suffix[type]);
1953 int depth = strlen (p->position);
1955 change_state (prevpos, p->position, head->last->afterward, " ");
1956 write_tree_1 (head, depth, type);
1958 for (p = head->first; p; p = p->next)
1959 if (p->success.first)
1960 write_tree (&p->success, p->position, type, 0);
1964 /* Write out a subroutine of type TYPE to do comparisons starting at
1968 write_subroutine (head, type)
1969 struct decision_head *head;
1970 enum routine_type type;
1972 static const char * const proto_pattern[] = {
1973 "%sint recog%s PROTO ((rtx, rtx, int *));\n",
1974 "%srtx split%s PROTO ((rtx, rtx));\n",
1975 "%srtx peephole2%s PROTO ((rtx, rtx, rtx *));\n"
1978 static const char * const decl_pattern[] = {
1980 recog%s (x0, insn, pnum_clobbers)\n\
1982 rtx insn ATTRIBUTE_UNUSED;\n\
1983 int *pnum_clobbers ATTRIBUTE_UNUSED;\n",
1986 split%s (x0, insn)\n\
1988 rtx insn ATTRIBUTE_UNUSED;\n",
1991 peephole2%s (x0, insn, _plast_insn)\n\
1993 rtx insn ATTRIBUTE_UNUSED;\n\
1994 rtx *_plast_insn ATTRIBUTE_UNUSED;\n"
1997 int subfunction = head->first ? head->first->subroutine_number : 0;
2002 s_or_e = subfunction ? "static " : "";
2005 sprintf (extension, "_%d", subfunction);
2006 else if (type == RECOG)
2007 extension[0] = '\0';
2009 strcpy (extension, "_insns");
2011 printf (proto_pattern[type], s_or_e, extension);
2012 printf (decl_pattern[type], s_or_e, extension);
2014 printf ("{\n register rtx * const operands = &recog_data.operand[0];\n");
2015 for (i = 1; i <= max_depth; i++)
2016 printf (" register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2018 if (type == PEEPHOLE2)
2019 printf (" register rtx _last_insn = insn;\n");
2020 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2023 write_tree (head, "", type, 1);
2025 printf (" goto ret0;\n");
2027 if (type == PEEPHOLE2)
2028 printf (" ret1:\n *_plast_insn = _last_insn;\n return tem;\n");
2029 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2032 /* In break_out_subroutines, we discovered the boundaries for the
2033 subroutines, but did not write them out. Do so now. */
2036 write_subroutines (head, type)
2037 struct decision_head *head;
2038 enum routine_type type;
2042 for (p = head->first; p ; p = p->next)
2043 if (p->success.first)
2044 write_subroutines (&p->success, type);
2046 if (head->first->subroutine_number > 0)
2047 write_subroutine (head, type);
2050 /* Begin the output file. */
2056 /* Generated automatically by the program `genrecog' from the target\n\
2057 machine description file. */\n\
2059 #include \"config.h\"\n\
2060 #include \"system.h\"\n\
2061 #include \"rtl.h\"\n\
2062 #include \"tm_p.h\"\n\
2063 #include \"function.h\"\n\
2064 #include \"insn-config.h\"\n\
2065 #include \"recog.h\"\n\
2066 #include \"real.h\"\n\
2067 #include \"output.h\"\n\
2068 #include \"flags.h\"\n\
2069 #include \"hard-reg-set.h\"\n\
2070 #include \"resource.h\"\n\
2074 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2075 X0 is a valid instruction.\n\
2077 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2078 returns a nonnegative number which is the insn code number for the\n\
2079 pattern that matched. This is the same as the order in the machine\n\
2080 description of the entry that matched. This number can be used as an\n\
2081 index into `insn_data' and other tables.\n\
2083 The third argument to recog is an optional pointer to an int. If\n\
2084 present, recog will accept a pattern if it matches except for missing\n\
2085 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2086 the optional pointer will be set to the number of CLOBBERs that need\n\
2087 to be added (it should be initialized to zero by the caller). If it\n\
2088 is set nonzero, the caller should allocate a PARALLEL of the\n\
2089 appropriate size, copy the initial entries, and call add_clobbers\n\
2090 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2094 The function split_insns returns 0 if the rtl could not\n\
2095 be split or the split rtl in a SEQUENCE if it can be.\n\
2097 The function peephole2_insns returns 0 if the rtl could not\n\
2098 be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2099 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2104 /* Construct and return a sequence of decisions
2105 that will recognize INSN.
2107 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2109 static struct decision_head
2110 make_insn_sequence (insn, type)
2112 enum routine_type type;
2115 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2116 struct decision *last;
2117 struct decision_test *test, **place;
2118 struct decision_head head;
2120 record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2122 if (type == PEEPHOLE2)
2126 /* peephole2 gets special treatment:
2127 - X always gets an outer parallel even if it's only one entry
2128 - we remove all traces of outer-level match_scratch and match_dup
2129 expressions here. */
2130 x = rtx_alloc (PARALLEL);
2131 PUT_MODE (x, VOIDmode);
2132 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2133 for (i = j = 0; i < XVECLEN (insn, 0); i++)
2135 rtx tmp = XVECEXP (insn, 0, i);
2136 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2138 XVECEXP (x, 0, j) = tmp;
2144 else if (XVECLEN (insn, type == RECOG) == 1)
2145 x = XVECEXP (insn, type == RECOG, 0);
2148 x = rtx_alloc (PARALLEL);
2149 XVEC (x, 0) = XVEC (insn, type == RECOG);
2150 PUT_MODE (x, VOIDmode);
2153 validate_pattern (x, 0);
2155 memset(&head, 0, sizeof(head));
2156 last = add_to_sequence (x, &head, "", type, 1);
2158 /* Find the end of the test chain on the last node. */
2159 for (test = last->tests; test->next; test = test->next)
2161 place = &test->next;
2165 /* Need a new node if we have another test to add. */
2166 if (test->type == DT_accept_op)
2168 last = new_decision ("", &last->success);
2169 place = &last->tests;
2171 test = new_decision_test (DT_c_test, &place);
2172 test->u.c_test = c_test;
2175 test = new_decision_test (DT_accept_insn, &place);
2176 test->u.insn.code_number = next_insn_code;
2177 test->u.insn.lineno = pattern_lineno;
2178 test->u.insn.num_clobbers_to_add = 0;
2183 /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2184 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2185 If so, set up to recognize the pattern without these CLOBBERs. */
2187 if (GET_CODE (x) == PARALLEL)
2191 /* Find the last non-clobber in the parallel. */
2192 for (i = XVECLEN (x, 0); i > 0; i--)
2194 rtx y = XVECEXP (x, 0, i - 1);
2195 if (GET_CODE (y) != CLOBBER
2196 || (GET_CODE (XEXP (y, 0)) != REG
2197 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2201 if (i != XVECLEN (x, 0))
2204 struct decision_head clobber_head;
2206 /* Build a similar insn without the clobbers. */
2208 new = XVECEXP (x, 0, 0);
2213 new = rtx_alloc (PARALLEL);
2214 XVEC (new, 0) = rtvec_alloc (i);
2215 for (j = i - 1; j >= 0; j--)
2216 XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2220 memset (&clobber_head, 0, sizeof(clobber_head));
2221 last = add_to_sequence (new, &clobber_head, "", type, 1);
2223 /* Find the end of the test chain on the last node. */
2224 for (test = last->tests; test->next; test = test->next)
2227 /* We definitely have a new test to add -- create a new
2229 place = &test->next;
2230 if (test->type == DT_accept_op)
2232 last = new_decision ("", &last->success);
2233 place = &last->tests;
2238 test = new_decision_test (DT_c_test, &place);
2239 test->u.c_test = c_test;
2242 test = new_decision_test (DT_accept_insn, &place);
2243 test->u.insn.code_number = next_insn_code;
2244 test->u.insn.lineno = pattern_lineno;
2245 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2247 merge_trees (&head, &clobber_head);
2253 /* Define the subroutine we will call below and emit in genemit. */
2254 printf ("extern rtx gen_split_%d PROTO ((rtx *));\n", next_insn_code);
2258 /* Define the subroutine we will call below and emit in genemit. */
2259 printf ("extern rtx gen_peephole2_%d PROTO ((rtx, rtx *));\n",
2269 process_tree (head, subroutine_type)
2270 struct decision_head *head;
2271 enum routine_type subroutine_type;
2273 if (head->first != NULL)
2275 factor_tests (head);
2276 simplify_tests (head);
2278 next_subroutine_number = 0;
2279 break_out_subroutines (head, 1);
2280 find_afterward (head, NULL);
2282 write_subroutines (head, subroutine_type);
2284 write_subroutine (head, subroutine_type);
2293 struct decision_head recog_tree, split_tree, peephole2_tree, h;
2297 progname = "genrecog";
2298 obstack_init (rtl_obstack);
2300 memset (&recog_tree, 0, sizeof recog_tree);
2301 memset (&split_tree, 0, sizeof split_tree);
2302 memset (&peephole2_tree, 0, sizeof peephole2_tree);
2305 fatal ("No input file name.");
2307 infile = fopen (argv[1], "r");
2311 return FATAL_EXIT_CODE;
2313 read_rtx_filename = argv[1];
2320 /* Read the machine description. */
2324 c = read_skip_spaces (infile);
2328 pattern_lineno = read_rtx_lineno;
2330 desc = read_rtx (infile);
2331 if (GET_CODE (desc) == DEFINE_INSN)
2333 h = make_insn_sequence (desc, RECOG);
2334 merge_trees (&recog_tree, &h);
2336 else if (GET_CODE (desc) == DEFINE_SPLIT)
2338 h = make_insn_sequence (desc, SPLIT);
2339 merge_trees (&split_tree, &h);
2341 else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2343 h = make_insn_sequence (desc, PEEPHOLE2);
2344 merge_trees (&peephole2_tree, &h);
2347 if (GET_CODE (desc) == DEFINE_PEEPHOLE
2348 || GET_CODE (desc) == DEFINE_EXPAND)
2354 return FATAL_EXIT_CODE;
2358 process_tree (&recog_tree, RECOG);
2359 process_tree (&split_tree, SPLIT);
2360 process_tree (&peephole2_tree, PEEPHOLE2);
2363 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2366 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
2368 get_insn_name (code)
2371 if (code < insn_name_ptr_size)
2372 return insn_name_ptr[code];
2378 record_insn_name (code, name)
2382 static const char *last_real_name = "insn";
2383 static int last_real_code = 0;
2386 if (insn_name_ptr_size <= code)
2389 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2391 (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2392 memset (insn_name_ptr + insn_name_ptr_size, 0,
2393 sizeof(char *) * (new_size - insn_name_ptr_size));
2394 insn_name_ptr_size = new_size;
2397 if (!name || name[0] == '\0')
2399 new = xmalloc (strlen (last_real_name) + 10);
2400 sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2404 last_real_name = new = xstrdup (name);
2405 last_real_code = code;
2408 insn_name_ptr[code] = new;
2415 register size_t len = strlen (input) + 1;
2416 register char *output = xmalloc (len);
2417 memcpy (output, input, len);
2422 xrealloc (old, size)
2428 ptr = (PTR) realloc (old, size);
2430 ptr = (PTR) malloc (size);
2432 fatal ("virtual memory exhausted");
2440 register PTR val = (PTR) malloc (size);
2443 fatal ("virtual memory exhausted");
2448 debug_decision_2 (test)
2449 struct decision_test *test;
2454 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2457 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2460 fprintf (stderr, "veclen=%d", test->u.veclen);
2462 case DT_elt_zero_int:
2463 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2465 case DT_elt_one_int:
2466 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2468 case DT_elt_zero_wide:
2469 fprintf (stderr, "elt0_w=");
2470 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2473 fprintf (stderr, "dup=%d", test->u.dup);
2476 fprintf (stderr, "pred=(%s,%s)",
2477 test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2482 strncpy (sub, test->u.c_test, sizeof(sub));
2483 memcpy (sub+16, "...", 4);
2484 fprintf (stderr, "c_test=\"%s\"", sub);
2488 fprintf (stderr, "A_op=%d", test->u.opno);
2490 case DT_accept_insn:
2491 fprintf (stderr, "A_insn=(%d,%d)",
2492 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2501 debug_decision_1 (d, indent)
2506 struct decision_test *test;
2510 for (i = 0; i < indent; ++i)
2512 fputs ("(nil)\n", stderr);
2516 for (i = 0; i < indent; ++i)
2523 debug_decision_2 (test);
2524 while ((test = test->next) != NULL)
2526 fputs (" + ", stderr);
2527 debug_decision_2 (test);
2530 fprintf (stderr, "} %d\n", d->number);
2534 debug_decision_0 (d, indent, maxdepth)
2536 int indent, maxdepth;
2545 for (i = 0; i < indent; ++i)
2547 fputs ("(nil)\n", stderr);
2551 debug_decision_1 (d, indent);
2552 for (n = d->success.first; n ; n = n->next)
2553 debug_decision_0 (n, indent + 2, maxdepth - 1);
2560 debug_decision_0 (d, 0, 1000000);
2564 debug_decision_list (d)
2569 debug_decision_0 (d, 0, 0);