2011-04-29 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / genrecog.c
blob824cf75f3354a946e452c2500cbdd2ce58ad9394
1 /* Generate code from machine description to recognize rtl as insns.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is used to produce insn-recog.c, which contains a
24 function called `recog' plus its subroutines. These functions
25 contain a decision tree that recognizes whether an rtx, the
26 argument given to recog, is a valid instruction.
28 recog returns -1 if the rtx is not valid. If the rtx is valid,
29 recog returns a nonnegative number which is the insn code number
30 for the pattern that matched. This is the same as the order in the
31 machine description of the entry that matched. This number can be
32 used as an index into various insn_* tables, such as insn_template,
33 insn_outfun, and insn_n_operands (found in insn-output.c).
35 The third argument to recog is an optional pointer to an int. If
36 present, recog will accept a pattern if it matches except for
37 missing CLOBBER expressions at the end. In that case, the value
38 pointed to by the optional pointer will be set to the number of
39 CLOBBERs that need to be added (it should be initialized to zero by
40 the caller). If it is set nonzero, the caller should allocate a
41 PARALLEL of the appropriate size, copy the initial entries, and
42 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
44 This program also generates the function `split_insns', which
45 returns 0 if the rtl could not be split, or it returns the split
46 rtl as an INSN list.
48 This program also generates the function `peephole2_insns', which
49 returns 0 if the rtl could not be matched. If there was a match,
50 the new rtl is returned in an INSN list, and LAST_INSN will point
51 to the last recognized insn in the old sequence. */
53 #include "bconfig.h"
54 #include "system.h"
55 #include "coretypes.h"
56 #include "tm.h"
57 #include "rtl.h"
58 #include "errors.h"
59 #include "read-md.h"
60 #include "gensupport.h"
62 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
63 printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
65 /* A listhead of decision trees. The alternatives to a node are kept
66 in a doubly-linked list so we can easily add nodes to the proper
67 place when merging. */
69 struct decision_head
71 struct decision *first;
72 struct decision *last;
75 /* These types are roughly in the order in which we'd like to test them. */
76 enum decision_type
78 DT_num_insns,
79 DT_mode, DT_code, DT_veclen,
80 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide, DT_elt_zero_wide_safe,
81 DT_const_int,
82 DT_veclen_ge, DT_dup, DT_pred, DT_c_test,
83 DT_accept_op, DT_accept_insn
86 /* A single test. The two accept types aren't tests per-se, but
87 their equality (or lack thereof) does affect tree merging so
88 it is convenient to keep them here. */
90 struct decision_test
92 /* A linked list through the tests attached to a node. */
93 struct decision_test *next;
95 enum decision_type type;
97 union
99 int num_insns; /* Number if insn in a define_peephole2. */
100 enum machine_mode mode; /* Machine mode of node. */
101 RTX_CODE code; /* Code to test. */
103 struct
105 const char *name; /* Predicate to call. */
106 const struct pred_data *data;
107 /* Optimization hints for this predicate. */
108 enum machine_mode mode; /* Machine mode for node. */
109 } pred;
111 const char *c_test; /* Additional test to perform. */
112 int veclen; /* Length of vector. */
113 int dup; /* Number of operand to compare against. */
114 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */
115 int opno; /* Operand number matched. */
117 struct {
118 int code_number; /* Insn number matched. */
119 int lineno; /* Line number of the insn. */
120 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */
121 } insn;
122 } u;
125 /* Data structure for decision tree for recognizing legitimate insns. */
127 struct decision
129 struct decision_head success; /* Nodes to test on success. */
130 struct decision *next; /* Node to test on failure. */
131 struct decision *prev; /* Node whose failure tests us. */
132 struct decision *afterward; /* Node to test on success,
133 but failure of successor nodes. */
135 const char *position; /* String denoting position in pattern. */
137 struct decision_test *tests; /* The tests for this node. */
139 int number; /* Node number, used for labels */
140 int subroutine_number; /* Number of subroutine this node starts */
141 int need_label; /* Label needs to be output. */
144 #define SUBROUTINE_THRESHOLD 100
146 static int next_subroutine_number;
148 /* We can write three types of subroutines: One for insn recognition,
149 one to split insns, and one for peephole-type optimizations. This
150 defines which type is being written. */
152 enum routine_type {
153 RECOG, SPLIT, PEEPHOLE2
156 #define IS_SPLIT(X) ((X) != RECOG)
158 /* Next available node number for tree nodes. */
160 static int next_number;
162 /* Next number to use as an insn_code. */
164 static int next_insn_code;
166 /* Record the highest depth we ever have so we know how many variables to
167 allocate in each subroutine we make. */
169 static int max_depth;
171 /* The line number of the start of the pattern currently being processed. */
172 static int pattern_lineno;
174 static struct decision *new_decision
175 (const char *, struct decision_head *);
176 static struct decision_test *new_decision_test
177 (enum decision_type, struct decision_test ***);
178 static rtx find_operand
179 (rtx, int, rtx);
180 static rtx find_matching_operand
181 (rtx, int);
182 static void validate_pattern
183 (rtx, rtx, rtx, int);
184 static struct decision *add_to_sequence
185 (rtx, struct decision_head *, const char *, enum routine_type, int);
187 static int maybe_both_true_2
188 (struct decision_test *, struct decision_test *);
189 static int maybe_both_true_1
190 (struct decision_test *, struct decision_test *);
191 static int maybe_both_true
192 (struct decision *, struct decision *, int);
194 static int nodes_identical_1
195 (struct decision_test *, struct decision_test *);
196 static int nodes_identical
197 (struct decision *, struct decision *);
198 static void merge_accept_insn
199 (struct decision *, struct decision *);
200 static void merge_trees
201 (struct decision_head *, struct decision_head *);
203 static void factor_tests
204 (struct decision_head *);
205 static void simplify_tests
206 (struct decision_head *);
207 static int break_out_subroutines
208 (struct decision_head *, int);
209 static void find_afterward
210 (struct decision_head *, struct decision *);
212 static void change_state
213 (const char *, const char *, const char *);
214 static void print_code
215 (enum rtx_code);
216 static void write_afterward
217 (struct decision *, struct decision *, const char *);
218 static struct decision *write_switch
219 (struct decision *, int);
220 static void write_cond
221 (struct decision_test *, int, enum routine_type);
222 static void write_action
223 (struct decision *, struct decision_test *, int, int,
224 struct decision *, enum routine_type);
225 static int is_unconditional
226 (struct decision_test *, enum routine_type);
227 static int write_node
228 (struct decision *, int, enum routine_type);
229 static void write_tree_1
230 (struct decision_head *, int, enum routine_type);
231 static void write_tree
232 (struct decision_head *, const char *, enum routine_type, int);
233 static void write_subroutine
234 (struct decision_head *, enum routine_type);
235 static void write_subroutines
236 (struct decision_head *, enum routine_type);
237 static void write_header
238 (void);
240 static struct decision_head make_insn_sequence
241 (rtx, enum routine_type);
242 static void process_tree
243 (struct decision_head *, enum routine_type);
245 static void debug_decision_0
246 (struct decision *, int, int);
247 static void debug_decision_1
248 (struct decision *, int);
249 static void debug_decision_2
250 (struct decision_test *);
251 extern void debug_decision
252 (struct decision *);
253 extern void debug_decision_list
254 (struct decision *);
256 /* Create a new node in sequence after LAST. */
258 static struct decision *
259 new_decision (const char *position, struct decision_head *last)
261 struct decision *new_decision = XCNEW (struct decision);
263 new_decision->success = *last;
264 new_decision->position = xstrdup (position);
265 new_decision->number = next_number++;
267 last->first = last->last = new_decision;
268 return new_decision;
271 /* Create a new test and link it in at PLACE. */
273 static struct decision_test *
274 new_decision_test (enum decision_type type, struct decision_test ***pplace)
276 struct decision_test **place = *pplace;
277 struct decision_test *test;
279 test = XNEW (struct decision_test);
280 test->next = *place;
281 test->type = type;
282 *place = test;
284 place = &test->next;
285 *pplace = place;
287 return test;
290 /* Search for and return operand N, stop when reaching node STOP. */
292 static rtx
293 find_operand (rtx pattern, int n, rtx stop)
295 const char *fmt;
296 RTX_CODE code;
297 int i, j, len;
298 rtx r;
300 if (pattern == stop)
301 return stop;
303 code = GET_CODE (pattern);
304 if ((code == MATCH_SCRATCH
305 || code == MATCH_OPERAND
306 || code == MATCH_OPERATOR
307 || code == MATCH_PARALLEL)
308 && XINT (pattern, 0) == n)
309 return pattern;
311 fmt = GET_RTX_FORMAT (code);
312 len = GET_RTX_LENGTH (code);
313 for (i = 0; i < len; i++)
315 switch (fmt[i])
317 case 'e': case 'u':
318 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX)
319 return r;
320 break;
322 case 'V':
323 if (! XVEC (pattern, i))
324 break;
325 /* Fall through. */
327 case 'E':
328 for (j = 0; j < XVECLEN (pattern, i); j++)
329 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop))
330 != NULL_RTX)
331 return r;
332 break;
334 case 'i': case 'w': case '0': case 's':
335 break;
337 default:
338 gcc_unreachable ();
342 return NULL;
345 /* Search for and return operand M, such that it has a matching
346 constraint for operand N. */
348 static rtx
349 find_matching_operand (rtx pattern, int n)
351 const char *fmt;
352 RTX_CODE code;
353 int i, j, len;
354 rtx r;
356 code = GET_CODE (pattern);
357 if (code == MATCH_OPERAND
358 && (XSTR (pattern, 2)[0] == '0' + n
359 || (XSTR (pattern, 2)[0] == '%'
360 && XSTR (pattern, 2)[1] == '0' + n)))
361 return pattern;
363 fmt = GET_RTX_FORMAT (code);
364 len = GET_RTX_LENGTH (code);
365 for (i = 0; i < len; i++)
367 switch (fmt[i])
369 case 'e': case 'u':
370 if ((r = find_matching_operand (XEXP (pattern, i), n)))
371 return r;
372 break;
374 case 'V':
375 if (! XVEC (pattern, i))
376 break;
377 /* Fall through. */
379 case 'E':
380 for (j = 0; j < XVECLEN (pattern, i); j++)
381 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n)))
382 return r;
383 break;
385 case 'i': case 'w': case '0': case 's':
386 break;
388 default:
389 gcc_unreachable ();
393 return NULL;
397 /* Check for various errors in patterns. SET is nonnull for a destination,
398 and is the complete set pattern. SET_CODE is '=' for normal sets, and
399 '+' within a context that requires in-out constraints. */
401 static void
402 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code)
404 const char *fmt;
405 RTX_CODE code;
406 size_t i, len;
407 int j;
409 code = GET_CODE (pattern);
410 switch (code)
412 case MATCH_SCRATCH:
413 return;
414 case MATCH_DUP:
415 case MATCH_OP_DUP:
416 case MATCH_PAR_DUP:
417 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern)
418 error_with_line (pattern_lineno,
419 "operand %i duplicated before defined",
420 XINT (pattern, 0));
421 break;
422 case MATCH_OPERAND:
423 case MATCH_OPERATOR:
425 const char *pred_name = XSTR (pattern, 1);
426 const struct pred_data *pred;
427 const char *c_test;
429 if (GET_CODE (insn) == DEFINE_INSN)
430 c_test = XSTR (insn, 2);
431 else
432 c_test = XSTR (insn, 1);
434 if (pred_name[0] != 0)
436 pred = lookup_predicate (pred_name);
437 if (!pred)
438 message_with_line (pattern_lineno,
439 "warning: unknown predicate '%s'",
440 pred_name);
442 else
443 pred = 0;
445 if (code == MATCH_OPERAND)
447 const char constraints0 = XSTR (pattern, 2)[0];
449 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
450 don't use the MATCH_OPERAND constraint, only the predicate.
451 This is confusing to folks doing new ports, so help them
452 not make the mistake. */
453 if (GET_CODE (insn) == DEFINE_EXPAND
454 || GET_CODE (insn) == DEFINE_SPLIT
455 || GET_CODE (insn) == DEFINE_PEEPHOLE2)
457 if (constraints0)
458 message_with_line (pattern_lineno,
459 "warning: constraints not supported in %s",
460 rtx_name[GET_CODE (insn)]);
463 /* A MATCH_OPERAND that is a SET should have an output reload. */
464 else if (set && constraints0)
466 if (set_code == '+')
468 if (constraints0 == '+')
470 /* If we've only got an output reload for this operand,
471 we'd better have a matching input operand. */
472 else if (constraints0 == '='
473 && find_matching_operand (insn, XINT (pattern, 0)))
475 else
476 error_with_line (pattern_lineno,
477 "operand %d missing in-out reload",
478 XINT (pattern, 0));
480 else if (constraints0 != '=' && constraints0 != '+')
481 error_with_line (pattern_lineno,
482 "operand %d missing output reload",
483 XINT (pattern, 0));
487 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
488 while not likely to occur at runtime, results in less efficient
489 code from insn-recog.c. */
490 if (set && pred && pred->allows_non_lvalue)
491 message_with_line (pattern_lineno,
492 "warning: destination operand %d "
493 "allows non-lvalue",
494 XINT (pattern, 0));
496 /* A modeless MATCH_OPERAND can be handy when we can check for
497 multiple modes in the c_test. In most other cases, it is a
498 mistake. Only DEFINE_INSN is eligible, since SPLIT and
499 PEEP2 can FAIL within the output pattern. Exclude special
500 predicates, which check the mode themselves. Also exclude
501 predicates that allow only constants. Exclude the SET_DEST
502 of a call instruction, as that is a common idiom. */
504 if (GET_MODE (pattern) == VOIDmode
505 && code == MATCH_OPERAND
506 && GET_CODE (insn) == DEFINE_INSN
507 && pred
508 && !pred->special
509 && pred->allows_non_const
510 && strstr (c_test, "operands") == NULL
511 && ! (set
512 && GET_CODE (set) == SET
513 && GET_CODE (SET_SRC (set)) == CALL))
514 message_with_line (pattern_lineno,
515 "warning: operand %d missing mode?",
516 XINT (pattern, 0));
517 return;
520 case SET:
522 enum machine_mode dmode, smode;
523 rtx dest, src;
525 dest = SET_DEST (pattern);
526 src = SET_SRC (pattern);
528 /* STRICT_LOW_PART is a wrapper. Its argument is the real
529 destination, and it's mode should match the source. */
530 if (GET_CODE (dest) == STRICT_LOW_PART)
531 dest = XEXP (dest, 0);
533 /* Find the referent for a DUP. */
535 if (GET_CODE (dest) == MATCH_DUP
536 || GET_CODE (dest) == MATCH_OP_DUP
537 || GET_CODE (dest) == MATCH_PAR_DUP)
538 dest = find_operand (insn, XINT (dest, 0), NULL);
540 if (GET_CODE (src) == MATCH_DUP
541 || GET_CODE (src) == MATCH_OP_DUP
542 || GET_CODE (src) == MATCH_PAR_DUP)
543 src = find_operand (insn, XINT (src, 0), NULL);
545 dmode = GET_MODE (dest);
546 smode = GET_MODE (src);
548 /* The mode of an ADDRESS_OPERAND is the mode of the memory
549 reference, not the mode of the address. */
550 if (GET_CODE (src) == MATCH_OPERAND
551 && ! strcmp (XSTR (src, 1), "address_operand"))
554 /* The operands of a SET must have the same mode unless one
555 is VOIDmode. */
556 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
557 error_with_line (pattern_lineno,
558 "mode mismatch in set: %smode vs %smode",
559 GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
561 /* If only one of the operands is VOIDmode, and PC or CC0 is
562 not involved, it's probably a mistake. */
563 else if (dmode != smode
564 && GET_CODE (dest) != PC
565 && GET_CODE (dest) != CC0
566 && GET_CODE (src) != PC
567 && GET_CODE (src) != CC0
568 && !CONST_INT_P (src)
569 && GET_CODE (src) != CALL)
571 const char *which;
572 which = (dmode == VOIDmode ? "destination" : "source");
573 message_with_line (pattern_lineno,
574 "warning: %s missing a mode?", which);
577 if (dest != SET_DEST (pattern))
578 validate_pattern (dest, insn, pattern, '=');
579 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
580 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0);
581 return;
584 case CLOBBER:
585 validate_pattern (SET_DEST (pattern), insn, pattern, '=');
586 return;
588 case ZERO_EXTRACT:
589 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
590 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0);
591 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0);
592 return;
594 case STRICT_LOW_PART:
595 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0);
596 return;
598 case LABEL_REF:
599 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
600 error_with_line (pattern_lineno,
601 "operand to label_ref %smode not VOIDmode",
602 GET_MODE_NAME (GET_MODE (XEXP (pattern, 0))));
603 break;
605 default:
606 break;
609 fmt = GET_RTX_FORMAT (code);
610 len = GET_RTX_LENGTH (code);
611 for (i = 0; i < len; i++)
613 switch (fmt[i])
615 case 'e': case 'u':
616 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0);
617 break;
619 case 'E':
620 for (j = 0; j < XVECLEN (pattern, i); j++)
621 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0);
622 break;
624 case 'i': case 'w': case '0': case 's':
625 break;
627 default:
628 gcc_unreachable ();
633 /* Create a chain of nodes to verify that an rtl expression matches
634 PATTERN.
636 LAST is a pointer to the listhead in the previous node in the chain (or
637 in the calling function, for the first node).
639 POSITION is the string representing the current position in the insn.
641 INSN_TYPE is the type of insn for which we are emitting code.
643 A pointer to the final node in the chain is returned. */
645 static struct decision *
646 add_to_sequence (rtx pattern, struct decision_head *last, const char *position,
647 enum routine_type insn_type, int top)
649 RTX_CODE code;
650 struct decision *this_decision, *sub;
651 struct decision_test *test;
652 struct decision_test **place;
653 char *subpos;
654 size_t i;
655 const char *fmt;
656 int depth = strlen (position);
657 int len;
658 enum machine_mode mode;
660 if (depth > max_depth)
661 max_depth = depth;
663 subpos = XNEWVAR (char, depth + 2);
664 strcpy (subpos, position);
665 subpos[depth + 1] = 0;
667 sub = this_decision = new_decision (position, last);
668 place = &this_decision->tests;
670 restart:
671 mode = GET_MODE (pattern);
672 code = GET_CODE (pattern);
674 switch (code)
676 case PARALLEL:
677 /* Toplevel peephole pattern. */
678 if (insn_type == PEEPHOLE2 && top)
680 int num_insns;
682 /* Check we have sufficient insns. This avoids complications
683 because we then know peep2_next_insn never fails. */
684 num_insns = XVECLEN (pattern, 0);
685 if (num_insns > 1)
687 test = new_decision_test (DT_num_insns, &place);
688 test->u.num_insns = num_insns;
689 last = &sub->success;
691 else
693 /* We don't need the node we just created -- unlink it. */
694 last->first = last->last = NULL;
697 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
699 /* Which insn we're looking at is represented by A-Z. We don't
700 ever use 'A', however; it is always implied. */
702 subpos[depth] = (i > 0 ? 'A' + i : 0);
703 sub = add_to_sequence (XVECEXP (pattern, 0, i),
704 last, subpos, insn_type, 0);
705 last = &sub->success;
707 goto ret;
710 /* Else nothing special. */
711 break;
713 case MATCH_PARALLEL:
714 /* The explicit patterns within a match_parallel enforce a minimum
715 length on the vector. The match_parallel predicate may allow
716 for more elements. We do need to check for this minimum here
717 or the code generated to match the internals may reference data
718 beyond the end of the vector. */
719 test = new_decision_test (DT_veclen_ge, &place);
720 test->u.veclen = XVECLEN (pattern, 2);
721 /* Fall through. */
723 case MATCH_OPERAND:
724 case MATCH_SCRATCH:
725 case MATCH_OPERATOR:
727 RTX_CODE was_code = code;
728 const char *pred_name;
729 bool allows_const_int = true;
731 if (code == MATCH_SCRATCH)
733 pred_name = "scratch_operand";
734 code = UNKNOWN;
736 else
738 pred_name = XSTR (pattern, 1);
739 if (code == MATCH_PARALLEL)
740 code = PARALLEL;
741 else
742 code = UNKNOWN;
745 if (pred_name[0] != 0)
747 const struct pred_data *pred;
749 test = new_decision_test (DT_pred, &place);
750 test->u.pred.name = pred_name;
751 test->u.pred.mode = mode;
753 /* See if we know about this predicate.
754 If we do, remember it for use below.
756 We can optimize the generated code a little if either
757 (a) the predicate only accepts one code, or (b) the
758 predicate does not allow CONST_INT, in which case it
759 can match only if the modes match. */
760 pred = lookup_predicate (pred_name);
761 if (pred)
763 test->u.pred.data = pred;
764 allows_const_int = pred->codes[CONST_INT];
765 if (was_code == MATCH_PARALLEL
766 && pred->singleton != PARALLEL)
767 message_with_line (pattern_lineno,
768 "predicate '%s' used in match_parallel "
769 "does not allow only PARALLEL", pred->name);
770 else
771 code = pred->singleton;
773 else
774 message_with_line (pattern_lineno,
775 "warning: unknown predicate '%s' in '%s' expression",
776 pred_name, GET_RTX_NAME (was_code));
779 /* Can't enforce a mode if we allow const_int. */
780 if (allows_const_int)
781 mode = VOIDmode;
783 /* Accept the operand, i.e. record it in `operands'. */
784 test = new_decision_test (DT_accept_op, &place);
785 test->u.opno = XINT (pattern, 0);
787 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
789 char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
790 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
792 subpos[depth] = i + base;
793 sub = add_to_sequence (XVECEXP (pattern, 2, i),
794 &sub->success, subpos, insn_type, 0);
797 goto fini;
800 case MATCH_OP_DUP:
801 code = UNKNOWN;
803 test = new_decision_test (DT_dup, &place);
804 test->u.dup = XINT (pattern, 0);
806 test = new_decision_test (DT_accept_op, &place);
807 test->u.opno = XINT (pattern, 0);
809 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
811 subpos[depth] = i + '0';
812 sub = add_to_sequence (XVECEXP (pattern, 1, i),
813 &sub->success, subpos, insn_type, 0);
815 goto fini;
817 case MATCH_DUP:
818 case MATCH_PAR_DUP:
819 code = UNKNOWN;
821 test = new_decision_test (DT_dup, &place);
822 test->u.dup = XINT (pattern, 0);
823 goto fini;
825 case ADDRESS:
826 pattern = XEXP (pattern, 0);
827 goto restart;
829 default:
830 break;
833 fmt = GET_RTX_FORMAT (code);
834 len = GET_RTX_LENGTH (code);
836 /* Do tests against the current node first. */
837 for (i = 0; i < (size_t) len; i++)
839 if (fmt[i] == 'i')
841 gcc_assert (i < 2);
843 if (!i)
845 test = new_decision_test (DT_elt_zero_int, &place);
846 test->u.intval = XINT (pattern, i);
848 else
850 test = new_decision_test (DT_elt_one_int, &place);
851 test->u.intval = XINT (pattern, i);
854 else if (fmt[i] == 'w')
856 /* If this value actually fits in an int, we can use a switch
857 statement here, so indicate that. */
858 enum decision_type type
859 = ((int) XWINT (pattern, i) == XWINT (pattern, i))
860 ? DT_elt_zero_wide_safe : DT_elt_zero_wide;
862 gcc_assert (!i);
864 test = new_decision_test (type, &place);
865 test->u.intval = XWINT (pattern, i);
867 else if (fmt[i] == 'E')
869 gcc_assert (!i);
871 test = new_decision_test (DT_veclen, &place);
872 test->u.veclen = XVECLEN (pattern, i);
876 /* Now test our sub-patterns. */
877 for (i = 0; i < (size_t) len; i++)
879 switch (fmt[i])
881 case 'e': case 'u':
882 subpos[depth] = '0' + i;
883 sub = add_to_sequence (XEXP (pattern, i), &sub->success,
884 subpos, insn_type, 0);
885 break;
887 case 'E':
889 int j;
890 for (j = 0; j < XVECLEN (pattern, i); j++)
892 subpos[depth] = 'a' + j;
893 sub = add_to_sequence (XVECEXP (pattern, i, j),
894 &sub->success, subpos, insn_type, 0);
896 break;
899 case 'i': case 'w':
900 /* Handled above. */
901 break;
902 case '0':
903 break;
905 default:
906 gcc_unreachable ();
910 fini:
911 /* Insert nodes testing mode and code, if they're still relevant,
912 before any of the nodes we may have added above. */
913 if (code != UNKNOWN)
915 place = &this_decision->tests;
916 test = new_decision_test (DT_code, &place);
917 test->u.code = code;
920 if (mode != VOIDmode)
922 place = &this_decision->tests;
923 test = new_decision_test (DT_mode, &place);
924 test->u.mode = mode;
927 /* If we didn't insert any tests or accept nodes, hork. */
928 gcc_assert (this_decision->tests);
930 ret:
931 free (subpos);
932 return sub;
935 /* A subroutine of maybe_both_true; examines only one test.
936 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
938 static int
939 maybe_both_true_2 (struct decision_test *d1, struct decision_test *d2)
941 if (d1->type == d2->type)
943 switch (d1->type)
945 case DT_num_insns:
946 if (d1->u.num_insns == d2->u.num_insns)
947 return 1;
948 else
949 return -1;
951 case DT_mode:
952 return d1->u.mode == d2->u.mode;
954 case DT_code:
955 return d1->u.code == d2->u.code;
957 case DT_veclen:
958 return d1->u.veclen == d2->u.veclen;
960 case DT_elt_zero_int:
961 case DT_elt_one_int:
962 case DT_elt_zero_wide:
963 case DT_elt_zero_wide_safe:
964 return d1->u.intval == d2->u.intval;
966 default:
967 break;
971 /* If either has a predicate that we know something about, set
972 things up so that D1 is the one that always has a known
973 predicate. Then see if they have any codes in common. */
975 if (d1->type == DT_pred || d2->type == DT_pred)
977 if (d2->type == DT_pred)
979 struct decision_test *tmp;
980 tmp = d1, d1 = d2, d2 = tmp;
983 /* If D2 tests a mode, see if it matches D1. */
984 if (d1->u.pred.mode != VOIDmode)
986 if (d2->type == DT_mode)
988 if (d1->u.pred.mode != d2->u.mode
989 /* The mode of an address_operand predicate is the
990 mode of the memory, not the operand. It can only
991 be used for testing the predicate, so we must
992 ignore it here. */
993 && strcmp (d1->u.pred.name, "address_operand") != 0)
994 return 0;
996 /* Don't check two predicate modes here, because if both predicates
997 accept CONST_INT, then both can still be true even if the modes
998 are different. If they don't accept CONST_INT, there will be a
999 separate DT_mode that will make maybe_both_true_1 return 0. */
1002 if (d1->u.pred.data)
1004 /* If D2 tests a code, see if it is in the list of valid
1005 codes for D1's predicate. */
1006 if (d2->type == DT_code)
1008 if (!d1->u.pred.data->codes[d2->u.code])
1009 return 0;
1012 /* Otherwise see if the predicates have any codes in common. */
1013 else if (d2->type == DT_pred && d2->u.pred.data)
1015 bool common = false;
1016 int c;
1018 for (c = 0; c < NUM_RTX_CODE; c++)
1019 if (d1->u.pred.data->codes[c] && d2->u.pred.data->codes[c])
1021 common = true;
1022 break;
1025 if (!common)
1026 return 0;
1031 /* Tests vs veclen may be known when strict equality is involved. */
1032 if (d1->type == DT_veclen && d2->type == DT_veclen_ge)
1033 return d1->u.veclen >= d2->u.veclen;
1034 if (d1->type == DT_veclen_ge && d2->type == DT_veclen)
1035 return d2->u.veclen >= d1->u.veclen;
1037 return -1;
1040 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1041 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1043 static int
1044 maybe_both_true_1 (struct decision_test *d1, struct decision_test *d2)
1046 struct decision_test *t1, *t2;
1048 /* A match_operand with no predicate can match anything. Recognize
1049 this by the existence of a lone DT_accept_op test. */
1050 if (d1->type == DT_accept_op || d2->type == DT_accept_op)
1051 return 1;
1053 /* Eliminate pairs of tests while they can exactly match. */
1054 while (d1 && d2 && d1->type == d2->type)
1056 if (maybe_both_true_2 (d1, d2) == 0)
1057 return 0;
1058 d1 = d1->next, d2 = d2->next;
1061 /* After that, consider all pairs. */
1062 for (t1 = d1; t1 ; t1 = t1->next)
1063 for (t2 = d2; t2 ; t2 = t2->next)
1064 if (maybe_both_true_2 (t1, t2) == 0)
1065 return 0;
1067 return -1;
1070 /* Return 0 if we can prove that there is no RTL that can match both
1071 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
1072 can match both or just that we couldn't prove there wasn't such an RTL).
1074 TOPLEVEL is nonzero if we are to only look at the top level and not
1075 recursively descend. */
1077 static int
1078 maybe_both_true (struct decision *d1, struct decision *d2,
1079 int toplevel)
1081 struct decision *p1, *p2;
1082 int cmp;
1084 /* Don't compare strings on the different positions in insn. Doing so
1085 is incorrect and results in false matches from constructs like
1087 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1088 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1090 [(set (match_operand:HI "register_operand" "r")
1091 (match_operand:HI "register_operand" "r"))]
1093 If we are presented with such, we are recursing through the remainder
1094 of a node's success nodes (from the loop at the end of this function).
1095 Skip forward until we come to a position that matches.
1097 Due to the way position strings are constructed, we know that iterating
1098 forward from the lexically lower position (e.g. "00") will run into
1099 the lexically higher position (e.g. "1") and not the other way around.
1100 This saves a bit of effort. */
1102 cmp = strcmp (d1->position, d2->position);
1103 if (cmp != 0)
1105 gcc_assert (!toplevel);
1107 /* If the d2->position was lexically lower, swap. */
1108 if (cmp > 0)
1109 p1 = d1, d1 = d2, d2 = p1;
1111 if (d1->success.first == 0)
1112 return 1;
1113 for (p1 = d1->success.first; p1; p1 = p1->next)
1114 if (maybe_both_true (p1, d2, 0))
1115 return 1;
1117 return 0;
1120 /* Test the current level. */
1121 cmp = maybe_both_true_1 (d1->tests, d2->tests);
1122 if (cmp >= 0)
1123 return cmp;
1125 /* We can't prove that D1 and D2 cannot both be true. If we are only
1126 to check the top level, return 1. Otherwise, see if we can prove
1127 that all choices in both successors are mutually exclusive. If
1128 either does not have any successors, we can't prove they can't both
1129 be true. */
1131 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
1132 return 1;
1134 for (p1 = d1->success.first; p1; p1 = p1->next)
1135 for (p2 = d2->success.first; p2; p2 = p2->next)
1136 if (maybe_both_true (p1, p2, 0))
1137 return 1;
1139 return 0;
1142 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
1144 static int
1145 nodes_identical_1 (struct decision_test *d1, struct decision_test *d2)
1147 switch (d1->type)
1149 case DT_num_insns:
1150 return d1->u.num_insns == d2->u.num_insns;
1152 case DT_mode:
1153 return d1->u.mode == d2->u.mode;
1155 case DT_code:
1156 return d1->u.code == d2->u.code;
1158 case DT_pred:
1159 return (d1->u.pred.mode == d2->u.pred.mode
1160 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1162 case DT_c_test:
1163 return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1165 case DT_veclen:
1166 case DT_veclen_ge:
1167 return d1->u.veclen == d2->u.veclen;
1169 case DT_dup:
1170 return d1->u.dup == d2->u.dup;
1172 case DT_elt_zero_int:
1173 case DT_elt_one_int:
1174 case DT_elt_zero_wide:
1175 case DT_elt_zero_wide_safe:
1176 return d1->u.intval == d2->u.intval;
1178 case DT_accept_op:
1179 return d1->u.opno == d2->u.opno;
1181 case DT_accept_insn:
1182 /* Differences will be handled in merge_accept_insn. */
1183 return 1;
1185 default:
1186 gcc_unreachable ();
1190 /* True iff the two nodes are identical (on one level only). Due
1191 to the way these lists are constructed, we shouldn't have to
1192 consider different orderings on the tests. */
1194 static int
1195 nodes_identical (struct decision *d1, struct decision *d2)
1197 struct decision_test *t1, *t2;
1199 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1201 if (t1->type != t2->type)
1202 return 0;
1203 if (! nodes_identical_1 (t1, t2))
1204 return 0;
1207 /* For success, they should now both be null. */
1208 if (t1 != t2)
1209 return 0;
1211 /* Check that their subnodes are at the same position, as any one set
1212 of sibling decisions must be at the same position. Allowing this
1213 requires complications to find_afterward and when change_state is
1214 invoked. */
1215 if (d1->success.first
1216 && d2->success.first
1217 && strcmp (d1->success.first->position, d2->success.first->position))
1218 return 0;
1220 return 1;
1223 /* A subroutine of merge_trees; given two nodes that have been declared
1224 identical, cope with two insn accept states. If they differ in the
1225 number of clobbers, then the conflict was created by make_insn_sequence
1226 and we can drop the with-clobbers version on the floor. If both
1227 nodes have no additional clobbers, we have found an ambiguity in the
1228 source machine description. */
1230 static void
1231 merge_accept_insn (struct decision *oldd, struct decision *addd)
1233 struct decision_test *old, *add;
1235 for (old = oldd->tests; old; old = old->next)
1236 if (old->type == DT_accept_insn)
1237 break;
1238 if (old == NULL)
1239 return;
1241 for (add = addd->tests; add; add = add->next)
1242 if (add->type == DT_accept_insn)
1243 break;
1244 if (add == NULL)
1245 return;
1247 /* If one node is for a normal insn and the second is for the base
1248 insn with clobbers stripped off, the second node should be ignored. */
1250 if (old->u.insn.num_clobbers_to_add == 0
1251 && add->u.insn.num_clobbers_to_add > 0)
1253 /* Nothing to do here. */
1255 else if (old->u.insn.num_clobbers_to_add > 0
1256 && add->u.insn.num_clobbers_to_add == 0)
1258 /* In this case, replace OLD with ADD. */
1259 old->u.insn = add->u.insn;
1261 else
1263 error_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1264 get_insn_name (add->u.insn.code_number),
1265 get_insn_name (old->u.insn.code_number));
1266 message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1267 get_insn_name (old->u.insn.code_number));
1271 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1273 static void
1274 merge_trees (struct decision_head *oldh, struct decision_head *addh)
1276 struct decision *next, *add;
1278 if (addh->first == 0)
1279 return;
1280 if (oldh->first == 0)
1282 *oldh = *addh;
1283 return;
1286 /* Trying to merge bits at different positions isn't possible. */
1287 gcc_assert (!strcmp (oldh->first->position, addh->first->position));
1289 for (add = addh->first; add ; add = next)
1291 struct decision *old, *insert_before = NULL;
1293 next = add->next;
1295 /* The semantics of pattern matching state that the tests are
1296 done in the order given in the MD file so that if an insn
1297 matches two patterns, the first one will be used. However,
1298 in practice, most, if not all, patterns are unambiguous so
1299 that their order is independent. In that case, we can merge
1300 identical tests and group all similar modes and codes together.
1302 Scan starting from the end of OLDH until we reach a point
1303 where we reach the head of the list or where we pass a
1304 pattern that could also be true if NEW is true. If we find
1305 an identical pattern, we can merge them. Also, record the
1306 last node that tests the same code and mode and the last one
1307 that tests just the same mode.
1309 If we have no match, place NEW after the closest match we found. */
1311 for (old = oldh->last; old; old = old->prev)
1313 if (nodes_identical (old, add))
1315 merge_accept_insn (old, add);
1316 merge_trees (&old->success, &add->success);
1317 goto merged_nodes;
1320 if (maybe_both_true (old, add, 0))
1321 break;
1323 /* Insert the nodes in DT test type order, which is roughly
1324 how expensive/important the test is. Given that the tests
1325 are also ordered within the list, examining the first is
1326 sufficient. */
1327 if ((int) add->tests->type < (int) old->tests->type)
1328 insert_before = old;
1331 if (insert_before == NULL)
1333 add->next = NULL;
1334 add->prev = oldh->last;
1335 oldh->last->next = add;
1336 oldh->last = add;
1338 else
1340 if ((add->prev = insert_before->prev) != NULL)
1341 add->prev->next = add;
1342 else
1343 oldh->first = add;
1344 add->next = insert_before;
1345 insert_before->prev = add;
1348 merged_nodes:;
1352 /* Walk the tree looking for sub-nodes that perform common tests.
1353 Factor out the common test into a new node. This enables us
1354 (depending on the test type) to emit switch statements later. */
1356 static void
1357 factor_tests (struct decision_head *head)
1359 struct decision *first, *next;
1361 for (first = head->first; first && first->next; first = next)
1363 enum decision_type type;
1364 struct decision *new_dec, *old_last;
1366 type = first->tests->type;
1367 next = first->next;
1369 /* Want at least two compatible sequential nodes. */
1370 if (next->tests->type != type)
1371 continue;
1373 /* Don't want all node types, just those we can turn into
1374 switch statements. */
1375 if (type != DT_mode
1376 && type != DT_code
1377 && type != DT_veclen
1378 && type != DT_elt_zero_int
1379 && type != DT_elt_one_int
1380 && type != DT_elt_zero_wide_safe)
1381 continue;
1383 /* If we'd been performing more than one test, create a new node
1384 below our first test. */
1385 if (first->tests->next != NULL)
1387 new_dec = new_decision (first->position, &first->success);
1388 new_dec->tests = first->tests->next;
1389 first->tests->next = NULL;
1392 /* Crop the node tree off after our first test. */
1393 first->next = NULL;
1394 old_last = head->last;
1395 head->last = first;
1397 /* For each compatible test, adjust to perform only one test in
1398 the top level node, then merge the node back into the tree. */
1401 struct decision_head h;
1403 if (next->tests->next != NULL)
1405 new_dec = new_decision (next->position, &next->success);
1406 new_dec->tests = next->tests->next;
1407 next->tests->next = NULL;
1409 new_dec = next;
1410 next = next->next;
1411 new_dec->next = NULL;
1412 h.first = h.last = new_dec;
1414 merge_trees (head, &h);
1416 while (next && next->tests->type == type);
1418 /* After we run out of compatible tests, graft the remaining nodes
1419 back onto the tree. */
1420 if (next)
1422 next->prev = head->last;
1423 head->last->next = next;
1424 head->last = old_last;
1428 /* Recurse. */
1429 for (first = head->first; first; first = first->next)
1430 factor_tests (&first->success);
1433 /* After factoring, try to simplify the tests on any one node.
1434 Tests that are useful for switch statements are recognizable
1435 by having only a single test on a node -- we'll be manipulating
1436 nodes with multiple tests:
1438 If we have mode tests or code tests that are redundant with
1439 predicates, remove them. */
1441 static void
1442 simplify_tests (struct decision_head *head)
1444 struct decision *tree;
1446 for (tree = head->first; tree; tree = tree->next)
1448 struct decision_test *a, *b;
1450 a = tree->tests;
1451 b = a->next;
1452 if (b == NULL)
1453 continue;
1455 /* Find a predicate node. */
1456 while (b && b->type != DT_pred)
1457 b = b->next;
1458 if (b)
1460 /* Due to how these tests are constructed, we don't even need
1461 to check that the mode and code are compatible -- they were
1462 generated from the predicate in the first place. */
1463 while (a->type == DT_mode || a->type == DT_code)
1464 a = a->next;
1465 tree->tests = a;
1469 /* Recurse. */
1470 for (tree = head->first; tree; tree = tree->next)
1471 simplify_tests (&tree->success);
1474 /* Count the number of subnodes of HEAD. If the number is high enough,
1475 make the first node in HEAD start a separate subroutine in the C code
1476 that is generated. */
1478 static int
1479 break_out_subroutines (struct decision_head *head, int initial)
1481 int size = 0;
1482 struct decision *sub;
1484 for (sub = head->first; sub; sub = sub->next)
1485 size += 1 + break_out_subroutines (&sub->success, 0);
1487 if (size > SUBROUTINE_THRESHOLD && ! initial)
1489 head->first->subroutine_number = ++next_subroutine_number;
1490 size = 1;
1492 return size;
1495 /* For each node p, find the next alternative that might be true
1496 when p is true. */
1498 static void
1499 find_afterward (struct decision_head *head, struct decision *real_afterward)
1501 struct decision *p, *q, *afterward;
1503 /* We can't propagate alternatives across subroutine boundaries.
1504 This is not incorrect, merely a minor optimization loss. */
1506 p = head->first;
1507 afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1509 for ( ; p ; p = p->next)
1511 /* Find the next node that might be true if this one fails. */
1512 for (q = p->next; q ; q = q->next)
1513 if (maybe_both_true (p, q, 1))
1514 break;
1516 /* If we reached the end of the list without finding one,
1517 use the incoming afterward position. */
1518 if (!q)
1519 q = afterward;
1520 p->afterward = q;
1521 if (q)
1522 q->need_label = 1;
1525 /* Recurse. */
1526 for (p = head->first; p ; p = p->next)
1527 if (p->success.first)
1528 find_afterward (&p->success, p->afterward);
1530 /* When we are generating a subroutine, record the real afterward
1531 position in the first node where write_tree can find it, and we
1532 can do the right thing at the subroutine call site. */
1533 p = head->first;
1534 if (p->subroutine_number > 0)
1535 p->afterward = real_afterward;
1538 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1539 actions are necessary to move to NEWPOS. If we fail to move to the
1540 new state, branch to node AFTERWARD if nonzero, otherwise return.
1542 Failure to move to the new state can only occur if we are trying to
1543 match multiple insns and we try to step past the end of the stream. */
1545 static void
1546 change_state (const char *oldpos, const char *newpos, const char *indent)
1548 int odepth = strlen (oldpos);
1549 int ndepth = strlen (newpos);
1550 int depth;
1552 /* Pop up as many levels as necessary. */
1553 for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1554 continue;
1556 /* Go down to desired level. */
1557 while (depth < ndepth)
1559 /* It's a different insn from the first one. */
1560 if (ISUPPER (newpos[depth]))
1562 printf ("%stem = peep2_next_insn (%d);\n",
1563 indent, newpos[depth] - 'A');
1564 printf ("%sx%d = PATTERN (tem);\n", indent, depth + 1);
1566 else if (ISLOWER (newpos[depth]))
1567 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1568 indent, depth + 1, depth, newpos[depth] - 'a');
1569 else
1570 printf ("%sx%d = XEXP (x%d, %c);\n",
1571 indent, depth + 1, depth, newpos[depth]);
1572 ++depth;
1576 /* Print the enumerator constant for CODE -- the upcase version of
1577 the name. */
1579 static void
1580 print_code (enum rtx_code code)
1582 const char *p;
1583 for (p = GET_RTX_NAME (code); *p; p++)
1584 putchar (TOUPPER (*p));
1587 /* Emit code to cross an afterward link -- change state and branch. */
1589 static void
1590 write_afterward (struct decision *start, struct decision *afterward,
1591 const char *indent)
1593 if (!afterward || start->subroutine_number > 0)
1594 printf("%sgoto ret0;\n", indent);
1595 else
1597 change_state (start->position, afterward->position, indent);
1598 printf ("%sgoto L%d;\n", indent, afterward->number);
1602 /* Emit a HOST_WIDE_INT as an integer constant expression. We need to take
1603 special care to avoid "decimal constant is so large that it is unsigned"
1604 warnings in the resulting code. */
1606 static void
1607 print_host_wide_int (HOST_WIDE_INT val)
1609 HOST_WIDE_INT min = (unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT-1);
1610 if (val == min)
1611 printf ("(" HOST_WIDE_INT_PRINT_DEC_C "-1)", val + 1);
1612 else
1613 printf (HOST_WIDE_INT_PRINT_DEC_C, val);
1616 /* Emit a switch statement, if possible, for an initial sequence of
1617 nodes at START. Return the first node yet untested. */
1619 static struct decision *
1620 write_switch (struct decision *start, int depth)
1622 struct decision *p = start;
1623 enum decision_type type = p->tests->type;
1624 struct decision *needs_label = NULL;
1626 /* If we have two or more nodes in sequence that test the same one
1627 thing, we may be able to use a switch statement. */
1629 if (!p->next
1630 || p->tests->next
1631 || p->next->tests->type != type
1632 || p->next->tests->next
1633 || nodes_identical_1 (p->tests, p->next->tests))
1634 return p;
1636 /* DT_code is special in that we can do interesting things with
1637 known predicates at the same time. */
1638 if (type == DT_code)
1640 char codemap[NUM_RTX_CODE];
1641 struct decision *ret;
1642 RTX_CODE code;
1644 memset (codemap, 0, sizeof(codemap));
1646 printf (" switch (GET_CODE (x%d))\n {\n", depth);
1647 code = p->tests->u.code;
1650 if (p != start && p->need_label && needs_label == NULL)
1651 needs_label = p;
1653 printf (" case ");
1654 print_code (code);
1655 printf (":\n goto L%d;\n", p->success.first->number);
1656 p->success.first->need_label = 1;
1658 codemap[code] = 1;
1659 p = p->next;
1661 while (p
1662 && ! p->tests->next
1663 && p->tests->type == DT_code
1664 && ! codemap[code = p->tests->u.code]);
1666 /* If P is testing a predicate that we know about and we haven't
1667 seen any of the codes that are valid for the predicate, we can
1668 write a series of "case" statement, one for each possible code.
1669 Since we are already in a switch, these redundant tests are very
1670 cheap and will reduce the number of predicates called. */
1672 /* Note that while we write out cases for these predicates here,
1673 we don't actually write the test here, as it gets kinda messy.
1674 It is trivial to leave this to later by telling our caller that
1675 we only processed the CODE tests. */
1676 if (needs_label != NULL)
1677 ret = needs_label;
1678 else
1679 ret = p;
1681 while (p && p->tests->type == DT_pred && p->tests->u.pred.data)
1683 const struct pred_data *data = p->tests->u.pred.data;
1684 int c;
1686 for (c = 0; c < NUM_RTX_CODE; c++)
1687 if (codemap[c] && data->codes[c])
1688 goto pred_done;
1690 for (c = 0; c < NUM_RTX_CODE; c++)
1691 if (data->codes[c])
1693 fputs (" case ", stdout);
1694 print_code ((enum rtx_code) c);
1695 fputs (":\n", stdout);
1696 codemap[c] = 1;
1699 printf (" goto L%d;\n", p->number);
1700 p->need_label = 1;
1701 p = p->next;
1704 pred_done:
1705 /* Make the default case skip the predicates we managed to match. */
1707 printf (" default:\n");
1708 if (p != ret)
1710 if (p)
1712 printf (" goto L%d;\n", p->number);
1713 p->need_label = 1;
1715 else
1716 write_afterward (start, start->afterward, " ");
1718 else
1719 printf (" break;\n");
1720 printf (" }\n");
1722 return ret;
1724 else if (type == DT_mode
1725 || type == DT_veclen
1726 || type == DT_elt_zero_int
1727 || type == DT_elt_one_int
1728 || type == DT_elt_zero_wide_safe)
1730 const char *indent = "";
1732 /* We cast switch parameter to integer, so we must ensure that the value
1733 fits. */
1734 if (type == DT_elt_zero_wide_safe)
1736 indent = " ";
1737 printf(" if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n", depth, depth);
1739 printf ("%s switch (", indent);
1740 switch (type)
1742 case DT_mode:
1743 printf ("GET_MODE (x%d)", depth);
1744 break;
1745 case DT_veclen:
1746 printf ("XVECLEN (x%d, 0)", depth);
1747 break;
1748 case DT_elt_zero_int:
1749 printf ("XINT (x%d, 0)", depth);
1750 break;
1751 case DT_elt_one_int:
1752 printf ("XINT (x%d, 1)", depth);
1753 break;
1754 case DT_elt_zero_wide_safe:
1755 /* Convert result of XWINT to int for portability since some C
1756 compilers won't do it and some will. */
1757 printf ("(int) XWINT (x%d, 0)", depth);
1758 break;
1759 default:
1760 gcc_unreachable ();
1762 printf (")\n%s {\n", indent);
1766 /* Merge trees will not unify identical nodes if their
1767 sub-nodes are at different levels. Thus we must check
1768 for duplicate cases. */
1769 struct decision *q;
1770 for (q = start; q != p; q = q->next)
1771 if (nodes_identical_1 (p->tests, q->tests))
1772 goto case_done;
1774 if (p != start && p->need_label && needs_label == NULL)
1775 needs_label = p;
1777 printf ("%s case ", indent);
1778 switch (type)
1780 case DT_mode:
1781 printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1782 break;
1783 case DT_veclen:
1784 printf ("%d", p->tests->u.veclen);
1785 break;
1786 case DT_elt_zero_int:
1787 case DT_elt_one_int:
1788 case DT_elt_zero_wide:
1789 case DT_elt_zero_wide_safe:
1790 print_host_wide_int (p->tests->u.intval);
1791 break;
1792 default:
1793 gcc_unreachable ();
1795 printf (":\n%s goto L%d;\n", indent, p->success.first->number);
1796 p->success.first->need_label = 1;
1798 p = p->next;
1800 while (p && p->tests->type == type && !p->tests->next);
1802 case_done:
1803 printf ("%s default:\n%s break;\n%s }\n",
1804 indent, indent, indent);
1806 return needs_label != NULL ? needs_label : p;
1808 else
1810 /* None of the other tests are amenable. */
1811 return p;
1815 /* Emit code for one test. */
1817 static void
1818 write_cond (struct decision_test *p, int depth,
1819 enum routine_type subroutine_type)
1821 switch (p->type)
1823 case DT_num_insns:
1824 printf ("peep2_current_count >= %d", p->u.num_insns);
1825 break;
1827 case DT_mode:
1828 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1829 break;
1831 case DT_code:
1832 printf ("GET_CODE (x%d) == ", depth);
1833 print_code (p->u.code);
1834 break;
1836 case DT_veclen:
1837 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1838 break;
1840 case DT_elt_zero_int:
1841 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1842 break;
1844 case DT_elt_one_int:
1845 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1846 break;
1848 case DT_elt_zero_wide:
1849 case DT_elt_zero_wide_safe:
1850 printf ("XWINT (x%d, 0) == ", depth);
1851 print_host_wide_int (p->u.intval);
1852 break;
1854 case DT_const_int:
1855 printf ("x%d == const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
1856 depth, (int) p->u.intval);
1857 break;
1859 case DT_veclen_ge:
1860 printf ("XVECLEN (x%d, 0) >= %d", depth, p->u.veclen);
1861 break;
1863 case DT_dup:
1864 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1865 break;
1867 case DT_pred:
1868 printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1869 GET_MODE_NAME (p->u.pred.mode));
1870 break;
1872 case DT_c_test:
1873 print_c_condition (p->u.c_test);
1874 break;
1876 case DT_accept_insn:
1877 gcc_assert (subroutine_type == RECOG);
1878 gcc_assert (p->u.insn.num_clobbers_to_add);
1879 printf ("pnum_clobbers != NULL");
1880 break;
1882 default:
1883 gcc_unreachable ();
1887 /* Emit code for one action. The previous tests have succeeded;
1888 TEST is the last of the chain. In the normal case we simply
1889 perform a state change. For the `accept' tests we must do more work. */
1891 static void
1892 write_action (struct decision *p, struct decision_test *test,
1893 int depth, int uncond, struct decision *success,
1894 enum routine_type subroutine_type)
1896 const char *indent;
1897 int want_close = 0;
1899 if (uncond)
1900 indent = " ";
1901 else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1903 fputs (" {\n", stdout);
1904 indent = " ";
1905 want_close = 1;
1907 else
1908 indent = " ";
1910 if (test->type == DT_accept_op)
1912 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1914 /* Only allow DT_accept_insn to follow. */
1915 if (test->next)
1917 test = test->next;
1918 gcc_assert (test->type == DT_accept_insn);
1922 /* Sanity check that we're now at the end of the list of tests. */
1923 gcc_assert (!test->next);
1925 if (test->type == DT_accept_insn)
1927 switch (subroutine_type)
1929 case RECOG:
1930 if (test->u.insn.num_clobbers_to_add != 0)
1931 printf ("%s*pnum_clobbers = %d;\n",
1932 indent, test->u.insn.num_clobbers_to_add);
1933 printf ("%sreturn %d; /* %s */\n", indent,
1934 test->u.insn.code_number,
1935 get_insn_name (test->u.insn.code_number));
1936 break;
1938 case SPLIT:
1939 printf ("%sreturn gen_split_%d (insn, operands);\n",
1940 indent, test->u.insn.code_number);
1941 break;
1943 case PEEPHOLE2:
1945 int match_len = 0, i;
1947 for (i = strlen (p->position) - 1; i >= 0; --i)
1948 if (ISUPPER (p->position[i]))
1950 match_len = p->position[i] - 'A';
1951 break;
1953 printf ("%s*_pmatch_len = %d;\n", indent, match_len);
1954 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1955 indent, test->u.insn.code_number);
1956 printf ("%sif (tem != 0)\n%s return tem;\n", indent, indent);
1958 break;
1960 default:
1961 gcc_unreachable ();
1964 else
1966 printf("%sgoto L%d;\n", indent, success->number);
1967 success->need_label = 1;
1970 if (want_close)
1971 fputs (" }\n", stdout);
1974 /* Return 1 if the test is always true and has no fallthru path. Return -1
1975 if the test does have a fallthru path, but requires that the condition be
1976 terminated. Otherwise return 0 for a normal test. */
1977 /* ??? is_unconditional is a stupid name for a tri-state function. */
1979 static int
1980 is_unconditional (struct decision_test *t, enum routine_type subroutine_type)
1982 if (t->type == DT_accept_op)
1983 return 1;
1985 if (t->type == DT_accept_insn)
1987 switch (subroutine_type)
1989 case RECOG:
1990 return (t->u.insn.num_clobbers_to_add == 0);
1991 case SPLIT:
1992 return 1;
1993 case PEEPHOLE2:
1994 return -1;
1995 default:
1996 gcc_unreachable ();
2000 return 0;
2003 /* Emit code for one node -- the conditional and the accompanying action.
2004 Return true if there is no fallthru path. */
2006 static int
2007 write_node (struct decision *p, int depth,
2008 enum routine_type subroutine_type)
2010 struct decision_test *test, *last_test;
2011 int uncond;
2013 /* Scan the tests and simplify comparisons against small
2014 constants. */
2015 for (test = p->tests; test; test = test->next)
2017 if (test->type == DT_code
2018 && test->u.code == CONST_INT
2019 && test->next
2020 && test->next->type == DT_elt_zero_wide_safe
2021 && -MAX_SAVED_CONST_INT <= test->next->u.intval
2022 && test->next->u.intval <= MAX_SAVED_CONST_INT)
2024 test->type = DT_const_int;
2025 test->u.intval = test->next->u.intval;
2026 test->next = test->next->next;
2030 last_test = test = p->tests;
2031 uncond = is_unconditional (test, subroutine_type);
2032 if (uncond == 0)
2034 printf (" if (");
2035 write_cond (test, depth, subroutine_type);
2037 while ((test = test->next) != NULL)
2039 last_test = test;
2040 if (is_unconditional (test, subroutine_type))
2041 break;
2043 printf ("\n && ");
2044 write_cond (test, depth, subroutine_type);
2047 printf (")\n");
2050 write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
2052 return uncond > 0;
2055 /* Emit code for all of the sibling nodes of HEAD. */
2057 static void
2058 write_tree_1 (struct decision_head *head, int depth,
2059 enum routine_type subroutine_type)
2061 struct decision *p, *next;
2062 int uncond = 0;
2064 for (p = head->first; p ; p = next)
2066 /* The label for the first element was printed in write_tree. */
2067 if (p != head->first && p->need_label)
2068 OUTPUT_LABEL (" ", p->number);
2070 /* Attempt to write a switch statement for a whole sequence. */
2071 next = write_switch (p, depth);
2072 if (p != next)
2073 uncond = 0;
2074 else
2076 /* Failed -- fall back and write one node. */
2077 uncond = write_node (p, depth, subroutine_type);
2078 next = p->next;
2082 /* Finished with this chain. Close a fallthru path by branching
2083 to the afterward node. */
2084 if (! uncond)
2085 write_afterward (head->last, head->last->afterward, " ");
2088 /* Write out the decision tree starting at HEAD. PREVPOS is the
2089 position at the node that branched to this node. */
2091 static void
2092 write_tree (struct decision_head *head, const char *prevpos,
2093 enum routine_type type, int initial)
2095 struct decision *p = head->first;
2097 putchar ('\n');
2098 if (p->need_label)
2099 OUTPUT_LABEL (" ", p->number);
2101 if (! initial && p->subroutine_number > 0)
2103 static const char * const name_prefix[] = {
2104 "recog", "split", "peephole2"
2107 static const char * const call_suffix[] = {
2108 ", pnum_clobbers", "", ", _pmatch_len"
2111 /* This node has been broken out into a separate subroutine.
2112 Call it, test the result, and branch accordingly. */
2114 if (p->afterward)
2116 printf (" tem = %s_%d (x0, insn%s);\n",
2117 name_prefix[type], p->subroutine_number, call_suffix[type]);
2118 if (IS_SPLIT (type))
2119 printf (" if (tem != 0)\n return tem;\n");
2120 else
2121 printf (" if (tem >= 0)\n return tem;\n");
2123 change_state (p->position, p->afterward->position, " ");
2124 printf (" goto L%d;\n", p->afterward->number);
2126 else
2128 printf (" return %s_%d (x0, insn%s);\n",
2129 name_prefix[type], p->subroutine_number, call_suffix[type]);
2132 else
2134 int depth = strlen (p->position);
2136 change_state (prevpos, p->position, " ");
2137 write_tree_1 (head, depth, type);
2139 for (p = head->first; p; p = p->next)
2140 if (p->success.first)
2141 write_tree (&p->success, p->position, type, 0);
2145 /* Write out a subroutine of type TYPE to do comparisons starting at
2146 node TREE. */
2148 static void
2149 write_subroutine (struct decision_head *head, enum routine_type type)
2151 int subfunction = head->first ? head->first->subroutine_number : 0;
2152 const char *s_or_e;
2153 char extension[32];
2154 int i;
2156 s_or_e = subfunction ? "static " : "";
2158 if (subfunction)
2159 sprintf (extension, "_%d", subfunction);
2160 else if (type == RECOG)
2161 extension[0] = '\0';
2162 else
2163 strcpy (extension, "_insns");
2165 switch (type)
2167 case RECOG:
2168 printf ("%sint\n\
2169 recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", s_or_e, extension);
2170 break;
2171 case SPLIT:
2172 printf ("%srtx\n\
2173 split%s (rtx x0 ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED)\n",
2174 s_or_e, extension);
2175 break;
2176 case PEEPHOLE2:
2177 printf ("%srtx\n\
2178 peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n",
2179 s_or_e, extension);
2180 break;
2183 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2184 for (i = 1; i <= max_depth; i++)
2185 printf (" rtx x%d ATTRIBUTE_UNUSED;\n", i);
2187 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2189 if (!subfunction)
2190 printf (" recog_data.insn = NULL_RTX;\n");
2192 if (head->first)
2193 write_tree (head, "", type, 1);
2194 else
2195 printf (" goto ret0;\n");
2197 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2200 /* In break_out_subroutines, we discovered the boundaries for the
2201 subroutines, but did not write them out. Do so now. */
2203 static void
2204 write_subroutines (struct decision_head *head, enum routine_type type)
2206 struct decision *p;
2208 for (p = head->first; p ; p = p->next)
2209 if (p->success.first)
2210 write_subroutines (&p->success, type);
2212 if (head->first->subroutine_number > 0)
2213 write_subroutine (head, type);
2216 /* Begin the output file. */
2218 static void
2219 write_header (void)
2221 puts ("\
2222 /* Generated automatically by the program `genrecog' from the target\n\
2223 machine description file. */\n\
2225 #include \"config.h\"\n\
2226 #include \"system.h\"\n\
2227 #include \"coretypes.h\"\n\
2228 #include \"tm.h\"\n\
2229 #include \"rtl.h\"\n\
2230 #include \"tm_p.h\"\n\
2231 #include \"function.h\"\n\
2232 #include \"insn-config.h\"\n\
2233 #include \"recog.h\"\n\
2234 #include \"output.h\"\n\
2235 #include \"flags.h\"\n\
2236 #include \"hard-reg-set.h\"\n\
2237 #include \"resource.h\"\n\
2238 #include \"diagnostic-core.h\"\n\
2239 #include \"reload.h\"\n\
2240 #include \"regs.h\"\n\
2241 #include \"tm-constrs.h\"\n\
2242 \n");
2244 puts ("\n\
2245 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2246 X0 is a valid instruction.\n\
2248 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2249 returns a nonnegative number which is the insn code number for the\n\
2250 pattern that matched. This is the same as the order in the machine\n\
2251 description of the entry that matched. This number can be used as an\n\
2252 index into `insn_data' and other tables.\n");
2253 puts ("\
2254 The third argument to recog is an optional pointer to an int. If\n\
2255 present, recog will accept a pattern if it matches except for missing\n\
2256 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2257 the optional pointer will be set to the number of CLOBBERs that need\n\
2258 to be added (it should be initialized to zero by the caller). If it");
2259 puts ("\
2260 is set nonzero, the caller should allocate a PARALLEL of the\n\
2261 appropriate size, copy the initial entries, and call add_clobbers\n\
2262 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2265 puts ("\n\
2266 The function split_insns returns 0 if the rtl could not\n\
2267 be split or the split rtl as an INSN list if it can be.\n\
2269 The function peephole2_insns returns 0 if the rtl could not\n\
2270 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
2271 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2272 */\n\n");
2276 /* Construct and return a sequence of decisions
2277 that will recognize INSN.
2279 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2281 static struct decision_head
2282 make_insn_sequence (rtx insn, enum routine_type type)
2284 rtx x;
2285 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2286 int truth = maybe_eval_c_test (c_test);
2287 struct decision *last;
2288 struct decision_test *test, **place;
2289 struct decision_head head;
2290 char c_test_pos[2];
2292 /* We should never see an insn whose C test is false at compile time. */
2293 gcc_assert (truth);
2295 c_test_pos[0] = '\0';
2296 if (type == PEEPHOLE2)
2298 int i, j;
2300 /* peephole2 gets special treatment:
2301 - X always gets an outer parallel even if it's only one entry
2302 - we remove all traces of outer-level match_scratch and match_dup
2303 expressions here. */
2304 x = rtx_alloc (PARALLEL);
2305 PUT_MODE (x, VOIDmode);
2306 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2307 for (i = j = 0; i < XVECLEN (insn, 0); i++)
2309 rtx tmp = XVECEXP (insn, 0, i);
2310 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2312 XVECEXP (x, 0, j) = tmp;
2313 j++;
2316 XVECLEN (x, 0) = j;
2318 c_test_pos[0] = 'A' + j - 1;
2319 c_test_pos[1] = '\0';
2321 else if (XVECLEN (insn, type == RECOG) == 1)
2322 x = XVECEXP (insn, type == RECOG, 0);
2323 else
2325 x = rtx_alloc (PARALLEL);
2326 XVEC (x, 0) = XVEC (insn, type == RECOG);
2327 PUT_MODE (x, VOIDmode);
2330 validate_pattern (x, insn, NULL_RTX, 0);
2332 memset(&head, 0, sizeof(head));
2333 last = add_to_sequence (x, &head, "", type, 1);
2335 /* Find the end of the test chain on the last node. */
2336 for (test = last->tests; test->next; test = test->next)
2337 continue;
2338 place = &test->next;
2340 /* Skip the C test if it's known to be true at compile time. */
2341 if (truth == -1)
2343 /* Need a new node if we have another test to add. */
2344 if (test->type == DT_accept_op)
2346 last = new_decision (c_test_pos, &last->success);
2347 place = &last->tests;
2349 test = new_decision_test (DT_c_test, &place);
2350 test->u.c_test = c_test;
2353 test = new_decision_test (DT_accept_insn, &place);
2354 test->u.insn.code_number = next_insn_code;
2355 test->u.insn.lineno = pattern_lineno;
2356 test->u.insn.num_clobbers_to_add = 0;
2358 switch (type)
2360 case RECOG:
2361 /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends
2362 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2363 If so, set up to recognize the pattern without these CLOBBERs. */
2365 if (GET_CODE (x) == PARALLEL)
2367 int i;
2369 /* Find the last non-clobber in the parallel. */
2370 for (i = XVECLEN (x, 0); i > 0; i--)
2372 rtx y = XVECEXP (x, 0, i - 1);
2373 if (GET_CODE (y) != CLOBBER
2374 || (!REG_P (XEXP (y, 0))
2375 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2376 break;
2379 if (i != XVECLEN (x, 0))
2381 rtx new_rtx;
2382 struct decision_head clobber_head;
2384 /* Build a similar insn without the clobbers. */
2385 if (i == 1)
2386 new_rtx = XVECEXP (x, 0, 0);
2387 else
2389 int j;
2391 new_rtx = rtx_alloc (PARALLEL);
2392 XVEC (new_rtx, 0) = rtvec_alloc (i);
2393 for (j = i - 1; j >= 0; j--)
2394 XVECEXP (new_rtx, 0, j) = XVECEXP (x, 0, j);
2397 /* Recognize it. */
2398 memset (&clobber_head, 0, sizeof(clobber_head));
2399 last = add_to_sequence (new_rtx, &clobber_head, "", type, 1);
2401 /* Find the end of the test chain on the last node. */
2402 for (test = last->tests; test->next; test = test->next)
2403 continue;
2405 /* We definitely have a new test to add -- create a new
2406 node if needed. */
2407 place = &test->next;
2408 if (test->type == DT_accept_op)
2410 last = new_decision ("", &last->success);
2411 place = &last->tests;
2414 /* Skip the C test if it's known to be true at compile
2415 time. */
2416 if (truth == -1)
2418 test = new_decision_test (DT_c_test, &place);
2419 test->u.c_test = c_test;
2422 test = new_decision_test (DT_accept_insn, &place);
2423 test->u.insn.code_number = next_insn_code;
2424 test->u.insn.lineno = pattern_lineno;
2425 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2427 merge_trees (&head, &clobber_head);
2430 break;
2432 case SPLIT:
2433 /* Define the subroutine we will call below and emit in genemit. */
2434 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", next_insn_code);
2435 break;
2437 case PEEPHOLE2:
2438 /* Define the subroutine we will call below and emit in genemit. */
2439 printf ("extern rtx gen_peephole2_%d (rtx, rtx *);\n",
2440 next_insn_code);
2441 break;
2444 return head;
2447 static void
2448 process_tree (struct decision_head *head, enum routine_type subroutine_type)
2450 if (head->first == NULL)
2452 /* We can elide peephole2_insns, but not recog or split_insns. */
2453 if (subroutine_type == PEEPHOLE2)
2454 return;
2456 else
2458 factor_tests (head);
2460 next_subroutine_number = 0;
2461 break_out_subroutines (head, 1);
2462 find_afterward (head, NULL);
2464 /* We run this after find_afterward, because find_afterward needs
2465 the redundant DT_mode tests on predicates to determine whether
2466 two tests can both be true or not. */
2467 simplify_tests(head);
2469 write_subroutines (head, subroutine_type);
2472 write_subroutine (head, subroutine_type);
2475 extern int main (int, char **);
2478 main (int argc, char **argv)
2480 rtx desc;
2481 struct decision_head recog_tree, split_tree, peephole2_tree, h;
2483 progname = "genrecog";
2485 memset (&recog_tree, 0, sizeof recog_tree);
2486 memset (&split_tree, 0, sizeof split_tree);
2487 memset (&peephole2_tree, 0, sizeof peephole2_tree);
2489 if (!init_rtx_reader_args (argc, argv))
2490 return (FATAL_EXIT_CODE);
2492 next_insn_code = 0;
2494 write_header ();
2496 /* Read the machine description. */
2498 while (1)
2500 desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2501 if (desc == NULL)
2502 break;
2504 switch (GET_CODE (desc))
2506 case DEFINE_INSN:
2507 h = make_insn_sequence (desc, RECOG);
2508 merge_trees (&recog_tree, &h);
2509 break;
2511 case DEFINE_SPLIT:
2512 h = make_insn_sequence (desc, SPLIT);
2513 merge_trees (&split_tree, &h);
2514 break;
2516 case DEFINE_PEEPHOLE2:
2517 h = make_insn_sequence (desc, PEEPHOLE2);
2518 merge_trees (&peephole2_tree, &h);
2520 default:
2521 /* do nothing */;
2525 if (have_error)
2526 return FATAL_EXIT_CODE;
2528 puts ("\n\n");
2530 process_tree (&recog_tree, RECOG);
2531 process_tree (&split_tree, SPLIT);
2532 process_tree (&peephole2_tree, PEEPHOLE2);
2534 fflush (stdout);
2535 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2538 static void
2539 debug_decision_2 (struct decision_test *test)
2541 switch (test->type)
2543 case DT_num_insns:
2544 fprintf (stderr, "num_insns=%d", test->u.num_insns);
2545 break;
2546 case DT_mode:
2547 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2548 break;
2549 case DT_code:
2550 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2551 break;
2552 case DT_veclen:
2553 fprintf (stderr, "veclen=%d", test->u.veclen);
2554 break;
2555 case DT_elt_zero_int:
2556 fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2557 break;
2558 case DT_elt_one_int:
2559 fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2560 break;
2561 case DT_elt_zero_wide:
2562 fprintf (stderr, "elt0_w=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2563 break;
2564 case DT_elt_zero_wide_safe:
2565 fprintf (stderr, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2566 break;
2567 case DT_veclen_ge:
2568 fprintf (stderr, "veclen>=%d", test->u.veclen);
2569 break;
2570 case DT_dup:
2571 fprintf (stderr, "dup=%d", test->u.dup);
2572 break;
2573 case DT_pred:
2574 fprintf (stderr, "pred=(%s,%s)",
2575 test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2576 break;
2577 case DT_c_test:
2579 char sub[16+4];
2580 strncpy (sub, test->u.c_test, sizeof(sub));
2581 memcpy (sub+16, "...", 4);
2582 fprintf (stderr, "c_test=\"%s\"", sub);
2584 break;
2585 case DT_accept_op:
2586 fprintf (stderr, "A_op=%d", test->u.opno);
2587 break;
2588 case DT_accept_insn:
2589 fprintf (stderr, "A_insn=(%d,%d)",
2590 test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2591 break;
2593 default:
2594 gcc_unreachable ();
2598 static void
2599 debug_decision_1 (struct decision *d, int indent)
2601 int i;
2602 struct decision_test *test;
2604 if (d == NULL)
2606 for (i = 0; i < indent; ++i)
2607 putc (' ', stderr);
2608 fputs ("(nil)\n", stderr);
2609 return;
2612 for (i = 0; i < indent; ++i)
2613 putc (' ', stderr);
2615 putc ('{', stderr);
2616 test = d->tests;
2617 if (test)
2619 debug_decision_2 (test);
2620 while ((test = test->next) != NULL)
2622 fputs (" + ", stderr);
2623 debug_decision_2 (test);
2626 fprintf (stderr, "} %d n %d a %d\n", d->number,
2627 (d->next ? d->next->number : -1),
2628 (d->afterward ? d->afterward->number : -1));
2631 static void
2632 debug_decision_0 (struct decision *d, int indent, int maxdepth)
2634 struct decision *n;
2635 int i;
2637 if (maxdepth < 0)
2638 return;
2639 if (d == NULL)
2641 for (i = 0; i < indent; ++i)
2642 putc (' ', stderr);
2643 fputs ("(nil)\n", stderr);
2644 return;
2647 debug_decision_1 (d, indent);
2648 for (n = d->success.first; n ; n = n->next)
2649 debug_decision_0 (n, indent + 2, maxdepth - 1);
2652 DEBUG_FUNCTION void
2653 debug_decision (struct decision *d)
2655 debug_decision_0 (d, 0, 1000000);
2658 DEBUG_FUNCTION void
2659 debug_decision_list (struct decision *d)
2661 while (d)
2663 debug_decision_0 (d, 0, 0);
2664 d = d->next;