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
;
690 mode
= GET_MODE (pattern
);
691 code
= GET_CODE (pattern
);
696 /* Toplevel peephole pattern. */
697 if (insn_type
== PEEPHOLE2
&& top
)
701 /* Check we have sufficient insns. This avoids complications
702 because we then know peep2_next_insn never fails. */
703 num_insns
= XVECLEN (pattern
, 0);
706 test
= new_decision_test (DT_num_insns
, &place
);
707 test
->u
.num_insns
= num_insns
;
708 last
= &sub
->success
;
712 /* We don't need the node we just created -- unlink it. */
713 last
->first
= last
->last
= NULL
;
716 subpos_ptr
= &peep2_insn_pos_list
;
717 for (i
= 0; i
< (size_t) XVECLEN (pattern
, 0); i
++)
719 subpos
= next_position (subpos_ptr
, &root_pos
,
721 sub
= add_to_sequence (XVECEXP (pattern
, 0, i
),
722 last
, subpos
, insn_type
, 0);
723 last
= &sub
->success
;
724 subpos_ptr
= &subpos
->next
;
729 /* Else nothing special. */
733 /* The explicit patterns within a match_parallel enforce a minimum
734 length on the vector. The match_parallel predicate may allow
735 for more elements. We do need to check for this minimum here
736 or the code generated to match the internals may reference data
737 beyond the end of the vector. */
738 test
= new_decision_test (DT_veclen_ge
, &place
);
739 test
->u
.veclen
= XVECLEN (pattern
, 2);
746 RTX_CODE was_code
= code
;
747 const char *pred_name
;
748 bool allows_const_int
= true;
750 if (code
== MATCH_SCRATCH
)
752 pred_name
= "scratch_operand";
757 pred_name
= XSTR (pattern
, 1);
758 if (code
== MATCH_PARALLEL
)
764 if (pred_name
[0] != 0)
766 const struct pred_data
*pred
;
768 test
= new_decision_test (DT_pred
, &place
);
769 test
->u
.pred
.name
= pred_name
;
770 test
->u
.pred
.mode
= mode
;
772 /* See if we know about this predicate.
773 If we do, remember it for use below.
775 We can optimize the generated code a little if either
776 (a) the predicate only accepts one code, or (b) the
777 predicate does not allow CONST_INT, in which case it
778 can match only if the modes match. */
779 pred
= lookup_predicate (pred_name
);
782 test
->u
.pred
.data
= pred
;
783 allows_const_int
= pred
->codes
[CONST_INT
];
784 if (was_code
== MATCH_PARALLEL
785 && pred
->singleton
!= PARALLEL
)
786 message_with_line (pattern_lineno
,
787 "predicate '%s' used in match_parallel "
788 "does not allow only PARALLEL", pred
->name
);
790 code
= pred
->singleton
;
793 message_with_line (pattern_lineno
,
794 "warning: unknown predicate '%s' in '%s' expression",
795 pred_name
, GET_RTX_NAME (was_code
));
798 /* Can't enforce a mode if we allow const_int. */
799 if (allows_const_int
)
802 /* Accept the operand, i.e. record it in `operands'. */
803 test
= new_decision_test (DT_accept_op
, &place
);
804 test
->u
.opno
= XINT (pattern
, 0);
806 if (was_code
== MATCH_OPERATOR
|| was_code
== MATCH_PARALLEL
)
808 if (was_code
== MATCH_OPERATOR
)
811 subpos_ptr
= &pos
->xexps
;
815 pos_type
= POS_XVECEXP0
;
816 subpos_ptr
= &pos
->xvecexp0s
;
818 for (i
= 0; i
< (size_t) XVECLEN (pattern
, 2); i
++)
820 subpos
= next_position (subpos_ptr
, pos
, pos_type
, i
);
821 sub
= add_to_sequence (XVECEXP (pattern
, 2, i
),
822 &sub
->success
, subpos
, insn_type
, 0);
823 subpos_ptr
= &subpos
->next
;
832 test
= new_decision_test (DT_dup
, &place
);
833 test
->u
.dup
= XINT (pattern
, 0);
835 test
= new_decision_test (DT_accept_op
, &place
);
836 test
->u
.opno
= XINT (pattern
, 0);
838 subpos_ptr
= &pos
->xexps
;
839 for (i
= 0; i
< (size_t) XVECLEN (pattern
, 1); i
++)
841 subpos
= next_position (subpos_ptr
, pos
, POS_XEXP
, i
);
842 sub
= add_to_sequence (XVECEXP (pattern
, 1, i
),
843 &sub
->success
, subpos
, insn_type
, 0);
844 subpos_ptr
= &subpos
->next
;
852 test
= new_decision_test (DT_dup
, &place
);
853 test
->u
.dup
= XINT (pattern
, 0);
860 fmt
= GET_RTX_FORMAT (code
);
861 len
= GET_RTX_LENGTH (code
);
863 /* Do tests against the current node first. */
864 for (i
= 0; i
< (size_t) len
; i
++)
872 test
= new_decision_test (DT_elt_zero_int
, &place
);
873 test
->u
.intval
= XINT (pattern
, i
);
877 test
= new_decision_test (DT_elt_one_int
, &place
);
878 test
->u
.intval
= XINT (pattern
, i
);
881 else if (fmt
[i
] == 'w')
883 /* If this value actually fits in an int, we can use a switch
884 statement here, so indicate that. */
885 enum decision_type type
886 = ((int) XWINT (pattern
, i
) == XWINT (pattern
, i
))
887 ? DT_elt_zero_wide_safe
: DT_elt_zero_wide
;
891 test
= new_decision_test (type
, &place
);
892 test
->u
.intval
= XWINT (pattern
, i
);
894 else if (fmt
[i
] == 'E')
898 test
= new_decision_test (DT_veclen
, &place
);
899 test
->u
.veclen
= XVECLEN (pattern
, i
);
903 /* Now test our sub-patterns. */
904 subpos_ptr
= &pos
->xexps
;
905 for (i
= 0; i
< (size_t) len
; i
++)
907 subpos
= next_position (subpos_ptr
, pos
, POS_XEXP
, i
);
911 sub
= add_to_sequence (XEXP (pattern
, i
), &sub
->success
,
912 subpos
, insn_type
, 0);
917 struct position
*subpos2
, **subpos2_ptr
;
920 subpos2_ptr
= &pos
->xvecexp0s
;
921 for (j
= 0; j
< XVECLEN (pattern
, i
); j
++)
923 subpos2
= next_position (subpos2_ptr
, pos
, POS_XVECEXP0
, j
);
924 sub
= add_to_sequence (XVECEXP (pattern
, i
, j
),
925 &sub
->success
, subpos2
, insn_type
, 0);
926 subpos2_ptr
= &subpos2
->next
;
940 subpos_ptr
= &subpos
->next
;
944 /* Insert nodes testing mode and code, if they're still relevant,
945 before any of the nodes we may have added above. */
948 place
= &this_decision
->tests
;
949 test
= new_decision_test (DT_code
, &place
);
953 if (mode
!= VOIDmode
)
955 place
= &this_decision
->tests
;
956 test
= new_decision_test (DT_mode
, &place
);
960 /* If we didn't insert any tests or accept nodes, hork. */
961 gcc_assert (this_decision
->tests
);
967 /* A subroutine of maybe_both_true; examines only one test.
968 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
971 maybe_both_true_2 (struct decision_test
*d1
, struct decision_test
*d2
)
973 if (d1
->type
== d2
->type
)
978 if (d1
->u
.num_insns
== d2
->u
.num_insns
)
984 return d1
->u
.mode
== d2
->u
.mode
;
987 return d1
->u
.code
== d2
->u
.code
;
990 return d1
->u
.veclen
== d2
->u
.veclen
;
992 case DT_elt_zero_int
:
994 case DT_elt_zero_wide
:
995 case DT_elt_zero_wide_safe
:
996 return d1
->u
.intval
== d2
->u
.intval
;
1003 /* If either has a predicate that we know something about, set
1004 things up so that D1 is the one that always has a known
1005 predicate. Then see if they have any codes in common. */
1007 if (d1
->type
== DT_pred
|| d2
->type
== DT_pred
)
1009 if (d2
->type
== DT_pred
)
1011 struct decision_test
*tmp
;
1012 tmp
= d1
, d1
= d2
, d2
= tmp
;
1015 /* If D2 tests a mode, see if it matches D1. */
1016 if (d1
->u
.pred
.mode
!= VOIDmode
)
1018 if (d2
->type
== DT_mode
)
1020 if (d1
->u
.pred
.mode
!= d2
->u
.mode
1021 /* The mode of an address_operand predicate is the
1022 mode of the memory, not the operand. It can only
1023 be used for testing the predicate, so we must
1025 && strcmp (d1
->u
.pred
.name
, "address_operand") != 0)
1028 /* Don't check two predicate modes here, because if both predicates
1029 accept CONST_INT, then both can still be true even if the modes
1030 are different. If they don't accept CONST_INT, there will be a
1031 separate DT_mode that will make maybe_both_true_1 return 0. */
1034 if (d1
->u
.pred
.data
)
1036 /* If D2 tests a code, see if it is in the list of valid
1037 codes for D1's predicate. */
1038 if (d2
->type
== DT_code
)
1040 if (!d1
->u
.pred
.data
->codes
[d2
->u
.code
])
1044 /* Otherwise see if the predicates have any codes in common. */
1045 else if (d2
->type
== DT_pred
&& d2
->u
.pred
.data
)
1047 bool common
= false;
1050 for (c
= 0; c
< NUM_RTX_CODE
; c
++)
1051 if (d1
->u
.pred
.data
->codes
[c
] && d2
->u
.pred
.data
->codes
[c
])
1063 /* Tests vs veclen may be known when strict equality is involved. */
1064 if (d1
->type
== DT_veclen
&& d2
->type
== DT_veclen_ge
)
1065 return d1
->u
.veclen
>= d2
->u
.veclen
;
1066 if (d1
->type
== DT_veclen_ge
&& d2
->type
== DT_veclen
)
1067 return d2
->u
.veclen
>= d1
->u
.veclen
;
1072 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1073 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */
1076 maybe_both_true_1 (struct decision_test
*d1
, struct decision_test
*d2
)
1078 struct decision_test
*t1
, *t2
;
1080 /* A match_operand with no predicate can match anything. Recognize
1081 this by the existence of a lone DT_accept_op test. */
1082 if (d1
->type
== DT_accept_op
|| d2
->type
== DT_accept_op
)
1085 /* Eliminate pairs of tests while they can exactly match. */
1086 while (d1
&& d2
&& d1
->type
== d2
->type
)
1088 if (maybe_both_true_2 (d1
, d2
) == 0)
1090 d1
= d1
->next
, d2
= d2
->next
;
1093 /* After that, consider all pairs. */
1094 for (t1
= d1
; t1
; t1
= t1
->next
)
1095 for (t2
= d2
; t2
; t2
= t2
->next
)
1096 if (maybe_both_true_2 (t1
, t2
) == 0)
1102 /* Return 0 if we can prove that there is no RTL that can match both
1103 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that
1104 can match both or just that we couldn't prove there wasn't such an RTL).
1106 TOPLEVEL is nonzero if we are to only look at the top level and not
1107 recursively descend. */
1110 maybe_both_true (struct decision
*d1
, struct decision
*d2
,
1113 struct decision
*p1
, *p2
;
1116 /* Don't compare strings on the different positions in insn. Doing so
1117 is incorrect and results in false matches from constructs like
1119 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1120 (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1122 [(set (match_operand:HI "register_operand" "r")
1123 (match_operand:HI "register_operand" "r"))]
1125 If we are presented with such, we are recursing through the remainder
1126 of a node's success nodes (from the loop at the end of this function).
1127 Skip forward until we come to a position that matches.
1129 Due to the way positions are constructed, we know that iterating
1130 forward from the lexically lower position will run into the lexically
1131 higher position and not the other way around. This saves a bit
1134 cmp
= compare_positions (d1
->position
, d2
->position
);
1137 gcc_assert (!toplevel
);
1139 /* If the d2->position was lexically lower, swap. */
1141 p1
= d1
, d1
= d2
, d2
= p1
;
1143 if (d1
->success
.first
== 0)
1145 for (p1
= d1
->success
.first
; p1
; p1
= p1
->next
)
1146 if (maybe_both_true (p1
, d2
, 0))
1152 /* Test the current level. */
1153 cmp
= maybe_both_true_1 (d1
->tests
, d2
->tests
);
1157 /* We can't prove that D1 and D2 cannot both be true. If we are only
1158 to check the top level, return 1. Otherwise, see if we can prove
1159 that all choices in both successors are mutually exclusive. If
1160 either does not have any successors, we can't prove they can't both
1163 if (toplevel
|| d1
->success
.first
== 0 || d2
->success
.first
== 0)
1166 for (p1
= d1
->success
.first
; p1
; p1
= p1
->next
)
1167 for (p2
= d2
->success
.first
; p2
; p2
= p2
->next
)
1168 if (maybe_both_true (p1
, p2
, 0))
1174 /* A subroutine of nodes_identical. Examine two tests for equivalence. */
1177 nodes_identical_1 (struct decision_test
*d1
, struct decision_test
*d2
)
1182 return d1
->u
.num_insns
== d2
->u
.num_insns
;
1185 return d1
->u
.mode
== d2
->u
.mode
;
1188 return d1
->u
.code
== d2
->u
.code
;
1191 return (d1
->u
.pred
.mode
== d2
->u
.pred
.mode
1192 && strcmp (d1
->u
.pred
.name
, d2
->u
.pred
.name
) == 0);
1195 return strcmp (d1
->u
.c_test
, d2
->u
.c_test
) == 0;
1199 return d1
->u
.veclen
== d2
->u
.veclen
;
1202 return d1
->u
.dup
== d2
->u
.dup
;
1204 case DT_elt_zero_int
:
1205 case DT_elt_one_int
:
1206 case DT_elt_zero_wide
:
1207 case DT_elt_zero_wide_safe
:
1208 return d1
->u
.intval
== d2
->u
.intval
;
1211 return d1
->u
.opno
== d2
->u
.opno
;
1213 case DT_accept_insn
:
1214 /* Differences will be handled in merge_accept_insn. */
1222 /* True iff the two nodes are identical (on one level only). Due
1223 to the way these lists are constructed, we shouldn't have to
1224 consider different orderings on the tests. */
1227 nodes_identical (struct decision
*d1
, struct decision
*d2
)
1229 struct decision_test
*t1
, *t2
;
1231 for (t1
= d1
->tests
, t2
= d2
->tests
; t1
&& t2
; t1
= t1
->next
, t2
= t2
->next
)
1233 if (t1
->type
!= t2
->type
)
1235 if (! nodes_identical_1 (t1
, t2
))
1239 /* For success, they should now both be null. */
1243 /* Check that their subnodes are at the same position, as any one set
1244 of sibling decisions must be at the same position. Allowing this
1245 requires complications to find_afterward and when change_state is
1247 if (d1
->success
.first
1248 && d2
->success
.first
1249 && d1
->success
.first
->position
!= d2
->success
.first
->position
)
1255 /* A subroutine of merge_trees; given two nodes that have been declared
1256 identical, cope with two insn accept states. If they differ in the
1257 number of clobbers, then the conflict was created by make_insn_sequence
1258 and we can drop the with-clobbers version on the floor. If both
1259 nodes have no additional clobbers, we have found an ambiguity in the
1260 source machine description. */
1263 merge_accept_insn (struct decision
*oldd
, struct decision
*addd
)
1265 struct decision_test
*old
, *add
;
1267 for (old
= oldd
->tests
; old
; old
= old
->next
)
1268 if (old
->type
== DT_accept_insn
)
1273 for (add
= addd
->tests
; add
; add
= add
->next
)
1274 if (add
->type
== DT_accept_insn
)
1279 /* If one node is for a normal insn and the second is for the base
1280 insn with clobbers stripped off, the second node should be ignored. */
1282 if (old
->u
.insn
.num_clobbers_to_add
== 0
1283 && add
->u
.insn
.num_clobbers_to_add
> 0)
1285 /* Nothing to do here. */
1287 else if (old
->u
.insn
.num_clobbers_to_add
> 0
1288 && add
->u
.insn
.num_clobbers_to_add
== 0)
1290 /* In this case, replace OLD with ADD. */
1291 old
->u
.insn
= add
->u
.insn
;
1295 error_with_line (add
->u
.insn
.lineno
, "`%s' matches `%s'",
1296 get_insn_name (add
->u
.insn
.code_number
),
1297 get_insn_name (old
->u
.insn
.code_number
));
1298 message_with_line (old
->u
.insn
.lineno
, "previous definition of `%s'",
1299 get_insn_name (old
->u
.insn
.code_number
));
1303 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */
1306 merge_trees (struct decision_head
*oldh
, struct decision_head
*addh
)
1308 struct decision
*next
, *add
;
1310 if (addh
->first
== 0)
1312 if (oldh
->first
== 0)
1318 /* Trying to merge bits at different positions isn't possible. */
1319 gcc_assert (oldh
->first
->position
== addh
->first
->position
);
1321 for (add
= addh
->first
; add
; add
= next
)
1323 struct decision
*old
, *insert_before
= NULL
;
1327 /* The semantics of pattern matching state that the tests are
1328 done in the order given in the MD file so that if an insn
1329 matches two patterns, the first one will be used. However,
1330 in practice, most, if not all, patterns are unambiguous so
1331 that their order is independent. In that case, we can merge
1332 identical tests and group all similar modes and codes together.
1334 Scan starting from the end of OLDH until we reach a point
1335 where we reach the head of the list or where we pass a
1336 pattern that could also be true if NEW is true. If we find
1337 an identical pattern, we can merge them. Also, record the
1338 last node that tests the same code and mode and the last one
1339 that tests just the same mode.
1341 If we have no match, place NEW after the closest match we found. */
1343 for (old
= oldh
->last
; old
; old
= old
->prev
)
1345 if (nodes_identical (old
, add
))
1347 merge_accept_insn (old
, add
);
1348 merge_trees (&old
->success
, &add
->success
);
1352 if (maybe_both_true (old
, add
, 0))
1355 /* Insert the nodes in DT test type order, which is roughly
1356 how expensive/important the test is. Given that the tests
1357 are also ordered within the list, examining the first is
1359 if ((int) add
->tests
->type
< (int) old
->tests
->type
)
1360 insert_before
= old
;
1363 if (insert_before
== NULL
)
1366 add
->prev
= oldh
->last
;
1367 oldh
->last
->next
= add
;
1372 if ((add
->prev
= insert_before
->prev
) != NULL
)
1373 add
->prev
->next
= add
;
1376 add
->next
= insert_before
;
1377 insert_before
->prev
= add
;
1384 /* Walk the tree looking for sub-nodes that perform common tests.
1385 Factor out the common test into a new node. This enables us
1386 (depending on the test type) to emit switch statements later. */
1389 factor_tests (struct decision_head
*head
)
1391 struct decision
*first
, *next
;
1393 for (first
= head
->first
; first
&& first
->next
; first
= next
)
1395 enum decision_type type
;
1396 struct decision
*new_dec
, *old_last
;
1398 type
= first
->tests
->type
;
1401 /* Want at least two compatible sequential nodes. */
1402 if (next
->tests
->type
!= type
)
1405 /* Don't want all node types, just those we can turn into
1406 switch statements. */
1409 && type
!= DT_veclen
1410 && type
!= DT_elt_zero_int
1411 && type
!= DT_elt_one_int
1412 && type
!= DT_elt_zero_wide_safe
)
1415 /* If we'd been performing more than one test, create a new node
1416 below our first test. */
1417 if (first
->tests
->next
!= NULL
)
1419 new_dec
= new_decision (first
->position
, &first
->success
);
1420 new_dec
->tests
= first
->tests
->next
;
1421 first
->tests
->next
= NULL
;
1424 /* Crop the node tree off after our first test. */
1426 old_last
= head
->last
;
1429 /* For each compatible test, adjust to perform only one test in
1430 the top level node, then merge the node back into the tree. */
1433 struct decision_head h
;
1435 if (next
->tests
->next
!= NULL
)
1437 new_dec
= new_decision (next
->position
, &next
->success
);
1438 new_dec
->tests
= next
->tests
->next
;
1439 next
->tests
->next
= NULL
;
1443 new_dec
->next
= NULL
;
1444 h
.first
= h
.last
= new_dec
;
1446 merge_trees (head
, &h
);
1448 while (next
&& next
->tests
->type
== type
);
1450 /* After we run out of compatible tests, graft the remaining nodes
1451 back onto the tree. */
1454 next
->prev
= head
->last
;
1455 head
->last
->next
= next
;
1456 head
->last
= old_last
;
1461 for (first
= head
->first
; first
; first
= first
->next
)
1462 factor_tests (&first
->success
);
1465 /* After factoring, try to simplify the tests on any one node.
1466 Tests that are useful for switch statements are recognizable
1467 by having only a single test on a node -- we'll be manipulating
1468 nodes with multiple tests:
1470 If we have mode tests or code tests that are redundant with
1471 predicates, remove them. */
1474 simplify_tests (struct decision_head
*head
)
1476 struct decision
*tree
;
1478 for (tree
= head
->first
; tree
; tree
= tree
->next
)
1480 struct decision_test
*a
, *b
;
1487 /* Find a predicate node. */
1488 while (b
&& b
->type
!= DT_pred
)
1492 /* Due to how these tests are constructed, we don't even need
1493 to check that the mode and code are compatible -- they were
1494 generated from the predicate in the first place. */
1495 while (a
->type
== DT_mode
|| a
->type
== DT_code
)
1502 for (tree
= head
->first
; tree
; tree
= tree
->next
)
1503 simplify_tests (&tree
->success
);
1506 /* Count the number of subnodes of HEAD. If the number is high enough,
1507 make the first node in HEAD start a separate subroutine in the C code
1508 that is generated. */
1511 break_out_subroutines (struct decision_head
*head
, int initial
)
1514 struct decision
*sub
;
1516 for (sub
= head
->first
; sub
; sub
= sub
->next
)
1517 size
+= 1 + break_out_subroutines (&sub
->success
, 0);
1519 if (size
> SUBROUTINE_THRESHOLD
&& ! initial
)
1521 head
->first
->subroutine_number
= ++next_subroutine_number
;
1527 /* For each node p, find the next alternative that might be true
1531 find_afterward (struct decision_head
*head
, struct decision
*real_afterward
)
1533 struct decision
*p
, *q
, *afterward
;
1535 /* We can't propagate alternatives across subroutine boundaries.
1536 This is not incorrect, merely a minor optimization loss. */
1539 afterward
= (p
->subroutine_number
> 0 ? NULL
: real_afterward
);
1541 for ( ; p
; p
= p
->next
)
1543 /* Find the next node that might be true if this one fails. */
1544 for (q
= p
->next
; q
; q
= q
->next
)
1545 if (maybe_both_true (p
, q
, 1))
1548 /* If we reached the end of the list without finding one,
1549 use the incoming afterward position. */
1558 for (p
= head
->first
; p
; p
= p
->next
)
1559 if (p
->success
.first
)
1560 find_afterward (&p
->success
, p
->afterward
);
1562 /* When we are generating a subroutine, record the real afterward
1563 position in the first node where write_tree can find it, and we
1564 can do the right thing at the subroutine call site. */
1566 if (p
->subroutine_number
> 0)
1567 p
->afterward
= real_afterward
;
1570 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1571 actions are necessary to move to NEWPOS. If we fail to move to the
1572 new state, branch to node AFTERWARD if nonzero, otherwise return.
1574 Failure to move to the new state can only occur if we are trying to
1575 match multiple insns and we try to step past the end of the stream. */
1578 change_state (struct position
*oldpos
, struct position
*newpos
,
1581 while (oldpos
->depth
> newpos
->depth
)
1582 oldpos
= oldpos
->base
;
1584 if (oldpos
!= newpos
)
1585 switch (newpos
->type
)
1587 case POS_PEEP2_INSN
:
1588 printf ("%stem = peep2_next_insn (%d);\n", indent
, newpos
->arg
);
1589 printf ("%sx%d = PATTERN (tem);\n", indent
, newpos
->depth
);
1593 change_state (oldpos
, newpos
->base
, indent
);
1594 printf ("%sx%d = XEXP (x%d, %d);\n",
1595 indent
, newpos
->depth
, newpos
->depth
- 1, newpos
->arg
);
1599 change_state (oldpos
, newpos
->base
, indent
);
1600 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1601 indent
, newpos
->depth
, newpos
->depth
- 1, newpos
->arg
);
1606 /* Print the enumerator constant for CODE -- the upcase version of
1610 print_code (enum rtx_code code
)
1613 for (p
= GET_RTX_NAME (code
); *p
; p
++)
1614 putchar (TOUPPER (*p
));
1617 /* Emit code to cross an afterward link -- change state and branch. */
1620 write_afterward (struct decision
*start
, struct decision
*afterward
,
1623 if (!afterward
|| start
->subroutine_number
> 0)
1624 printf("%sgoto ret0;\n", indent
);
1627 change_state (start
->position
, afterward
->position
, indent
);
1628 printf ("%sgoto L%d;\n", indent
, afterward
->number
);
1632 /* Emit a HOST_WIDE_INT as an integer constant expression. We need to take
1633 special care to avoid "decimal constant is so large that it is unsigned"
1634 warnings in the resulting code. */
1637 print_host_wide_int (HOST_WIDE_INT val
)
1639 HOST_WIDE_INT min
= (unsigned HOST_WIDE_INT
)1 << (HOST_BITS_PER_WIDE_INT
-1);
1641 printf ("(" HOST_WIDE_INT_PRINT_DEC_C
"-1)", val
+ 1);
1643 printf (HOST_WIDE_INT_PRINT_DEC_C
, val
);
1646 /* Emit a switch statement, if possible, for an initial sequence of
1647 nodes at START. Return the first node yet untested. */
1649 static struct decision
*
1650 write_switch (struct decision
*start
, int depth
)
1652 struct decision
*p
= start
;
1653 enum decision_type type
= p
->tests
->type
;
1654 struct decision
*needs_label
= NULL
;
1656 /* If we have two or more nodes in sequence that test the same one
1657 thing, we may be able to use a switch statement. */
1661 || p
->next
->tests
->type
!= type
1662 || p
->next
->tests
->next
1663 || nodes_identical_1 (p
->tests
, p
->next
->tests
))
1666 /* DT_code is special in that we can do interesting things with
1667 known predicates at the same time. */
1668 if (type
== DT_code
)
1670 char codemap
[NUM_RTX_CODE
];
1671 struct decision
*ret
;
1674 memset (codemap
, 0, sizeof(codemap
));
1676 printf (" switch (GET_CODE (x%d))\n {\n", depth
);
1677 code
= p
->tests
->u
.code
;
1680 if (p
!= start
&& p
->need_label
&& needs_label
== NULL
)
1685 printf (":\n goto L%d;\n", p
->success
.first
->number
);
1686 p
->success
.first
->need_label
= 1;
1693 && p
->tests
->type
== DT_code
1694 && ! codemap
[code
= p
->tests
->u
.code
]);
1696 /* If P is testing a predicate that we know about and we haven't
1697 seen any of the codes that are valid for the predicate, we can
1698 write a series of "case" statement, one for each possible code.
1699 Since we are already in a switch, these redundant tests are very
1700 cheap and will reduce the number of predicates called. */
1702 /* Note that while we write out cases for these predicates here,
1703 we don't actually write the test here, as it gets kinda messy.
1704 It is trivial to leave this to later by telling our caller that
1705 we only processed the CODE tests. */
1706 if (needs_label
!= NULL
)
1711 while (p
&& p
->tests
->type
== DT_pred
&& p
->tests
->u
.pred
.data
)
1713 const struct pred_data
*data
= p
->tests
->u
.pred
.data
;
1716 for (c
= 0; c
< NUM_RTX_CODE
; c
++)
1717 if (codemap
[c
] && data
->codes
[c
])
1720 for (c
= 0; c
< NUM_RTX_CODE
; c
++)
1723 fputs (" case ", stdout
);
1724 print_code ((enum rtx_code
) c
);
1725 fputs (":\n", stdout
);
1729 printf (" goto L%d;\n", p
->number
);
1735 /* Make the default case skip the predicates we managed to match. */
1737 printf (" default:\n");
1742 printf (" goto L%d;\n", p
->number
);
1746 write_afterward (start
, start
->afterward
, " ");
1749 printf (" break;\n");
1754 else if (type
== DT_mode
1755 || type
== DT_veclen
1756 || type
== DT_elt_zero_int
1757 || type
== DT_elt_one_int
1758 || type
== DT_elt_zero_wide_safe
)
1760 const char *indent
= "";
1762 /* We cast switch parameter to integer, so we must ensure that the value
1764 if (type
== DT_elt_zero_wide_safe
)
1767 printf(" if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n", depth
, depth
);
1769 printf ("%s switch (", indent
);
1773 printf ("GET_MODE (x%d)", depth
);
1776 printf ("XVECLEN (x%d, 0)", depth
);
1778 case DT_elt_zero_int
:
1779 printf ("XINT (x%d, 0)", depth
);
1781 case DT_elt_one_int
:
1782 printf ("XINT (x%d, 1)", depth
);
1784 case DT_elt_zero_wide_safe
:
1785 /* Convert result of XWINT to int for portability since some C
1786 compilers won't do it and some will. */
1787 printf ("(int) XWINT (x%d, 0)", depth
);
1792 printf (")\n%s {\n", indent
);
1796 /* Merge trees will not unify identical nodes if their
1797 sub-nodes are at different levels. Thus we must check
1798 for duplicate cases. */
1800 for (q
= start
; q
!= p
; q
= q
->next
)
1801 if (nodes_identical_1 (p
->tests
, q
->tests
))
1804 if (p
!= start
&& p
->need_label
&& needs_label
== NULL
)
1807 printf ("%s case ", indent
);
1811 printf ("%smode", GET_MODE_NAME (p
->tests
->u
.mode
));
1814 printf ("%d", p
->tests
->u
.veclen
);
1816 case DT_elt_zero_int
:
1817 case DT_elt_one_int
:
1818 case DT_elt_zero_wide
:
1819 case DT_elt_zero_wide_safe
:
1820 print_host_wide_int (p
->tests
->u
.intval
);
1825 printf (":\n%s goto L%d;\n", indent
, p
->success
.first
->number
);
1826 p
->success
.first
->need_label
= 1;
1830 while (p
&& p
->tests
->type
== type
&& !p
->tests
->next
);
1833 printf ("%s default:\n%s break;\n%s }\n",
1834 indent
, indent
, indent
);
1836 return needs_label
!= NULL
? needs_label
: p
;
1840 /* None of the other tests are amenable. */
1845 /* Emit code for one test. */
1848 write_cond (struct decision_test
*p
, int depth
,
1849 enum routine_type subroutine_type
)
1854 printf ("peep2_current_count >= %d", p
->u
.num_insns
);
1858 printf ("GET_MODE (x%d) == %smode", depth
, GET_MODE_NAME (p
->u
.mode
));
1862 printf ("GET_CODE (x%d) == ", depth
);
1863 print_code (p
->u
.code
);
1867 printf ("XVECLEN (x%d, 0) == %d", depth
, p
->u
.veclen
);
1870 case DT_elt_zero_int
:
1871 printf ("XINT (x%d, 0) == %d", depth
, (int) p
->u
.intval
);
1874 case DT_elt_one_int
:
1875 printf ("XINT (x%d, 1) == %d", depth
, (int) p
->u
.intval
);
1878 case DT_elt_zero_wide
:
1879 case DT_elt_zero_wide_safe
:
1880 printf ("XWINT (x%d, 0) == ", depth
);
1881 print_host_wide_int (p
->u
.intval
);
1885 printf ("x%d == const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
1886 depth
, (int) p
->u
.intval
);
1890 printf ("XVECLEN (x%d, 0) >= %d", depth
, p
->u
.veclen
);
1894 printf ("rtx_equal_p (x%d, operands[%d])", depth
, p
->u
.dup
);
1898 printf ("%s (x%d, %smode)", p
->u
.pred
.name
, depth
,
1899 GET_MODE_NAME (p
->u
.pred
.mode
));
1903 print_c_condition (p
->u
.c_test
);
1906 case DT_accept_insn
:
1907 gcc_assert (subroutine_type
== RECOG
);
1908 gcc_assert (p
->u
.insn
.num_clobbers_to_add
);
1909 printf ("pnum_clobbers != NULL");
1917 /* Emit code for one action. The previous tests have succeeded;
1918 TEST is the last of the chain. In the normal case we simply
1919 perform a state change. For the `accept' tests we must do more work. */
1922 write_action (struct decision
*p
, struct decision_test
*test
,
1923 int depth
, int uncond
, struct decision
*success
,
1924 enum routine_type subroutine_type
)
1931 else if (test
->type
== DT_accept_op
|| test
->type
== DT_accept_insn
)
1933 fputs (" {\n", stdout
);
1940 if (test
->type
== DT_accept_op
)
1942 printf("%soperands[%d] = x%d;\n", indent
, test
->u
.opno
, depth
);
1944 /* Only allow DT_accept_insn to follow. */
1948 gcc_assert (test
->type
== DT_accept_insn
);
1952 /* Sanity check that we're now at the end of the list of tests. */
1953 gcc_assert (!test
->next
);
1955 if (test
->type
== DT_accept_insn
)
1957 switch (subroutine_type
)
1960 if (test
->u
.insn
.num_clobbers_to_add
!= 0)
1961 printf ("%s*pnum_clobbers = %d;\n",
1962 indent
, test
->u
.insn
.num_clobbers_to_add
);
1963 printf ("%sreturn %d; /* %s */\n", indent
,
1964 test
->u
.insn
.code_number
,
1965 get_insn_name (test
->u
.insn
.code_number
));
1969 printf ("%sreturn gen_split_%d (insn, operands);\n",
1970 indent
, test
->u
.insn
.code_number
);
1976 struct position
*pos
;
1978 for (pos
= p
->position
; pos
; pos
= pos
->base
)
1979 if (pos
->type
== POS_PEEP2_INSN
)
1981 match_len
= pos
->arg
;
1984 printf ("%s*_pmatch_len = %d;\n", indent
, match_len
);
1985 printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1986 indent
, test
->u
.insn
.code_number
);
1987 printf ("%sif (tem != 0)\n%s return tem;\n", indent
, indent
);
1997 printf("%sgoto L%d;\n", indent
, success
->number
);
1998 success
->need_label
= 1;
2002 fputs (" }\n", stdout
);
2005 /* Return 1 if the test is always true and has no fallthru path. Return -1
2006 if the test does have a fallthru path, but requires that the condition be
2007 terminated. Otherwise return 0 for a normal test. */
2008 /* ??? is_unconditional is a stupid name for a tri-state function. */
2011 is_unconditional (struct decision_test
*t
, enum routine_type subroutine_type
)
2013 if (t
->type
== DT_accept_op
)
2016 if (t
->type
== DT_accept_insn
)
2018 switch (subroutine_type
)
2021 return (t
->u
.insn
.num_clobbers_to_add
== 0);
2034 /* Emit code for one node -- the conditional and the accompanying action.
2035 Return true if there is no fallthru path. */
2038 write_node (struct decision
*p
, int depth
,
2039 enum routine_type subroutine_type
)
2041 struct decision_test
*test
, *last_test
;
2044 /* Scan the tests and simplify comparisons against small
2046 for (test
= p
->tests
; test
; test
= test
->next
)
2048 if (test
->type
== DT_code
2049 && test
->u
.code
== CONST_INT
2051 && test
->next
->type
== DT_elt_zero_wide_safe
2052 && -MAX_SAVED_CONST_INT
<= test
->next
->u
.intval
2053 && test
->next
->u
.intval
<= MAX_SAVED_CONST_INT
)
2055 test
->type
= DT_const_int
;
2056 test
->u
.intval
= test
->next
->u
.intval
;
2057 test
->next
= test
->next
->next
;
2061 last_test
= test
= p
->tests
;
2062 uncond
= is_unconditional (test
, subroutine_type
);
2066 write_cond (test
, depth
, subroutine_type
);
2068 while ((test
= test
->next
) != NULL
)
2071 if (is_unconditional (test
, subroutine_type
))
2075 write_cond (test
, depth
, subroutine_type
);
2081 write_action (p
, last_test
, depth
, uncond
, p
->success
.first
, subroutine_type
);
2086 /* Emit code for all of the sibling nodes of HEAD. */
2089 write_tree_1 (struct decision_head
*head
, int depth
,
2090 enum routine_type subroutine_type
)
2092 struct decision
*p
, *next
;
2095 for (p
= head
->first
; p
; p
= next
)
2097 /* The label for the first element was printed in write_tree. */
2098 if (p
!= head
->first
&& p
->need_label
)
2099 OUTPUT_LABEL (" ", p
->number
);
2101 /* Attempt to write a switch statement for a whole sequence. */
2102 next
= write_switch (p
, depth
);
2107 /* Failed -- fall back and write one node. */
2108 uncond
= write_node (p
, depth
, subroutine_type
);
2113 /* Finished with this chain. Close a fallthru path by branching
2114 to the afterward node. */
2116 write_afterward (head
->last
, head
->last
->afterward
, " ");
2119 /* Write out the decision tree starting at HEAD. PREVPOS is the
2120 position at the node that branched to this node. */
2123 write_tree (struct decision_head
*head
, struct position
*prevpos
,
2124 enum routine_type type
, int initial
)
2126 struct decision
*p
= head
->first
;
2130 OUTPUT_LABEL (" ", p
->number
);
2132 if (! initial
&& p
->subroutine_number
> 0)
2134 static const char * const name_prefix
[] = {
2135 "recog", "split", "peephole2"
2138 static const char * const call_suffix
[] = {
2139 ", pnum_clobbers", "", ", _pmatch_len"
2142 /* This node has been broken out into a separate subroutine.
2143 Call it, test the result, and branch accordingly. */
2147 printf (" tem = %s_%d (x0, insn%s);\n",
2148 name_prefix
[type
], p
->subroutine_number
, call_suffix
[type
]);
2149 if (IS_SPLIT (type
))
2150 printf (" if (tem != 0)\n return tem;\n");
2152 printf (" if (tem >= 0)\n return tem;\n");
2154 change_state (p
->position
, p
->afterward
->position
, " ");
2155 printf (" goto L%d;\n", p
->afterward
->number
);
2159 printf (" return %s_%d (x0, insn%s);\n",
2160 name_prefix
[type
], p
->subroutine_number
, call_suffix
[type
]);
2165 change_state (prevpos
, p
->position
, " ");
2166 write_tree_1 (head
, p
->position
->depth
, type
);
2168 for (p
= head
->first
; p
; p
= p
->next
)
2169 if (p
->success
.first
)
2170 write_tree (&p
->success
, p
->position
, type
, 0);
2174 /* Write out a subroutine of type TYPE to do comparisons starting at
2178 write_subroutine (struct decision_head
*head
, enum routine_type type
)
2180 int subfunction
= head
->first
? head
->first
->subroutine_number
: 0;
2185 s_or_e
= subfunction
? "static " : "";
2188 sprintf (extension
, "_%d", subfunction
);
2189 else if (type
== RECOG
)
2190 extension
[0] = '\0';
2192 strcpy (extension
, "_insns");
2198 recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", s_or_e
, extension
);
2202 split%s (rtx x0 ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED)\n",
2207 peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n",
2212 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2213 for (i
= 1; i
<= max_depth
; i
++)
2214 printf (" rtx x%d ATTRIBUTE_UNUSED;\n", i
);
2216 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type
) ? "rtx" : "int");
2219 printf (" recog_data.insn = NULL_RTX;\n");
2222 write_tree (head
, &root_pos
, type
, 1);
2224 printf (" goto ret0;\n");
2226 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type
) ? 0 : -1);
2229 /* In break_out_subroutines, we discovered the boundaries for the
2230 subroutines, but did not write them out. Do so now. */
2233 write_subroutines (struct decision_head
*head
, enum routine_type type
)
2237 for (p
= head
->first
; p
; p
= p
->next
)
2238 if (p
->success
.first
)
2239 write_subroutines (&p
->success
, type
);
2241 if (head
->first
->subroutine_number
> 0)
2242 write_subroutine (head
, type
);
2245 /* Begin the output file. */
2251 /* Generated automatically by the program `genrecog' from the target\n\
2252 machine description file. */\n\
2254 #include \"config.h\"\n\
2255 #include \"system.h\"\n\
2256 #include \"coretypes.h\"\n\
2257 #include \"tm.h\"\n\
2258 #include \"rtl.h\"\n\
2259 #include \"tm_p.h\"\n\
2260 #include \"function.h\"\n\
2261 #include \"insn-config.h\"\n\
2262 #include \"recog.h\"\n\
2263 #include \"output.h\"\n\
2264 #include \"flags.h\"\n\
2265 #include \"hard-reg-set.h\"\n\
2266 #include \"resource.h\"\n\
2267 #include \"diagnostic-core.h\"\n\
2268 #include \"reload.h\"\n\
2269 #include \"regs.h\"\n\
2270 #include \"tm-constrs.h\"\n\
2274 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2275 X0 is a valid instruction.\n\
2277 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\
2278 returns a nonnegative number which is the insn code number for the\n\
2279 pattern that matched. This is the same as the order in the machine\n\
2280 description of the entry that matched. This number can be used as an\n\
2281 index into `insn_data' and other tables.\n");
2283 The third argument to recog is an optional pointer to an int. If\n\
2284 present, recog will accept a pattern if it matches except for missing\n\
2285 CLOBBER expressions at the end. In that case, the value pointed to by\n\
2286 the optional pointer will be set to the number of CLOBBERs that need\n\
2287 to be added (it should be initialized to zero by the caller). If it");
2289 is set nonzero, the caller should allocate a PARALLEL of the\n\
2290 appropriate size, copy the initial entries, and call add_clobbers\n\
2291 (found in insn-emit.c) to fill in the CLOBBERs.\n\
2295 The function split_insns returns 0 if the rtl could not\n\
2296 be split or the split rtl as an INSN list if it can be.\n\
2298 The function peephole2_insns returns 0 if the rtl could not\n\
2299 be matched. If there was a match, the new rtl is returned in an INSN list,\n\
2300 and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2305 /* Construct and return a sequence of decisions
2306 that will recognize INSN.
2308 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
2310 static struct decision_head
2311 make_insn_sequence (rtx insn
, enum routine_type type
)
2314 const char *c_test
= XSTR (insn
, type
== RECOG
? 2 : 1);
2315 int truth
= maybe_eval_c_test (c_test
);
2316 struct decision
*last
;
2317 struct decision_test
*test
, **place
;
2318 struct decision_head head
;
2319 struct position
*c_test_pos
, **pos_ptr
;
2321 /* We should never see an insn whose C test is false at compile time. */
2324 c_test_pos
= &root_pos
;
2325 if (type
== PEEPHOLE2
)
2329 /* peephole2 gets special treatment:
2330 - X always gets an outer parallel even if it's only one entry
2331 - we remove all traces of outer-level match_scratch and match_dup
2332 expressions here. */
2333 x
= rtx_alloc (PARALLEL
);
2334 PUT_MODE (x
, VOIDmode
);
2335 XVEC (x
, 0) = rtvec_alloc (XVECLEN (insn
, 0));
2336 pos_ptr
= &peep2_insn_pos_list
;
2337 for (i
= j
= 0; i
< XVECLEN (insn
, 0); i
++)
2339 rtx tmp
= XVECEXP (insn
, 0, i
);
2340 if (GET_CODE (tmp
) != MATCH_SCRATCH
&& GET_CODE (tmp
) != MATCH_DUP
)
2342 c_test_pos
= next_position (pos_ptr
, &root_pos
,
2344 XVECEXP (x
, 0, j
) = tmp
;
2346 pos_ptr
= &c_test_pos
->next
;
2351 else if (XVECLEN (insn
, type
== RECOG
) == 1)
2352 x
= XVECEXP (insn
, type
== RECOG
, 0);
2355 x
= rtx_alloc (PARALLEL
);
2356 XVEC (x
, 0) = XVEC (insn
, type
== RECOG
);
2357 PUT_MODE (x
, VOIDmode
);
2360 validate_pattern (x
, insn
, NULL_RTX
, 0);
2362 memset(&head
, 0, sizeof(head
));
2363 last
= add_to_sequence (x
, &head
, &root_pos
, type
, 1);
2365 /* Find the end of the test chain on the last node. */
2366 for (test
= last
->tests
; test
->next
; test
= test
->next
)
2368 place
= &test
->next
;
2370 /* Skip the C test if it's known to be true at compile time. */
2373 /* Need a new node if we have another test to add. */
2374 if (test
->type
== DT_accept_op
)
2376 last
= new_decision (c_test_pos
, &last
->success
);
2377 place
= &last
->tests
;
2379 test
= new_decision_test (DT_c_test
, &place
);
2380 test
->u
.c_test
= c_test
;
2383 test
= new_decision_test (DT_accept_insn
, &place
);
2384 test
->u
.insn
.code_number
= next_insn_code
;
2385 test
->u
.insn
.lineno
= pattern_lineno
;
2386 test
->u
.insn
.num_clobbers_to_add
= 0;
2391 /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends
2392 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2393 If so, set up to recognize the pattern without these CLOBBERs. */
2395 if (GET_CODE (x
) == PARALLEL
)
2399 /* Find the last non-clobber in the parallel. */
2400 for (i
= XVECLEN (x
, 0); i
> 0; i
--)
2402 rtx y
= XVECEXP (x
, 0, i
- 1);
2403 if (GET_CODE (y
) != CLOBBER
2404 || (!REG_P (XEXP (y
, 0))
2405 && GET_CODE (XEXP (y
, 0)) != MATCH_SCRATCH
))
2409 if (i
!= XVECLEN (x
, 0))
2412 struct decision_head clobber_head
;
2414 /* Build a similar insn without the clobbers. */
2416 new_rtx
= XVECEXP (x
, 0, 0);
2421 new_rtx
= rtx_alloc (PARALLEL
);
2422 XVEC (new_rtx
, 0) = rtvec_alloc (i
);
2423 for (j
= i
- 1; j
>= 0; j
--)
2424 XVECEXP (new_rtx
, 0, j
) = XVECEXP (x
, 0, j
);
2428 memset (&clobber_head
, 0, sizeof(clobber_head
));
2429 last
= add_to_sequence (new_rtx
, &clobber_head
, &root_pos
,
2432 /* Find the end of the test chain on the last node. */
2433 for (test
= last
->tests
; test
->next
; test
= test
->next
)
2436 /* We definitely have a new test to add -- create a new
2438 place
= &test
->next
;
2439 if (test
->type
== DT_accept_op
)
2441 last
= new_decision (&root_pos
, &last
->success
);
2442 place
= &last
->tests
;
2445 /* Skip the C test if it's known to be true at compile
2449 test
= new_decision_test (DT_c_test
, &place
);
2450 test
->u
.c_test
= c_test
;
2453 test
= new_decision_test (DT_accept_insn
, &place
);
2454 test
->u
.insn
.code_number
= next_insn_code
;
2455 test
->u
.insn
.lineno
= pattern_lineno
;
2456 test
->u
.insn
.num_clobbers_to_add
= XVECLEN (x
, 0) - i
;
2458 merge_trees (&head
, &clobber_head
);
2464 /* Define the subroutine we will call below and emit in genemit. */
2465 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", next_insn_code
);
2469 /* Define the subroutine we will call below and emit in genemit. */
2470 printf ("extern rtx gen_peephole2_%d (rtx, rtx *);\n",
2479 process_tree (struct decision_head
*head
, enum routine_type subroutine_type
)
2481 if (head
->first
== NULL
)
2483 /* We can elide peephole2_insns, but not recog or split_insns. */
2484 if (subroutine_type
== PEEPHOLE2
)
2489 factor_tests (head
);
2491 next_subroutine_number
= 0;
2492 break_out_subroutines (head
, 1);
2493 find_afterward (head
, NULL
);
2495 /* We run this after find_afterward, because find_afterward needs
2496 the redundant DT_mode tests on predicates to determine whether
2497 two tests can both be true or not. */
2498 simplify_tests(head
);
2500 write_subroutines (head
, subroutine_type
);
2503 write_subroutine (head
, subroutine_type
);
2506 extern int main (int, char **);
2509 main (int argc
, char **argv
)
2512 struct decision_head recog_tree
, split_tree
, peephole2_tree
, h
;
2514 progname
= "genrecog";
2516 memset (&recog_tree
, 0, sizeof recog_tree
);
2517 memset (&split_tree
, 0, sizeof split_tree
);
2518 memset (&peephole2_tree
, 0, sizeof peephole2_tree
);
2520 if (!init_rtx_reader_args (argc
, argv
))
2521 return (FATAL_EXIT_CODE
);
2527 /* Read the machine description. */
2531 desc
= read_md_rtx (&pattern_lineno
, &next_insn_code
);
2535 switch (GET_CODE (desc
))
2538 h
= make_insn_sequence (desc
, RECOG
);
2539 merge_trees (&recog_tree
, &h
);
2543 h
= make_insn_sequence (desc
, SPLIT
);
2544 merge_trees (&split_tree
, &h
);
2547 case DEFINE_PEEPHOLE2
:
2548 h
= make_insn_sequence (desc
, PEEPHOLE2
);
2549 merge_trees (&peephole2_tree
, &h
);
2557 return FATAL_EXIT_CODE
;
2561 process_tree (&recog_tree
, RECOG
);
2562 process_tree (&split_tree
, SPLIT
);
2563 process_tree (&peephole2_tree
, PEEPHOLE2
);
2566 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
2570 debug_decision_2 (struct decision_test
*test
)
2575 fprintf (stderr
, "num_insns=%d", test
->u
.num_insns
);
2578 fprintf (stderr
, "mode=%s", GET_MODE_NAME (test
->u
.mode
));
2581 fprintf (stderr
, "code=%s", GET_RTX_NAME (test
->u
.code
));
2584 fprintf (stderr
, "veclen=%d", test
->u
.veclen
);
2586 case DT_elt_zero_int
:
2587 fprintf (stderr
, "elt0_i=%d", (int) test
->u
.intval
);
2589 case DT_elt_one_int
:
2590 fprintf (stderr
, "elt1_i=%d", (int) test
->u
.intval
);
2592 case DT_elt_zero_wide
:
2593 fprintf (stderr
, "elt0_w=" HOST_WIDE_INT_PRINT_DEC
, test
->u
.intval
);
2595 case DT_elt_zero_wide_safe
:
2596 fprintf (stderr
, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC
, test
->u
.intval
);
2599 fprintf (stderr
, "veclen>=%d", test
->u
.veclen
);
2602 fprintf (stderr
, "dup=%d", test
->u
.dup
);
2605 fprintf (stderr
, "pred=(%s,%s)",
2606 test
->u
.pred
.name
, GET_MODE_NAME(test
->u
.pred
.mode
));
2611 strncpy (sub
, test
->u
.c_test
, sizeof(sub
));
2612 memcpy (sub
+16, "...", 4);
2613 fprintf (stderr
, "c_test=\"%s\"", sub
);
2617 fprintf (stderr
, "A_op=%d", test
->u
.opno
);
2619 case DT_accept_insn
:
2620 fprintf (stderr
, "A_insn=(%d,%d)",
2621 test
->u
.insn
.code_number
, test
->u
.insn
.num_clobbers_to_add
);
2630 debug_decision_1 (struct decision
*d
, int indent
)
2633 struct decision_test
*test
;
2637 for (i
= 0; i
< indent
; ++i
)
2639 fputs ("(nil)\n", stderr
);
2643 for (i
= 0; i
< indent
; ++i
)
2650 debug_decision_2 (test
);
2651 while ((test
= test
->next
) != NULL
)
2653 fputs (" + ", stderr
);
2654 debug_decision_2 (test
);
2657 fprintf (stderr
, "} %d n %d a %d\n", d
->number
,
2658 (d
->next
? d
->next
->number
: -1),
2659 (d
->afterward
? d
->afterward
->number
: -1));
2663 debug_decision_0 (struct decision
*d
, int indent
, int maxdepth
)
2672 for (i
= 0; i
< indent
; ++i
)
2674 fputs ("(nil)\n", stderr
);
2678 debug_decision_1 (d
, indent
);
2679 for (n
= d
->success
.first
; n
; n
= n
->next
)
2680 debug_decision_0 (n
, indent
+ 2, maxdepth
- 1);
2684 debug_decision (struct decision
*d
)
2686 debug_decision_0 (d
, 0, 1000000);
2690 debug_decision_list (struct decision
*d
)
2694 debug_decision_0 (d
, 0, 0);