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)
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
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. */
55 #include "coretypes.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 /* Ways of obtaining an rtx to be tested. */
67 /* PATTERN (peep2_next_insn (ARG)). */
70 /* XEXP (BASE, ARG). */
73 /* XVECEXP (BASE, 0, ARG). */
77 /* The position of an rtx relative to X0. Each useful position is
78 represented by exactly one instance of this structure. */
81 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */
82 struct position
*base
;
84 /* A position with the same BASE and TYPE, but with the next value
86 struct position
*next
;
88 /* A list of all POS_XEXP positions that use this one as their base,
89 chained by NEXT fields. The first entry represents XEXP (this, 0),
90 the second represents XEXP (this, 1), and so on. */
91 struct position
*xexps
;
93 /* A list of POS_XVECEXP0 positions that use this one as their base,
94 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0),
95 the second represents XVECEXP (this, 0, 1), and so on. */
96 struct position
*xvecexp0s
;
98 /* The type of position. */
99 enum position_type type
;
101 /* The argument to TYPE (shown as ARG in the position_type comments). */
104 /* The depth of this position, with 0 as the root. */
108 /* A listhead of decision trees. The alternatives to a node are kept
109 in a doubly-linked list so we can easily add nodes to the proper
110 place when merging. */
114 struct decision
*first
;
115 struct decision
*last
;
118 /* These types are roughly in the order in which we'd like to test them. */
122 DT_mode
, DT_code
, DT_veclen
,
123 DT_elt_zero_int
, DT_elt_one_int
, DT_elt_zero_wide
, DT_elt_zero_wide_safe
,
125 DT_veclen_ge
, DT_dup
, DT_pred
, DT_c_test
,
126 DT_accept_op
, DT_accept_insn
129 /* A single test. The two accept types aren't tests per-se, but
130 their equality (or lack thereof) does affect tree merging so
131 it is convenient to keep them here. */
135 /* A linked list through the tests attached to a node. */
136 struct decision_test
*next
;
138 enum decision_type type
;
142 int num_insns
; /* Number if insn in a define_peephole2. */
143 enum machine_mode mode
; /* Machine mode of node. */
144 RTX_CODE code
; /* Code to test. */
148 const char *name
; /* Predicate to call. */
149 const struct pred_data
*data
;
150 /* Optimization hints for this predicate. */
151 enum machine_mode mode
; /* Machine mode for node. */
154 const char *c_test
; /* Additional test to perform. */
155 int veclen
; /* Length of vector. */
156 int dup
; /* Number of operand to compare against. */
157 HOST_WIDE_INT intval
; /* Value for XINT for XWINT. */
158 int opno
; /* Operand number matched. */
161 int code_number
; /* Insn number matched. */
162 int lineno
; /* Line number of the insn. */
163 int num_clobbers_to_add
; /* Number of CLOBBERs to be added. */
168 /* Data structure for decision tree for recognizing legitimate insns. */
172 struct decision_head success
; /* Nodes to test on success. */
173 struct decision
*next
; /* Node to test on failure. */
174 struct decision
*prev
; /* Node whose failure tests us. */
175 struct decision
*afterward
; /* Node to test on success,
176 but failure of successor nodes. */
178 struct position
*position
; /* Position in pattern. */
180 struct decision_test
*tests
; /* The tests for this node. */
182 int number
; /* Node number, used for labels */
183 int subroutine_number
; /* Number of subroutine this node starts */
184 int need_label
; /* Label needs to be output. */
187 #define SUBROUTINE_THRESHOLD 100
189 static int next_subroutine_number
;
191 /* We can write three types of subroutines: One for insn recognition,
192 one to split insns, and one for peephole-type optimizations. This
193 defines which type is being written. */
196 RECOG
, SPLIT
, PEEPHOLE2
199 #define IS_SPLIT(X) ((X) != RECOG)
201 /* Next available node number for tree nodes. */
203 static int next_number
;
205 /* Next number to use as an insn_code. */
207 static int next_insn_code
;
209 /* Record the highest depth we ever have so we know how many variables to
210 allocate in each subroutine we make. */
212 static int max_depth
;
214 /* The line number of the start of the pattern currently being processed. */
215 static int pattern_lineno
;
217 /* The root position (x0). */
218 static struct position root_pos
;
220 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position,
221 since we are given that instruction's pattern as x0. */
222 static struct position
*peep2_insn_pos_list
= &root_pos
;
224 extern void debug_decision
226 extern void debug_decision_list
229 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR
230 points to where the unique object that represents the position
231 should be stored. Create the object if it doesn't already exist,
232 otherwise reuse the object that is already there. */
234 static struct position
*
235 next_position (struct position
**next_ptr
, struct position
*base
,
236 enum position_type type
, int arg
)
238 struct position
*pos
;
243 pos
= XCNEW (struct position
);
247 pos
->depth
= base
->depth
+ 1;
253 /* Compare positions POS1 and POS2 lexicographically. */
256 compare_positions (struct position
*pos1
, struct position
*pos2
)
260 diff
= pos1
->depth
- pos2
->depth
;
264 while (pos1
->depth
!= pos2
->depth
);
268 while (pos1
->depth
!= pos2
->depth
);
271 diff
= (int) pos1
->type
- (int) pos2
->type
;
273 diff
= pos1
->arg
- pos2
->arg
;
280 /* Create a new node in sequence after LAST. */
282 static struct decision
*
283 new_decision (struct position
*pos
, struct decision_head
*last
)
285 struct decision
*new_decision
= XCNEW (struct decision
);
287 new_decision
->success
= *last
;
288 new_decision
->position
= pos
;
289 new_decision
->number
= next_number
++;
291 last
->first
= last
->last
= new_decision
;
295 /* Create a new test and link it in at PLACE. */
297 static struct decision_test
*
298 new_decision_test (enum decision_type type
, struct decision_test
***pplace
)
300 struct decision_test
**place
= *pplace
;
301 struct decision_test
*test
;
303 test
= XNEW (struct decision_test
);
314 /* Search for and return operand N, stop when reaching node STOP. */
317 find_operand (rtx pattern
, int n
, rtx stop
)
327 code
= GET_CODE (pattern
);
328 if ((code
== MATCH_SCRATCH
329 || code
== MATCH_OPERAND
330 || code
== MATCH_OPERATOR
331 || code
== MATCH_PARALLEL
)
332 && XINT (pattern
, 0) == n
)
335 fmt
= GET_RTX_FORMAT (code
);
336 len
= GET_RTX_LENGTH (code
);
337 for (i
= 0; i
< len
; i
++)
342 if ((r
= find_operand (XEXP (pattern
, i
), n
, stop
)) != NULL_RTX
)
347 if (! XVEC (pattern
, i
))
352 for (j
= 0; j
< XVECLEN (pattern
, i
); j
++)
353 if ((r
= find_operand (XVECEXP (pattern
, i
, j
), n
, stop
))
358 case 'i': case 'w': case '0': case 's':
369 /* Search for and return operand M, such that it has a matching
370 constraint for operand N. */
373 find_matching_operand (rtx pattern
, int n
)
380 code
= GET_CODE (pattern
);
381 if (code
== MATCH_OPERAND
382 && (XSTR (pattern
, 2)[0] == '0' + n
383 || (XSTR (pattern
, 2)[0] == '%'
384 && XSTR (pattern
, 2)[1] == '0' + n
)))
387 fmt
= GET_RTX_FORMAT (code
);
388 len
= GET_RTX_LENGTH (code
);
389 for (i
= 0; i
< len
; i
++)
394 if ((r
= find_matching_operand (XEXP (pattern
, i
), n
)))
399 if (! XVEC (pattern
, i
))
404 for (j
= 0; j
< XVECLEN (pattern
, i
); j
++)
405 if ((r
= find_matching_operand (XVECEXP (pattern
, i
, j
), n
)))
409 case 'i': case 'w': case '0': case 's':
421 /* Check for various errors in patterns. SET is nonnull for a destination,
422 and is the complete set pattern. SET_CODE is '=' for normal sets, and
423 '+' within a context that requires in-out constraints. */
426 validate_pattern (rtx pattern
, rtx insn
, rtx set
, int set_code
)
433 code
= GET_CODE (pattern
);
441 if (find_operand (insn
, XINT (pattern
, 0), pattern
) == pattern
)
442 error_with_line (pattern_lineno
,
443 "operand %i duplicated before defined",
449 const char *pred_name
= XSTR (pattern
, 1);
450 const struct pred_data
*pred
;
453 if (GET_CODE (insn
) == DEFINE_INSN
)
454 c_test
= XSTR (insn
, 2);
456 c_test
= XSTR (insn
, 1);
458 if (pred_name
[0] != 0)
460 pred
= lookup_predicate (pred_name
);
462 message_with_line (pattern_lineno
,
463 "warning: unknown predicate '%s'",
469 if (code
== MATCH_OPERAND
)
471 const char constraints0
= XSTR (pattern
, 2)[0];
473 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we
474 don't use the MATCH_OPERAND constraint, only the predicate.
475 This is confusing to folks doing new ports, so help them
476 not make the mistake. */
477 if (GET_CODE (insn
) == DEFINE_EXPAND
478 || GET_CODE (insn
) == DEFINE_SPLIT
479 || GET_CODE (insn
) == DEFINE_PEEPHOLE2
)
482 message_with_line (pattern_lineno
,
483 "warning: constraints not supported in %s",
484 rtx_name
[GET_CODE (insn
)]);
487 /* A MATCH_OPERAND that is a SET should have an output reload. */
488 else if (set
&& constraints0
)
492 if (constraints0
== '+')
494 /* If we've only got an output reload for this operand,
495 we'd better have a matching input operand. */
496 else if (constraints0
== '='
497 && find_matching_operand (insn
, XINT (pattern
, 0)))
500 error_with_line (pattern_lineno
,
501 "operand %d missing in-out reload",
504 else if (constraints0
!= '=' && constraints0
!= '+')
505 error_with_line (pattern_lineno
,
506 "operand %d missing output reload",
511 /* Allowing non-lvalues in destinations -- particularly CONST_INT --
512 while not likely to occur at runtime, results in less efficient
513 code from insn-recog.c. */
514 if (set
&& pred
&& pred
->allows_non_lvalue
)
515 message_with_line (pattern_lineno
,
516 "warning: destination operand %d "
520 /* A modeless MATCH_OPERAND can be handy when we can check for
521 multiple modes in the c_test. In most other cases, it is a
522 mistake. Only DEFINE_INSN is eligible, since SPLIT and
523 PEEP2 can FAIL within the output pattern. Exclude special
524 predicates, which check the mode themselves. Also exclude
525 predicates that allow only constants. Exclude the SET_DEST
526 of a call instruction, as that is a common idiom. */
528 if (GET_MODE (pattern
) == VOIDmode
529 && code
== MATCH_OPERAND
530 && GET_CODE (insn
) == DEFINE_INSN
533 && pred
->allows_non_const
534 && strstr (c_test
, "operands") == NULL
536 && GET_CODE (set
) == SET
537 && GET_CODE (SET_SRC (set
)) == CALL
))
538 message_with_line (pattern_lineno
,
539 "warning: operand %d missing mode?",
546 enum machine_mode dmode
, smode
;
549 dest
= SET_DEST (pattern
);
550 src
= SET_SRC (pattern
);
552 /* STRICT_LOW_PART is a wrapper. Its argument is the real
553 destination, and it's mode should match the source. */
554 if (GET_CODE (dest
) == STRICT_LOW_PART
)
555 dest
= XEXP (dest
, 0);
557 /* Find the referent for a DUP. */
559 if (GET_CODE (dest
) == MATCH_DUP
560 || GET_CODE (dest
) == MATCH_OP_DUP
561 || GET_CODE (dest
) == MATCH_PAR_DUP
)
562 dest
= find_operand (insn
, XINT (dest
, 0), NULL
);
564 if (GET_CODE (src
) == MATCH_DUP
565 || GET_CODE (src
) == MATCH_OP_DUP
566 || GET_CODE (src
) == MATCH_PAR_DUP
)
567 src
= find_operand (insn
, XINT (src
, 0), NULL
);
569 dmode
= GET_MODE (dest
);
570 smode
= GET_MODE (src
);
572 /* The mode of an ADDRESS_OPERAND is the mode of the memory
573 reference, not the mode of the address. */
574 if (GET_CODE (src
) == MATCH_OPERAND
575 && ! strcmp (XSTR (src
, 1), "address_operand"))
578 /* The operands of a SET must have the same mode unless one
580 else if (dmode
!= VOIDmode
&& smode
!= VOIDmode
&& dmode
!= smode
)
581 error_with_line (pattern_lineno
,
582 "mode mismatch in set: %smode vs %smode",
583 GET_MODE_NAME (dmode
), GET_MODE_NAME (smode
));
585 /* If only one of the operands is VOIDmode, and PC or CC0 is
586 not involved, it's probably a mistake. */
587 else if (dmode
!= smode
588 && GET_CODE (dest
) != PC
589 && GET_CODE (dest
) != CC0
590 && GET_CODE (src
) != PC
591 && GET_CODE (src
) != CC0
592 && !CONST_INT_P (src
)
593 && GET_CODE (src
) != CALL
)
596 which
= (dmode
== VOIDmode
? "destination" : "source");
597 message_with_line (pattern_lineno
,
598 "warning: %s missing a mode?", which
);
601 if (dest
!= SET_DEST (pattern
))
602 validate_pattern (dest
, insn
, pattern
, '=');
603 validate_pattern (SET_DEST (pattern
), insn
, pattern
, '=');
604 validate_pattern (SET_SRC (pattern
), insn
, NULL_RTX
, 0);
609 validate_pattern (SET_DEST (pattern
), insn
, pattern
, '=');
613 validate_pattern (XEXP (pattern
, 0), insn
, set
, set
? '+' : 0);
614 validate_pattern (XEXP (pattern
, 1), insn
, NULL_RTX
, 0);
615 validate_pattern (XEXP (pattern
, 2), insn
, NULL_RTX
, 0);
618 case STRICT_LOW_PART
:
619 validate_pattern (XEXP (pattern
, 0), insn
, set
, set
? '+' : 0);
623 if (GET_MODE (XEXP (pattern
, 0)) != VOIDmode
)
624 error_with_line (pattern_lineno
,
625 "operand to label_ref %smode not VOIDmode",
626 GET_MODE_NAME (GET_MODE (XEXP (pattern
, 0))));
633 fmt
= GET_RTX_FORMAT (code
);
634 len
= GET_RTX_LENGTH (code
);
635 for (i
= 0; i
< len
; i
++)
640 validate_pattern (XEXP (pattern
, i
), insn
, NULL_RTX
, 0);
644 for (j
= 0; j
< XVECLEN (pattern
, i
); j
++)
645 validate_pattern (XVECEXP (pattern
, i
, j
), insn
, NULL_RTX
, 0);
648 case 'i': case 'w': case '0': case 's':
657 /* Create a chain of nodes to verify that an rtl expression matches
660 LAST is a pointer to the listhead in the previous node in the chain (or
661 in the calling function, for the first node).
663 POSITION is the current position in the insn.
665 INSN_TYPE is the type of insn for which we are emitting code.
667 A pointer to the final node in the chain is returned. */
669 static struct decision
*
670 add_to_sequence (rtx pattern
, struct decision_head
*last
,
671 struct position
*pos
, enum routine_type insn_type
, int top
)
674 struct decision
*this_decision
, *sub
;
675 struct decision_test
*test
;
676 struct decision_test
**place
;
677 struct position
*subpos
, **subpos_ptr
;
681 enum machine_mode mode
;
682 enum position_type pos_type
;
684 if (pos
->depth
> max_depth
)
685 max_depth
= pos
->depth
;
687 sub
= this_decision
= new_decision (pos
, last
);
688 place
= &this_decision
->tests
;
691 mode
= GET_MODE (pattern
);
692 code
= GET_CODE (pattern
);
697 /* Toplevel peephole pattern. */
698 if (insn_type
== PEEPHOLE2
&& top
)
702 /* Check we have sufficient insns. This avoids complications
703 because we then know peep2_next_insn never fails. */
704 num_insns
= XVECLEN (pattern
, 0);
707 test
= new_decision_test (DT_num_insns
, &place
);
708 test
->u
.num_insns
= num_insns
;
709 last
= &sub
->success
;
713 /* We don't need the node we just created -- unlink it. */
714 last
->first
= last
->last
= NULL
;
717 subpos_ptr
= &peep2_insn_pos_list
;
718 for (i
= 0; i
< (size_t) XVECLEN (pattern
, 0); i
++)
720 subpos
= next_position (subpos_ptr
, &root_pos
,
722 sub
= add_to_sequence (XVECEXP (pattern
, 0, i
),
723 last
, subpos
, insn_type
, 0);
724 last
= &sub
->success
;
725 subpos_ptr
= &subpos
->next
;
730 /* Else nothing special. */
734 /* The explicit patterns within a match_parallel enforce a minimum
735 length on the vector. The match_parallel predicate may allow
736 for more elements. We do need to check for this minimum here
737 or the code generated to match the internals may reference data
738 beyond the end of the vector. */
739 test
= new_decision_test (DT_veclen_ge
, &place
);
740 test
->u
.veclen
= XVECLEN (pattern
, 2);
747 RTX_CODE was_code
= code
;
748 const char *pred_name
;
749 bool allows_const_int
= true;
751 if (code
== MATCH_SCRATCH
)
753 pred_name
= "scratch_operand";
758 pred_name
= XSTR (pattern
, 1);
759 if (code
== MATCH_PARALLEL
)
765 if (pred_name
[0] != 0)
767 const struct pred_data
*pred
;
769 test
= new_decision_test (DT_pred
, &place
);
770 test
->u
.pred
.name
= pred_name
;
771 test
->u
.pred
.mode
= mode
;
773 /* See if we know about this predicate.
774 If we do, remember it for use below.
776 We can optimize the generated code a little if either
777 (a) the predicate only accepts one code, or (b) the
778 predicate does not allow CONST_INT, in which case it
779 can match only if the modes match. */
780 pred
= lookup_predicate (pred_name
);
783 test
->u
.pred
.data
= pred
;
784 allows_const_int
= pred
->codes
[CONST_INT
];
785 if (was_code
== MATCH_PARALLEL
786 && pred
->singleton
!= PARALLEL
)
787 message_with_line (pattern_lineno
,
788 "predicate '%s' used in match_parallel "
789 "does not allow only PARALLEL", pred
->name
);
791 code
= pred
->singleton
;
794 message_with_line (pattern_lineno
,
795 "warning: unknown predicate '%s' in '%s' expression",
796 pred_name
, GET_RTX_NAME (was_code
));
799 /* Can't enforce a mode if we allow const_int. */
800 if (allows_const_int
)
803 /* Accept the operand, i.e. record it in `operands'. */
804 test
= new_decision_test (DT_accept_op
, &place
);
805 test
->u
.opno
= XINT (pattern
, 0);
807 if (was_code
== MATCH_OPERATOR
|| was_code
== MATCH_PARALLEL
)
809 if (was_code
== MATCH_OPERATOR
)
812 subpos_ptr
= &pos
->xexps
;
816 pos_type
= POS_XVECEXP0
;
817 subpos_ptr
= &pos
->xvecexp0s
;
819 for (i
= 0; i
< (size_t) XVECLEN (pattern
, 2); i
++)
821 subpos
= next_position (subpos_ptr
, pos
, pos_type
, i
);
822 sub
= add_to_sequence (XVECEXP (pattern
, 2, i
),
823 &sub
->success
, subpos
, insn_type
, 0);
824 subpos_ptr
= &subpos
->next
;
833 test
= new_decision_test (DT_dup
, &place
);
834 test
->u
.dup
= XINT (pattern
, 0);
836 test
= new_decision_test (DT_accept_op
, &place
);
837 test
->u
.opno
= XINT (pattern
, 0);
839 subpos_ptr
= &pos
->xexps
;
840 for (i
= 0; i
< (size_t) XVECLEN (pattern
, 1); i
++)
842 subpos
= next_position (subpos_ptr
, pos
, POS_XEXP
, i
);
843 sub
= add_to_sequence (XVECEXP (pattern
, 1, i
),
844 &sub
->success
, subpos
, insn_type
, 0);
845 subpos_ptr
= &subpos
->next
;
853 test
= new_decision_test (DT_dup
, &place
);
854 test
->u
.dup
= XINT (pattern
, 0);
858 pattern
= XEXP (pattern
, 0);
865 fmt
= GET_RTX_FORMAT (code
);
866 len
= GET_RTX_LENGTH (code
);
868 /* Do tests against the current node first. */
869 for (i
= 0; i
< (size_t) len
; i
++)
877 test
= new_decision_test (DT_elt_zero_int
, &place
);
878 test
->u
.intval
= XINT (pattern
, i
);
882 test
= new_decision_test (DT_elt_one_int
, &place
);
883 test
->u
.intval
= XINT (pattern
, i
);
886 else if (fmt
[i
] == 'w')
888 /* If this value actually fits in an int, we can use a switch
889 statement here, so indicate that. */
890 enum decision_type type
891 = ((int) XWINT (pattern
, i
) == XWINT (pattern
, i
))
892 ? DT_elt_zero_wide_safe
: DT_elt_zero_wide
;
896 test
= new_decision_test (type
, &place
);
897 test
->u
.intval
= XWINT (pattern
, i
);
899 else if (fmt
[i
] == 'E')
903 test
= new_decision_test (DT_veclen
, &place
);
904 test
->u
.veclen
= XVECLEN (pattern
, i
);
908 /* Now test our sub-patterns. */
909 subpos_ptr
= &pos
->xexps
;
910 for (i
= 0; i
< (size_t) len
; i
++)
912 subpos
= next_position (subpos_ptr
, pos
, POS_XEXP
, i
);
916 sub
= add_to_sequence (XEXP (pattern
, i
), &sub
->success
,
917 subpos
, insn_type
, 0);
922 struct position
*subpos2
, **subpos2_ptr
;
925 subpos2_ptr
= &pos
->xvecexp0s
;
926 for (j
= 0; j
< XVECLEN (pattern
, i
); j
++)
928 subpos2
= next_position (subpos2_ptr
, pos
, POS_XVECEXP0
, j
);
929 sub
= add_to_sequence (XVECEXP (pattern
, i
, j
),
930 &sub
->success
, subpos2
, insn_type
, 0);
931 subpos2_ptr
= &subpos2
->next
;
945 subpos_ptr
= &subpos
->next
;
949 /* Insert nodes testing mode and code, if they're still relevant,
950 before any of the nodes we may have added above. */
953 place
= &this_decision
->tests
;
954 test
= new_decision_test (DT_code
, &place
);
958 if (mode
!= VOIDmode
)
960 place
= &this_decision
->tests
;
961 test
= new_decision_test (DT_mode
, &place
);
965 /* If we didn't insert any tests or accept nodes, hork. */
966 gcc_assert (this_decision
->tests
);
972 /* A subroutine of maybe_both_true; examines only one test.
973 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
976 maybe_both_true_2 (struct decision_test
*d1
, struct decision_test
*d2
)
978 if (d1
->type
== d2
->type
)
983 if (d1
->u
.num_insns
== d2
->u
.num_insns
)
989 return d1
->u
.mode
== d2
->u
.mode
;
992 return d1
->u
.code
== d2
->u
.code
;
995 return d1
->u
.veclen
== d2
->u
.veclen
;
997 case DT_elt_zero_int
:
999 case DT_elt_zero_wide
:
1000 case DT_elt_zero_wide_safe
:
1001 return d1
->u
.intval
== d2
->u
.intval
;
1008 /* If either has a predicate that we know something about, set
1009 things up so that D1 is the one that always has a known
1010 predicate. Then see if they have any codes in common. */
1012 if (d1
->type
== DT_pred
|| d2
->type
== DT_pred
)
1014 if (d2
->type
== DT_pred
)
1016 struct decision_test
*tmp
;
1017 tmp
= d1
, d1
= d2
, d2
= tmp
;
1020 /* If D2 tests a mode, see if it matches D1. */
1021 if (d1
->u
.pred
.mode
!= VOIDmode
)
1023 if (d2
->type
== DT_mode
)
1025 if (d1
->u
.pred
.mode
!= d2
->u
.mode
1026 /* The mode of an address_operand predicate is the
1027 mode of the memory, not the operand. It can only
1028 be used for testing the predicate, so we must
1030 && strcmp (d1
->u
.pred
.name
, "address_operand") != 0)
1033 /* Don't check two predicate modes here, because if both predicates
1034 accept CONST_INT, then both can still be true even if the modes
1035 are different. If they don't accept CONST_INT, there will be a
1036 separate DT_mode that will make maybe_both_true_1 return 0. */
1039 if (d1
->u
.pred
.data
)
1041 /* If D2 tests a code, see if it is in the list of valid
1042 codes for D1's predicate. */
1043 if (d2
->type
== DT_code
)
1045 if (!d1
->u
.pred
.data
->codes
[d2
->u
.code
])
1049 /* Otherwise see if the predicates have any codes in common. */
1050 else if (d2
->type
== DT_pred
&& d2
->u
.pred
.data
)
1052 bool common
= false;
1055 for (c
= 0; c
< NUM_RTX_CODE
; c
++)
1056 if (d1
->u
.pred
.data
->codes
[c
] && d2
->u
.pred
.data
->codes
[c
])
1068 /* Tests vs veclen may be known when strict equality is involved. */
1069 if (d1
->type
== DT_veclen
&& d2
->type
== DT_veclen_ge
)
1070 return d1
->u
.veclen
>= d2
->u
.veclen
;
1071 if (d1
->type
== DT_veclen_ge
&& d2
->type
== DT_veclen
)
1072 return d2
->u
.veclen
>= d1
->u
.veclen
;
1077 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1078 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1081 maybe_both_true_1 (struct decision_test
*d1
, struct decision_test
*d2
)
1083 struct decision_test
*t1
, *t2
;
1085 /* A match_operand with no predicate can match anything. Recognize
1086 this by the existence of a lone DT_accept_op test. */
1087 if (d1
->type
== DT_accept_op
|| d2
->type
== DT_accept_op
)
1090 /* Eliminate pairs of tests while they can exactly match. */
1091 while (d1
&& d2
&& d1
->type
== d2
->type
)
1093 if (maybe_both_true_2 (d1
, d2
) == 0)
1095 d1
= d1
->next
, d2
= d2
->next
;
1098 /* After that, consider all pairs. */
1099 for (t1
= d1
; t1
; t1
= t1
->next
)
1100 for (t2
= d2
; t2
; t2
= t2
->next
)
1101 if (maybe_both_true_2 (t1
, t2
) == 0)
1107 /* Return 0 if we can prove that there is no RTL that can match both
1108 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
1109 can match both or just that we couldn't prove there wasn't such an RTL).
1111 TOPLEVEL is nonzero if we are to only look at the top level and not
1112 recursively descend. */
1115 maybe_both_true (struct decision
*d1
, struct decision
*d2
,
1118 struct decision
*p1
, *p2
;
1121 /* Don't compare strings on the different positions in insn. Doing so
1122 is incorrect and results in false matches from constructs like
1124 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1125 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1127 [(set (match_operand:HI "register_operand" "r")
1128 (match_operand:HI "register_operand" "r"))]
1130 If we are presented with such, we are recursing through the remainder
1131 of a node's success nodes (from the loop at the end of this function).
1132 Skip forward until we come to a position that matches.
1134 Due to the way positions are constructed, we know that iterating
1135 forward from the lexically lower position will run into the lexically
1136 higher position and not the other way around. This saves a bit
1139 cmp
= compare_positions (d1
->position
, d2
->position
);
1142 gcc_assert (!toplevel
);
1144 /* If the d2->position was lexically lower, swap. */
1146 p1
= d1
, d1
= d2
, d2
= p1
;
1148 if (d1
->success
.first
== 0)
1150 for (p1
= d1
->success
.first
; p1
; p1
= p1
->next
)
1151 if (maybe_both_true (p1
, d2
, 0))
1157 /* Test the current level. */
1158 cmp
= maybe_both_true_1 (d1
->tests
, d2
->tests
);
1162 /* We can't prove that D1 and D2 cannot both be true. If we are only
1163 to check the top level, return 1. Otherwise, see if we can prove
1164 that all choices in both successors are mutually exclusive. If
1165 either does not have any successors, we can't prove they can't both
1168 if (toplevel
|| d1
->success
.first
== 0 || d2
->success
.first
== 0)
1171 for (p1
= d1
->success
.first
; p1
; p1
= p1
->next
)
1172 for (p2
= d2
->success
.first
; p2
; p2
= p2
->next
)
1173 if (maybe_both_true (p1
, p2
, 0))
1179 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
1182 nodes_identical_1 (struct decision_test
*d1
, struct decision_test
*d2
)
1187 return d1
->u
.num_insns
== d2
->u
.num_insns
;
1190 return d1
->u
.mode
== d2
->u
.mode
;
1193 return d1
->u
.code
== d2
->u
.code
;
1196 return (d1
->u
.pred
.mode
== d2
->u
.pred
.mode
1197 && strcmp (d1
->u
.pred
.name
, d2
->u
.pred
.name
) == 0);
1200 return strcmp (d1
->u
.c_test
, d2
->u
.c_test
) == 0;
1204 return d1
->u
.veclen
== d2
->u
.veclen
;
1207 return d1
->u
.dup
== d2
->u
.dup
;
1209 case DT_elt_zero_int
:
1210 case DT_elt_one_int
:
1211 case DT_elt_zero_wide
:
1212 case DT_elt_zero_wide_safe
:
1213 return d1
->u
.intval
== d2
->u
.intval
;
1216 return d1
->u
.opno
== d2
->u
.opno
;
1218 case DT_accept_insn
:
1219 /* Differences will be handled in merge_accept_insn. */
1227 /* True iff the two nodes are identical (on one level only). Due
1228 to the way these lists are constructed, we shouldn't have to
1229 consider different orderings on the tests. */
1232 nodes_identical (struct decision
*d1
, struct decision
*d2
)
1234 struct decision_test
*t1
, *t2
;
1236 for (t1
= d1
->tests
, t2
= d2
->tests
; t1
&& t2
; t1
= t1
->next
, t2
= t2
->next
)
1238 if (t1
->type
!= t2
->type
)
1240 if (! nodes_identical_1 (t1
, t2
))
1244 /* For success, they should now both be null. */
1248 /* Check that their subnodes are at the same position, as any one set
1249 of sibling decisions must be at the same position. Allowing this
1250 requires complications to find_afterward and when change_state is
1252 if (d1
->success
.first
1253 && d2
->success
.first
1254 && d1
->success
.first
->position
!= d2
->success
.first
->position
)
1260 /* A subroutine of merge_trees; given two nodes that have been declared
1261 identical, cope with two insn accept states. If they differ in the
1262 number of clobbers, then the conflict was created by make_insn_sequence
1263 and we can drop the with-clobbers version on the floor. If both
1264 nodes have no additional clobbers, we have found an ambiguity in the
1265 source machine description. */
1268 merge_accept_insn (struct decision
*oldd
, struct decision
*addd
)
1270 struct decision_test
*old
, *add
;
1272 for (old
= oldd
->tests
; old
; old
= old
->next
)
1273 if (old
->type
== DT_accept_insn
)
1278 for (add
= addd
->tests
; add
; add
= add
->next
)
1279 if (add
->type
== DT_accept_insn
)
1284 /* If one node is for a normal insn and the second is for the base
1285 insn with clobbers stripped off, the second node should be ignored. */
1287 if (old
->u
.insn
.num_clobbers_to_add
== 0
1288 && add
->u
.insn
.num_clobbers_to_add
> 0)
1290 /* Nothing to do here. */
1292 else if (old
->u
.insn
.num_clobbers_to_add
> 0
1293 && add
->u
.insn
.num_clobbers_to_add
== 0)
1295 /* In this case, replace OLD with ADD. */
1296 old
->u
.insn
= add
->u
.insn
;
1300 error_with_line (add
->u
.insn
.lineno
, "`%s' matches `%s'",
1301 get_insn_name (add
->u
.insn
.code_number
),
1302 get_insn_name (old
->u
.insn
.code_number
));
1303 message_with_line (old
->u
.insn
.lineno
, "previous definition of `%s'",
1304 get_insn_name (old
->u
.insn
.code_number
));
1308 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1311 merge_trees (struct decision_head
*oldh
, struct decision_head
*addh
)
1313 struct decision
*next
, *add
;
1315 if (addh
->first
== 0)
1317 if (oldh
->first
== 0)
1323 /* Trying to merge bits at different positions isn't possible. */
1324 gcc_assert (oldh
->first
->position
== addh
->first
->position
);
1326 for (add
= addh
->first
; add
; add
= next
)
1328 struct decision
*old
, *insert_before
= NULL
;
1332 /* The semantics of pattern matching state that the tests are
1333 done in the order given in the MD file so that if an insn
1334 matches two patterns, the first one will be used. However,
1335 in practice, most, if not all, patterns are unambiguous so
1336 that their order is independent. In that case, we can merge
1337 identical tests and group all similar modes and codes together.
1339 Scan starting from the end of OLDH until we reach a point
1340 where we reach the head of the list or where we pass a
1341 pattern that could also be true if NEW is true. If we find
1342 an identical pattern, we can merge them. Also, record the
1343 last node that tests the same code and mode and the last one
1344 that tests just the same mode.
1346 If we have no match, place NEW after the closest match we found. */
1348 for (old
= oldh
->last
; old
; old
= old
->prev
)
1350 if (nodes_identical (old
, add
))
1352 merge_accept_insn (old
, add
);
1353 merge_trees (&old
->success
, &add
->success
);
1357 if (maybe_both_true (old
, add
, 0))
1360 /* Insert the nodes in DT test type order, which is roughly
1361 how expensive/important the test is. Given that the tests
1362 are also ordered within the list, examining the first is
1364 if ((int) add
->tests
->type
< (int) old
->tests
->type
)
1365 insert_before
= old
;
1368 if (insert_before
== NULL
)
1371 add
->prev
= oldh
->last
;
1372 oldh
->last
->next
= add
;
1377 if ((add
->prev
= insert_before
->prev
) != NULL
)
1378 add
->prev
->next
= add
;
1381 add
->next
= insert_before
;
1382 insert_before
->prev
= add
;
1389 /* Walk the tree looking for sub-nodes that perform common tests.
1390 Factor out the common test into a new node. This enables us
1391 (depending on the test type) to emit switch statements later. */
1394 factor_tests (struct decision_head
*head
)
1396 struct decision
*first
, *next
;
1398 for (first
= head
->first
; first
&& first
->next
; first
= next
)
1400 enum decision_type type
;
1401 struct decision
*new_dec
, *old_last
;
1403 type
= first
->tests
->type
;
1406 /* Want at least two compatible sequential nodes. */
1407 if (next
->tests
->type
!= type
)
1410 /* Don't want all node types, just those we can turn into
1411 switch statements. */
1414 && type
!= DT_veclen
1415 && type
!= DT_elt_zero_int
1416 && type
!= DT_elt_one_int
1417 && type
!= DT_elt_zero_wide_safe
)
1420 /* If we'd been performing more than one test, create a new node
1421 below our first test. */
1422 if (first
->tests
->next
!= NULL
)
1424 new_dec
= new_decision (first
->position
, &first
->success
);
1425 new_dec
->tests
= first
->tests
->next
;
1426 first
->tests
->next
= NULL
;
1429 /* Crop the node tree off after our first test. */
1431 old_last
= head
->last
;
1434 /* For each compatible test, adjust to perform only one test in
1435 the top level node, then merge the node back into the tree. */
1438 struct decision_head h
;
1440 if (next
->tests
->next
!= NULL
)
1442 new_dec
= new_decision (next
->position
, &next
->success
);
1443 new_dec
->tests
= next
->tests
->next
;
1444 next
->tests
->next
= NULL
;
1448 new_dec
->next
= NULL
;
1449 h
.first
= h
.last
= new_dec
;
1451 merge_trees (head
, &h
);
1453 while (next
&& next
->tests
->type
== type
);
1455 /* After we run out of compatible tests, graft the remaining nodes
1456 back onto the tree. */
1459 next
->prev
= head
->last
;
1460 head
->last
->next
= next
;
1461 head
->last
= old_last
;
1466 for (first
= head
->first
; first
; first
= first
->next
)
1467 factor_tests (&first
->success
);
1470 /* After factoring, try to simplify the tests on any one node.
1471 Tests that are useful for switch statements are recognizable
1472 by having only a single test on a node -- we'll be manipulating
1473 nodes with multiple tests:
1475 If we have mode tests or code tests that are redundant with
1476 predicates, remove them. */
1479 simplify_tests (struct decision_head
*head
)
1481 struct decision
*tree
;
1483 for (tree
= head
->first
; tree
; tree
= tree
->next
)
1485 struct decision_test
*a
, *b
;
1492 /* Find a predicate node. */
1493 while (b
&& b
->type
!= DT_pred
)
1497 /* Due to how these tests are constructed, we don't even need
1498 to check that the mode and code are compatible -- they were
1499 generated from the predicate in the first place. */
1500 while (a
->type
== DT_mode
|| a
->type
== DT_code
)
1507 for (tree
= head
->first
; tree
; tree
= tree
->next
)
1508 simplify_tests (&tree
->success
);
1511 /* Count the number of subnodes of HEAD. If the number is high enough,
1512 make the first node in HEAD start a separate subroutine in the C code
1513 that is generated. */
1516 break_out_subroutines (struct decision_head
*head
, int initial
)
1519 struct decision
*sub
;
1521 for (sub
= head
->first
; sub
; sub
= sub
->next
)
1522 size
+= 1 + break_out_subroutines (&sub
->success
, 0);
1524 if (size
> SUBROUTINE_THRESHOLD
&& ! initial
)
1526 head
->first
->subroutine_number
= ++next_subroutine_number
;
1532 /* For each node p, find the next alternative that might be true
1536 find_afterward (struct decision_head
*head
, struct decision
*real_afterward
)
1538 struct decision
*p
, *q
, *afterward
;
1540 /* We can't propagate alternatives across subroutine boundaries.
1541 This is not incorrect, merely a minor optimization loss. */
1544 afterward
= (p
->subroutine_number
> 0 ? NULL
: real_afterward
);
1546 for ( ; p
; p
= p
->next
)
1548 /* Find the next node that might be true if this one fails. */
1549 for (q
= p
->next
; q
; q
= q
->next
)
1550 if (maybe_both_true (p
, q
, 1))
1553 /* If we reached the end of the list without finding one,
1554 use the incoming afterward position. */
1563 for (p
= head
->first
; p
; p
= p
->next
)
1564 if (p
->success
.first
)
1565 find_afterward (&p
->success
, p
->afterward
);
1567 /* When we are generating a subroutine, record the real afterward
1568 position in the first node where write_tree can find it, and we
1569 can do the right thing at the subroutine call site. */
1571 if (p
->subroutine_number
> 0)
1572 p
->afterward
= real_afterward
;
1575 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1576 actions are necessary to move to NEWPOS. If we fail to move to the
1577 new state, branch to node AFTERWARD if nonzero, otherwise return.
1579 Failure to move to the new state can only occur if we are trying to
1580 match multiple insns and we try to step past the end of the stream. */
1583 change_state (struct position
*oldpos
, struct position
*newpos
,
1586 while (oldpos
->depth
> newpos
->depth
)
1587 oldpos
= oldpos
->base
;
1589 if (oldpos
!= newpos
)
1590 switch (newpos
->type
)
1592 case POS_PEEP2_INSN
:
1593 printf ("%stem = peep2_next_insn (%d);\n", indent
, newpos
->arg
);
1594 printf ("%sx%d = PATTERN (tem);\n", indent
, newpos
->depth
);
1598 change_state (oldpos
, newpos
->base
, indent
);
1599 printf ("%sx%d = XEXP (x%d, %d);\n",
1600 indent
, newpos
->depth
, newpos
->depth
- 1, newpos
->arg
);
1604 change_state (oldpos
, newpos
->base
, indent
);
1605 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1606 indent
, newpos
->depth
, newpos
->depth
- 1, newpos
->arg
);
1611 /* Print the enumerator constant for CODE -- the upcase version of
1615 print_code (enum rtx_code code
)
1618 for (p
= GET_RTX_NAME (code
); *p
; p
++)
1619 putchar (TOUPPER (*p
));
1622 /* Emit code to cross an afterward link -- change state and branch. */
1625 write_afterward (struct decision
*start
, struct decision
*afterward
,
1628 if (!afterward
|| start
->subroutine_number
> 0)
1629 printf("%sgoto ret0;\n", indent
);
1632 change_state (start
->position
, afterward
->position
, indent
);
1633 printf ("%sgoto L%d;\n", indent
, afterward
->number
);
1637 /* Emit a HOST_WIDE_INT as an integer constant expression. We need to take
1638 special care to avoid "decimal constant is so large that it is unsigned"
1639 warnings in the resulting code. */
1642 print_host_wide_int (HOST_WIDE_INT val
)
1644 HOST_WIDE_INT min
= (unsigned HOST_WIDE_INT
)1 << (HOST_BITS_PER_WIDE_INT
-1);
1646 printf ("(" HOST_WIDE_INT_PRINT_DEC_C
"-1)", val
+ 1);
1648 printf (HOST_WIDE_INT_PRINT_DEC_C
, val
);
1651 /* Emit a switch statement, if possible, for an initial sequence of
1652 nodes at START. Return the first node yet untested. */
1654 static struct decision
*
1655 write_switch (struct decision
*start
, int depth
)
1657 struct decision
*p
= start
;
1658 enum decision_type type
= p
->tests
->type
;
1659 struct decision
*needs_label
= NULL
;
1661 /* If we have two or more nodes in sequence that test the same one
1662 thing, we may be able to use a switch statement. */
1666 || p
->next
->tests
->type
!= type
1667 || p
->next
->tests
->next
1668 || nodes_identical_1 (p
->tests
, p
->next
->tests
))
1671 /* DT_code is special in that we can do interesting things with
1672 known predicates at the same time. */
1673 if (type
== DT_code
)
1675 char codemap
[NUM_RTX_CODE
];
1676 struct decision
*ret
;
1679 memset (codemap
, 0, sizeof(codemap
));
1681 printf (" switch (GET_CODE (x%d))\n {\n", depth
);
1682 code
= p
->tests
->u
.code
;
1685 if (p
!= start
&& p
->need_label
&& needs_label
== NULL
)
1690 printf (":\n goto L%d;\n", p
->success
.first
->number
);
1691 p
->success
.first
->need_label
= 1;
1698 && p
->tests
->type
== DT_code
1699 && ! codemap
[code
= p
->tests
->u
.code
]);
1701 /* If P is testing a predicate that we know about and we haven't
1702 seen any of the codes that are valid for the predicate, we can
1703 write a series of "case" statement, one for each possible code.
1704 Since we are already in a switch, these redundant tests are very
1705 cheap and will reduce the number of predicates called. */
1707 /* Note that while we write out cases for these predicates here,
1708 we don't actually write the test here, as it gets kinda messy.
1709 It is trivial to leave this to later by telling our caller that
1710 we only processed the CODE tests. */
1711 if (needs_label
!= NULL
)
1716 while (p
&& p
->tests
->type
== DT_pred
&& p
->tests
->u
.pred
.data
)
1718 const struct pred_data
*data
= p
->tests
->u
.pred
.data
;
1721 for (c
= 0; c
< NUM_RTX_CODE
; c
++)
1722 if (codemap
[c
] && data
->codes
[c
])
1725 for (c
= 0; c
< NUM_RTX_CODE
; c
++)
1728 fputs (" case ", stdout
);
1729 print_code ((enum rtx_code
) c
);
1730 fputs (":\n", stdout
);
1734 printf (" goto L%d;\n", p
->number
);
1740 /* Make the default case skip the predicates we managed to match. */
1742 printf (" default:\n");
1747 printf (" goto L%d;\n", p
->number
);
1751 write_afterward (start
, start
->afterward
, " ");
1754 printf (" break;\n");
1759 else if (type
== DT_mode
1760 || type
== DT_veclen
1761 || type
== DT_elt_zero_int
1762 || type
== DT_elt_one_int
1763 || type
== DT_elt_zero_wide_safe
)
1765 const char *indent
= "";
1767 /* We cast switch parameter to integer, so we must ensure that the value
1769 if (type
== DT_elt_zero_wide_safe
)
1772 printf(" if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n", depth
, depth
);
1774 printf ("%s switch (", indent
);
1778 printf ("GET_MODE (x%d)", depth
);
1781 printf ("XVECLEN (x%d, 0)", depth
);
1783 case DT_elt_zero_int
:
1784 printf ("XINT (x%d, 0)", depth
);
1786 case DT_elt_one_int
:
1787 printf ("XINT (x%d, 1)", depth
);
1789 case DT_elt_zero_wide_safe
:
1790 /* Convert result of XWINT to int for portability since some C
1791 compilers won't do it and some will. */
1792 printf ("(int) XWINT (x%d, 0)", depth
);
1797 printf (")\n%s {\n", indent
);
1801 /* Merge trees will not unify identical nodes if their
1802 sub-nodes are at different levels. Thus we must check
1803 for duplicate cases. */
1805 for (q
= start
; q
!= p
; q
= q
->next
)
1806 if (nodes_identical_1 (p
->tests
, q
->tests
))
1809 if (p
!= start
&& p
->need_label
&& needs_label
== NULL
)
1812 printf ("%s case ", indent
);
1816 printf ("%smode", GET_MODE_NAME (p
->tests
->u
.mode
));
1819 printf ("%d", p
->tests
->u
.veclen
);
1821 case DT_elt_zero_int
:
1822 case DT_elt_one_int
:
1823 case DT_elt_zero_wide
:
1824 case DT_elt_zero_wide_safe
:
1825 print_host_wide_int (p
->tests
->u
.intval
);
1830 printf (":\n%s goto L%d;\n", indent
, p
->success
.first
->number
);
1831 p
->success
.first
->need_label
= 1;
1835 while (p
&& p
->tests
->type
== type
&& !p
->tests
->next
);
1838 printf ("%s default:\n%s break;\n%s }\n",
1839 indent
, indent
, indent
);
1841 return needs_label
!= NULL
? needs_label
: p
;
1845 /* None of the other tests are amenable. */
1850 /* Emit code for one test. */
1853 write_cond (struct decision_test
*p
, int depth
,
1854 enum routine_type subroutine_type
)
1859 printf ("peep2_current_count >= %d", p
->u
.num_insns
);
1863 printf ("GET_MODE (x%d) == %smode", depth
, GET_MODE_NAME (p
->u
.mode
));
1867 printf ("GET_CODE (x%d) == ", depth
);
1868 print_code (p
->u
.code
);
1872 printf ("XVECLEN (x%d, 0) == %d", depth
, p
->u
.veclen
);
1875 case DT_elt_zero_int
:
1876 printf ("XINT (x%d, 0) == %d", depth
, (int) p
->u
.intval
);
1879 case DT_elt_one_int
:
1880 printf ("XINT (x%d, 1) == %d", depth
, (int) p
->u
.intval
);
1883 case DT_elt_zero_wide
:
1884 case DT_elt_zero_wide_safe
:
1885 printf ("XWINT (x%d, 0) == ", depth
);
1886 print_host_wide_int (p
->u
.intval
);
1890 printf ("x%d == const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
1891 depth
, (int) p
->u
.intval
);
1895 printf ("XVECLEN (x%d, 0) >= %d", depth
, p
->u
.veclen
);
1899 printf ("rtx_equal_p (x%d, operands[%d])", depth
, p
->u
.dup
);
1903 printf ("%s (x%d, %smode)", p
->u
.pred
.name
, depth
,
1904 GET_MODE_NAME (p
->u
.pred
.mode
));
1908 print_c_condition (p
->u
.c_test
);
1911 case DT_accept_insn
:
1912 gcc_assert (subroutine_type
== RECOG
);
1913 gcc_assert (p
->u
.insn
.num_clobbers_to_add
);
1914 printf ("pnum_clobbers != NULL");
1922 /* Emit code for one action. The previous tests have succeeded;
1923 TEST is the last of the chain. In the normal case we simply
1924 perform a state change. For the `accept' tests we must do more work. */
1927 write_action (struct decision
*p
, struct decision_test
*test
,
1928 int depth
, int uncond
, struct decision
*success
,
1929 enum routine_type subroutine_type
)
1936 else if (test
->type
== DT_accept_op
|| test
->type
== DT_accept_insn
)
1938 fputs (" {\n", stdout
);
1945 if (test
->type
== DT_accept_op
)
1947 printf("%soperands[%d] = x%d;\n", indent
, test
->u
.opno
, depth
);
1949 /* Only allow DT_accept_insn to follow. */
1953 gcc_assert (test
->type
== DT_accept_insn
);
1957 /* Sanity check that we're now at the end of the list of tests. */
1958 gcc_assert (!test
->next
);
1960 if (test
->type
== DT_accept_insn
)
1962 switch (subroutine_type
)
1965 if (test
->u
.insn
.num_clobbers_to_add
!= 0)
1966 printf ("%s*pnum_clobbers = %d;\n",
1967 indent
, test
->u
.insn
.num_clobbers_to_add
);
1968 printf ("%sreturn %d; /* %s */\n", indent
,
1969 test
->u
.insn
.code_number
,
1970 get_insn_name (test
->u
.insn
.code_number
));
1974 printf ("%sreturn gen_split_%d (insn, operands);\n",
1975 indent
, test
->u
.insn
.code_number
);
1981 struct position
*pos
;
1983 for (pos
= p
->position
; pos
; pos
= pos
->base
)
1984 if (pos
->type
== POS_PEEP2_INSN
)
1986 match_len
= pos
->arg
;
1989 printf ("%s*_pmatch_len = %d;\n", indent
, match_len
);
1990 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1991 indent
, test
->u
.insn
.code_number
);
1992 printf ("%sif (tem != 0)\n%s return tem;\n", indent
, indent
);
2002 printf("%sgoto L%d;\n", indent
, success
->number
);
2003 success
->need_label
= 1;
2007 fputs (" }\n", stdout
);
2010 /* Return 1 if the test is always true and has no fallthru path. Return -1
2011 if the test does have a fallthru path, but requires that the condition be
2012 terminated. Otherwise return 0 for a normal test. */
2013 /* ??? is_unconditional is a stupid name for a tri-state function. */
2016 is_unconditional (struct decision_test
*t
, enum routine_type subroutine_type
)
2018 if (t
->type
== DT_accept_op
)
2021 if (t
->type
== DT_accept_insn
)
2023 switch (subroutine_type
)
2026 return (t
->u
.insn
.num_clobbers_to_add
== 0);
2039 /* Emit code for one node -- the conditional and the accompanying action.
2040 Return true if there is no fallthru path. */
2043 write_node (struct decision
*p
, int depth
,
2044 enum routine_type subroutine_type
)
2046 struct decision_test
*test
, *last_test
;
2049 /* Scan the tests and simplify comparisons against small
2051 for (test
= p
->tests
; test
; test
= test
->next
)
2053 if (test
->type
== DT_code
2054 && test
->u
.code
== CONST_INT
2056 && test
->next
->type
== DT_elt_zero_wide_safe
2057 && -MAX_SAVED_CONST_INT
<= test
->next
->u
.intval
2058 && test
->next
->u
.intval
<= MAX_SAVED_CONST_INT
)
2060 test
->type
= DT_const_int
;
2061 test
->u
.intval
= test
->next
->u
.intval
;
2062 test
->next
= test
->next
->next
;
2066 last_test
= test
= p
->tests
;
2067 uncond
= is_unconditional (test
, subroutine_type
);
2071 write_cond (test
, depth
, subroutine_type
);
2073 while ((test
= test
->next
) != NULL
)
2076 if (is_unconditional (test
, subroutine_type
))
2080 write_cond (test
, depth
, subroutine_type
);
2086 write_action (p
, last_test
, depth
, uncond
, p
->success
.first
, subroutine_type
);
2091 /* Emit code for all of the sibling nodes of HEAD. */
2094 write_tree_1 (struct decision_head
*head
, int depth
,
2095 enum routine_type subroutine_type
)
2097 struct decision
*p
, *next
;
2100 for (p
= head
->first
; p
; p
= next
)
2102 /* The label for the first element was printed in write_tree. */
2103 if (p
!= head
->first
&& p
->need_label
)
2104 OUTPUT_LABEL (" ", p
->number
);
2106 /* Attempt to write a switch statement for a whole sequence. */
2107 next
= write_switch (p
, depth
);
2112 /* Failed -- fall back and write one node. */
2113 uncond
= write_node (p
, depth
, subroutine_type
);
2118 /* Finished with this chain. Close a fallthru path by branching
2119 to the afterward node. */
2121 write_afterward (head
->last
, head
->last
->afterward
, " ");
2124 /* Write out the decision tree starting at HEAD. PREVPOS is the
2125 position at the node that branched to this node. */
2128 write_tree (struct decision_head
*head
, struct position
*prevpos
,
2129 enum routine_type type
, int initial
)
2131 struct decision
*p
= head
->first
;
2135 OUTPUT_LABEL (" ", p
->number
);
2137 if (! initial
&& p
->subroutine_number
> 0)
2139 static const char * const name_prefix
[] = {
2140 "recog", "split", "peephole2"
2143 static const char * const call_suffix
[] = {
2144 ", pnum_clobbers", "", ", _pmatch_len"
2147 /* This node has been broken out into a separate subroutine.
2148 Call it, test the result, and branch accordingly. */
2152 printf (" tem = %s_%d (x0, insn%s);\n",
2153 name_prefix
[type
], p
->subroutine_number
, call_suffix
[type
]);
2154 if (IS_SPLIT (type
))
2155 printf (" if (tem != 0)\n return tem;\n");
2157 printf (" if (tem >= 0)\n return tem;\n");
2159 change_state (p
->position
, p
->afterward
->position
, " ");
2160 printf (" goto L%d;\n", p
->afterward
->number
);
2164 printf (" return %s_%d (x0, insn%s);\n",
2165 name_prefix
[type
], p
->subroutine_number
, call_suffix
[type
]);
2170 change_state (prevpos
, p
->position
, " ");
2171 write_tree_1 (head
, p
->position
->depth
, type
);
2173 for (p
= head
->first
; p
; p
= p
->next
)
2174 if (p
->success
.first
)
2175 write_tree (&p
->success
, p
->position
, type
, 0);
2179 /* Write out a subroutine of type TYPE to do comparisons starting at
2183 write_subroutine (struct decision_head
*head
, enum routine_type type
)
2185 int subfunction
= head
->first
? head
->first
->subroutine_number
: 0;
2190 s_or_e
= subfunction
? "static " : "";
2193 sprintf (extension
, "_%d", subfunction
);
2194 else if (type
== RECOG
)
2195 extension
[0] = '\0';
2197 strcpy (extension
, "_insns");
2203 recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", s_or_e
, extension
);
2207 split%s (rtx x0 ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED)\n",
2212 peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n",
2217 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2218 for (i
= 1; i
<= max_depth
; i
++)
2219 printf (" rtx x%d ATTRIBUTE_UNUSED;\n", i
);
2221 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type
) ? "rtx" : "int");
2224 printf (" recog_data.insn = NULL_RTX;\n");
2227 write_tree (head
, &root_pos
, type
, 1);
2229 printf (" goto ret0;\n");
2231 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type
) ? 0 : -1);
2234 /* In break_out_subroutines, we discovered the boundaries for the
2235 subroutines, but did not write them out. Do so now. */
2238 write_subroutines (struct decision_head
*head
, enum routine_type type
)
2242 for (p
= head
->first
; p
; p
= p
->next
)
2243 if (p
->success
.first
)
2244 write_subroutines (&p
->success
, type
);
2246 if (head
->first
->subroutine_number
> 0)
2247 write_subroutine (head
, type
);
2250 /* Begin the output file. */
2256 /* Generated automatically by the program `genrecog' from the target\n\
2257 machine description file. */\n\
2259 #include \"config.h\"\n\
2260 #include \"system.h\"\n\
2261 #include \"coretypes.h\"\n\
2262 #include \"tm.h\"\n\
2263 #include \"rtl.h\"\n\
2264 #include \"tm_p.h\"\n\
2265 #include \"function.h\"\n\
2266 #include \"insn-config.h\"\n\
2267 #include \"recog.h\"\n\
2268 #include \"output.h\"\n\
2269 #include \"flags.h\"\n\
2270 #include \"hard-reg-set.h\"\n\
2271 #include \"resource.h\"\n\
2272 #include \"diagnostic-core.h\"\n\
2273 #include \"reload.h\"\n\
2274 #include \"regs.h\"\n\
2275 #include \"tm-constrs.h\"\n\
2279 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2280 X0 is a valid instruction.\n\
2282 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2283 returns a nonnegative number which is the insn code number for the\n\
2284 pattern that matched. This is the same as the order in the machine\n\
2285 description of the entry that matched. This number can be used as an\n\
2286 index into `insn_data' and other tables.\n");
2288 The third argument to recog is an optional pointer to an int. If\n\
2289 present, recog will accept a pattern if it matches except for missing\n\
2290 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2291 the optional pointer will be set to the number of CLOBBERs that need\n\
2292 to be added (it should be initialized to zero by the caller). If it");
2294 is set nonzero, the caller should allocate a PARALLEL of the\n\
2295 appropriate size, copy the initial entries, and call add_clobbers\n\
2296 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2300 The function split_insns returns 0 if the rtl could not\n\
2301 be split or the split rtl as an INSN list if it can be.\n\
2303 The function peephole2_insns returns 0 if the rtl could not\n\
2304 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
2305 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2310 /* Construct and return a sequence of decisions
2311 that will recognize INSN.
2313 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2315 static struct decision_head
2316 make_insn_sequence (rtx insn
, enum routine_type type
)
2319 const char *c_test
= XSTR (insn
, type
== RECOG
? 2 : 1);
2320 int truth
= maybe_eval_c_test (c_test
);
2321 struct decision
*last
;
2322 struct decision_test
*test
, **place
;
2323 struct decision_head head
;
2324 struct position
*c_test_pos
, **pos_ptr
;
2326 /* We should never see an insn whose C test is false at compile time. */
2329 c_test_pos
= &root_pos
;
2330 if (type
== PEEPHOLE2
)
2334 /* peephole2 gets special treatment:
2335 - X always gets an outer parallel even if it's only one entry
2336 - we remove all traces of outer-level match_scratch and match_dup
2337 expressions here. */
2338 x
= rtx_alloc (PARALLEL
);
2339 PUT_MODE (x
, VOIDmode
);
2340 XVEC (x
, 0) = rtvec_alloc (XVECLEN (insn
, 0));
2341 pos_ptr
= &peep2_insn_pos_list
;
2342 for (i
= j
= 0; i
< XVECLEN (insn
, 0); i
++)
2344 rtx tmp
= XVECEXP (insn
, 0, i
);
2345 if (GET_CODE (tmp
) != MATCH_SCRATCH
&& GET_CODE (tmp
) != MATCH_DUP
)
2347 c_test_pos
= next_position (pos_ptr
, &root_pos
,
2349 XVECEXP (x
, 0, j
) = tmp
;
2351 pos_ptr
= &c_test_pos
->next
;
2356 else if (XVECLEN (insn
, type
== RECOG
) == 1)
2357 x
= XVECEXP (insn
, type
== RECOG
, 0);
2360 x
= rtx_alloc (PARALLEL
);
2361 XVEC (x
, 0) = XVEC (insn
, type
== RECOG
);
2362 PUT_MODE (x
, VOIDmode
);
2365 validate_pattern (x
, insn
, NULL_RTX
, 0);
2367 memset(&head
, 0, sizeof(head
));
2368 last
= add_to_sequence (x
, &head
, &root_pos
, type
, 1);
2370 /* Find the end of the test chain on the last node. */
2371 for (test
= last
->tests
; test
->next
; test
= test
->next
)
2373 place
= &test
->next
;
2375 /* Skip the C test if it's known to be true at compile time. */
2378 /* Need a new node if we have another test to add. */
2379 if (test
->type
== DT_accept_op
)
2381 last
= new_decision (c_test_pos
, &last
->success
);
2382 place
= &last
->tests
;
2384 test
= new_decision_test (DT_c_test
, &place
);
2385 test
->u
.c_test
= c_test
;
2388 test
= new_decision_test (DT_accept_insn
, &place
);
2389 test
->u
.insn
.code_number
= next_insn_code
;
2390 test
->u
.insn
.lineno
= pattern_lineno
;
2391 test
->u
.insn
.num_clobbers_to_add
= 0;
2396 /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends
2397 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2398 If so, set up to recognize the pattern without these CLOBBERs. */
2400 if (GET_CODE (x
) == PARALLEL
)
2404 /* Find the last non-clobber in the parallel. */
2405 for (i
= XVECLEN (x
, 0); i
> 0; i
--)
2407 rtx y
= XVECEXP (x
, 0, i
- 1);
2408 if (GET_CODE (y
) != CLOBBER
2409 || (!REG_P (XEXP (y
, 0))
2410 && GET_CODE (XEXP (y
, 0)) != MATCH_SCRATCH
))
2414 if (i
!= XVECLEN (x
, 0))
2417 struct decision_head clobber_head
;
2419 /* Build a similar insn without the clobbers. */
2421 new_rtx
= XVECEXP (x
, 0, 0);
2426 new_rtx
= rtx_alloc (PARALLEL
);
2427 XVEC (new_rtx
, 0) = rtvec_alloc (i
);
2428 for (j
= i
- 1; j
>= 0; j
--)
2429 XVECEXP (new_rtx
, 0, j
) = XVECEXP (x
, 0, j
);
2433 memset (&clobber_head
, 0, sizeof(clobber_head
));
2434 last
= add_to_sequence (new_rtx
, &clobber_head
, &root_pos
,
2437 /* Find the end of the test chain on the last node. */
2438 for (test
= last
->tests
; test
->next
; test
= test
->next
)
2441 /* We definitely have a new test to add -- create a new
2443 place
= &test
->next
;
2444 if (test
->type
== DT_accept_op
)
2446 last
= new_decision (&root_pos
, &last
->success
);
2447 place
= &last
->tests
;
2450 /* Skip the C test if it's known to be true at compile
2454 test
= new_decision_test (DT_c_test
, &place
);
2455 test
->u
.c_test
= c_test
;
2458 test
= new_decision_test (DT_accept_insn
, &place
);
2459 test
->u
.insn
.code_number
= next_insn_code
;
2460 test
->u
.insn
.lineno
= pattern_lineno
;
2461 test
->u
.insn
.num_clobbers_to_add
= XVECLEN (x
, 0) - i
;
2463 merge_trees (&head
, &clobber_head
);
2469 /* Define the subroutine we will call below and emit in genemit. */
2470 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", next_insn_code
);
2474 /* Define the subroutine we will call below and emit in genemit. */
2475 printf ("extern rtx gen_peephole2_%d (rtx, rtx *);\n",
2484 process_tree (struct decision_head
*head
, enum routine_type subroutine_type
)
2486 if (head
->first
== NULL
)
2488 /* We can elide peephole2_insns, but not recog or split_insns. */
2489 if (subroutine_type
== PEEPHOLE2
)
2494 factor_tests (head
);
2496 next_subroutine_number
= 0;
2497 break_out_subroutines (head
, 1);
2498 find_afterward (head
, NULL
);
2500 /* We run this after find_afterward, because find_afterward needs
2501 the redundant DT_mode tests on predicates to determine whether
2502 two tests can both be true or not. */
2503 simplify_tests(head
);
2505 write_subroutines (head
, subroutine_type
);
2508 write_subroutine (head
, subroutine_type
);
2511 extern int main (int, char **);
2514 main (int argc
, char **argv
)
2517 struct decision_head recog_tree
, split_tree
, peephole2_tree
, h
;
2519 progname
= "genrecog";
2521 memset (&recog_tree
, 0, sizeof recog_tree
);
2522 memset (&split_tree
, 0, sizeof split_tree
);
2523 memset (&peephole2_tree
, 0, sizeof peephole2_tree
);
2525 if (!init_rtx_reader_args (argc
, argv
))
2526 return (FATAL_EXIT_CODE
);
2532 /* Read the machine description. */
2536 desc
= read_md_rtx (&pattern_lineno
, &next_insn_code
);
2540 switch (GET_CODE (desc
))
2543 h
= make_insn_sequence (desc
, RECOG
);
2544 merge_trees (&recog_tree
, &h
);
2548 h
= make_insn_sequence (desc
, SPLIT
);
2549 merge_trees (&split_tree
, &h
);
2552 case DEFINE_PEEPHOLE2
:
2553 h
= make_insn_sequence (desc
, PEEPHOLE2
);
2554 merge_trees (&peephole2_tree
, &h
);
2562 return FATAL_EXIT_CODE
;
2566 process_tree (&recog_tree
, RECOG
);
2567 process_tree (&split_tree
, SPLIT
);
2568 process_tree (&peephole2_tree
, PEEPHOLE2
);
2571 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
2575 debug_decision_2 (struct decision_test
*test
)
2580 fprintf (stderr
, "num_insns=%d", test
->u
.num_insns
);
2583 fprintf (stderr
, "mode=%s", GET_MODE_NAME (test
->u
.mode
));
2586 fprintf (stderr
, "code=%s", GET_RTX_NAME (test
->u
.code
));
2589 fprintf (stderr
, "veclen=%d", test
->u
.veclen
);
2591 case DT_elt_zero_int
:
2592 fprintf (stderr
, "elt0_i=%d", (int) test
->u
.intval
);
2594 case DT_elt_one_int
:
2595 fprintf (stderr
, "elt1_i=%d", (int) test
->u
.intval
);
2597 case DT_elt_zero_wide
:
2598 fprintf (stderr
, "elt0_w=" HOST_WIDE_INT_PRINT_DEC
, test
->u
.intval
);
2600 case DT_elt_zero_wide_safe
:
2601 fprintf (stderr
, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC
, test
->u
.intval
);
2604 fprintf (stderr
, "veclen>=%d", test
->u
.veclen
);
2607 fprintf (stderr
, "dup=%d", test
->u
.dup
);
2610 fprintf (stderr
, "pred=(%s,%s)",
2611 test
->u
.pred
.name
, GET_MODE_NAME(test
->u
.pred
.mode
));
2616 strncpy (sub
, test
->u
.c_test
, sizeof(sub
));
2617 memcpy (sub
+16, "...", 4);
2618 fprintf (stderr
, "c_test=\"%s\"", sub
);
2622 fprintf (stderr
, "A_op=%d", test
->u
.opno
);
2624 case DT_accept_insn
:
2625 fprintf (stderr
, "A_insn=(%d,%d)",
2626 test
->u
.insn
.code_number
, test
->u
.insn
.num_clobbers_to_add
);
2635 debug_decision_1 (struct decision
*d
, int indent
)
2638 struct decision_test
*test
;
2642 for (i
= 0; i
< indent
; ++i
)
2644 fputs ("(nil)\n", stderr
);
2648 for (i
= 0; i
< indent
; ++i
)
2655 debug_decision_2 (test
);
2656 while ((test
= test
->next
) != NULL
)
2658 fputs (" + ", stderr
);
2659 debug_decision_2 (test
);
2662 fprintf (stderr
, "} %d n %d a %d\n", d
->number
,
2663 (d
->next
? d
->next
->number
: -1),
2664 (d
->afterward
? d
->afterward
->number
: -1));
2668 debug_decision_0 (struct decision
*d
, int indent
, int maxdepth
)
2677 for (i
= 0; i
< indent
; ++i
)
2679 fputs ("(nil)\n", stderr
);
2683 debug_decision_1 (d
, indent
);
2684 for (n
= d
->success
.first
; n
; n
= n
->next
)
2685 debug_decision_0 (n
, indent
+ 2, maxdepth
- 1);
2689 debug_decision (struct decision
*d
)
2691 debug_decision_0 (d
, 0, 1000000);
2695 debug_decision_list (struct decision
*d
)
2699 debug_decision_0 (d
, 0, 0);