1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
4 Copyright (C) 2014-2018 Free Software Foundation, Inc.
5 Contributed by Richard Biener <rguenther@suse.de>
6 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
29 #include "hash-table.h"
34 /* Stubs for GGC referenced through instantiations triggered by hash-map. */
35 void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
36 size_t, size_t MEM_STAT_DECL
)
40 void ggc_free (void *)
47 /* Verboseness. 0 is quiet, 1 adds some warnings, 2 is for debugging. */
53 static struct line_maps
*line_table
;
55 /* The rich_location class within libcpp requires a way to expand
56 source_location instances, and relies on the client code
57 providing a symbol named
58 linemap_client_expand_location_to_spelling_point
61 This is the implementation for genmatch. */
64 linemap_client_expand_location_to_spelling_point (source_location loc
,
67 const struct line_map_ordinary
*map
;
68 loc
= linemap_resolve_location (line_table
, loc
, LRK_SPELLING_LOCATION
, &map
);
69 return linemap_expand_location (line_table
, map
, loc
);
73 #if GCC_VERSION >= 4001
74 __attribute__((format (printf
, 5, 0)))
76 error_cb (cpp_reader
*, int errtype
, int, rich_location
*richloc
,
77 const char *msg
, va_list *ap
)
79 const line_map_ordinary
*map
;
80 source_location location
= richloc
->get_loc ();
81 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
82 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
83 fprintf (stderr
, "%s:%d:%d %s: ", loc
.file
, loc
.line
, loc
.column
,
84 (errtype
== CPP_DL_WARNING
) ? "warning" : "error");
85 vfprintf (stderr
, msg
, *ap
);
86 fprintf (stderr
, "\n");
87 FILE *f
= fopen (loc
.file
, "r");
93 if (!fgets (buf
, 128, f
))
95 if (buf
[strlen (buf
) - 1] != '\n')
102 fprintf (stderr
, "%s", buf
);
103 for (int i
= 0; i
< loc
.column
- 1; ++i
)
106 fputc ('\n', stderr
);
111 if (errtype
== CPP_DL_FATAL
)
117 #if GCC_VERSION >= 4001
118 __attribute__((format (printf
, 2, 3)))
120 fatal_at (const cpp_token
*tk
, const char *msg
, ...)
122 rich_location
richloc (line_table
, tk
->src_loc
);
125 error_cb (NULL
, CPP_DL_FATAL
, 0, &richloc
, msg
, &ap
);
130 #if GCC_VERSION >= 4001
131 __attribute__((format (printf
, 2, 3)))
133 fatal_at (source_location loc
, const char *msg
, ...)
135 rich_location
richloc (line_table
, loc
);
138 error_cb (NULL
, CPP_DL_FATAL
, 0, &richloc
, msg
, &ap
);
143 #if GCC_VERSION >= 4001
144 __attribute__((format (printf
, 2, 3)))
146 warning_at (const cpp_token
*tk
, const char *msg
, ...)
148 rich_location
richloc (line_table
, tk
->src_loc
);
151 error_cb (NULL
, CPP_DL_WARNING
, 0, &richloc
, msg
, &ap
);
156 #if GCC_VERSION >= 4001
157 __attribute__((format (printf
, 2, 3)))
159 warning_at (source_location loc
, const char *msg
, ...)
161 rich_location
richloc (line_table
, loc
);
164 error_cb (NULL
, CPP_DL_WARNING
, 0, &richloc
, msg
, &ap
);
168 /* Like fprintf, but print INDENT spaces at the beginning. */
171 #if GCC_VERSION >= 4001
172 __attribute__((format (printf
, 3, 4)))
174 fprintf_indent (FILE *f
, unsigned int indent
, const char *format
, ...)
177 for (; indent
>= 8; indent
-= 8)
179 fprintf (f
, "%*s", indent
, "");
180 va_start (ap
, format
);
181 vfprintf (f
, format
, ap
);
186 output_line_directive (FILE *f
, source_location location
,
187 bool dumpfile
= false)
189 const line_map_ordinary
*map
;
190 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
191 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
194 /* When writing to a dumpfile only dump the filename. */
195 const char *file
= strrchr (loc
.file
, DIR_SEPARATOR
);
196 #if defined(DIR_SEPARATOR_2)
197 const char *pos2
= strrchr (loc
.file
, DIR_SEPARATOR_2
);
198 if (pos2
&& (!file
|| (pos2
> file
)))
205 fprintf (f
, "%s:%d", file
, loc
.line
);
208 /* Other gen programs really output line directives here, at least for
209 development it's right now more convenient to have line information
210 from the generated file. Still keep the directives as comment for now
211 to easily back-point to the meta-description. */
212 fprintf (f
, "/* #line %d \"%s\" */\n", loc
.line
, loc
.file
);
216 /* Pull in tree codes and builtin function codes from their
219 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
232 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
233 enum built_in_function
{
234 #include "builtins.def"
238 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE,
240 #include "internal-fn.def"
244 /* Return true if CODE represents a commutative tree code. Otherwise
247 commutative_tree_code (enum tree_code code
)
253 case MULT_HIGHPART_EXPR
:
268 case WIDEN_MULT_EXPR
:
269 case VEC_WIDEN_MULT_HI_EXPR
:
270 case VEC_WIDEN_MULT_LO_EXPR
:
271 case VEC_WIDEN_MULT_EVEN_EXPR
:
272 case VEC_WIDEN_MULT_ODD_EXPR
:
281 /* Return true if CODE represents a ternary tree code for which the
282 first two operands are commutative. Otherwise return false. */
284 commutative_ternary_tree_code (enum tree_code code
)
288 case WIDEN_MULT_PLUS_EXPR
:
289 case WIDEN_MULT_MINUS_EXPR
:
300 /* Return true if CODE is a comparison. */
303 comparison_code_p (enum tree_code code
)
330 /* Base class for all identifiers the parser knows. */
332 struct id_base
: nofree_ptr_hash
<id_base
>
334 enum id_kind
{ CODE
, FN
, PREDICATE
, USER
, NULL_ID
} kind
;
336 id_base (id_kind
, const char *, int = -1);
342 /* hash_table support. */
343 static inline hashval_t
hash (const id_base
*);
344 static inline int equal (const id_base
*, const id_base
*);
348 id_base::hash (const id_base
*op
)
354 id_base::equal (const id_base
*op1
,
357 return (op1
->hashval
== op2
->hashval
358 && strcmp (op1
->id
, op2
->id
) == 0);
361 /* The special id "null", which matches nothing. */
362 static id_base
*null_id
;
364 /* Hashtable of known pattern operators. This is pre-seeded from
365 all known tree codes and all known builtin function ids. */
366 static hash_table
<id_base
> *operators
;
368 id_base::id_base (id_kind kind_
, const char *id_
, int nargs_
)
373 hashval
= htab_hash_string (id
);
376 /* Identifier that maps to a tree code. */
378 struct operator_id
: public id_base
380 operator_id (enum tree_code code_
, const char *id_
, unsigned nargs_
,
382 : id_base (id_base::CODE
, id_
, nargs_
), code (code_
), tcc (tcc_
) {}
387 /* Identifier that maps to a builtin or internal function code. */
389 struct fn_id
: public id_base
391 fn_id (enum built_in_function fn_
, const char *id_
)
392 : id_base (id_base::FN
, id_
), fn (fn_
) {}
393 fn_id (enum internal_fn fn_
, const char *id_
)
394 : id_base (id_base::FN
, id_
), fn (int (END_BUILTINS
) + int (fn_
)) {}
400 /* Identifier that maps to a user-defined predicate. */
402 struct predicate_id
: public id_base
404 predicate_id (const char *id_
)
405 : id_base (id_base::PREDICATE
, id_
), matchers (vNULL
) {}
406 vec
<simplify
*> matchers
;
409 /* Identifier that maps to a operator defined by a 'for' directive. */
411 struct user_id
: public id_base
413 user_id (const char *id_
, bool is_oper_list_
= false)
414 : id_base (id_base::USER
, id_
), substitutes (vNULL
),
415 used (false), is_oper_list (is_oper_list_
) {}
416 vec
<id_base
*> substitutes
;
424 is_a_helper
<fn_id
*>::test (id_base
*id
)
426 return id
->kind
== id_base::FN
;
432 is_a_helper
<operator_id
*>::test (id_base
*id
)
434 return id
->kind
== id_base::CODE
;
440 is_a_helper
<predicate_id
*>::test (id_base
*id
)
442 return id
->kind
== id_base::PREDICATE
;
448 is_a_helper
<user_id
*>::test (id_base
*id
)
450 return id
->kind
== id_base::USER
;
453 /* Add a predicate identifier to the hash. */
455 static predicate_id
*
456 add_predicate (const char *id
)
458 predicate_id
*p
= new predicate_id (id
);
459 id_base
**slot
= operators
->find_slot_with_hash (p
, p
->hashval
, INSERT
);
461 fatal ("duplicate id definition");
466 /* Add a tree code identifier to the hash. */
469 add_operator (enum tree_code code
, const char *id
,
470 const char *tcc
, unsigned nargs
)
472 if (strcmp (tcc
, "tcc_unary") != 0
473 && strcmp (tcc
, "tcc_binary") != 0
474 && strcmp (tcc
, "tcc_comparison") != 0
475 && strcmp (tcc
, "tcc_expression") != 0
476 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
477 && strcmp (tcc
, "tcc_reference") != 0
478 /* To have INTEGER_CST and friends as "predicate operators". */
479 && strcmp (tcc
, "tcc_constant") != 0
480 /* And allow CONSTRUCTOR for vector initializers. */
481 && !(code
== CONSTRUCTOR
)
482 /* Allow SSA_NAME as predicate operator. */
483 && !(code
== SSA_NAME
))
485 /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
486 if (code
== ADDR_EXPR
)
488 operator_id
*op
= new operator_id (code
, id
, nargs
, tcc
);
489 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
491 fatal ("duplicate id definition");
495 /* Add a built-in or internal function identifier to the hash. ID is
496 the name of its CFN_* enumeration value. */
498 template <typename T
>
500 add_function (T code
, const char *id
)
502 fn_id
*fn
= new fn_id (code
, id
);
503 id_base
**slot
= operators
->find_slot_with_hash (fn
, fn
->hashval
, INSERT
);
505 fatal ("duplicate id definition");
509 /* Helper for easy comparing ID with tree code CODE. */
512 operator==(id_base
&id
, enum tree_code code
)
514 if (operator_id
*oid
= dyn_cast
<operator_id
*> (&id
))
515 return oid
->code
== code
;
519 /* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */
522 get_operator (const char *id
, bool allow_null
= false)
524 if (allow_null
&& strcmp (id
, "null") == 0)
527 id_base
tem (id_base::CODE
, id
);
529 id_base
*op
= operators
->find_with_hash (&tem
, tem
.hashval
);
532 /* If this is a user-defined identifier track whether it was used. */
533 if (user_id
*uid
= dyn_cast
<user_id
*> (op
))
539 bool all_upper
= true;
540 bool all_lower
= true;
541 for (unsigned int i
= 0; id
[i
]; ++i
)
544 else if (ISLOWER (id
[i
]))
548 /* Try in caps with _EXPR appended. */
549 id2
= ACONCAT ((id
, "_EXPR", NULL
));
550 for (unsigned int i
= 0; id2
[i
]; ++i
)
551 id2
[i
] = TOUPPER (id2
[i
]);
553 else if (all_upper
&& strncmp (id
, "IFN_", 4) == 0)
554 /* Try CFN_ instead of IFN_. */
555 id2
= ACONCAT (("CFN_", id
+ 4, NULL
));
556 else if (all_upper
&& strncmp (id
, "BUILT_IN_", 9) == 0)
557 /* Try prepending CFN_. */
558 id2
= ACONCAT (("CFN_", id
, NULL
));
562 new (&tem
) id_base (id_base::CODE
, id2
);
563 return operators
->find_with_hash (&tem
, tem
.hashval
);
566 /* Return the comparison operators that results if the operands are
567 swapped. This is safe for floating-point. */
570 swap_tree_comparison (operator_id
*p
)
582 return get_operator ("LT_EXPR");
584 return get_operator ("LE_EXPR");
586 return get_operator ("GT_EXPR");
588 return get_operator ("GE_EXPR");
590 return get_operator ("UNLT_EXPR");
592 return get_operator ("UNLE_EXPR");
594 return get_operator ("UNGT_EXPR");
596 return get_operator ("UNGE_EXPR");
602 typedef hash_map
<nofree_string_hash
, unsigned> cid_map_t
;
605 /* The AST produced by parsing of the pattern definitions. */
610 /* The base class for operands. */
613 enum op_type
{ OP_PREDICATE
, OP_EXPR
, OP_CAPTURE
, OP_C_EXPR
, OP_IF
, OP_WITH
};
614 operand (enum op_type type_
, source_location loc_
)
615 : type (type_
), location (loc_
) {}
617 source_location location
;
618 virtual void gen_transform (FILE *, int, const char *, bool, int,
619 const char *, capture_info
*,
622 { gcc_unreachable (); }
625 /* A predicate operand. Predicates are leafs in the AST. */
627 struct predicate
: public operand
629 predicate (predicate_id
*p_
, source_location loc
)
630 : operand (OP_PREDICATE
, loc
), p (p_
) {}
634 /* An operand that constitutes an expression. Expressions include
635 function calls and user-defined predicate invocations. */
637 struct expr
: public operand
639 expr (id_base
*operation_
, source_location loc
, bool is_commutative_
= false)
640 : operand (OP_EXPR
, loc
), operation (operation_
),
641 ops (vNULL
), expr_type (NULL
), is_commutative (is_commutative_
),
642 is_generic (false), force_single_use (false) {}
644 : operand (OP_EXPR
, e
->location
), operation (e
->operation
),
645 ops (vNULL
), expr_type (e
->expr_type
), is_commutative (e
->is_commutative
),
646 is_generic (e
->is_generic
), force_single_use (e
->force_single_use
) {}
647 void append_op (operand
*op
) { ops
.safe_push (op
); }
648 /* The operator and its operands. */
651 /* An explicitely specified type - used exclusively for conversions. */
652 const char *expr_type
;
653 /* Whether the operation is to be applied commutatively. This is
654 later lowered to two separate patterns. */
656 /* Whether the expression is expected to be in GENERIC form. */
658 /* Whether pushing any stmt to the sequence should be conditional
659 on this expression having a single-use. */
660 bool force_single_use
;
661 virtual void gen_transform (FILE *f
, int, const char *, bool, int,
662 const char *, capture_info
*,
663 dt_operand
** = 0, int = 0);
666 /* An operator that is represented by native C code. This is always
667 a leaf operand in the AST. This class is also used to represent
668 the code to be generated for 'if' and 'with' expressions. */
670 struct c_expr
: public operand
672 /* A mapping of an identifier and its replacement. Used to apply
677 id_tab (const char *id_
, const char *oper_
): id (id_
), oper (oper_
) {}
680 c_expr (cpp_reader
*r_
, source_location loc
,
681 vec
<cpp_token
> code_
, unsigned nr_stmts_
,
682 vec
<id_tab
> ids_
, cid_map_t
*capture_ids_
)
683 : operand (OP_C_EXPR
, loc
), r (r_
), code (code_
),
684 capture_ids (capture_ids_
), nr_stmts (nr_stmts_
), ids (ids_
) {}
685 /* cpplib tokens and state to transform this back to source. */
688 cid_map_t
*capture_ids
;
689 /* The number of statements parsed (well, the number of ';'s). */
691 /* The identifier replacement vector. */
693 virtual void gen_transform (FILE *f
, int, const char *, bool, int,
694 const char *, capture_info
*,
695 dt_operand
** = 0, int = 0);
698 /* A wrapper around another operand that captures its value. */
700 struct capture
: public operand
702 capture (source_location loc
, unsigned where_
, operand
*what_
, bool value_
)
703 : operand (OP_CAPTURE
, loc
), where (where_
), value_match (value_
),
705 /* Identifier index for the value. */
707 /* Whether in a match of two operands the compare should be for
708 equal values rather than equal atoms (boils down to a type
711 /* The captured value. */
713 virtual void gen_transform (FILE *f
, int, const char *, bool, int,
714 const char *, capture_info
*,
715 dt_operand
** = 0, int = 0);
720 struct if_expr
: public operand
722 if_expr (source_location loc
)
723 : operand (OP_IF
, loc
), cond (NULL
), trueexpr (NULL
), falseexpr (NULL
) {}
729 /* with expression. */
731 struct with_expr
: public operand
733 with_expr (source_location loc
)
734 : operand (OP_WITH
, loc
), with (NULL
), subexpr (NULL
) {}
742 is_a_helper
<capture
*>::test (operand
*op
)
744 return op
->type
== operand::OP_CAPTURE
;
750 is_a_helper
<predicate
*>::test (operand
*op
)
752 return op
->type
== operand::OP_PREDICATE
;
758 is_a_helper
<c_expr
*>::test (operand
*op
)
760 return op
->type
== operand::OP_C_EXPR
;
766 is_a_helper
<expr
*>::test (operand
*op
)
768 return op
->type
== operand::OP_EXPR
;
774 is_a_helper
<if_expr
*>::test (operand
*op
)
776 return op
->type
== operand::OP_IF
;
782 is_a_helper
<with_expr
*>::test (operand
*op
)
784 return op
->type
== operand::OP_WITH
;
787 /* The main class of a pattern and its transform. This is used to
788 represent both (simplify ...) and (match ...) kinds. The AST
789 duplicates all outer 'if' and 'for' expressions here so each
790 simplify can exist in isolation. */
794 enum simplify_kind
{ SIMPLIFY
, MATCH
};
796 simplify (simplify_kind kind_
, unsigned id_
, operand
*match_
,
797 operand
*result_
, vec
<vec
<user_id
*> > for_vec_
,
798 cid_map_t
*capture_ids_
)
799 : kind (kind_
), id (id_
), match (match_
), result (result_
),
800 for_vec (for_vec_
), for_subst_vec (vNULL
),
801 capture_ids (capture_ids_
), capture_max (capture_ids_
->elements () - 1) {}
804 /* ID. This is kept to easily associate related simplifies expanded
805 from the same original one. */
807 /* The expression that is matched against the GENERIC or GIMPLE IL. */
809 /* For a (simplify ...) an expression with ifs and withs with the expression
810 produced when the pattern applies in the leafs.
811 For a (match ...) the leafs are either empty if it is a simple predicate
812 or the single expression specifying the matched operands. */
813 struct operand
*result
;
814 /* Collected 'for' expression operators that have to be replaced
815 in the lowering phase. */
816 vec
<vec
<user_id
*> > for_vec
;
817 vec
<std::pair
<user_id
*, id_base
*> > for_subst_vec
;
818 /* A map of capture identifiers to indexes. */
819 cid_map_t
*capture_ids
;
823 /* Debugging routines for dumping the AST. */
826 print_operand (operand
*o
, FILE *f
= stderr
, bool flattened
= false)
828 if (capture
*c
= dyn_cast
<capture
*> (o
))
830 if (c
->what
&& flattened
== false)
831 print_operand (c
->what
, f
, flattened
);
832 fprintf (f
, "@%u", c
->where
);
835 else if (predicate
*p
= dyn_cast
<predicate
*> (o
))
836 fprintf (f
, "%s", p
->p
->id
);
838 else if (is_a
<c_expr
*> (o
))
839 fprintf (f
, "c_expr");
841 else if (expr
*e
= dyn_cast
<expr
*> (o
))
843 if (e
->ops
.length () == 0)
844 fprintf (f
, "%s", e
->operation
->id
);
847 fprintf (f
, "(%s", e
->operation
->id
);
849 if (flattened
== false)
851 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
854 print_operand (e
->ops
[i
], f
, flattened
);
866 print_matches (struct simplify
*s
, FILE *f
= stderr
)
868 fprintf (f
, "for expression: ");
869 print_operand (s
->match
, f
);
876 /* Lowering of commutative operators. */
879 cartesian_product (const vec
< vec
<operand
*> >& ops_vector
,
880 vec
< vec
<operand
*> >& result
, vec
<operand
*>& v
, unsigned n
)
882 if (n
== ops_vector
.length ())
884 vec
<operand
*> xv
= v
.copy ();
885 result
.safe_push (xv
);
889 for (unsigned i
= 0; i
< ops_vector
[n
].length (); ++i
)
891 v
[n
] = ops_vector
[n
][i
];
892 cartesian_product (ops_vector
, result
, v
, n
+ 1);
896 /* Lower OP to two operands in case it is marked as commutative. */
898 static vec
<operand
*>
899 commutate (operand
*op
, vec
<vec
<user_id
*> > &for_vec
)
901 vec
<operand
*> ret
= vNULL
;
903 if (capture
*c
= dyn_cast
<capture
*> (op
))
910 vec
<operand
*> v
= commutate (c
->what
, for_vec
);
911 for (unsigned i
= 0; i
< v
.length (); ++i
)
913 capture
*nc
= new capture (c
->location
, c
->where
, v
[i
],
920 expr
*e
= dyn_cast
<expr
*> (op
);
921 if (!e
|| e
->ops
.length () == 0)
927 vec
< vec
<operand
*> > ops_vector
= vNULL
;
928 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
929 ops_vector
.safe_push (commutate (e
->ops
[i
], for_vec
));
931 auto_vec
< vec
<operand
*> > result
;
932 auto_vec
<operand
*> v (e
->ops
.length ());
933 v
.quick_grow_cleared (e
->ops
.length ());
934 cartesian_product (ops_vector
, result
, v
, 0);
937 for (unsigned i
= 0; i
< result
.length (); ++i
)
939 expr
*ne
= new expr (e
);
940 ne
->is_commutative
= false;
941 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
942 ne
->append_op (result
[i
][j
]);
946 if (!e
->is_commutative
)
949 for (unsigned i
= 0; i
< result
.length (); ++i
)
951 expr
*ne
= new expr (e
);
952 if (operator_id
*p
= dyn_cast
<operator_id
*> (ne
->operation
))
954 if (comparison_code_p (p
->code
))
955 ne
->operation
= swap_tree_comparison (p
);
957 else if (user_id
*p
= dyn_cast
<user_id
*> (ne
->operation
))
959 bool found_compare
= false;
960 for (unsigned j
= 0; j
< p
->substitutes
.length (); ++j
)
961 if (operator_id
*q
= dyn_cast
<operator_id
*> (p
->substitutes
[j
]))
963 if (comparison_code_p (q
->code
)
964 && swap_tree_comparison (q
) != q
)
966 found_compare
= true;
972 user_id
*newop
= new user_id ("<internal>");
973 for (unsigned j
= 0; j
< p
->substitutes
.length (); ++j
)
975 id_base
*subst
= p
->substitutes
[j
];
976 if (operator_id
*q
= dyn_cast
<operator_id
*> (subst
))
978 if (comparison_code_p (q
->code
))
979 subst
= swap_tree_comparison (q
);
981 newop
->substitutes
.safe_push (subst
);
983 ne
->operation
= newop
;
984 /* Search for 'p' inside the for vector and push 'newop'
985 to the same level. */
986 for (unsigned j
= 0; newop
&& j
< for_vec
.length (); ++j
)
987 for (unsigned k
= 0; k
< for_vec
[j
].length (); ++k
)
988 if (for_vec
[j
][k
] == p
)
990 for_vec
[j
].safe_push (newop
);
996 ne
->is_commutative
= false;
997 // result[i].length () is 2 since e->operation is binary
998 for (unsigned j
= result
[i
].length (); j
; --j
)
999 ne
->append_op (result
[i
][j
-1]);
1006 /* Lower operations marked as commutative in the AST of S and push
1007 the resulting patterns to SIMPLIFIERS. */
1010 lower_commutative (simplify
*s
, vec
<simplify
*>& simplifiers
)
1012 vec
<operand
*> matchers
= commutate (s
->match
, s
->for_vec
);
1013 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
1015 simplify
*ns
= new simplify (s
->kind
, s
->id
, matchers
[i
], s
->result
,
1016 s
->for_vec
, s
->capture_ids
);
1017 simplifiers
.safe_push (ns
);
1021 /* Strip conditional conversios using operator OPER from O and its
1022 children if STRIP, else replace them with an unconditional convert. */
1025 lower_opt_convert (operand
*o
, enum tree_code oper
,
1026 enum tree_code to_oper
, bool strip
)
1028 if (capture
*c
= dyn_cast
<capture
*> (o
))
1031 return new capture (c
->location
, c
->where
,
1032 lower_opt_convert (c
->what
, oper
, to_oper
, strip
),
1038 expr
*e
= dyn_cast
<expr
*> (o
);
1042 if (*e
->operation
== oper
)
1045 return lower_opt_convert (e
->ops
[0], oper
, to_oper
, strip
);
1047 expr
*ne
= new expr (e
);
1048 ne
->operation
= (to_oper
== CONVERT_EXPR
1049 ? get_operator ("CONVERT_EXPR")
1050 : get_operator ("VIEW_CONVERT_EXPR"));
1051 ne
->append_op (lower_opt_convert (e
->ops
[0], oper
, to_oper
, strip
));
1055 expr
*ne
= new expr (e
);
1056 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1057 ne
->append_op (lower_opt_convert (e
->ops
[i
], oper
, to_oper
, strip
));
1062 /* Determine whether O or its children uses the conditional conversion
1066 has_opt_convert (operand
*o
, enum tree_code oper
)
1068 if (capture
*c
= dyn_cast
<capture
*> (o
))
1071 return has_opt_convert (c
->what
, oper
);
1076 expr
*e
= dyn_cast
<expr
*> (o
);
1080 if (*e
->operation
== oper
)
1083 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1084 if (has_opt_convert (e
->ops
[i
], oper
))
1090 /* Lower conditional convert operators in O, expanding it to a vector
1093 static vec
<operand
*>
1094 lower_opt_convert (operand
*o
)
1096 vec
<operand
*> v1
= vNULL
, v2
;
1100 enum tree_code opers
[]
1101 = { CONVERT0
, CONVERT_EXPR
,
1102 CONVERT1
, CONVERT_EXPR
,
1103 CONVERT2
, CONVERT_EXPR
,
1104 VIEW_CONVERT0
, VIEW_CONVERT_EXPR
,
1105 VIEW_CONVERT1
, VIEW_CONVERT_EXPR
,
1106 VIEW_CONVERT2
, VIEW_CONVERT_EXPR
};
1108 /* Conditional converts are lowered to a pattern with the
1109 conversion and one without. The three different conditional
1110 convert codes are lowered separately. */
1112 for (unsigned i
= 0; i
< sizeof (opers
) / sizeof (enum tree_code
); i
+= 2)
1115 for (unsigned j
= 0; j
< v1
.length (); ++j
)
1116 if (has_opt_convert (v1
[j
], opers
[i
]))
1118 v2
.safe_push (lower_opt_convert (v1
[j
],
1119 opers
[i
], opers
[i
+1], false));
1120 v2
.safe_push (lower_opt_convert (v1
[j
],
1121 opers
[i
], opers
[i
+1], true));
1127 for (unsigned j
= 0; j
< v2
.length (); ++j
)
1128 v1
.safe_push (v2
[j
]);
1135 /* Lower conditional convert operators in the AST of S and push
1136 the resulting multiple patterns to SIMPLIFIERS. */
1139 lower_opt_convert (simplify
*s
, vec
<simplify
*>& simplifiers
)
1141 vec
<operand
*> matchers
= lower_opt_convert (s
->match
);
1142 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
1144 simplify
*ns
= new simplify (s
->kind
, s
->id
, matchers
[i
], s
->result
,
1145 s
->for_vec
, s
->capture_ids
);
1146 simplifiers
.safe_push (ns
);
1150 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
1151 GENERIC and a GIMPLE variant. */
1153 static vec
<operand
*>
1154 lower_cond (operand
*o
)
1156 vec
<operand
*> ro
= vNULL
;
1158 if (capture
*c
= dyn_cast
<capture
*> (o
))
1162 vec
<operand
*> lop
= vNULL
;
1163 lop
= lower_cond (c
->what
);
1165 for (unsigned i
= 0; i
< lop
.length (); ++i
)
1166 ro
.safe_push (new capture (c
->location
, c
->where
, lop
[i
],
1172 expr
*e
= dyn_cast
<expr
*> (o
);
1173 if (!e
|| e
->ops
.length () == 0)
1179 vec
< vec
<operand
*> > ops_vector
= vNULL
;
1180 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1181 ops_vector
.safe_push (lower_cond (e
->ops
[i
]));
1183 auto_vec
< vec
<operand
*> > result
;
1184 auto_vec
<operand
*> v (e
->ops
.length ());
1185 v
.quick_grow_cleared (e
->ops
.length ());
1186 cartesian_product (ops_vector
, result
, v
, 0);
1188 for (unsigned i
= 0; i
< result
.length (); ++i
)
1190 expr
*ne
= new expr (e
);
1191 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
1192 ne
->append_op (result
[i
][j
]);
1194 /* If this is a COND with a captured expression or an
1195 expression with two operands then also match a GENERIC
1196 form on the compare. */
1197 if ((*e
->operation
== COND_EXPR
1198 || *e
->operation
== VEC_COND_EXPR
)
1199 && ((is_a
<capture
*> (e
->ops
[0])
1200 && as_a
<capture
*> (e
->ops
[0])->what
1201 && is_a
<expr
*> (as_a
<capture
*> (e
->ops
[0])->what
)
1203 (as_a
<capture
*> (e
->ops
[0])->what
)->ops
.length () == 2)
1204 || (is_a
<expr
*> (e
->ops
[0])
1205 && as_a
<expr
*> (e
->ops
[0])->ops
.length () == 2)))
1207 expr
*ne
= new expr (e
);
1208 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
1209 ne
->append_op (result
[i
][j
]);
1210 if (capture
*c
= dyn_cast
<capture
*> (ne
->ops
[0]))
1212 expr
*ocmp
= as_a
<expr
*> (c
->what
);
1213 expr
*cmp
= new expr (ocmp
);
1214 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
1215 cmp
->append_op (ocmp
->ops
[j
]);
1216 cmp
->is_generic
= true;
1217 ne
->ops
[0] = new capture (c
->location
, c
->where
, cmp
,
1222 expr
*ocmp
= as_a
<expr
*> (ne
->ops
[0]);
1223 expr
*cmp
= new expr (ocmp
);
1224 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
1225 cmp
->append_op (ocmp
->ops
[j
]);
1226 cmp
->is_generic
= true;
1236 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
1237 GENERIC and a GIMPLE variant. */
1240 lower_cond (simplify
*s
, vec
<simplify
*>& simplifiers
)
1242 vec
<operand
*> matchers
= lower_cond (s
->match
);
1243 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
1245 simplify
*ns
= new simplify (s
->kind
, s
->id
, matchers
[i
], s
->result
,
1246 s
->for_vec
, s
->capture_ids
);
1247 simplifiers
.safe_push (ns
);
1251 /* Return true if O refers to ID. */
1254 contains_id (operand
*o
, user_id
*id
)
1256 if (capture
*c
= dyn_cast
<capture
*> (o
))
1257 return c
->what
&& contains_id (c
->what
, id
);
1259 if (expr
*e
= dyn_cast
<expr
*> (o
))
1261 if (e
->operation
== id
)
1263 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1264 if (contains_id (e
->ops
[i
], id
))
1269 if (with_expr
*w
= dyn_cast
<with_expr
*> (o
))
1270 return (contains_id (w
->with
, id
)
1271 || contains_id (w
->subexpr
, id
));
1273 if (if_expr
*ife
= dyn_cast
<if_expr
*> (o
))
1274 return (contains_id (ife
->cond
, id
)
1275 || contains_id (ife
->trueexpr
, id
)
1276 || (ife
->falseexpr
&& contains_id (ife
->falseexpr
, id
)));
1278 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
1279 return ce
->capture_ids
&& ce
->capture_ids
->get (id
->id
);
1285 /* In AST operand O replace operator ID with operator WITH. */
1288 replace_id (operand
*o
, user_id
*id
, id_base
*with
)
1290 /* Deep-copy captures and expressions, replacing operations as
1292 if (capture
*c
= dyn_cast
<capture
*> (o
))
1296 return new capture (c
->location
, c
->where
,
1297 replace_id (c
->what
, id
, with
), c
->value_match
);
1299 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1301 expr
*ne
= new expr (e
);
1302 if (e
->operation
== id
)
1303 ne
->operation
= with
;
1304 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1305 ne
->append_op (replace_id (e
->ops
[i
], id
, with
));
1308 else if (with_expr
*w
= dyn_cast
<with_expr
*> (o
))
1310 with_expr
*nw
= new with_expr (w
->location
);
1311 nw
->with
= as_a
<c_expr
*> (replace_id (w
->with
, id
, with
));
1312 nw
->subexpr
= replace_id (w
->subexpr
, id
, with
);
1315 else if (if_expr
*ife
= dyn_cast
<if_expr
*> (o
))
1317 if_expr
*nife
= new if_expr (ife
->location
);
1318 nife
->cond
= as_a
<c_expr
*> (replace_id (ife
->cond
, id
, with
));
1319 nife
->trueexpr
= replace_id (ife
->trueexpr
, id
, with
);
1321 nife
->falseexpr
= replace_id (ife
->falseexpr
, id
, with
);
1325 /* For c_expr we simply record a string replacement table which is
1326 applied at code-generation time. */
1327 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
1329 vec
<c_expr::id_tab
> ids
= ce
->ids
.copy ();
1330 ids
.safe_push (c_expr::id_tab (id
->id
, with
->id
));
1331 return new c_expr (ce
->r
, ce
->location
,
1332 ce
->code
, ce
->nr_stmts
, ids
, ce
->capture_ids
);
1338 /* Return true if the binary operator OP is ok for delayed substitution
1339 during for lowering. */
1342 binary_ok (operator_id
*op
)
1349 case TRUNC_DIV_EXPR
:
1351 case FLOOR_DIV_EXPR
:
1352 case ROUND_DIV_EXPR
:
1353 case TRUNC_MOD_EXPR
:
1355 case FLOOR_MOD_EXPR
:
1356 case ROUND_MOD_EXPR
:
1358 case EXACT_DIV_EXPR
:
1370 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1373 lower_for (simplify
*sin
, vec
<simplify
*>& simplifiers
)
1375 vec
<vec
<user_id
*> >& for_vec
= sin
->for_vec
;
1376 unsigned worklist_start
= 0;
1377 auto_vec
<simplify
*> worklist
;
1378 worklist
.safe_push (sin
);
1380 /* Lower each recorded for separately, operating on the
1381 set of simplifiers created by the previous one.
1382 Lower inner-to-outer so inner for substitutes can refer
1383 to operators replaced by outer fors. */
1384 for (int fi
= for_vec
.length () - 1; fi
>= 0; --fi
)
1386 vec
<user_id
*>& ids
= for_vec
[fi
];
1387 unsigned n_ids
= ids
.length ();
1388 unsigned max_n_opers
= 0;
1389 bool can_delay_subst
= (sin
->kind
== simplify::SIMPLIFY
);
1390 for (unsigned i
= 0; i
< n_ids
; ++i
)
1392 if (ids
[i
]->substitutes
.length () > max_n_opers
)
1393 max_n_opers
= ids
[i
]->substitutes
.length ();
1394 /* Require that all substitutes are of the same kind so that
1395 if we delay substitution to the result op code generation
1396 can look at the first substitute for deciding things like
1397 types of operands. */
1398 enum id_base::id_kind kind
= ids
[i
]->substitutes
[0]->kind
;
1399 for (unsigned j
= 0; j
< ids
[i
]->substitutes
.length (); ++j
)
1400 if (ids
[i
]->substitutes
[j
]->kind
!= kind
)
1401 can_delay_subst
= false;
1402 else if (operator_id
*op
1403 = dyn_cast
<operator_id
*> (ids
[i
]->substitutes
[j
]))
1406 = as_a
<operator_id
*> (ids
[i
]->substitutes
[0]);
1407 if (strcmp (op
->tcc
, "tcc_comparison") == 0
1408 && strcmp (op0
->tcc
, "tcc_comparison") == 0)
1410 /* Unfortunately we can't just allow all tcc_binary. */
1411 else if (strcmp (op
->tcc
, "tcc_binary") == 0
1412 && strcmp (op0
->tcc
, "tcc_binary") == 0
1416 else if ((strcmp (op
->id
+ 1, "SHIFT_EXPR") == 0
1417 || strcmp (op
->id
+ 1, "ROTATE_EXPR") == 0)
1418 && (strcmp (op0
->id
+ 1, "SHIFT_EXPR") == 0
1419 || strcmp (op0
->id
+ 1, "ROTATE_EXPR") == 0))
1422 can_delay_subst
= false;
1424 else if (is_a
<fn_id
*> (ids
[i
]->substitutes
[j
]))
1427 can_delay_subst
= false;
1430 unsigned worklist_end
= worklist
.length ();
1431 for (unsigned si
= worklist_start
; si
< worklist_end
; ++si
)
1433 simplify
*s
= worklist
[si
];
1434 for (unsigned j
= 0; j
< max_n_opers
; ++j
)
1436 operand
*match_op
= s
->match
;
1437 operand
*result_op
= s
->result
;
1438 auto_vec
<std::pair
<user_id
*, id_base
*> > subst (n_ids
);
1440 for (unsigned i
= 0; i
< n_ids
; ++i
)
1442 user_id
*id
= ids
[i
];
1443 id_base
*oper
= id
->substitutes
[j
% id
->substitutes
.length ()];
1445 && (contains_id (match_op
, id
)
1446 || contains_id (result_op
, id
)))
1451 subst
.quick_push (std::make_pair (id
, oper
));
1452 match_op
= replace_id (match_op
, id
, oper
);
1454 && !can_delay_subst
)
1455 result_op
= replace_id (result_op
, id
, oper
);
1460 simplify
*ns
= new simplify (s
->kind
, s
->id
, match_op
, result_op
,
1461 vNULL
, s
->capture_ids
);
1462 ns
->for_subst_vec
.safe_splice (s
->for_subst_vec
);
1465 ns
->for_subst_vec
.safe_splice (subst
);
1467 worklist
.safe_push (ns
);
1470 worklist_start
= worklist_end
;
1473 /* Copy out the result from the last for lowering. */
1474 for (unsigned i
= worklist_start
; i
< worklist
.length (); ++i
)
1475 simplifiers
.safe_push (worklist
[i
]);
1478 /* Lower the AST for everything in SIMPLIFIERS. */
1481 lower (vec
<simplify
*>& simplifiers
, bool gimple
)
1483 auto_vec
<simplify
*> out_simplifiers
;
1484 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1485 lower_opt_convert (simplifiers
[i
], out_simplifiers
);
1487 simplifiers
.truncate (0);
1488 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1489 lower_commutative (out_simplifiers
[i
], simplifiers
);
1491 out_simplifiers
.truncate (0);
1493 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1494 lower_cond (simplifiers
[i
], out_simplifiers
);
1496 out_simplifiers
.safe_splice (simplifiers
);
1499 simplifiers
.truncate (0);
1500 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1501 lower_for (out_simplifiers
[i
], simplifiers
);
1507 /* The decision tree built for generating GIMPLE and GENERIC pattern
1508 matching code. It represents the 'match' expression of all
1509 simplifies and has those as its leafs. */
1513 /* A hash-map collecting semantically equivalent leafs in the decision
1514 tree for splitting out to separate functions. */
1523 struct sinfo_hashmap_traits
: simple_hashmap_traits
<pointer_hash
<dt_simplify
>,
1526 static inline hashval_t
hash (const key_type
&);
1527 static inline bool equal_keys (const key_type
&, const key_type
&);
1528 template <typename T
> static inline void remove (T
&) {}
1531 typedef hash_map
<void * /* unused */, sinfo
*, sinfo_hashmap_traits
>
1534 /* Current simplifier ID we are processing during insertion into the
1536 static unsigned current_id
;
1538 /* Decision tree base class, used for DT_NODE. */
1542 enum dt_type
{ DT_NODE
, DT_OPERAND
, DT_TRUE
, DT_MATCH
, DT_SIMPLIFY
};
1547 vec
<dt_node
*> kids
;
1551 unsigned total_size
;
1554 dt_node (enum dt_type type_
, dt_node
*parent_
)
1555 : type (type_
), level (0), parent (parent_
), kids (vNULL
) {}
1557 dt_node
*append_node (dt_node
*);
1558 dt_node
*append_op (operand
*, dt_node
*parent
, unsigned pos
);
1559 dt_node
*append_true_op (operand
*, dt_node
*parent
, unsigned pos
);
1560 dt_node
*append_match_op (operand
*, dt_operand
*, dt_node
*parent
,
1562 dt_node
*append_simplify (simplify
*, unsigned, dt_operand
**);
1564 virtual void gen (FILE *, int, bool) {}
1566 void gen_kids (FILE *, int, bool);
1567 void gen_kids_1 (FILE *, int, bool,
1568 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_operand
*>,
1569 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_node
*>);
1571 void analyze (sinfo_map_t
&);
1574 /* Generic decision tree node used for DT_OPERAND, DT_MATCH and DT_TRUE. */
1576 struct dt_operand
: public dt_node
1579 dt_operand
*match_dop
;
1584 dt_operand (enum dt_type type
, operand
*op_
, dt_operand
*match_dop_
,
1585 dt_operand
*parent_
, unsigned pos_
)
1586 : dt_node (type
, parent_
), op (op_
), match_dop (match_dop_
),
1587 pos (pos_
), value_match (false), for_id (current_id
) {}
1589 void gen (FILE *, int, bool);
1590 unsigned gen_predicate (FILE *, int, const char *, bool);
1591 unsigned gen_match_op (FILE *, int, const char *, bool);
1593 unsigned gen_gimple_expr (FILE *, int);
1594 unsigned gen_generic_expr (FILE *, int, const char *);
1596 char *get_name (char *);
1597 void gen_opname (char *, unsigned);
1600 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1602 struct dt_simplify
: public dt_node
1605 unsigned pattern_no
;
1606 dt_operand
**indexes
;
1609 dt_simplify (simplify
*s_
, unsigned pattern_no_
, dt_operand
**indexes_
)
1610 : dt_node (DT_SIMPLIFY
, NULL
), s (s_
), pattern_no (pattern_no_
),
1611 indexes (indexes_
), info (NULL
) {}
1613 void gen_1 (FILE *, int, bool, operand
*);
1614 void gen (FILE *f
, int, bool);
1620 is_a_helper
<dt_operand
*>::test (dt_node
*n
)
1622 return (n
->type
== dt_node::DT_OPERAND
1623 || n
->type
== dt_node::DT_MATCH
1624 || n
->type
== dt_node::DT_TRUE
);
1630 is_a_helper
<dt_simplify
*>::test (dt_node
*n
)
1632 return n
->type
== dt_node::DT_SIMPLIFY
;
1637 /* A container for the actual decision tree. */
1639 struct decision_tree
1643 void insert (struct simplify
*, unsigned);
1644 void gen (FILE *f
, bool gimple
);
1645 void print (FILE *f
= stderr
);
1647 decision_tree () { root
= new dt_node (dt_node::DT_NODE
, NULL
); }
1649 static dt_node
*insert_operand (dt_node
*, operand
*, dt_operand
**indexes
,
1650 unsigned pos
= 0, dt_node
*parent
= 0);
1651 static dt_node
*find_node (vec
<dt_node
*>&, dt_node
*);
1652 static bool cmp_node (dt_node
*, dt_node
*);
1653 static void print_node (dt_node
*, FILE *f
= stderr
, unsigned = 0);
1656 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1659 cmp_operand (operand
*o1
, operand
*o2
)
1661 if (!o1
|| !o2
|| o1
->type
!= o2
->type
)
1664 if (o1
->type
== operand::OP_PREDICATE
)
1666 predicate
*p1
= as_a
<predicate
*>(o1
);
1667 predicate
*p2
= as_a
<predicate
*>(o2
);
1668 return p1
->p
== p2
->p
;
1670 else if (o1
->type
== operand::OP_EXPR
)
1672 expr
*e1
= static_cast<expr
*>(o1
);
1673 expr
*e2
= static_cast<expr
*>(o2
);
1674 return (e1
->operation
== e2
->operation
1675 && e1
->is_generic
== e2
->is_generic
);
1681 /* Compare two decision tree nodes N1 and N2 and return true if they
1685 decision_tree::cmp_node (dt_node
*n1
, dt_node
*n2
)
1687 if (!n1
|| !n2
|| n1
->type
!= n2
->type
)
1693 if (n1
->type
== dt_node::DT_TRUE
)
1696 if (n1
->type
== dt_node::DT_OPERAND
)
1697 return cmp_operand ((as_a
<dt_operand
*> (n1
))->op
,
1698 (as_a
<dt_operand
*> (n2
))->op
);
1699 else if (n1
->type
== dt_node::DT_MATCH
)
1700 return (((as_a
<dt_operand
*> (n1
))->match_dop
1701 == (as_a
<dt_operand
*> (n2
))->match_dop
)
1702 && ((as_a
<dt_operand
*> (n1
))->value_match
1703 == (as_a
<dt_operand
*> (n2
))->value_match
));
1707 /* Search OPS for a decision tree node like P and return it if found. */
1710 decision_tree::find_node (vec
<dt_node
*>& ops
, dt_node
*p
)
1712 /* We can merge adjacent DT_TRUE. */
1713 if (p
->type
== dt_node::DT_TRUE
1715 && ops
.last ()->type
== dt_node::DT_TRUE
)
1717 dt_operand
*true_node
= NULL
;
1718 for (int i
= ops
.length () - 1; i
>= 0; --i
)
1720 /* But we can't merge across DT_TRUE nodes as they serve as
1721 pattern order barriers to make sure that patterns apply
1722 in order of appearance in case multiple matches are possible. */
1723 if (ops
[i
]->type
== dt_node::DT_TRUE
)
1726 || as_a
<dt_operand
*> (ops
[i
])->for_id
> true_node
->for_id
)
1727 true_node
= as_a
<dt_operand
*> (ops
[i
]);
1729 if (decision_tree::cmp_node (ops
[i
], p
))
1731 /* Unless we are processing the same pattern or the blocking
1732 pattern is before the one we are going to merge with. */
1734 && true_node
->for_id
!= current_id
1735 && true_node
->for_id
> as_a
<dt_operand
*> (ops
[i
])->for_id
)
1739 source_location p_loc
= 0;
1740 if (p
->type
== dt_node::DT_OPERAND
)
1741 p_loc
= as_a
<dt_operand
*> (p
)->op
->location
;
1742 source_location op_loc
= 0;
1743 if (ops
[i
]->type
== dt_node::DT_OPERAND
)
1744 op_loc
= as_a
<dt_operand
*> (ops
[i
])->op
->location
;
1745 source_location true_loc
= 0;
1746 true_loc
= true_node
->op
->location
;
1748 "failed to merge decision tree node");
1750 "with the following");
1751 warning_at (true_loc
,
1752 "because of the following which serves as ordering "
1763 /* Append N to the decision tree if it there is not already an existing
1767 dt_node::append_node (dt_node
*n
)
1771 kid
= decision_tree::find_node (kids
, n
);
1776 n
->level
= this->level
+ 1;
1781 /* Append OP to the decision tree. */
1784 dt_node::append_op (operand
*op
, dt_node
*parent
, unsigned pos
)
1786 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1787 dt_operand
*n
= new dt_operand (DT_OPERAND
, op
, 0, parent_
, pos
);
1788 return append_node (n
);
1791 /* Append a DT_TRUE decision tree node. */
1794 dt_node::append_true_op (operand
*op
, dt_node
*parent
, unsigned pos
)
1796 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1797 dt_operand
*n
= new dt_operand (DT_TRUE
, op
, 0, parent_
, pos
);
1798 return append_node (n
);
1801 /* Append a DT_MATCH decision tree node. */
1804 dt_node::append_match_op (operand
*op
, dt_operand
*match_dop
,
1805 dt_node
*parent
, unsigned pos
)
1807 dt_operand
*parent_
= as_a
<dt_operand
*> (parent
);
1808 dt_operand
*n
= new dt_operand (DT_MATCH
, op
, match_dop
, parent_
, pos
);
1809 return append_node (n
);
1812 /* Append S to the decision tree. */
1815 dt_node::append_simplify (simplify
*s
, unsigned pattern_no
,
1816 dt_operand
**indexes
)
1818 dt_simplify
*n
= new dt_simplify (s
, pattern_no
, indexes
);
1819 for (unsigned i
= 0; i
< kids
.length (); ++i
)
1820 if (dt_simplify
*s2
= dyn_cast
<dt_simplify
*> (kids
[i
]))
1822 warning_at (s
->match
->location
, "duplicate pattern");
1823 warning_at (s2
->s
->match
->location
, "previous pattern defined here");
1824 print_operand (s
->match
, stderr
);
1825 fprintf (stderr
, "\n");
1827 return append_node (n
);
1830 /* Analyze the node and its children. */
1833 dt_node::analyze (sinfo_map_t
&map
)
1839 if (type
== DT_SIMPLIFY
)
1841 /* Populate the map of equivalent simplifies. */
1842 dt_simplify
*s
= as_a
<dt_simplify
*> (this);
1844 sinfo
*&si
= map
.get_or_insert (s
, &existed
);
1859 for (unsigned i
= 0; i
< kids
.length (); ++i
)
1861 kids
[i
]->analyze (map
);
1862 num_leafs
+= kids
[i
]->num_leafs
;
1863 total_size
+= kids
[i
]->total_size
;
1864 max_level
= MAX (max_level
, kids
[i
]->max_level
);
1868 /* Insert O into the decision tree and return the decision tree node found
1872 decision_tree::insert_operand (dt_node
*p
, operand
*o
, dt_operand
**indexes
,
1873 unsigned pos
, dt_node
*parent
)
1875 dt_node
*q
, *elm
= 0;
1877 if (capture
*c
= dyn_cast
<capture
*> (o
))
1879 unsigned capt_index
= c
->where
;
1881 if (indexes
[capt_index
] == 0)
1884 q
= insert_operand (p
, c
->what
, indexes
, pos
, parent
);
1887 q
= elm
= p
->append_true_op (o
, parent
, pos
);
1890 // get to the last capture
1891 for (operand
*what
= c
->what
;
1892 what
&& is_a
<capture
*> (what
);
1893 c
= as_a
<capture
*> (what
), what
= c
->what
)
1898 unsigned cc_index
= c
->where
;
1899 dt_operand
*match_op
= indexes
[cc_index
];
1901 dt_operand
temp (dt_node::DT_TRUE
, 0, 0, 0, 0);
1902 elm
= decision_tree::find_node (p
->kids
, &temp
);
1906 dt_operand
temp (dt_node::DT_MATCH
, 0, match_op
, 0, 0);
1907 temp
.value_match
= c
->value_match
;
1908 elm
= decision_tree::find_node (p
->kids
, &temp
);
1913 dt_operand
temp (dt_node::DT_OPERAND
, c
->what
, 0, 0, 0);
1914 elm
= decision_tree::find_node (p
->kids
, &temp
);
1918 gcc_assert (elm
->type
== dt_node::DT_TRUE
1919 || elm
->type
== dt_node::DT_OPERAND
1920 || elm
->type
== dt_node::DT_MATCH
);
1921 indexes
[capt_index
] = static_cast<dt_operand
*> (elm
);
1926 p
= p
->append_match_op (o
, indexes
[capt_index
], parent
, pos
);
1927 as_a
<dt_operand
*>(p
)->value_match
= c
->value_match
;
1929 return insert_operand (p
, c
->what
, indexes
, 0, p
);
1934 p
= p
->append_op (o
, parent
, pos
);
1937 if (expr
*e
= dyn_cast
<expr
*>(o
))
1939 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1940 q
= decision_tree::insert_operand (q
, e
->ops
[i
], indexes
, i
, p
);
1946 /* Insert S into the decision tree. */
1949 decision_tree::insert (struct simplify
*s
, unsigned pattern_no
)
1952 dt_operand
**indexes
= XCNEWVEC (dt_operand
*, s
->capture_max
+ 1);
1953 dt_node
*p
= decision_tree::insert_operand (root
, s
->match
, indexes
);
1954 p
->append_simplify (s
, pattern_no
, indexes
);
1957 /* Debug functions to dump the decision tree. */
1960 decision_tree::print_node (dt_node
*p
, FILE *f
, unsigned indent
)
1962 if (p
->type
== dt_node::DT_NODE
)
1963 fprintf (f
, "root");
1967 for (unsigned i
= 0; i
< indent
; i
++)
1970 if (p
->type
== dt_node::DT_OPERAND
)
1972 dt_operand
*dop
= static_cast<dt_operand
*>(p
);
1973 print_operand (dop
->op
, f
, true);
1975 else if (p
->type
== dt_node::DT_TRUE
)
1976 fprintf (f
, "true");
1977 else if (p
->type
== dt_node::DT_MATCH
)
1978 fprintf (f
, "match (%p)", (void *)((as_a
<dt_operand
*>(p
))->match_dop
));
1979 else if (p
->type
== dt_node::DT_SIMPLIFY
)
1981 dt_simplify
*s
= static_cast<dt_simplify
*> (p
);
1982 fprintf (f
, "simplify_%u { ", s
->pattern_no
);
1983 for (int i
= 0; i
<= s
->s
->capture_max
; ++i
)
1984 fprintf (f
, "%p, ", (void *) s
->indexes
[i
]);
1987 if (is_a
<dt_operand
*> (p
))
1988 fprintf (f
, " [%u]", as_a
<dt_operand
*> (p
)->for_id
);
1991 fprintf (stderr
, " (%p, %p), %u, %u\n",
1992 (void *) p
, (void *) p
->parent
, p
->level
, p
->kids
.length ());
1994 for (unsigned i
= 0; i
< p
->kids
.length (); ++i
)
1995 decision_tree::print_node (p
->kids
[i
], f
, indent
+ 2);
1999 decision_tree::print (FILE *f
)
2001 return decision_tree::print_node (root
, f
);
2005 /* For GENERIC we have to take care of wrapping multiple-used
2006 expressions with side-effects in save_expr and preserve side-effects
2007 of expressions with omit_one_operand. Analyze captures in
2008 match, result and with expressions and perform early-outs
2009 on the outermost match expression operands for cases we cannot
2014 capture_info (simplify
*s
, operand
*, bool);
2015 void walk_match (operand
*o
, unsigned toplevel_arg
, bool, bool);
2016 bool walk_result (operand
*o
, bool, operand
*);
2017 void walk_c_expr (c_expr
*);
2023 bool force_no_side_effects_p
;
2024 bool force_single_use
;
2025 bool cond_expr_cond_p
;
2026 unsigned long toplevel_msk
;
2027 unsigned match_use_count
;
2028 unsigned result_use_count
;
2033 auto_vec
<cinfo
> info
;
2034 unsigned long force_no_side_effects
;
2038 /* Analyze captures in S. */
2040 capture_info::capture_info (simplify
*s
, operand
*result
, bool gimple_
)
2045 if (s
->kind
== simplify::MATCH
)
2047 force_no_side_effects
= -1;
2051 force_no_side_effects
= 0;
2052 info
.safe_grow_cleared (s
->capture_max
+ 1);
2053 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2054 info
[i
].same_as
= i
;
2056 e
= as_a
<expr
*> (s
->match
);
2057 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
2058 walk_match (e
->ops
[i
], i
,
2059 (i
!= 0 && *e
->operation
== COND_EXPR
)
2060 || *e
->operation
== TRUTH_ANDIF_EXPR
2061 || *e
->operation
== TRUTH_ORIF_EXPR
,
2063 && (*e
->operation
== COND_EXPR
2064 || *e
->operation
== VEC_COND_EXPR
));
2066 walk_result (s
->result
, false, result
);
2069 /* Analyze captures in the match expression piece O. */
2072 capture_info::walk_match (operand
*o
, unsigned toplevel_arg
,
2073 bool conditional_p
, bool cond_expr_cond_p
)
2075 if (capture
*c
= dyn_cast
<capture
*> (o
))
2077 unsigned where
= c
->where
;
2078 info
[where
].match_use_count
++;
2079 info
[where
].toplevel_msk
|= 1 << toplevel_arg
;
2080 info
[where
].force_no_side_effects_p
|= conditional_p
;
2081 info
[where
].cond_expr_cond_p
|= cond_expr_cond_p
;
2086 /* Recurse to exprs and captures. */
2087 if (is_a
<capture
*> (c
->what
)
2088 || is_a
<expr
*> (c
->what
))
2089 walk_match (c
->what
, toplevel_arg
, conditional_p
, false);
2090 /* We need to look past multiple captures to find a captured
2091 expression as with conditional converts two captures
2092 can be collapsed onto the same expression. Also collect
2093 what captures capture the same thing. */
2094 while (c
->what
&& is_a
<capture
*> (c
->what
))
2096 c
= as_a
<capture
*> (c
->what
);
2097 if (info
[c
->where
].same_as
!= c
->where
2098 && info
[c
->where
].same_as
!= info
[where
].same_as
)
2099 fatal_at (c
->location
, "cannot handle this collapsed capture");
2100 info
[c
->where
].same_as
= info
[where
].same_as
;
2102 /* Mark expr (non-leaf) captures and forced single-use exprs. */
2105 && (e
= dyn_cast
<expr
*> (c
->what
)))
2107 /* Zero-operand expression captures like ADDR_EXPR@0 are
2108 similar as predicates -- if they are not mentioned in
2109 the result we have to force them to have no side-effects. */
2110 if (e
->ops
.length () != 0)
2111 info
[where
].expr_p
= true;
2112 info
[where
].force_single_use
|= e
->force_single_use
;
2115 else if (expr
*e
= dyn_cast
<expr
*> (o
))
2117 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
2119 bool cond_p
= conditional_p
;
2120 bool cond_expr_cond_p
= false;
2121 if (i
!= 0 && *e
->operation
== COND_EXPR
)
2123 else if (*e
->operation
== TRUTH_ANDIF_EXPR
2124 || *e
->operation
== TRUTH_ORIF_EXPR
)
2127 && (*e
->operation
== COND_EXPR
2128 || *e
->operation
== VEC_COND_EXPR
))
2129 cond_expr_cond_p
= true;
2130 walk_match (e
->ops
[i
], toplevel_arg
, cond_p
, cond_expr_cond_p
);
2133 else if (is_a
<predicate
*> (o
))
2135 /* Mark non-captured leafs toplevel arg for checking. */
2136 force_no_side_effects
|= 1 << toplevel_arg
;
2139 warning_at (o
->location
,
2140 "forcing no side-effects on possibly lost leaf");
2146 /* Analyze captures in the result expression piece O. Return true
2147 if RESULT was visited in one of the children. Only visit
2148 non-if/with children if they are rooted on RESULT. */
2151 capture_info::walk_result (operand
*o
, bool conditional_p
, operand
*result
)
2153 if (capture
*c
= dyn_cast
<capture
*> (o
))
2155 unsigned where
= info
[c
->where
].same_as
;
2156 info
[where
].result_use_count
++;
2157 /* If we substitute an expression capture we don't know
2158 which captures this will end up using (well, we don't
2159 compute that). Force the uses to be side-effect free
2160 which means forcing the toplevels that reach the
2161 expression side-effect free. */
2162 if (info
[where
].expr_p
)
2163 force_no_side_effects
|= info
[where
].toplevel_msk
;
2164 /* Mark CSE capture uses as forced to have no side-effects. */
2166 && is_a
<expr
*> (c
->what
))
2168 info
[where
].cse_p
= true;
2169 walk_result (c
->what
, true, result
);
2172 else if (expr
*e
= dyn_cast
<expr
*> (o
))
2174 id_base
*opr
= e
->operation
;
2175 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
2176 opr
= uid
->substitutes
[0];
2177 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
2179 bool cond_p
= conditional_p
;
2180 if (i
!= 0 && *e
->operation
== COND_EXPR
)
2182 else if (*e
->operation
== TRUTH_ANDIF_EXPR
2183 || *e
->operation
== TRUTH_ORIF_EXPR
)
2185 walk_result (e
->ops
[i
], cond_p
, result
);
2188 else if (if_expr
*e
= dyn_cast
<if_expr
*> (o
))
2190 /* 'if' conditions should be all fine. */
2191 if (e
->trueexpr
== result
)
2193 walk_result (e
->trueexpr
, false, result
);
2196 if (e
->falseexpr
== result
)
2198 walk_result (e
->falseexpr
, false, result
);
2202 if (is_a
<if_expr
*> (e
->trueexpr
)
2203 || is_a
<with_expr
*> (e
->trueexpr
))
2204 res
|= walk_result (e
->trueexpr
, false, result
);
2206 && (is_a
<if_expr
*> (e
->falseexpr
)
2207 || is_a
<with_expr
*> (e
->falseexpr
)))
2208 res
|= walk_result (e
->falseexpr
, false, result
);
2211 else if (with_expr
*e
= dyn_cast
<with_expr
*> (o
))
2213 bool res
= (e
->subexpr
== result
);
2215 || is_a
<if_expr
*> (e
->subexpr
)
2216 || is_a
<with_expr
*> (e
->subexpr
))
2217 res
|= walk_result (e
->subexpr
, false, result
);
2219 walk_c_expr (e
->with
);
2222 else if (c_expr
*e
= dyn_cast
<c_expr
*> (o
))
2230 /* Look for captures in the C expr E. */
2233 capture_info::walk_c_expr (c_expr
*e
)
2235 /* Give up for C exprs mentioning captures not inside TREE_TYPE,
2236 TREE_REAL_CST, TREE_CODE or a predicate where they cannot
2237 really escape through. */
2238 unsigned p_depth
= 0;
2239 for (unsigned i
= 0; i
< e
->code
.length (); ++i
)
2241 const cpp_token
*t
= &e
->code
[i
];
2242 const cpp_token
*n
= i
< e
->code
.length () - 1 ? &e
->code
[i
+1] : NULL
;
2244 if (t
->type
== CPP_NAME
2245 && (strcmp ((const char *)CPP_HASHNODE
2246 (t
->val
.node
.node
)->ident
.str
, "TREE_TYPE") == 0
2247 || strcmp ((const char *)CPP_HASHNODE
2248 (t
->val
.node
.node
)->ident
.str
, "TREE_CODE") == 0
2249 || strcmp ((const char *)CPP_HASHNODE
2250 (t
->val
.node
.node
)->ident
.str
, "TREE_REAL_CST") == 0
2251 || ((id
= get_operator ((const char *)CPP_HASHNODE
2252 (t
->val
.node
.node
)->ident
.str
))
2253 && is_a
<predicate_id
*> (id
)))
2254 && n
->type
== CPP_OPEN_PAREN
)
2256 else if (t
->type
== CPP_CLOSE_PAREN
2259 else if (p_depth
== 0
2260 && t
->type
== CPP_ATSIGN
2261 && (n
->type
== CPP_NUMBER
2262 || n
->type
== CPP_NAME
)
2263 && !(n
->flags
& PREV_WHITE
))
2266 if (n
->type
== CPP_NUMBER
)
2267 id
= (const char *)n
->val
.str
.text
;
2269 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
2270 unsigned *where
= e
->capture_ids
->get(id
);
2272 fatal_at (n
, "unknown capture id '%s'", id
);
2273 info
[info
[*where
].same_as
].force_no_side_effects_p
= true;
2276 warning_at (t
, "capture escapes");
2282 /* Code generation off the decision tree and the refered AST nodes. */
2285 is_conversion (id_base
*op
)
2287 return (*op
== CONVERT_EXPR
2289 || *op
== FLOAT_EXPR
2290 || *op
== FIX_TRUNC_EXPR
2291 || *op
== VIEW_CONVERT_EXPR
);
2294 /* Get the type to be used for generating operand POS of OP from the
2298 get_operand_type (id_base
*op
, unsigned pos
,
2299 const char *in_type
,
2300 const char *expr_type
,
2301 const char *other_oprnd_type
)
2303 /* Generally operands whose type does not match the type of the
2304 expression generated need to know their types but match and
2305 thus can fall back to 'other_oprnd_type'. */
2306 if (is_conversion (op
))
2307 return other_oprnd_type
;
2308 else if (*op
== REALPART_EXPR
2309 || *op
== IMAGPART_EXPR
)
2310 return other_oprnd_type
;
2311 else if (is_a
<operator_id
*> (op
)
2312 && strcmp (as_a
<operator_id
*> (op
)->tcc
, "tcc_comparison") == 0)
2313 return other_oprnd_type
;
2314 else if (*op
== COND_EXPR
2316 return "boolean_type_node";
2319 /* Otherwise all types should match - choose one in order of
2326 return other_oprnd_type
;
2330 /* Generate transform code for an expression. */
2333 expr::gen_transform (FILE *f
, int indent
, const char *dest
, bool gimple
,
2334 int depth
, const char *in_type
, capture_info
*cinfo
,
2335 dt_operand
**indexes
, int)
2337 id_base
*opr
= operation
;
2338 /* When we delay operator substituting during lowering of fors we
2339 make sure that for code-gen purposes the effects of each substitute
2340 are the same. Thus just look at that. */
2341 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
2342 opr
= uid
->substitutes
[0];
2344 bool conversion_p
= is_conversion (opr
);
2345 const char *type
= expr_type
;
2348 /* If there was a type specification in the pattern use it. */
2350 else if (conversion_p
)
2351 /* For conversions we need to build the expression using the
2352 outer type passed in. */
2354 else if (*opr
== REALPART_EXPR
2355 || *opr
== IMAGPART_EXPR
)
2357 /* __real and __imag use the component type of its operand. */
2358 sprintf (optype
, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth
);
2361 else if (is_a
<operator_id
*> (opr
)
2362 && !strcmp (as_a
<operator_id
*> (opr
)->tcc
, "tcc_comparison"))
2364 /* comparisons use boolean_type_node (or what gets in), but
2365 their operands need to figure out the types themselves. */
2370 sprintf (optype
, "boolean_type_node");
2375 else if (*opr
== COND_EXPR
2376 || *opr
== VEC_COND_EXPR
)
2378 /* Conditions are of the same type as their first alternative. */
2379 sprintf (optype
, "TREE_TYPE (ops%d[1])", depth
);
2384 /* Other operations are of the same type as their first operand. */
2385 sprintf (optype
, "TREE_TYPE (ops%d[0])", depth
);
2389 fatal_at (location
, "cannot determine type of operand");
2391 fprintf_indent (f
, indent
, "{\n");
2393 fprintf_indent (f
, indent
, "tree ops%d[%u], res;\n", depth
, ops
.length ());
2395 snprintf (op0type
, 64, "TREE_TYPE (ops%d[0])", depth
);
2396 for (unsigned i
= 0; i
< ops
.length (); ++i
)
2399 snprintf (dest
, 32, "ops%d[%u]", depth
, i
);
2401 = get_operand_type (opr
, i
, in_type
, expr_type
,
2402 i
== 0 ? NULL
: op0type
);
2403 ops
[i
]->gen_transform (f
, indent
, dest
, gimple
, depth
+ 1, optype
,
2406 || *opr
== VEC_COND_EXPR
) && i
== 0 ? 1 : 2);
2409 const char *opr_name
;
2410 if (*operation
== CONVERT_EXPR
)
2411 opr_name
= "NOP_EXPR";
2413 opr_name
= operation
->id
;
2417 if (*opr
== CONVERT_EXPR
)
2419 fprintf_indent (f
, indent
,
2420 "if (%s != TREE_TYPE (ops%d[0])\n",
2422 fprintf_indent (f
, indent
,
2423 " && !useless_type_conversion_p (%s, TREE_TYPE (ops%d[0])))\n",
2425 fprintf_indent (f
, indent
+ 2, "{\n");
2428 /* ??? Building a stmt can fail for various reasons here, seq being
2429 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
2430 So if we fail here we should continue matching other patterns. */
2431 fprintf_indent (f
, indent
, "code_helper tem_code = %s;\n", opr_name
);
2432 fprintf_indent (f
, indent
, "tree tem_ops[3] = { ");
2433 for (unsigned i
= 0; i
< ops
.length (); ++i
)
2434 fprintf (f
, "ops%d[%u]%s", depth
, i
,
2435 i
== ops
.length () - 1 ? " };\n" : ", ");
2436 fprintf_indent (f
, indent
,
2437 "gimple_resimplify%d (lseq, &tem_code, %s, tem_ops, valueize);\n",
2438 ops
.length (), type
);
2439 fprintf_indent (f
, indent
,
2440 "res = maybe_push_res_to_seq (tem_code, %s, tem_ops, lseq);\n",
2442 fprintf_indent (f
, indent
,
2443 "if (!res) return false;\n");
2444 if (*opr
== CONVERT_EXPR
)
2447 fprintf_indent (f
, indent
, " }\n");
2448 fprintf_indent (f
, indent
, "else\n");
2449 fprintf_indent (f
, indent
, " res = ops%d[0];\n", depth
);
2454 if (*opr
== CONVERT_EXPR
)
2456 fprintf_indent (f
, indent
, "if (TREE_TYPE (ops%d[0]) != %s)\n",
2460 if (opr
->kind
== id_base::CODE
)
2461 fprintf_indent (f
, indent
, "res = fold_build%d_loc (loc, %s, %s",
2462 ops
.length(), opr_name
, type
);
2465 fprintf_indent (f
, indent
, "{\n");
2466 fprintf_indent (f
, indent
, " res = maybe_build_call_expr_loc (loc, "
2467 "%s, %s, %d", opr_name
, type
, ops
.length());
2469 for (unsigned i
= 0; i
< ops
.length (); ++i
)
2470 fprintf (f
, ", ops%d[%u]", depth
, i
);
2471 fprintf (f
, ");\n");
2472 if (opr
->kind
!= id_base::CODE
)
2474 fprintf_indent (f
, indent
, " if (!res)\n");
2475 fprintf_indent (f
, indent
, " return NULL_TREE;\n");
2476 fprintf_indent (f
, indent
, "}\n");
2478 if (*opr
== CONVERT_EXPR
)
2481 fprintf_indent (f
, indent
, "else\n");
2482 fprintf_indent (f
, indent
, " res = ops%d[0];\n", depth
);
2485 fprintf_indent (f
, indent
, "%s = res;\n", dest
);
2487 fprintf_indent (f
, indent
, "}\n");
2490 /* Generate code for a c_expr which is either the expression inside
2491 an if statement or a sequence of statements which computes a
2492 result to be stored to DEST. */
2495 c_expr::gen_transform (FILE *f
, int indent
, const char *dest
,
2496 bool, int, const char *, capture_info
*,
2499 if (dest
&& nr_stmts
== 1)
2500 fprintf_indent (f
, indent
, "%s = ", dest
);
2502 unsigned stmt_nr
= 1;
2503 for (unsigned i
= 0; i
< code
.length (); ++i
)
2505 const cpp_token
*token
= &code
[i
];
2507 /* Replace captures for code-gen. */
2508 if (token
->type
== CPP_ATSIGN
)
2510 const cpp_token
*n
= &code
[i
+1];
2511 if ((n
->type
== CPP_NUMBER
2512 || n
->type
== CPP_NAME
)
2513 && !(n
->flags
& PREV_WHITE
))
2515 if (token
->flags
& PREV_WHITE
)
2518 if (n
->type
== CPP_NUMBER
)
2519 id
= (const char *)n
->val
.str
.text
;
2521 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
2522 unsigned *cid
= capture_ids
->get (id
);
2524 fatal_at (token
, "unknown capture id");
2525 fprintf (f
, "captures[%u]", *cid
);
2531 if (token
->flags
& PREV_WHITE
)
2534 if (token
->type
== CPP_NAME
)
2536 const char *id
= (const char *) NODE_NAME (token
->val
.node
.node
);
2538 for (j
= 0; j
< ids
.length (); ++j
)
2540 if (strcmp (id
, ids
[j
].id
) == 0)
2542 fprintf (f
, "%s", ids
[j
].oper
);
2546 if (j
< ids
.length ())
2550 /* Output the token as string. */
2551 char *tk
= (char *)cpp_token_as_text (r
, token
);
2554 if (token
->type
== CPP_SEMICOLON
)
2558 if (dest
&& stmt_nr
== nr_stmts
)
2559 fprintf_indent (f
, indent
, "%s = ", dest
);
2564 /* Generate transform code for a capture. */
2567 capture::gen_transform (FILE *f
, int indent
, const char *dest
, bool gimple
,
2568 int depth
, const char *in_type
, capture_info
*cinfo
,
2569 dt_operand
**indexes
, int cond_handling
)
2571 if (what
&& is_a
<expr
*> (what
))
2573 if (indexes
[where
] == 0)
2576 sprintf (buf
, "captures[%u]", where
);
2577 what
->gen_transform (f
, indent
, buf
, gimple
, depth
, in_type
,
2582 /* If in GENERIC some capture is used multiple times, unshare it except
2583 when emitting the last use. */
2585 && cinfo
->info
.exists ()
2586 && cinfo
->info
[cinfo
->info
[where
].same_as
].result_use_count
> 1)
2588 fprintf_indent (f
, indent
, "%s = unshare_expr (captures[%u]);\n",
2590 cinfo
->info
[cinfo
->info
[where
].same_as
].result_use_count
--;
2593 fprintf_indent (f
, indent
, "%s = captures[%u];\n", dest
, where
);
2595 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2596 with substituting a capture of that. */
2598 && cond_handling
!= 0
2599 && cinfo
->info
[where
].cond_expr_cond_p
)
2601 /* If substituting into a cond_expr condition, unshare. */
2602 if (cond_handling
== 1)
2603 fprintf_indent (f
, indent
, "%s = unshare_expr (%s);\n", dest
, dest
);
2604 /* If substituting elsewhere we might need to decompose it. */
2605 else if (cond_handling
== 2)
2607 /* ??? Returning false here will also not allow any other patterns
2608 to match unless this generator was split out. */
2609 fprintf_indent (f
, indent
, "if (COMPARISON_CLASS_P (%s))\n", dest
);
2610 fprintf_indent (f
, indent
, " {\n");
2611 fprintf_indent (f
, indent
, " if (!seq) return false;\n");
2612 fprintf_indent (f
, indent
, " %s = gimple_build (seq,"
2614 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
2615 " TREE_OPERAND (%s, 1));\n",
2616 dest
, dest
, dest
, dest
, dest
);
2617 fprintf_indent (f
, indent
, " }\n");
2622 /* Return the name of the operand representing the decision tree node.
2623 Use NAME as space to generate it. */
2626 dt_operand::get_name (char *name
)
2629 sprintf (name
, "t");
2630 else if (parent
->level
== 1)
2631 sprintf (name
, "op%u", pos
);
2632 else if (parent
->type
== dt_node::DT_MATCH
)
2633 return as_a
<dt_operand
*> (parent
)->get_name (name
);
2635 sprintf (name
, "o%u%u", parent
->level
, pos
);
2639 /* Fill NAME with the operand name at position POS. */
2642 dt_operand::gen_opname (char *name
, unsigned pos
)
2645 sprintf (name
, "op%u", pos
);
2647 sprintf (name
, "o%u%u", level
, pos
);
2650 /* Generate matching code for the decision tree operand which is
2654 dt_operand::gen_predicate (FILE *f
, int indent
, const char *opname
, bool gimple
)
2656 predicate
*p
= as_a
<predicate
*> (op
);
2658 if (p
->p
->matchers
.exists ())
2660 /* If this is a predicate generated from a pattern mangle its
2661 name and pass on the valueize hook. */
2663 fprintf_indent (f
, indent
, "if (gimple_%s (%s, valueize))\n",
2666 fprintf_indent (f
, indent
, "if (tree_%s (%s))\n", p
->p
->id
, opname
);
2669 fprintf_indent (f
, indent
, "if (%s (%s))\n", p
->p
->id
, opname
);
2670 fprintf_indent (f
, indent
+ 2, "{\n");
2674 /* Generate matching code for the decision tree operand which is
2678 dt_operand::gen_match_op (FILE *f
, int indent
, const char *opname
, bool)
2680 char match_opname
[20];
2681 match_dop
->get_name (match_opname
);
2683 fprintf_indent (f
, indent
, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
2684 opname
, match_opname
, opname
, match_opname
);
2686 fprintf_indent (f
, indent
, "if (%s == %s || (operand_equal_p (%s, %s, 0) "
2687 "&& types_match (%s, %s)))\n",
2688 opname
, match_opname
, opname
, match_opname
,
2689 opname
, match_opname
);
2690 fprintf_indent (f
, indent
+ 2, "{\n");
2694 /* Generate GIMPLE matching code for the decision tree operand. */
2697 dt_operand::gen_gimple_expr (FILE *f
, int indent
)
2699 expr
*e
= static_cast<expr
*> (op
);
2700 id_base
*id
= e
->operation
;
2701 unsigned n_ops
= e
->ops
.length ();
2702 unsigned n_braces
= 0;
2704 for (unsigned i
= 0; i
< n_ops
; ++i
)
2706 char child_opname
[20];
2707 gen_opname (child_opname
, i
);
2709 if (id
->kind
== id_base::CODE
)
2712 || *id
== REALPART_EXPR
|| *id
== IMAGPART_EXPR
2713 || *id
== BIT_FIELD_REF
|| *id
== VIEW_CONVERT_EXPR
)
2715 /* ??? If this is a memory operation we can't (and should not)
2716 match this. The only sensible operand types are
2717 SSA names and invariants. */
2722 fprintf_indent (f
, indent
,
2723 "tree %s = TREE_OPERAND (%s, %i);\n",
2724 child_opname
, opname
, i
);
2727 fprintf_indent (f
, indent
,
2728 "tree %s = TREE_OPERAND "
2729 "(gimple_assign_rhs1 (def), %i);\n",
2731 fprintf_indent (f
, indent
,
2732 "if ((TREE_CODE (%s) == SSA_NAME\n",
2734 fprintf_indent (f
, indent
,
2735 " || is_gimple_min_invariant (%s)))\n",
2737 fprintf_indent (f
, indent
,
2741 fprintf_indent (f
, indent
,
2742 "%s = do_valueize (valueize, %s);\n",
2743 child_opname
, child_opname
);
2747 fprintf_indent (f
, indent
,
2748 "tree %s = gimple_assign_rhs%u (def);\n",
2749 child_opname
, i
+ 1);
2752 fprintf_indent (f
, indent
,
2753 "tree %s = gimple_call_arg (def, %u);\n",
2755 fprintf_indent (f
, indent
,
2756 "%s = do_valueize (valueize, %s);\n",
2757 child_opname
, child_opname
);
2759 /* While the toplevel operands are canonicalized by the caller
2760 after valueizing operands of sub-expressions we have to
2761 re-canonicalize operand order. */
2762 if (operator_id
*code
= dyn_cast
<operator_id
*> (id
))
2764 /* ??? We can't canonicalize tcc_comparison operands here
2765 because that requires changing the comparison code which
2766 we already matched... */
2767 if (commutative_tree_code (code
->code
)
2768 || commutative_ternary_tree_code (code
->code
))
2770 char child_opname0
[20], child_opname1
[20];
2771 gen_opname (child_opname0
, 0);
2772 gen_opname (child_opname1
, 1);
2773 fprintf_indent (f
, indent
,
2774 "if (tree_swap_operands_p (%s, %s))\n",
2775 child_opname0
, child_opname1
);
2776 fprintf_indent (f
, indent
,
2777 " std::swap (%s, %s);\n",
2778 child_opname0
, child_opname1
);
2785 /* Generate GENERIC matching code for the decision tree operand. */
2788 dt_operand::gen_generic_expr (FILE *f
, int indent
, const char *opname
)
2790 expr
*e
= static_cast<expr
*> (op
);
2791 unsigned n_ops
= e
->ops
.length ();
2793 for (unsigned i
= 0; i
< n_ops
; ++i
)
2795 char child_opname
[20];
2796 gen_opname (child_opname
, i
);
2798 if (e
->operation
->kind
== id_base::CODE
)
2799 fprintf_indent (f
, indent
, "tree %s = TREE_OPERAND (%s, %u);\n",
2800 child_opname
, opname
, i
);
2802 fprintf_indent (f
, indent
, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2803 child_opname
, opname
, i
);
2809 /* Generate matching code for the children of the decision tree node. */
2812 dt_node::gen_kids (FILE *f
, int indent
, bool gimple
)
2814 auto_vec
<dt_operand
*> gimple_exprs
;
2815 auto_vec
<dt_operand
*> generic_exprs
;
2816 auto_vec
<dt_operand
*> fns
;
2817 auto_vec
<dt_operand
*> generic_fns
;
2818 auto_vec
<dt_operand
*> preds
;
2819 auto_vec
<dt_node
*> others
;
2821 for (unsigned i
= 0; i
< kids
.length (); ++i
)
2823 if (kids
[i
]->type
== dt_node::DT_OPERAND
)
2825 dt_operand
*op
= as_a
<dt_operand
*> (kids
[i
]);
2826 if (expr
*e
= dyn_cast
<expr
*> (op
->op
))
2828 if (e
->ops
.length () == 0
2829 && (!gimple
|| !(*e
->operation
== CONSTRUCTOR
)))
2830 generic_exprs
.safe_push (op
);
2831 else if (e
->operation
->kind
== id_base::FN
)
2836 generic_fns
.safe_push (op
);
2838 else if (e
->operation
->kind
== id_base::PREDICATE
)
2839 preds
.safe_push (op
);
2842 if (gimple
&& !e
->is_generic
)
2843 gimple_exprs
.safe_push (op
);
2845 generic_exprs
.safe_push (op
);
2848 else if (op
->op
->type
== operand::OP_PREDICATE
)
2849 others
.safe_push (kids
[i
]);
2853 else if (kids
[i
]->type
== dt_node::DT_SIMPLIFY
)
2854 others
.safe_push (kids
[i
]);
2855 else if (kids
[i
]->type
== dt_node::DT_MATCH
2856 || kids
[i
]->type
== dt_node::DT_TRUE
)
2858 /* A DT_TRUE operand serves as a barrier - generate code now
2859 for what we have collected sofar.
2860 Like DT_TRUE, DT_MATCH serves as a barrier as it can cause
2861 dependent matches to get out-of-order. Generate code now
2862 for what we have collected sofar. */
2863 gen_kids_1 (f
, indent
, gimple
, gimple_exprs
, generic_exprs
,
2864 fns
, generic_fns
, preds
, others
);
2865 /* And output the true operand itself. */
2866 kids
[i
]->gen (f
, indent
, gimple
);
2867 gimple_exprs
.truncate (0);
2868 generic_exprs
.truncate (0);
2870 generic_fns
.truncate (0);
2872 others
.truncate (0);
2878 /* Generate code for the remains. */
2879 gen_kids_1 (f
, indent
, gimple
, gimple_exprs
, generic_exprs
,
2880 fns
, generic_fns
, preds
, others
);
2883 /* Generate matching code for the children of the decision tree node. */
2886 dt_node::gen_kids_1 (FILE *f
, int indent
, bool gimple
,
2887 vec
<dt_operand
*> gimple_exprs
,
2888 vec
<dt_operand
*> generic_exprs
,
2889 vec
<dt_operand
*> fns
,
2890 vec
<dt_operand
*> generic_fns
,
2891 vec
<dt_operand
*> preds
,
2892 vec
<dt_node
*> others
)
2895 char *kid_opname
= buf
;
2897 unsigned exprs_len
= gimple_exprs
.length ();
2898 unsigned gexprs_len
= generic_exprs
.length ();
2899 unsigned fns_len
= fns
.length ();
2900 unsigned gfns_len
= generic_fns
.length ();
2902 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2905 gimple_exprs
[0]->get_name (kid_opname
);
2907 fns
[0]->get_name (kid_opname
);
2909 generic_fns
[0]->get_name (kid_opname
);
2911 generic_exprs
[0]->get_name (kid_opname
);
2913 fprintf_indent (f
, indent
, "switch (TREE_CODE (%s))\n", kid_opname
);
2914 fprintf_indent (f
, indent
, " {\n");
2918 if (exprs_len
|| fns_len
)
2920 fprintf_indent (f
, indent
,
2921 "case SSA_NAME:\n");
2922 fprintf_indent (f
, indent
,
2923 " if (gimple *def_stmt = get_def (valueize, %s))\n",
2925 fprintf_indent (f
, indent
,
2930 fprintf_indent (f
, indent
,
2931 "if (gassign *def = dyn_cast <gassign *> (def_stmt))\n");
2932 fprintf_indent (f
, indent
,
2933 " switch (gimple_assign_rhs_code (def))\n");
2935 fprintf_indent (f
, indent
, "{\n");
2936 for (unsigned i
= 0; i
< exprs_len
; ++i
)
2938 expr
*e
= as_a
<expr
*> (gimple_exprs
[i
]->op
);
2939 id_base
*op
= e
->operation
;
2940 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2941 fprintf_indent (f
, indent
, "CASE_CONVERT:\n");
2943 fprintf_indent (f
, indent
, "case %s:\n", op
->id
);
2944 fprintf_indent (f
, indent
, " {\n");
2945 gimple_exprs
[i
]->gen (f
, indent
+ 4, true);
2946 fprintf_indent (f
, indent
, " break;\n");
2947 fprintf_indent (f
, indent
, " }\n");
2949 fprintf_indent (f
, indent
, "default:;\n");
2950 fprintf_indent (f
, indent
, "}\n");
2956 fprintf_indent (f
, indent
,
2957 "%sif (gcall *def = dyn_cast <gcall *>"
2959 exprs_len
? "else " : "");
2960 fprintf_indent (f
, indent
,
2961 " switch (gimple_call_combined_fn (def))\n");
2964 fprintf_indent (f
, indent
, "{\n");
2965 for (unsigned i
= 0; i
< fns_len
; ++i
)
2967 expr
*e
= as_a
<expr
*>(fns
[i
]->op
);
2968 fprintf_indent (f
, indent
, "case %s:\n", e
->operation
->id
);
2969 fprintf_indent (f
, indent
, " {\n");
2970 fns
[i
]->gen (f
, indent
+ 4, true);
2971 fprintf_indent (f
, indent
, " break;\n");
2972 fprintf_indent (f
, indent
, " }\n");
2975 fprintf_indent (f
, indent
, "default:;\n");
2976 fprintf_indent (f
, indent
, "}\n");
2981 fprintf_indent (f
, indent
, " }\n");
2982 /* See if there is SSA_NAME among generic_exprs and if yes, emit it
2983 here rather than in the next loop. */
2984 for (unsigned i
= 0; i
< generic_exprs
.length (); ++i
)
2986 expr
*e
= as_a
<expr
*>(generic_exprs
[i
]->op
);
2987 id_base
*op
= e
->operation
;
2988 if (*op
== SSA_NAME
&& (exprs_len
|| fns_len
))
2990 fprintf_indent (f
, indent
+ 4, "{\n");
2991 generic_exprs
[i
]->gen (f
, indent
+ 6, gimple
);
2992 fprintf_indent (f
, indent
+ 4, "}\n");
2996 fprintf_indent (f
, indent
, " break;\n");
2999 for (unsigned i
= 0; i
< generic_exprs
.length (); ++i
)
3001 expr
*e
= as_a
<expr
*>(generic_exprs
[i
]->op
);
3002 id_base
*op
= e
->operation
;
3003 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
3004 fprintf_indent (f
, indent
, "CASE_CONVERT:\n");
3005 else if (*op
== SSA_NAME
&& (exprs_len
|| fns_len
))
3006 /* Already handled above. */
3009 fprintf_indent (f
, indent
, "case %s:\n", op
->id
);
3010 fprintf_indent (f
, indent
, " {\n");
3011 generic_exprs
[i
]->gen (f
, indent
+ 4, gimple
);
3012 fprintf_indent (f
, indent
, " break;\n");
3013 fprintf_indent (f
, indent
, " }\n");
3018 fprintf_indent (f
, indent
,
3019 "case CALL_EXPR:\n");
3020 fprintf_indent (f
, indent
,
3021 " switch (get_call_combined_fn (%s))\n",
3023 fprintf_indent (f
, indent
,
3027 for (unsigned j
= 0; j
< generic_fns
.length (); ++j
)
3029 expr
*e
= as_a
<expr
*>(generic_fns
[j
]->op
);
3030 gcc_assert (e
->operation
->kind
== id_base::FN
);
3032 fprintf_indent (f
, indent
, "case %s:\n", e
->operation
->id
);
3033 fprintf_indent (f
, indent
, " {\n");
3034 generic_fns
[j
]->gen (f
, indent
+ 4, false);
3035 fprintf_indent (f
, indent
, " break;\n");
3036 fprintf_indent (f
, indent
, " }\n");
3038 fprintf_indent (f
, indent
, "default:;\n");
3041 fprintf_indent (f
, indent
, " }\n");
3042 fprintf_indent (f
, indent
, " break;\n");
3045 /* Close switch (TREE_CODE ()). */
3046 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
3049 fprintf_indent (f
, indent
, " default:;\n");
3050 fprintf_indent (f
, indent
, " }\n");
3053 for (unsigned i
= 0; i
< preds
.length (); ++i
)
3055 expr
*e
= as_a
<expr
*> (preds
[i
]->op
);
3056 predicate_id
*p
= as_a
<predicate_id
*> (e
->operation
);
3057 preds
[i
]->get_name (kid_opname
);
3058 fprintf_indent (f
, indent
, "{\n");
3060 fprintf_indent (f
, indent
, "tree %s_pops[%d];\n", kid_opname
, p
->nargs
);
3061 fprintf_indent (f
, indent
, "if (%s_%s (%s, %s_pops%s))\n",
3062 gimple
? "gimple" : "tree",
3063 p
->id
, kid_opname
, kid_opname
,
3064 gimple
? ", valueize" : "");
3065 fprintf_indent (f
, indent
, " {\n");
3066 for (int j
= 0; j
< p
->nargs
; ++j
)
3068 char child_opname
[20];
3069 preds
[i
]->gen_opname (child_opname
, j
);
3070 fprintf_indent (f
, indent
+ 4, "tree %s = %s_pops[%d];\n",
3071 child_opname
, kid_opname
, j
);
3073 preds
[i
]->gen_kids (f
, indent
+ 4, gimple
);
3076 fprintf_indent (f
, indent
, "}\n");
3079 for (unsigned i
= 0; i
< others
.length (); ++i
)
3080 others
[i
]->gen (f
, indent
, gimple
);
3083 /* Generate matching code for the decision tree operand. */
3086 dt_operand::gen (FILE *f
, int indent
, bool gimple
)
3091 unsigned n_braces
= 0;
3093 if (type
== DT_OPERAND
)
3096 case operand::OP_PREDICATE
:
3097 n_braces
= gen_predicate (f
, indent
, opname
, gimple
);
3100 case operand::OP_EXPR
:
3102 n_braces
= gen_gimple_expr (f
, indent
);
3104 n_braces
= gen_generic_expr (f
, indent
, opname
);
3110 else if (type
== DT_TRUE
)
3112 else if (type
== DT_MATCH
)
3113 n_braces
= gen_match_op (f
, indent
, opname
, gimple
);
3117 indent
+= 4 * n_braces
;
3118 gen_kids (f
, indent
, gimple
);
3120 for (unsigned i
= 0; i
< n_braces
; ++i
)
3125 fprintf_indent (f
, indent
, " }\n");
3130 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3131 step of a '(simplify ...)' or '(match ...)'. This handles everything
3132 that is not part of the decision tree (simplify->match).
3133 Main recursive worker. */
3136 dt_simplify::gen_1 (FILE *f
, int indent
, bool gimple
, operand
*result
)
3140 if (with_expr
*w
= dyn_cast
<with_expr
*> (result
))
3142 fprintf_indent (f
, indent
, "{\n");
3144 output_line_directive (f
, w
->location
);
3145 w
->with
->gen_transform (f
, indent
, NULL
, true, 1, "type", NULL
);
3146 gen_1 (f
, indent
, gimple
, w
->subexpr
);
3148 fprintf_indent (f
, indent
, "}\n");
3151 else if (if_expr
*ife
= dyn_cast
<if_expr
*> (result
))
3153 output_line_directive (f
, ife
->location
);
3154 fprintf_indent (f
, indent
, "if (");
3155 ife
->cond
->gen_transform (f
, indent
, NULL
, true, 1, "type", NULL
);
3157 fprintf_indent (f
, indent
+ 2, "{\n");
3159 gen_1 (f
, indent
, gimple
, ife
->trueexpr
);
3161 fprintf_indent (f
, indent
+ 2, "}\n");
3164 fprintf_indent (f
, indent
, "else\n");
3165 fprintf_indent (f
, indent
+ 2, "{\n");
3167 gen_1 (f
, indent
, gimple
, ife
->falseexpr
);
3169 fprintf_indent (f
, indent
+ 2, "}\n");
3175 /* Analyze captures and perform early-outs on the incoming arguments
3176 that cover cases we cannot handle. */
3177 capture_info
cinfo (s
, result
, gimple
);
3178 if (s
->kind
== simplify::SIMPLIFY
)
3182 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
3183 if (cinfo
.force_no_side_effects
& (1 << i
))
3185 fprintf_indent (f
, indent
,
3186 "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n",
3189 warning_at (as_a
<expr
*> (s
->match
)->ops
[i
]->location
,
3190 "forcing toplevel operand to have no "
3193 for (int i
= 0; i
<= s
->capture_max
; ++i
)
3194 if (cinfo
.info
[i
].cse_p
)
3196 else if (cinfo
.info
[i
].force_no_side_effects_p
3197 && (cinfo
.info
[i
].toplevel_msk
3198 & cinfo
.force_no_side_effects
) == 0)
3200 fprintf_indent (f
, indent
,
3201 "if (TREE_SIDE_EFFECTS (captures[%d])) "
3202 "return NULL_TREE;\n", i
);
3204 warning_at (cinfo
.info
[i
].c
->location
,
3205 "forcing captured operand to have no "
3208 else if ((cinfo
.info
[i
].toplevel_msk
3209 & cinfo
.force_no_side_effects
) != 0)
3210 /* Mark capture as having no side-effects if we had to verify
3211 that via forced toplevel operand checks. */
3212 cinfo
.info
[i
].force_no_side_effects_p
= true;
3216 /* Force single-use restriction by only allowing simple
3217 results via setting seq to NULL. */
3218 fprintf_indent (f
, indent
, "gimple_seq *lseq = seq;\n");
3219 bool first_p
= true;
3220 for (int i
= 0; i
<= s
->capture_max
; ++i
)
3221 if (cinfo
.info
[i
].force_single_use
)
3225 fprintf_indent (f
, indent
, "if (lseq\n");
3226 fprintf_indent (f
, indent
, " && (");
3232 fprintf_indent (f
, indent
, " || ");
3234 fprintf (f
, "!single_use (captures[%d])", i
);
3238 fprintf (f
, "))\n");
3239 fprintf_indent (f
, indent
, " lseq = NULL;\n");
3244 fprintf_indent (f
, indent
, "if (dump_file && (dump_flags & TDF_FOLDING)) "
3245 "fprintf (dump_file, \"Applying pattern ");
3246 output_line_directive (f
,
3247 result
? result
->location
: s
->match
->location
, true);
3248 fprintf (f
, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
3252 /* If there is no result then this is a predicate implementation. */
3253 fprintf_indent (f
, indent
, "return true;\n");
3257 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
3258 in outermost position). */
3259 if (result
->type
== operand::OP_EXPR
3260 && *as_a
<expr
*> (result
)->operation
== NON_LVALUE_EXPR
)
3261 result
= as_a
<expr
*> (result
)->ops
[0];
3262 if (result
->type
== operand::OP_EXPR
)
3264 expr
*e
= as_a
<expr
*> (result
);
3265 id_base
*opr
= e
->operation
;
3266 bool is_predicate
= false;
3267 /* When we delay operator substituting during lowering of fors we
3268 make sure that for code-gen purposes the effects of each substitute
3269 are the same. Thus just look at that. */
3270 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
3271 opr
= uid
->substitutes
[0];
3272 else if (is_a
<predicate_id
*> (opr
))
3273 is_predicate
= true;
3275 fprintf_indent (f
, indent
, "*res_code = %s;\n",
3276 *e
->operation
== CONVERT_EXPR
3277 ? "NOP_EXPR" : e
->operation
->id
);
3278 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
3281 snprintf (dest
, 32, "res_ops[%d]", j
);
3283 = get_operand_type (opr
, j
,
3284 "type", e
->expr_type
,
3285 j
== 0 ? NULL
: "TREE_TYPE (res_ops[0])");
3286 /* We need to expand GENERIC conditions we captured from
3287 COND_EXPRs and we need to unshare them when substituting
3289 int cond_handling
= 0;
3291 cond_handling
= ((*opr
== COND_EXPR
3292 || *opr
== VEC_COND_EXPR
) && j
== 0) ? 1 : 2;
3293 e
->ops
[j
]->gen_transform (f
, indent
, dest
, true, 1, optype
,
3294 &cinfo
, indexes
, cond_handling
);
3297 /* Re-fold the toplevel result. It's basically an embedded
3298 gimple_build w/o actually building the stmt. */
3300 fprintf_indent (f
, indent
,
3301 "gimple_resimplify%d (lseq, res_code, type, "
3302 "res_ops, valueize);\n", e
->ops
.length ());
3304 else if (result
->type
== operand::OP_CAPTURE
3305 || result
->type
== operand::OP_C_EXPR
)
3307 result
->gen_transform (f
, indent
, "res_ops[0]", true, 1, "type",
3309 fprintf_indent (f
, indent
, "*res_code = TREE_CODE (res_ops[0]);\n");
3310 if (is_a
<capture
*> (result
)
3311 && cinfo
.info
[as_a
<capture
*> (result
)->where
].cond_expr_cond_p
)
3313 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
3314 with substituting a capture of that. */
3315 fprintf_indent (f
, indent
,
3316 "if (COMPARISON_CLASS_P (res_ops[0]))\n");
3317 fprintf_indent (f
, indent
,
3319 fprintf_indent (f
, indent
,
3320 " tree tem = res_ops[0];\n");
3321 fprintf_indent (f
, indent
,
3322 " res_ops[0] = TREE_OPERAND (tem, 0);\n");
3323 fprintf_indent (f
, indent
,
3324 " res_ops[1] = TREE_OPERAND (tem, 1);\n");
3325 fprintf_indent (f
, indent
,
3331 fprintf_indent (f
, indent
, "return true;\n");
3335 bool is_predicate
= false;
3336 if (result
->type
== operand::OP_EXPR
)
3338 expr
*e
= as_a
<expr
*> (result
);
3339 id_base
*opr
= e
->operation
;
3340 /* When we delay operator substituting during lowering of fors we
3341 make sure that for code-gen purposes the effects of each substitute
3342 are the same. Thus just look at that. */
3343 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
3344 opr
= uid
->substitutes
[0];
3345 else if (is_a
<predicate_id
*> (opr
))
3346 is_predicate
= true;
3347 /* Search for captures used multiple times in the result expression
3348 and wrap them in a SAVE_EXPR. Allow as many uses as in the
3349 original expression. */
3351 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
3353 if (cinfo
.info
[i
].same_as
!= (unsigned)i
3354 || cinfo
.info
[i
].cse_p
)
3356 if (cinfo
.info
[i
].result_use_count
3357 > cinfo
.info
[i
].match_use_count
)
3358 fprintf_indent (f
, indent
,
3359 "if (! tree_invariant_p (captures[%d])) "
3360 "return NULL_TREE;\n", i
);
3362 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
3366 snprintf (dest
, 32, "res_ops[%d]", j
);
3369 fprintf_indent (f
, indent
, "tree res_op%d;\n", j
);
3370 snprintf (dest
, 32, "res_op%d", j
);
3373 = get_operand_type (opr
, j
,
3374 "type", e
->expr_type
,
3376 ? NULL
: "TREE_TYPE (res_op0)");
3377 e
->ops
[j
]->gen_transform (f
, indent
, dest
, false, 1, optype
,
3381 fprintf_indent (f
, indent
, "return true;\n");
3384 fprintf_indent (f
, indent
, "tree res;\n");
3385 /* Re-fold the toplevel result. Use non_lvalue to
3386 build NON_LVALUE_EXPRs so they get properly
3387 ignored when in GIMPLE form. */
3388 if (*opr
== NON_LVALUE_EXPR
)
3389 fprintf_indent (f
, indent
,
3390 "res = non_lvalue_loc (loc, res_op0);\n");
3393 if (is_a
<operator_id
*> (opr
))
3394 fprintf_indent (f
, indent
,
3395 "res = fold_build%d_loc (loc, %s, type",
3397 *e
->operation
== CONVERT_EXPR
3398 ? "NOP_EXPR" : e
->operation
->id
);
3400 fprintf_indent (f
, indent
,
3401 "res = maybe_build_call_expr_loc (loc, "
3402 "%s, type, %d", e
->operation
->id
,
3404 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
3405 fprintf (f
, ", res_op%d", j
);
3406 fprintf (f
, ");\n");
3407 if (!is_a
<operator_id
*> (opr
))
3409 fprintf_indent (f
, indent
, "if (!res)\n");
3410 fprintf_indent (f
, indent
, " return NULL_TREE;\n");
3415 else if (result
->type
== operand::OP_CAPTURE
3416 || result
->type
== operand::OP_C_EXPR
)
3419 fprintf_indent (f
, indent
, "tree res;\n");
3420 result
->gen_transform (f
, indent
, "res", false, 1, "type",
3427 /* Search for captures not used in the result expression and dependent
3428 on TREE_SIDE_EFFECTS emit omit_one_operand. */
3429 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
3431 if (cinfo
.info
[i
].same_as
!= (unsigned)i
)
3433 if (!cinfo
.info
[i
].force_no_side_effects_p
3434 && !cinfo
.info
[i
].expr_p
3435 && cinfo
.info
[i
].result_use_count
== 0)
3437 fprintf_indent (f
, indent
,
3438 "if (TREE_SIDE_EFFECTS (captures[%d]))\n",
3440 fprintf_indent (f
, indent
+ 2,
3441 "res = build2_loc (loc, COMPOUND_EXPR, type, "
3442 "fold_ignored_result (captures[%d]), res);\n",
3446 fprintf_indent (f
, indent
, "return res;\n");
3451 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3452 step of a '(simplify ...)' or '(match ...)'. This handles everything
3453 that is not part of the decision tree (simplify->match). */
3456 dt_simplify::gen (FILE *f
, int indent
, bool gimple
)
3458 fprintf_indent (f
, indent
, "{\n");
3460 output_line_directive (f
,
3461 s
->result
? s
->result
->location
: s
->match
->location
);
3462 if (s
->capture_max
>= 0)
3465 fprintf_indent (f
, indent
, "tree captures[%u] ATTRIBUTE_UNUSED = { %s",
3466 s
->capture_max
+ 1, indexes
[0]->get_name (opname
));
3468 for (int i
= 1; i
<= s
->capture_max
; ++i
)
3472 fprintf (f
, ", %s", indexes
[i
]->get_name (opname
));
3474 fprintf (f
, " };\n");
3477 /* If we have a split-out function for the actual transform, call it. */
3478 if (info
&& info
->fname
)
3482 fprintf_indent (f
, indent
, "if (%s (res_code, res_ops, seq, "
3483 "valueize, type, captures", info
->fname
);
3484 for (unsigned i
= 0; i
< s
->for_subst_vec
.length (); ++i
)
3485 if (s
->for_subst_vec
[i
].first
->used
)
3486 fprintf (f
, ", %s", s
->for_subst_vec
[i
].second
->id
);
3487 fprintf (f
, "))\n");
3488 fprintf_indent (f
, indent
, " return true;\n");
3492 fprintf_indent (f
, indent
, "tree res = %s (loc, type",
3494 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
3495 fprintf (f
, ", op%d", i
);
3496 fprintf (f
, ", captures");
3497 for (unsigned i
= 0; i
< s
->for_subst_vec
.length (); ++i
)
3499 if (s
->for_subst_vec
[i
].first
->used
)
3500 fprintf (f
, ", %s", s
->for_subst_vec
[i
].second
->id
);
3502 fprintf (f
, ");\n");
3503 fprintf_indent (f
, indent
, "if (res) return res;\n");
3508 for (unsigned i
= 0; i
< s
->for_subst_vec
.length (); ++i
)
3510 if (! s
->for_subst_vec
[i
].first
->used
)
3512 if (is_a
<operator_id
*> (s
->for_subst_vec
[i
].second
))
3513 fprintf_indent (f
, indent
, "const enum tree_code %s = %s;\n",
3514 s
->for_subst_vec
[i
].first
->id
,
3515 s
->for_subst_vec
[i
].second
->id
);
3516 else if (is_a
<fn_id
*> (s
->for_subst_vec
[i
].second
))
3517 fprintf_indent (f
, indent
, "const combined_fn %s = %s;\n",
3518 s
->for_subst_vec
[i
].first
->id
,
3519 s
->for_subst_vec
[i
].second
->id
);
3523 gen_1 (f
, indent
, gimple
, s
->result
);
3527 fprintf_indent (f
, indent
, "}\n");
3531 /* Hash function for finding equivalent transforms. */
3534 sinfo_hashmap_traits::hash (const key_type
&v
)
3536 /* Only bother to compare those originating from the same source pattern. */
3537 return v
->s
->result
->location
;
3540 /* Compare function for finding equivalent transforms. */
3543 compare_op (operand
*o1
, simplify
*s1
, operand
*o2
, simplify
*s2
)
3545 if (o1
->type
!= o2
->type
)
3550 case operand::OP_IF
:
3552 if_expr
*if1
= as_a
<if_expr
*> (o1
);
3553 if_expr
*if2
= as_a
<if_expr
*> (o2
);
3554 /* ??? Properly compare c-exprs. */
3555 if (if1
->cond
!= if2
->cond
)
3557 if (!compare_op (if1
->trueexpr
, s1
, if2
->trueexpr
, s2
))
3559 if (if1
->falseexpr
!= if2
->falseexpr
3561 && !compare_op (if1
->falseexpr
, s1
, if2
->falseexpr
, s2
)))
3565 case operand::OP_WITH
:
3567 with_expr
*with1
= as_a
<with_expr
*> (o1
);
3568 with_expr
*with2
= as_a
<with_expr
*> (o2
);
3569 if (with1
->with
!= with2
->with
)
3571 return compare_op (with1
->subexpr
, s1
, with2
->subexpr
, s2
);
3576 /* We've hit a result. Time to compare capture-infos - this is required
3577 in addition to the conservative pointer-equivalency of the result IL. */
3578 capture_info
cinfo1 (s1
, o1
, true);
3579 capture_info
cinfo2 (s2
, o2
, true);
3581 if (cinfo1
.force_no_side_effects
!= cinfo2
.force_no_side_effects
3582 || cinfo1
.info
.length () != cinfo2
.info
.length ())
3585 for (unsigned i
= 0; i
< cinfo1
.info
.length (); ++i
)
3587 if (cinfo1
.info
[i
].expr_p
!= cinfo2
.info
[i
].expr_p
3588 || cinfo1
.info
[i
].cse_p
!= cinfo2
.info
[i
].cse_p
3589 || (cinfo1
.info
[i
].force_no_side_effects_p
3590 != cinfo2
.info
[i
].force_no_side_effects_p
)
3591 || cinfo1
.info
[i
].force_single_use
!= cinfo2
.info
[i
].force_single_use
3592 || cinfo1
.info
[i
].cond_expr_cond_p
!= cinfo2
.info
[i
].cond_expr_cond_p
3593 /* toplevel_msk is an optimization */
3594 || cinfo1
.info
[i
].result_use_count
!= cinfo2
.info
[i
].result_use_count
3595 || cinfo1
.info
[i
].same_as
!= cinfo2
.info
[i
].same_as
3596 /* the pointer back to the capture is for diagnostics only */)
3600 /* ??? Deep-compare the actual result. */
3605 sinfo_hashmap_traits::equal_keys (const key_type
&v
,
3606 const key_type
&candidate
)
3608 return compare_op (v
->s
->result
, v
->s
, candidate
->s
->result
, candidate
->s
);
3612 /* Main entry to generate code for matching GIMPLE IL off the decision
3616 decision_tree::gen (FILE *f
, bool gimple
)
3622 fprintf (stderr
, "%s decision tree has %u leafs, maximum depth %u and "
3623 "a total number of %u nodes\n",
3624 gimple
? "GIMPLE" : "GENERIC",
3625 root
->num_leafs
, root
->max_level
, root
->total_size
);
3627 /* First split out the transform part of equal leafs. */
3630 for (sinfo_map_t::iterator iter
= si
.begin ();
3631 iter
!= si
.end (); ++iter
)
3633 sinfo
*s
= (*iter
).second
;
3634 /* Do not split out single uses. */
3641 fprintf (stderr
, "found %u uses of", s
->cnt
);
3642 output_line_directive (stderr
, s
->s
->s
->result
->location
);
3645 /* Generate a split out function with the leaf transform code. */
3646 s
->fname
= xasprintf ("%s_simplify_%u", gimple
? "gimple" : "generic",
3649 fprintf (f
, "\nstatic bool\n"
3650 "%s (code_helper *res_code, tree *res_ops,\n"
3651 " gimple_seq *seq, tree (*valueize)(tree) "
3652 "ATTRIBUTE_UNUSED,\n"
3653 " const tree ARG_UNUSED (type), tree *ARG_UNUSED "
3658 fprintf (f
, "\nstatic tree\n"
3659 "%s (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type),\n",
3660 (*iter
).second
->fname
);
3661 for (unsigned i
= 0;
3662 i
< as_a
<expr
*>(s
->s
->s
->match
)->ops
.length (); ++i
)
3663 fprintf (f
, " tree ARG_UNUSED (op%d),", i
);
3664 fprintf (f
, " tree *captures\n");
3666 for (unsigned i
= 0; i
< s
->s
->s
->for_subst_vec
.length (); ++i
)
3668 if (! s
->s
->s
->for_subst_vec
[i
].first
->used
)
3670 if (is_a
<operator_id
*> (s
->s
->s
->for_subst_vec
[i
].second
))
3671 fprintf (f
, ", const enum tree_code ARG_UNUSED (%s)",
3672 s
->s
->s
->for_subst_vec
[i
].first
->id
);
3673 else if (is_a
<fn_id
*> (s
->s
->s
->for_subst_vec
[i
].second
))
3674 fprintf (f
, ", const combined_fn ARG_UNUSED (%s)",
3675 s
->s
->s
->for_subst_vec
[i
].first
->id
);
3678 fprintf (f
, ")\n{\n");
3679 s
->s
->gen_1 (f
, 2, gimple
, s
->s
->s
->result
);
3681 fprintf (f
, " return false;\n");
3683 fprintf (f
, " return NULL_TREE;\n");
3686 fprintf (stderr
, "removed %u duplicate tails\n", rcnt
);
3688 for (unsigned n
= 1; n
<= 3; ++n
)
3690 /* First generate split-out functions. */
3691 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
3693 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
3694 expr
*e
= static_cast<expr
*>(dop
->op
);
3695 if (e
->ops
.length () != n
3696 /* Builtin simplifications are somewhat premature on
3697 GENERIC. The following drops patterns with outermost
3698 calls. It's easy to emit overloads for function code
3699 though if necessary. */
3701 && e
->operation
->kind
!= id_base::CODE
))
3705 fprintf (f
, "\nstatic bool\n"
3706 "gimple_simplify_%s (code_helper *res_code, tree *res_ops,\n"
3707 " gimple_seq *seq, tree (*valueize)(tree) "
3708 "ATTRIBUTE_UNUSED,\n"
3709 " code_helper ARG_UNUSED (code), tree "
3710 "ARG_UNUSED (type)\n",
3713 fprintf (f
, "\nstatic tree\n"
3714 "generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
3715 "tree_code ARG_UNUSED (code), const tree ARG_UNUSED (type)",
3717 for (unsigned i
= 0; i
< n
; ++i
)
3718 fprintf (f
, ", tree op%d", i
);
3721 dop
->gen_kids (f
, 2, gimple
);
3723 fprintf (f
, " return false;\n");
3725 fprintf (f
, " return NULL_TREE;\n");
3729 /* Then generate the main entry with the outermost switch and
3730 tail-calls to the split-out functions. */
3732 fprintf (f
, "\nstatic bool\n"
3733 "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
3734 " gimple_seq *seq, tree (*valueize)(tree),\n"
3735 " code_helper code, const tree type");
3737 fprintf (f
, "\ntree\n"
3738 "generic_simplify (location_t loc, enum tree_code code, "
3739 "const tree type ATTRIBUTE_UNUSED");
3740 for (unsigned i
= 0; i
< n
; ++i
)
3741 fprintf (f
, ", tree op%d", i
);
3746 fprintf (f
, " switch (code.get_rep())\n"
3749 fprintf (f
, " switch (code)\n"
3751 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
3753 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
3754 expr
*e
= static_cast<expr
*>(dop
->op
);
3755 if (e
->ops
.length () != n
3756 /* Builtin simplifications are somewhat premature on
3757 GENERIC. The following drops patterns with outermost
3758 calls. It's easy to emit overloads for function code
3759 though if necessary. */
3761 && e
->operation
->kind
!= id_base::CODE
))
3764 if (*e
->operation
== CONVERT_EXPR
3765 || *e
->operation
== NOP_EXPR
)
3766 fprintf (f
, " CASE_CONVERT:\n");
3768 fprintf (f
, " case %s%s:\n",
3769 is_a
<fn_id
*> (e
->operation
) ? "-" : "",
3772 fprintf (f
, " return gimple_simplify_%s (res_code, res_ops, "
3773 "seq, valueize, code, type", e
->operation
->id
);
3775 fprintf (f
, " return generic_simplify_%s (loc, code, type",
3777 for (unsigned i
= 0; i
< n
; ++i
)
3778 fprintf (f
, ", op%d", i
);
3779 fprintf (f
, ");\n");
3781 fprintf (f
, " default:;\n"
3785 fprintf (f
, " return false;\n");
3787 fprintf (f
, " return NULL_TREE;\n");
3792 /* Output code to implement the predicate P from the decision tree DT. */
3795 write_predicate (FILE *f
, predicate_id
*p
, decision_tree
&dt
, bool gimple
)
3797 fprintf (f
, "\nbool\n"
3798 "%s%s (tree t%s%s)\n"
3799 "{\n", gimple
? "gimple_" : "tree_", p
->id
,
3800 p
->nargs
> 0 ? ", tree *res_ops" : "",
3801 gimple
? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
3802 /* Conveniently make 'type' available. */
3803 fprintf_indent (f
, 2, "const tree type = TREE_TYPE (t);\n");
3806 fprintf_indent (f
, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
3807 dt
.root
->gen_kids (f
, 2, gimple
);
3809 fprintf_indent (f
, 2, "return false;\n"
3813 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
3816 write_header (FILE *f
, const char *head
)
3818 fprintf (f
, "/* Generated automatically by the program `genmatch' from\n");
3819 fprintf (f
, " a IL pattern matching and simplification description. */\n");
3821 /* Include the header instead of writing it awkwardly quoted here. */
3822 fprintf (f
, "\n#include \"%s\"\n", head
);
3832 parser (cpp_reader
*);
3835 const cpp_token
*next ();
3836 const cpp_token
*peek (unsigned = 1);
3837 const cpp_token
*peek_ident (const char * = NULL
, unsigned = 1);
3838 const cpp_token
*expect (enum cpp_ttype
);
3839 const cpp_token
*eat_token (enum cpp_ttype
);
3840 const char *get_string ();
3841 const char *get_ident ();
3842 const cpp_token
*eat_ident (const char *);
3843 const char *get_number ();
3845 unsigned get_internal_capture_id ();
3847 id_base
*parse_operation ();
3848 operand
*parse_capture (operand
*, bool);
3849 operand
*parse_expr ();
3850 c_expr
*parse_c_expr (cpp_ttype
);
3851 operand
*parse_op ();
3853 void record_operlist (source_location
, user_id
*);
3855 void parse_pattern ();
3856 operand
*parse_result (operand
*, predicate_id
*);
3857 void push_simplify (simplify::simplify_kind
,
3858 vec
<simplify
*>&, operand
*, operand
*);
3859 void parse_simplify (simplify::simplify_kind
,
3860 vec
<simplify
*>&, predicate_id
*, operand
*);
3861 void parse_for (source_location
);
3862 void parse_if (source_location
);
3863 void parse_predicates (source_location
);
3864 void parse_operator_list (source_location
);
3866 void finish_match_operand (operand
*);
3869 vec
<c_expr
*> active_ifs
;
3870 vec
<vec
<user_id
*> > active_fors
;
3871 hash_set
<user_id
*> *oper_lists_set
;
3872 vec
<user_id
*> oper_lists
;
3874 cid_map_t
*capture_ids
;
3878 vec
<simplify
*> simplifiers
;
3879 vec
<predicate_id
*> user_predicates
;
3880 bool parsing_match_operand
;
3883 /* Lexing helpers. */
3885 /* Read the next non-whitespace token from R. */
3890 const cpp_token
*token
;
3893 token
= cpp_get_token (r
);
3895 while (token
->type
== CPP_PADDING
);
3899 /* Peek at the next non-whitespace token from R. */
3902 parser::peek (unsigned num
)
3904 const cpp_token
*token
;
3908 token
= cpp_peek_token (r
, i
++);
3910 while (token
->type
== CPP_PADDING
3912 /* If we peek at EOF this is a fatal error as it leaves the
3913 cpp_reader in unusable state. Assume we really wanted a
3914 token and thus this EOF is unexpected. */
3915 if (token
->type
== CPP_EOF
)
3916 fatal_at (token
, "unexpected end of file");
3920 /* Peek at the next identifier token (or return NULL if the next
3921 token is not an identifier or equal to ID if supplied). */
3924 parser::peek_ident (const char *id
, unsigned num
)
3926 const cpp_token
*token
= peek (num
);
3927 if (token
->type
!= CPP_NAME
)
3933 const char *t
= (const char *) CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
3934 if (strcmp (id
, t
) == 0)
3940 /* Read the next token from R and assert it is of type TK. */
3943 parser::expect (enum cpp_ttype tk
)
3945 const cpp_token
*token
= next ();
3946 if (token
->type
!= tk
)
3947 fatal_at (token
, "expected %s, got %s",
3948 cpp_type2name (tk
, 0), cpp_type2name (token
->type
, 0));
3953 /* Consume the next token from R and assert it is of type TK. */
3956 parser::eat_token (enum cpp_ttype tk
)
3961 /* Read the next token from R and assert it is of type CPP_STRING and
3962 return its value. */
3965 parser::get_string ()
3967 const cpp_token
*token
= expect (CPP_STRING
);
3968 return (const char *)token
->val
.str
.text
;
3971 /* Read the next token from R and assert it is of type CPP_NAME and
3972 return its value. */
3975 parser::get_ident ()
3977 const cpp_token
*token
= expect (CPP_NAME
);
3978 return (const char *)CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
3981 /* Eat an identifier token with value S from R. */
3984 parser::eat_ident (const char *s
)
3986 const cpp_token
*token
= peek ();
3987 const char *t
= get_ident ();
3988 if (strcmp (s
, t
) != 0)
3989 fatal_at (token
, "expected '%s' got '%s'\n", s
, t
);
3993 /* Read the next token from R and assert it is of type CPP_NUMBER and
3994 return its value. */
3997 parser::get_number ()
3999 const cpp_token
*token
= expect (CPP_NUMBER
);
4000 return (const char *)token
->val
.str
.text
;
4003 /* Return a capture ID that can be used internally. */
4006 parser::get_internal_capture_id ()
4008 unsigned newid
= capture_ids
->elements ();
4009 /* Big enough for a 32-bit UINT_MAX plus prefix. */
4012 sprintf (id
, "__%u", newid
);
4013 capture_ids
->get_or_insert (xstrdup (id
), &existed
);
4015 fatal ("reserved capture id '%s' already used", id
);
4019 /* Record an operator-list use for transparent for handling. */
4022 parser::record_operlist (source_location loc
, user_id
*p
)
4024 if (!oper_lists_set
->add (p
))
4026 if (!oper_lists
.is_empty ()
4027 && oper_lists
[0]->substitutes
.length () != p
->substitutes
.length ())
4028 fatal_at (loc
, "User-defined operator list does not have the "
4029 "same number of entries as others used in the pattern");
4030 oper_lists
.safe_push (p
);
4034 /* Parse the operator ID, special-casing convert?, convert1? and
4038 parser::parse_operation ()
4040 const cpp_token
*id_tok
= peek ();
4041 const char *id
= get_ident ();
4042 const cpp_token
*token
= peek ();
4043 if (strcmp (id
, "convert0") == 0)
4044 fatal_at (id_tok
, "use 'convert?' here");
4045 else if (strcmp (id
, "view_convert0") == 0)
4046 fatal_at (id_tok
, "use 'view_convert?' here");
4047 if (token
->type
== CPP_QUERY
4048 && !(token
->flags
& PREV_WHITE
))
4050 if (strcmp (id
, "convert") == 0)
4052 else if (strcmp (id
, "convert1") == 0)
4054 else if (strcmp (id
, "convert2") == 0)
4056 else if (strcmp (id
, "view_convert") == 0)
4057 id
= "view_convert0";
4058 else if (strcmp (id
, "view_convert1") == 0)
4060 else if (strcmp (id
, "view_convert2") == 0)
4063 fatal_at (id_tok
, "non-convert operator conditionalized");
4065 if (!parsing_match_operand
)
4066 fatal_at (id_tok
, "conditional convert can only be used in "
4067 "match expression");
4068 eat_token (CPP_QUERY
);
4070 else if (strcmp (id
, "convert1") == 0
4071 || strcmp (id
, "convert2") == 0
4072 || strcmp (id
, "view_convert1") == 0
4073 || strcmp (id
, "view_convert2") == 0)
4074 fatal_at (id_tok
, "expected '?' after conditional operator");
4075 id_base
*op
= get_operator (id
);
4077 fatal_at (id_tok
, "unknown operator %s", id
);
4079 user_id
*p
= dyn_cast
<user_id
*> (op
);
4080 if (p
&& p
->is_oper_list
)
4082 if (active_fors
.length() == 0)
4083 record_operlist (id_tok
->src_loc
, p
);
4085 fatal_at (id_tok
, "operator-list %s cannot be exapnded inside 'for'", id
);
4091 capture = '@'<number> */
4094 parser::parse_capture (operand
*op
, bool require_existing
)
4096 source_location src_loc
= eat_token (CPP_ATSIGN
)->src_loc
;
4097 const cpp_token
*token
= peek ();
4098 const char *id
= NULL
;
4099 bool value_match
= false;
4100 /* For matches parse @@ as a value-match denoting the prevailing operand. */
4101 if (token
->type
== CPP_ATSIGN
4102 && ! (token
->flags
& PREV_WHITE
)
4103 && parsing_match_operand
)
4105 eat_token (CPP_ATSIGN
);
4109 if (token
->type
== CPP_NUMBER
)
4111 else if (token
->type
== CPP_NAME
)
4114 fatal_at (token
, "expected number or identifier");
4115 unsigned next_id
= capture_ids
->elements ();
4117 unsigned &num
= capture_ids
->get_or_insert (id
, &existed
);
4120 if (require_existing
)
4121 fatal_at (src_loc
, "unknown capture id");
4124 return new capture (src_loc
, num
, op
, value_match
);
4127 /* Parse an expression
4128 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
4131 parser::parse_expr ()
4133 const cpp_token
*token
= peek ();
4134 expr
*e
= new expr (parse_operation (), token
->src_loc
);
4137 bool is_commutative
= false;
4138 bool force_capture
= false;
4139 const char *expr_type
= NULL
;
4141 if (token
->type
== CPP_COLON
4142 && !(token
->flags
& PREV_WHITE
))
4144 eat_token (CPP_COLON
);
4146 if (token
->type
== CPP_NAME
4147 && !(token
->flags
& PREV_WHITE
))
4149 const char *s
= get_ident ();
4150 if (!parsing_match_operand
)
4160 = dyn_cast
<operator_id
*> (e
->operation
))
4162 if (!commutative_tree_code (p
->code
)
4163 && !comparison_code_p (p
->code
))
4164 fatal_at (token
, "operation is not commutative");
4166 else if (user_id
*p
= dyn_cast
<user_id
*> (e
->operation
))
4167 for (unsigned i
= 0;
4168 i
< p
->substitutes
.length (); ++i
)
4171 = dyn_cast
<operator_id
*> (p
->substitutes
[i
]))
4173 if (!commutative_tree_code (q
->code
)
4174 && !comparison_code_p (q
->code
))
4175 fatal_at (token
, "operation %s is not "
4176 "commutative", q
->id
);
4179 is_commutative
= true;
4181 else if (*sp
== 'C')
4182 is_commutative
= true;
4183 else if (*sp
== 's')
4185 e
->force_single_use
= true;
4186 force_capture
= true;
4189 fatal_at (token
, "flag %c not recognized", *sp
);
4196 fatal_at (token
, "expected flag or type specifying identifier");
4199 if (token
->type
== CPP_ATSIGN
4200 && !(token
->flags
& PREV_WHITE
))
4201 op
= parse_capture (e
, false);
4202 else if (force_capture
)
4204 unsigned num
= get_internal_capture_id ();
4205 op
= new capture (token
->src_loc
, num
, e
, false);
4211 const cpp_token
*token
= peek ();
4212 if (token
->type
== CPP_CLOSE_PAREN
)
4214 if (e
->operation
->nargs
!= -1
4215 && e
->operation
->nargs
!= (int) e
->ops
.length ())
4216 fatal_at (token
, "'%s' expects %u operands, not %u",
4217 e
->operation
->id
, e
->operation
->nargs
, e
->ops
.length ());
4220 if (e
->ops
.length () == 2)
4221 e
->is_commutative
= true;
4223 fatal_at (token
, "only binary operators or function with "
4224 "two arguments can be marked commutative");
4226 e
->expr_type
= expr_type
;
4229 else if (!(token
->flags
& PREV_WHITE
))
4230 fatal_at (token
, "expected expression operand");
4232 e
->append_op (parse_op ());
4237 /* Lex native C code delimited by START recording the preprocessing tokens
4238 for later processing.
4239 c_expr = ('{'|'(') <pp token>... ('}'|')') */
4242 parser::parse_c_expr (cpp_ttype start
)
4244 const cpp_token
*token
;
4247 vec
<cpp_token
> code
= vNULL
;
4248 unsigned nr_stmts
= 0;
4249 source_location loc
= eat_token (start
)->src_loc
;
4250 if (start
== CPP_OPEN_PAREN
)
4251 end
= CPP_CLOSE_PAREN
;
4252 else if (start
== CPP_OPEN_BRACE
)
4253 end
= CPP_CLOSE_BRACE
;
4261 /* Count brace pairs to find the end of the expr to match. */
4262 if (token
->type
== start
)
4264 else if (token
->type
== end
4267 else if (token
->type
== CPP_EOF
)
4268 fatal_at (token
, "unexpected end of file");
4270 /* This is a lame way of counting the number of statements. */
4271 if (token
->type
== CPP_SEMICOLON
)
4274 /* If this is possibly a user-defined identifier mark it used. */
4275 if (token
->type
== CPP_NAME
)
4277 id_base
*idb
= get_operator ((const char *)CPP_HASHNODE
4278 (token
->val
.node
.node
)->ident
.str
);
4280 if (idb
&& (p
= dyn_cast
<user_id
*> (idb
)) && p
->is_oper_list
)
4281 record_operlist (token
->src_loc
, p
);
4284 /* Record the token. */
4285 code
.safe_push (*token
);
4288 return new c_expr (r
, loc
, code
, nr_stmts
, vNULL
, capture_ids
);
4291 /* Parse an operand which is either an expression, a predicate or
4292 a standalone capture.
4293 op = predicate | expr | c_expr | capture */
4298 const cpp_token
*token
= peek ();
4299 struct operand
*op
= NULL
;
4300 if (token
->type
== CPP_OPEN_PAREN
)
4302 eat_token (CPP_OPEN_PAREN
);
4304 eat_token (CPP_CLOSE_PAREN
);
4306 else if (token
->type
== CPP_OPEN_BRACE
)
4308 op
= parse_c_expr (CPP_OPEN_BRACE
);
4312 /* Remaining ops are either empty or predicates */
4313 if (token
->type
== CPP_NAME
)
4315 const char *id
= get_ident ();
4316 id_base
*opr
= get_operator (id
);
4318 fatal_at (token
, "expected predicate name");
4319 if (operator_id
*code
= dyn_cast
<operator_id
*> (opr
))
4321 if (code
->nargs
!= 0)
4322 fatal_at (token
, "using an operator with operands as predicate");
4323 /* Parse the zero-operand operator "predicates" as
4325 op
= new expr (opr
, token
->src_loc
);
4327 else if (user_id
*code
= dyn_cast
<user_id
*> (opr
))
4329 if (code
->nargs
!= 0)
4330 fatal_at (token
, "using an operator with operands as predicate");
4331 /* Parse the zero-operand operator "predicates" as
4333 op
= new expr (opr
, token
->src_loc
);
4335 else if (predicate_id
*p
= dyn_cast
<predicate_id
*> (opr
))
4336 op
= new predicate (p
, token
->src_loc
);
4338 fatal_at (token
, "using an unsupported operator as predicate");
4339 if (!parsing_match_operand
)
4340 fatal_at (token
, "predicates are only allowed in match expression");
4342 if (token
->flags
& PREV_WHITE
)
4345 else if (token
->type
!= CPP_COLON
4346 && token
->type
!= CPP_ATSIGN
)
4347 fatal_at (token
, "expected expression or predicate");
4348 /* optionally followed by a capture and a predicate. */
4349 if (token
->type
== CPP_COLON
)
4350 fatal_at (token
, "not implemented: predicate on leaf operand");
4351 if (token
->type
== CPP_ATSIGN
)
4352 op
= parse_capture (op
, !parsing_match_operand
);
4358 /* Create a new simplify from the current parsing state and MATCH,
4359 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
4362 parser::push_simplify (simplify::simplify_kind kind
,
4363 vec
<simplify
*>& simplifiers
,
4364 operand
*match
, operand
*result
)
4366 /* Build and push a temporary for operator list uses in expressions. */
4367 if (!oper_lists
.is_empty ())
4368 active_fors
.safe_push (oper_lists
);
4370 simplifiers
.safe_push
4371 (new simplify (kind
, last_id
++, match
, result
,
4372 active_fors
.copy (), capture_ids
));
4374 if (!oper_lists
.is_empty ())
4379 <result-op> = <op> | <if> | <with>
4380 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
4381 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
4385 parser::parse_result (operand
*result
, predicate_id
*matcher
)
4387 const cpp_token
*token
= peek ();
4388 if (token
->type
!= CPP_OPEN_PAREN
)
4391 eat_token (CPP_OPEN_PAREN
);
4392 if (peek_ident ("if"))
4395 if_expr
*ife
= new if_expr (token
->src_loc
);
4396 ife
->cond
= parse_c_expr (CPP_OPEN_PAREN
);
4397 if (peek ()->type
== CPP_OPEN_PAREN
)
4399 ife
->trueexpr
= parse_result (result
, matcher
);
4400 if (peek ()->type
== CPP_OPEN_PAREN
)
4401 ife
->falseexpr
= parse_result (result
, matcher
);
4402 else if (peek ()->type
!= CPP_CLOSE_PAREN
)
4403 ife
->falseexpr
= parse_op ();
4405 else if (peek ()->type
!= CPP_CLOSE_PAREN
)
4407 ife
->trueexpr
= parse_op ();
4408 if (peek ()->type
== CPP_OPEN_PAREN
)
4409 ife
->falseexpr
= parse_result (result
, matcher
);
4410 else if (peek ()->type
!= CPP_CLOSE_PAREN
)
4411 ife
->falseexpr
= parse_op ();
4413 /* If this if is immediately closed then it contains a
4414 manual matcher or is part of a predicate definition. */
4415 else /* if (peek ()->type == CPP_CLOSE_PAREN) */
4418 fatal_at (peek (), "manual transform not implemented");
4419 ife
->trueexpr
= result
;
4421 eat_token (CPP_CLOSE_PAREN
);
4424 else if (peek_ident ("with"))
4427 with_expr
*withe
= new with_expr (token
->src_loc
);
4428 /* Parse (with c-expr expr) as (if-with (true) expr). */
4429 withe
->with
= parse_c_expr (CPP_OPEN_BRACE
);
4430 withe
->with
->nr_stmts
= 0;
4431 withe
->subexpr
= parse_result (result
, matcher
);
4432 eat_token (CPP_CLOSE_PAREN
);
4435 else if (peek_ident ("switch"))
4437 token
= eat_ident ("switch");
4438 source_location ifloc
= eat_token (CPP_OPEN_PAREN
)->src_loc
;
4440 if_expr
*ife
= new if_expr (ifloc
);
4442 ife
->cond
= parse_c_expr (CPP_OPEN_PAREN
);
4443 if (peek ()->type
== CPP_OPEN_PAREN
)
4444 ife
->trueexpr
= parse_result (result
, matcher
);
4446 ife
->trueexpr
= parse_op ();
4447 eat_token (CPP_CLOSE_PAREN
);
4448 if (peek ()->type
!= CPP_OPEN_PAREN
4449 || !peek_ident ("if", 2))
4450 fatal_at (token
, "switch can be implemented with a single if");
4451 while (peek ()->type
!= CPP_CLOSE_PAREN
)
4453 if (peek ()->type
== CPP_OPEN_PAREN
)
4455 if (peek_ident ("if", 2))
4457 ifloc
= eat_token (CPP_OPEN_PAREN
)->src_loc
;
4459 ife
->falseexpr
= new if_expr (ifloc
);
4460 ife
= as_a
<if_expr
*> (ife
->falseexpr
);
4461 ife
->cond
= parse_c_expr (CPP_OPEN_PAREN
);
4462 if (peek ()->type
== CPP_OPEN_PAREN
)
4463 ife
->trueexpr
= parse_result (result
, matcher
);
4465 ife
->trueexpr
= parse_op ();
4466 eat_token (CPP_CLOSE_PAREN
);
4470 /* switch default clause */
4471 ife
->falseexpr
= parse_result (result
, matcher
);
4472 eat_token (CPP_CLOSE_PAREN
);
4478 /* switch default clause */
4479 ife
->falseexpr
= parse_op ();
4480 eat_token (CPP_CLOSE_PAREN
);
4484 eat_token (CPP_CLOSE_PAREN
);
4489 operand
*op
= result
;
4492 eat_token (CPP_CLOSE_PAREN
);
4498 simplify = 'simplify' <expr> <result-op>
4500 match = 'match' <ident> <expr> [<result-op>]
4501 and fill SIMPLIFIERS with the results. */
4504 parser::parse_simplify (simplify::simplify_kind kind
,
4505 vec
<simplify
*>& simplifiers
, predicate_id
*matcher
,
4508 /* Reset the capture map. */
4510 capture_ids
= new cid_map_t
;
4511 /* Reset oper_lists and set. */
4512 hash_set
<user_id
*> olist
;
4513 oper_lists_set
= &olist
;
4516 const cpp_token
*loc
= peek ();
4517 parsing_match_operand
= true;
4518 struct operand
*match
= parse_op ();
4519 finish_match_operand (match
);
4520 parsing_match_operand
= false;
4521 if (match
->type
== operand::OP_CAPTURE
&& !matcher
)
4522 fatal_at (loc
, "outermost expression cannot be captured");
4523 if (match
->type
== operand::OP_EXPR
4524 && is_a
<predicate_id
*> (as_a
<expr
*> (match
)->operation
))
4525 fatal_at (loc
, "outermost expression cannot be a predicate");
4527 /* Splice active_ifs onto result and continue parsing the
4529 if_expr
*active_if
= NULL
;
4530 for (int i
= active_ifs
.length (); i
> 0; --i
)
4532 if_expr
*ifc
= new if_expr (active_ifs
[i
-1]->location
);
4533 ifc
->cond
= active_ifs
[i
-1];
4534 ifc
->trueexpr
= active_if
;
4537 if_expr
*outermost_if
= active_if
;
4538 while (active_if
&& active_if
->trueexpr
)
4539 active_if
= as_a
<if_expr
*> (active_if
->trueexpr
);
4541 const cpp_token
*token
= peek ();
4543 /* If this if is immediately closed then it is part of a predicate
4544 definition. Push it. */
4545 if (token
->type
== CPP_CLOSE_PAREN
)
4548 fatal_at (token
, "expected transform expression");
4551 active_if
->trueexpr
= result
;
4552 result
= outermost_if
;
4554 push_simplify (kind
, simplifiers
, match
, result
);
4558 operand
*tem
= parse_result (result
, matcher
);
4561 active_if
->trueexpr
= tem
;
4562 result
= outermost_if
;
4567 push_simplify (kind
, simplifiers
, match
, result
);
4570 /* Parsing of the outer control structures. */
4572 /* Parse a for expression
4573 for = '(' 'for' <subst>... <pattern> ')'
4574 subst = <ident> '(' <ident>... ')' */
4577 parser::parse_for (source_location
)
4579 auto_vec
<const cpp_token
*> user_id_tokens
;
4580 vec
<user_id
*> user_ids
= vNULL
;
4581 const cpp_token
*token
;
4582 unsigned min_n_opers
= 0, max_n_opers
= 0;
4587 if (token
->type
!= CPP_NAME
)
4590 /* Insert the user defined operators into the operator hash. */
4591 const char *id
= get_ident ();
4592 if (get_operator (id
, true) != NULL
)
4593 fatal_at (token
, "operator already defined");
4594 user_id
*op
= new user_id (id
);
4595 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
4597 user_ids
.safe_push (op
);
4598 user_id_tokens
.safe_push (token
);
4600 eat_token (CPP_OPEN_PAREN
);
4603 while ((token
= peek_ident ()) != 0)
4605 const char *oper
= get_ident ();
4606 id_base
*idb
= get_operator (oper
, true);
4608 fatal_at (token
, "no such operator '%s'", oper
);
4609 if (*idb
== CONVERT0
|| *idb
== CONVERT1
|| *idb
== CONVERT2
4610 || *idb
== VIEW_CONVERT0
|| *idb
== VIEW_CONVERT1
4611 || *idb
== VIEW_CONVERT2
)
4612 fatal_at (token
, "conditional operators cannot be used inside for");
4616 else if (idb
->nargs
== -1)
4618 else if (idb
->nargs
!= arity
)
4619 fatal_at (token
, "operator '%s' with arity %d does not match "
4620 "others with arity %d", oper
, idb
->nargs
, arity
);
4622 user_id
*p
= dyn_cast
<user_id
*> (idb
);
4625 if (p
->is_oper_list
)
4626 op
->substitutes
.safe_splice (p
->substitutes
);
4628 fatal_at (token
, "iterator cannot be used as operator-list");
4631 op
->substitutes
.safe_push (idb
);
4634 token
= expect (CPP_CLOSE_PAREN
);
4636 unsigned nsubstitutes
= op
->substitutes
.length ();
4637 if (nsubstitutes
== 0)
4638 fatal_at (token
, "A user-defined operator must have at least "
4639 "one substitution");
4640 if (max_n_opers
== 0)
4642 min_n_opers
= nsubstitutes
;
4643 max_n_opers
= nsubstitutes
;
4647 if (nsubstitutes
% min_n_opers
!= 0
4648 && min_n_opers
% nsubstitutes
!= 0)
4649 fatal_at (token
, "All user-defined identifiers must have a "
4650 "multiple number of operator substitutions of the "
4651 "smallest number of substitutions");
4652 if (nsubstitutes
< min_n_opers
)
4653 min_n_opers
= nsubstitutes
;
4654 else if (nsubstitutes
> max_n_opers
)
4655 max_n_opers
= nsubstitutes
;
4659 unsigned n_ids
= user_ids
.length ();
4661 fatal_at (token
, "for requires at least one user-defined identifier");
4664 if (token
->type
== CPP_CLOSE_PAREN
)
4665 fatal_at (token
, "no pattern defined in for");
4667 active_fors
.safe_push (user_ids
);
4671 if (token
->type
== CPP_CLOSE_PAREN
)
4677 /* Remove user-defined operators from the hash again. */
4678 for (unsigned i
= 0; i
< user_ids
.length (); ++i
)
4680 if (!user_ids
[i
]->used
)
4681 warning_at (user_id_tokens
[i
],
4682 "operator %s defined but not used", user_ids
[i
]->id
);
4683 operators
->remove_elt (user_ids
[i
]);
4687 /* Parse an identifier associated with a list of operators.
4688 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
4691 parser::parse_operator_list (source_location
)
4693 const cpp_token
*token
= peek ();
4694 const char *id
= get_ident ();
4696 if (get_operator (id
, true) != 0)
4697 fatal_at (token
, "operator %s already defined", id
);
4699 user_id
*op
= new user_id (id
, true);
4702 while ((token
= peek_ident ()) != 0)
4705 const char *oper
= get_ident ();
4706 id_base
*idb
= get_operator (oper
, true);
4709 fatal_at (token
, "no such operator '%s'", oper
);
4713 else if (idb
->nargs
== -1)
4715 else if (arity
!= idb
->nargs
)
4716 fatal_at (token
, "operator '%s' with arity %d does not match "
4717 "others with arity %d", oper
, idb
->nargs
, arity
);
4719 /* We allow composition of multiple operator lists. */
4720 if (user_id
*p
= dyn_cast
<user_id
*> (idb
))
4721 op
->substitutes
.safe_splice (p
->substitutes
);
4723 op
->substitutes
.safe_push (idb
);
4726 // Check that there is no junk after id-list
4728 if (token
->type
!= CPP_CLOSE_PAREN
)
4729 fatal_at (token
, "expected identifier got %s", cpp_type2name (token
->type
, 0));
4731 if (op
->substitutes
.length () == 0)
4732 fatal_at (token
, "operator-list cannot be empty");
4735 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
4739 /* Parse an outer if expression.
4740 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
4743 parser::parse_if (source_location
)
4745 c_expr
*ifexpr
= parse_c_expr (CPP_OPEN_PAREN
);
4747 const cpp_token
*token
= peek ();
4748 if (token
->type
== CPP_CLOSE_PAREN
)
4749 fatal_at (token
, "no pattern defined in if");
4751 active_ifs
.safe_push (ifexpr
);
4754 const cpp_token
*token
= peek ();
4755 if (token
->type
== CPP_CLOSE_PAREN
)
4763 /* Parse a list of predefined predicate identifiers.
4764 preds = '(' 'define_predicates' <ident>... ')' */
4767 parser::parse_predicates (source_location
)
4771 const cpp_token
*token
= peek ();
4772 if (token
->type
!= CPP_NAME
)
4775 add_predicate (get_ident ());
4780 /* Parse outer control structures.
4781 pattern = <preds>|<for>|<if>|<simplify>|<match> */
4784 parser::parse_pattern ()
4786 /* All clauses start with '('. */
4787 eat_token (CPP_OPEN_PAREN
);
4788 const cpp_token
*token
= peek ();
4789 const char *id
= get_ident ();
4790 if (strcmp (id
, "simplify") == 0)
4792 parse_simplify (simplify::SIMPLIFY
, simplifiers
, NULL
, NULL
);
4795 else if (strcmp (id
, "match") == 0)
4797 bool with_args
= false;
4798 source_location e_loc
= peek ()->src_loc
;
4799 if (peek ()->type
== CPP_OPEN_PAREN
)
4801 eat_token (CPP_OPEN_PAREN
);
4804 const char *name
= get_ident ();
4805 id_base
*id
= get_operator (name
);
4809 p
= add_predicate (name
);
4810 user_predicates
.safe_push (p
);
4812 else if ((p
= dyn_cast
<predicate_id
*> (id
)))
4815 fatal_at (token
, "cannot add a match to a non-predicate ID");
4816 /* Parse (match <id> <arg>... (match-expr)) here. */
4820 capture_ids
= new cid_map_t
;
4821 e
= new expr (p
, e_loc
);
4822 while (peek ()->type
== CPP_ATSIGN
)
4823 e
->append_op (parse_capture (NULL
, false));
4824 eat_token (CPP_CLOSE_PAREN
);
4827 && ((e
&& e
->ops
.length () != (unsigned)p
->nargs
)
4828 || (!e
&& p
->nargs
!= 0)))
4829 fatal_at (token
, "non-matching number of match operands");
4830 p
->nargs
= e
? e
->ops
.length () : 0;
4831 parse_simplify (simplify::MATCH
, p
->matchers
, p
, e
);
4834 else if (strcmp (id
, "for") == 0)
4835 parse_for (token
->src_loc
);
4836 else if (strcmp (id
, "if") == 0)
4837 parse_if (token
->src_loc
);
4838 else if (strcmp (id
, "define_predicates") == 0)
4840 if (active_ifs
.length () > 0
4841 || active_fors
.length () > 0)
4842 fatal_at (token
, "define_predicates inside if or for is not supported");
4843 parse_predicates (token
->src_loc
);
4845 else if (strcmp (id
, "define_operator_list") == 0)
4847 if (active_ifs
.length () > 0
4848 || active_fors
.length () > 0)
4849 fatal_at (token
, "operator-list inside if or for is not supported");
4850 parse_operator_list (token
->src_loc
);
4853 fatal_at (token
, "expected %s'simplify', 'match', 'for' or 'if'",
4854 active_ifs
.length () == 0 && active_fors
.length () == 0
4855 ? "'define_predicates', " : "");
4857 eat_token (CPP_CLOSE_PAREN
);
4860 /* Helper for finish_match_operand, collecting captures of OP in CPTS
4864 walk_captures (operand
*op
, vec
<vec
<capture
*> > cpts
)
4869 if (capture
*c
= dyn_cast
<capture
*> (op
))
4871 cpts
[c
->where
].safe_push (c
);
4872 walk_captures (c
->what
, cpts
);
4874 else if (expr
*e
= dyn_cast
<expr
*> (op
))
4875 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
4876 walk_captures (e
->ops
[i
], cpts
);
4879 /* Finish up OP which is a match operand. */
4882 parser::finish_match_operand (operand
*op
)
4884 /* Look for matching captures, diagnose mis-uses of @@ and apply
4885 early lowering and distribution of value_match. */
4886 auto_vec
<vec
<capture
*> > cpts
;
4887 cpts
.safe_grow_cleared (capture_ids
->elements ());
4888 walk_captures (op
, cpts
);
4889 for (unsigned i
= 0; i
< cpts
.length (); ++i
)
4891 capture
*value_match
= NULL
;
4892 for (unsigned j
= 0; j
< cpts
[i
].length (); ++j
)
4894 if (cpts
[i
][j
]->value_match
)
4897 fatal_at (cpts
[i
][j
]->location
, "duplicate @@");
4898 value_match
= cpts
[i
][j
];
4901 if (cpts
[i
].length () == 1 && value_match
)
4902 fatal_at (value_match
->location
, "@@ without a matching capture");
4905 /* Duplicate prevailing capture with the existing ID, create
4906 a fake ID and rewrite all captures to use it. This turns
4907 @@1 into @__<newid>@1 and @1 into @__<newid>. */
4908 value_match
->what
= new capture (value_match
->location
,
4910 value_match
->what
, false);
4911 /* Create a fake ID and rewrite all captures to use it. */
4912 unsigned newid
= get_internal_capture_id ();
4913 for (unsigned j
= 0; j
< cpts
[i
].length (); ++j
)
4915 cpts
[i
][j
]->where
= newid
;
4916 cpts
[i
][j
]->value_match
= true;
4923 /* Main entry of the parser. Repeatedly parse outer control structures. */
4925 parser::parser (cpp_reader
*r_
)
4929 active_fors
= vNULL
;
4930 simplifiers
= vNULL
;
4931 oper_lists_set
= NULL
;
4934 user_predicates
= vNULL
;
4935 parsing_match_operand
= false;
4938 const cpp_token
*token
= next ();
4939 while (token
->type
!= CPP_EOF
)
4941 _cpp_backup_tokens (r
, 1);
4948 /* Helper for the linemap code. */
4951 round_alloc_size (size_t s
)
4957 /* The genmatch generator progam. It reads from a pattern description
4958 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
4961 main (int argc
, char **argv
)
4965 progname
= "genmatch";
4971 char *input
= argv
[argc
-1];
4972 for (int i
= 1; i
< argc
- 1; ++i
)
4974 if (strcmp (argv
[i
], "--gimple") == 0)
4976 else if (strcmp (argv
[i
], "--generic") == 0)
4978 else if (strcmp (argv
[i
], "-v") == 0)
4980 else if (strcmp (argv
[i
], "-vv") == 0)
4984 fprintf (stderr
, "Usage: genmatch "
4985 "[--gimple] [--generic] [-v[v]] input\n");
4990 line_table
= XCNEW (struct line_maps
);
4991 linemap_init (line_table
, 0);
4992 line_table
->reallocator
= xrealloc
;
4993 line_table
->round_alloc_size
= round_alloc_size
;
4995 r
= cpp_create_reader (CLK_GNUC99
, NULL
, line_table
);
4996 cpp_callbacks
*cb
= cpp_get_callbacks (r
);
4997 cb
->error
= error_cb
;
4999 /* Add the build directory to the #include "" search path. */
5000 cpp_dir
*dir
= XCNEW (cpp_dir
);
5001 dir
->name
= getpwd ();
5003 dir
->name
= ASTRDUP (".");
5004 cpp_set_include_chains (r
, dir
, NULL
, false);
5006 if (!cpp_read_main_file (r
, input
))
5008 cpp_define (r
, gimple
? "GIMPLE=1": "GENERIC=1");
5009 cpp_define (r
, gimple
? "GENERIC=0": "GIMPLE=0");
5011 null_id
= new id_base (id_base::NULL_ID
, "null");
5013 /* Pre-seed operators. */
5014 operators
= new hash_table
<id_base
> (1024);
5015 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
5016 add_operator (SYM, # SYM, # TYPE, NARGS);
5017 #define END_OF_BASE_TREE_CODES
5019 add_operator (CONVERT0
, "convert0", "tcc_unary", 1);
5020 add_operator (CONVERT1
, "convert1", "tcc_unary", 1);
5021 add_operator (CONVERT2
, "convert2", "tcc_unary", 1);
5022 add_operator (VIEW_CONVERT0
, "view_convert0", "tcc_unary", 1);
5023 add_operator (VIEW_CONVERT1
, "view_convert1", "tcc_unary", 1);
5024 add_operator (VIEW_CONVERT2
, "view_convert2", "tcc_unary", 1);
5025 #undef END_OF_BASE_TREE_CODES
5028 /* Pre-seed builtin functions.
5029 ??? Cannot use N (name) as that is targetm.emultls.get_address
5030 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
5031 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
5032 add_function (ENUM, "CFN_" # ENUM);
5033 #include "builtins.def"
5035 #define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \
5036 add_function (IFN_##CODE, "CFN_" #CODE);
5037 #include "internal-fn.def"
5043 write_header (stdout
, "gimple-match-head.c");
5045 write_header (stdout
, "generic-match-head.c");
5047 /* Go over all predicates defined with patterns and perform
5048 lowering and code generation. */
5049 for (unsigned i
= 0; i
< p
.user_predicates
.length (); ++i
)
5051 predicate_id
*pred
= p
.user_predicates
[i
];
5052 lower (pred
->matchers
, gimple
);
5055 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
5056 print_matches (pred
->matchers
[i
]);
5059 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
5060 dt
.insert (pred
->matchers
[i
], i
);
5065 write_predicate (stdout
, pred
, dt
, gimple
);
5068 /* Lower the main simplifiers and generate code for them. */
5069 lower (p
.simplifiers
, gimple
);
5072 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
5073 print_matches (p
.simplifiers
[i
]);
5076 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
5077 dt
.insert (p
.simplifiers
[i
], i
);
5082 dt
.gen (stdout
, gimple
);
5085 cpp_finish (r
, NULL
);