1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
4 Copyright (C) 2014-2016 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
)
66 const struct line_map_ordinary
*map
;
67 loc
= linemap_resolve_location (line_table
, loc
, LRK_SPELLING_LOCATION
, &map
);
68 return linemap_expand_location (line_table
, map
, loc
);
72 #if GCC_VERSION >= 4001
73 __attribute__((format (printf
, 5, 0)))
75 error_cb (cpp_reader
*, int errtype
, int, rich_location
*richloc
,
76 const char *msg
, va_list *ap
)
78 const line_map_ordinary
*map
;
79 source_location location
= richloc
->get_loc ();
80 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
81 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
82 fprintf (stderr
, "%s:%d:%d %s: ", loc
.file
, loc
.line
, loc
.column
,
83 (errtype
== CPP_DL_WARNING
) ? "warning" : "error");
84 vfprintf (stderr
, msg
, *ap
);
85 fprintf (stderr
, "\n");
86 FILE *f
= fopen (loc
.file
, "r");
92 if (!fgets (buf
, 128, f
))
94 if (buf
[strlen (buf
) - 1] != '\n')
101 fprintf (stderr
, "%s", buf
);
102 for (int i
= 0; i
< loc
.column
- 1; ++i
)
105 fputc ('\n', stderr
);
110 if (errtype
== CPP_DL_FATAL
)
116 #if GCC_VERSION >= 4001
117 __attribute__((format (printf
, 2, 3)))
119 fatal_at (const cpp_token
*tk
, const char *msg
, ...)
121 rich_location
richloc (line_table
, tk
->src_loc
);
124 error_cb (NULL
, CPP_DL_FATAL
, 0, &richloc
, msg
, &ap
);
129 #if GCC_VERSION >= 4001
130 __attribute__((format (printf
, 2, 3)))
132 fatal_at (source_location loc
, const char *msg
, ...)
134 rich_location
richloc (line_table
, loc
);
137 error_cb (NULL
, CPP_DL_FATAL
, 0, &richloc
, msg
, &ap
);
142 #if GCC_VERSION >= 4001
143 __attribute__((format (printf
, 2, 3)))
145 warning_at (const cpp_token
*tk
, const char *msg
, ...)
147 rich_location
richloc (line_table
, tk
->src_loc
);
150 error_cb (NULL
, CPP_DL_WARNING
, 0, &richloc
, msg
, &ap
);
155 #if GCC_VERSION >= 4001
156 __attribute__((format (printf
, 2, 3)))
158 warning_at (source_location loc
, const char *msg
, ...)
160 rich_location
richloc (line_table
, loc
);
163 error_cb (NULL
, CPP_DL_WARNING
, 0, &richloc
, msg
, &ap
);
167 /* Like fprintf, but print INDENT spaces at the beginning. */
170 #if GCC_VERSION >= 4001
171 __attribute__((format (printf
, 3, 4)))
173 fprintf_indent (FILE *f
, unsigned int indent
, const char *format
, ...)
176 for (; indent
>= 8; indent
-= 8)
178 fprintf (f
, "%*s", indent
, "");
179 va_start (ap
, format
);
180 vfprintf (f
, format
, ap
);
185 output_line_directive (FILE *f
, source_location location
,
186 bool dumpfile
= false)
188 const line_map_ordinary
*map
;
189 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
190 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
193 /* When writing to a dumpfile only dump the filename. */
194 const char *file
= strrchr (loc
.file
, DIR_SEPARATOR
);
199 fprintf (f
, "%s:%d", file
, loc
.line
);
202 /* Other gen programs really output line directives here, at least for
203 development it's right now more convenient to have line information
204 from the generated file. Still keep the directives as comment for now
205 to easily back-point to the meta-description. */
206 fprintf (f
, "/* #line %d \"%s\" */\n", loc
.line
, loc
.file
);
210 /* Pull in tree codes and builtin function codes from their
213 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
226 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
227 enum built_in_function
{
228 #include "builtins.def"
232 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE,
234 #include "internal-fn.def"
238 /* Return true if CODE represents a commutative tree code. Otherwise
241 commutative_tree_code (enum tree_code code
)
247 case MULT_HIGHPART_EXPR
:
262 case WIDEN_MULT_EXPR
:
263 case VEC_WIDEN_MULT_HI_EXPR
:
264 case VEC_WIDEN_MULT_LO_EXPR
:
265 case VEC_WIDEN_MULT_EVEN_EXPR
:
266 case VEC_WIDEN_MULT_ODD_EXPR
:
275 /* Return true if CODE represents a ternary tree code for which the
276 first two operands are commutative. Otherwise return false. */
278 commutative_ternary_tree_code (enum tree_code code
)
282 case WIDEN_MULT_PLUS_EXPR
:
283 case WIDEN_MULT_MINUS_EXPR
:
294 /* Return true if CODE is a comparison. */
297 comparison_code_p (enum tree_code code
)
324 /* Base class for all identifiers the parser knows. */
326 struct id_base
: nofree_ptr_hash
<id_base
>
328 enum id_kind
{ CODE
, FN
, PREDICATE
, USER
, NULL_ID
} kind
;
330 id_base (id_kind
, const char *, int = -1);
336 /* hash_table support. */
337 static inline hashval_t
hash (const id_base
*);
338 static inline int equal (const id_base
*, const id_base
*);
342 id_base::hash (const id_base
*op
)
348 id_base::equal (const id_base
*op1
,
351 return (op1
->hashval
== op2
->hashval
352 && strcmp (op1
->id
, op2
->id
) == 0);
355 /* The special id "null", which matches nothing. */
356 static id_base
*null_id
;
358 /* Hashtable of known pattern operators. This is pre-seeded from
359 all known tree codes and all known builtin function ids. */
360 static hash_table
<id_base
> *operators
;
362 id_base::id_base (id_kind kind_
, const char *id_
, int nargs_
)
367 hashval
= htab_hash_string (id
);
370 /* Identifier that maps to a tree code. */
372 struct operator_id
: public id_base
374 operator_id (enum tree_code code_
, const char *id_
, unsigned nargs_
,
376 : id_base (id_base::CODE
, id_
, nargs_
), code (code_
), tcc (tcc_
) {}
381 /* Identifier that maps to a builtin or internal function code. */
383 struct fn_id
: public id_base
385 fn_id (enum built_in_function fn_
, const char *id_
)
386 : id_base (id_base::FN
, id_
), fn (fn_
) {}
387 fn_id (enum internal_fn fn_
, const char *id_
)
388 : id_base (id_base::FN
, id_
), fn (int (END_BUILTINS
) + int (fn_
)) {}
394 /* Identifier that maps to a user-defined predicate. */
396 struct predicate_id
: public id_base
398 predicate_id (const char *id_
)
399 : id_base (id_base::PREDICATE
, id_
), matchers (vNULL
) {}
400 vec
<simplify
*> matchers
;
403 /* Identifier that maps to a operator defined by a 'for' directive. */
405 struct user_id
: public id_base
407 user_id (const char *id_
, bool is_oper_list_
= false)
408 : id_base (id_base::USER
, id_
), substitutes (vNULL
),
409 used (false), is_oper_list (is_oper_list_
) {}
410 vec
<id_base
*> substitutes
;
418 is_a_helper
<fn_id
*>::test (id_base
*id
)
420 return id
->kind
== id_base::FN
;
426 is_a_helper
<operator_id
*>::test (id_base
*id
)
428 return id
->kind
== id_base::CODE
;
434 is_a_helper
<predicate_id
*>::test (id_base
*id
)
436 return id
->kind
== id_base::PREDICATE
;
442 is_a_helper
<user_id
*>::test (id_base
*id
)
444 return id
->kind
== id_base::USER
;
447 /* Add a predicate identifier to the hash. */
449 static predicate_id
*
450 add_predicate (const char *id
)
452 predicate_id
*p
= new predicate_id (id
);
453 id_base
**slot
= operators
->find_slot_with_hash (p
, p
->hashval
, INSERT
);
455 fatal ("duplicate id definition");
460 /* Add a tree code identifier to the hash. */
463 add_operator (enum tree_code code
, const char *id
,
464 const char *tcc
, unsigned nargs
)
466 if (strcmp (tcc
, "tcc_unary") != 0
467 && strcmp (tcc
, "tcc_binary") != 0
468 && strcmp (tcc
, "tcc_comparison") != 0
469 && strcmp (tcc
, "tcc_expression") != 0
470 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
471 && strcmp (tcc
, "tcc_reference") != 0
472 /* To have INTEGER_CST and friends as "predicate operators". */
473 && strcmp (tcc
, "tcc_constant") != 0
474 /* And allow CONSTRUCTOR for vector initializers. */
475 && !(code
== CONSTRUCTOR
)
476 /* Allow SSA_NAME as predicate operator. */
477 && !(code
== SSA_NAME
))
479 /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
480 if (code
== ADDR_EXPR
)
482 operator_id
*op
= new operator_id (code
, id
, nargs
, tcc
);
483 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
485 fatal ("duplicate id definition");
489 /* Add a built-in or internal function identifier to the hash. ID is
490 the name of its CFN_* enumeration value. */
492 template <typename T
>
494 add_function (T code
, const char *id
)
496 fn_id
*fn
= new fn_id (code
, id
);
497 id_base
**slot
= operators
->find_slot_with_hash (fn
, fn
->hashval
, INSERT
);
499 fatal ("duplicate id definition");
503 /* Helper for easy comparing ID with tree code CODE. */
506 operator==(id_base
&id
, enum tree_code code
)
508 if (operator_id
*oid
= dyn_cast
<operator_id
*> (&id
))
509 return oid
->code
== code
;
513 /* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */
516 get_operator (const char *id
, bool allow_null
= false)
518 if (allow_null
&& strcmp (id
, "null") == 0)
521 id_base
tem (id_base::CODE
, id
);
523 id_base
*op
= operators
->find_with_hash (&tem
, tem
.hashval
);
526 /* If this is a user-defined identifier track whether it was used. */
527 if (user_id
*uid
= dyn_cast
<user_id
*> (op
))
533 bool all_upper
= true;
534 bool all_lower
= true;
535 for (unsigned int i
= 0; id
[i
]; ++i
)
538 else if (ISLOWER (id
[i
]))
542 /* Try in caps with _EXPR appended. */
543 id2
= ACONCAT ((id
, "_EXPR", NULL
));
544 for (unsigned int i
= 0; id2
[i
]; ++i
)
545 id2
[i
] = TOUPPER (id2
[i
]);
547 else if (all_upper
&& strncmp (id
, "IFN_", 4) == 0)
548 /* Try CFN_ instead of IFN_. */
549 id2
= ACONCAT (("CFN_", id
+ 4, NULL
));
550 else if (all_upper
&& strncmp (id
, "BUILT_IN_", 9) == 0)
551 /* Try prepending CFN_. */
552 id2
= ACONCAT (("CFN_", id
, NULL
));
556 new (&tem
) id_base (id_base::CODE
, id2
);
557 return operators
->find_with_hash (&tem
, tem
.hashval
);
560 /* Return the comparison operators that results if the operands are
561 swapped. This is safe for floating-point. */
564 swap_tree_comparison (operator_id
*p
)
576 return get_operator ("LT_EXPR");
578 return get_operator ("LE_EXPR");
580 return get_operator ("GT_EXPR");
582 return get_operator ("GE_EXPR");
584 return get_operator ("UNLT_EXPR");
586 return get_operator ("UNLE_EXPR");
588 return get_operator ("UNGT_EXPR");
590 return get_operator ("UNGE_EXPR");
596 typedef hash_map
<nofree_string_hash
, unsigned> cid_map_t
;
599 /* The AST produced by parsing of the pattern definitions. */
604 /* The base class for operands. */
607 enum op_type
{ OP_PREDICATE
, OP_EXPR
, OP_CAPTURE
, OP_C_EXPR
, OP_IF
, OP_WITH
};
608 operand (enum op_type type_
, source_location loc_
)
609 : type (type_
), location (loc_
) {}
611 source_location location
;
612 virtual void gen_transform (FILE *, int, const char *, bool, int,
613 const char *, capture_info
*,
616 { gcc_unreachable (); }
619 /* A predicate operand. Predicates are leafs in the AST. */
621 struct predicate
: public operand
623 predicate (predicate_id
*p_
, source_location loc
)
624 : operand (OP_PREDICATE
, loc
), p (p_
) {}
628 /* An operand that constitutes an expression. Expressions include
629 function calls and user-defined predicate invocations. */
631 struct expr
: public operand
633 expr (id_base
*operation_
, source_location loc
, bool is_commutative_
= false)
634 : operand (OP_EXPR
, loc
), operation (operation_
),
635 ops (vNULL
), expr_type (NULL
), is_commutative (is_commutative_
),
636 is_generic (false), force_single_use (false) {}
638 : operand (OP_EXPR
, e
->location
), operation (e
->operation
),
639 ops (vNULL
), expr_type (e
->expr_type
), is_commutative (e
->is_commutative
),
640 is_generic (e
->is_generic
), force_single_use (e
->force_single_use
) {}
641 void append_op (operand
*op
) { ops
.safe_push (op
); }
642 /* The operator and its operands. */
645 /* An explicitely specified type - used exclusively for conversions. */
646 const char *expr_type
;
647 /* Whether the operation is to be applied commutatively. This is
648 later lowered to two separate patterns. */
650 /* Whether the expression is expected to be in GENERIC form. */
652 /* Whether pushing any stmt to the sequence should be conditional
653 on this expression having a single-use. */
654 bool force_single_use
;
655 virtual void gen_transform (FILE *f
, int, const char *, bool, int,
656 const char *, capture_info
*,
657 dt_operand
** = 0, int = 0);
660 /* An operator that is represented by native C code. This is always
661 a leaf operand in the AST. This class is also used to represent
662 the code to be generated for 'if' and 'with' expressions. */
664 struct c_expr
: public operand
666 /* A mapping of an identifier and its replacement. Used to apply
671 id_tab (const char *id_
, const char *oper_
): id (id_
), oper (oper_
) {}
674 c_expr (cpp_reader
*r_
, source_location loc
,
675 vec
<cpp_token
> code_
, unsigned nr_stmts_
,
676 vec
<id_tab
> ids_
, cid_map_t
*capture_ids_
)
677 : operand (OP_C_EXPR
, loc
), r (r_
), code (code_
),
678 capture_ids (capture_ids_
), nr_stmts (nr_stmts_
), ids (ids_
) {}
679 /* cpplib tokens and state to transform this back to source. */
682 cid_map_t
*capture_ids
;
683 /* The number of statements parsed (well, the number of ';'s). */
685 /* The identifier replacement vector. */
687 virtual void gen_transform (FILE *f
, int, const char *, bool, int,
688 const char *, capture_info
*,
689 dt_operand
** = 0, int = 0);
692 /* A wrapper around another operand that captures its value. */
694 struct capture
: public operand
696 capture (source_location loc
, unsigned where_
, operand
*what_
)
697 : operand (OP_CAPTURE
, loc
), where (where_
), what (what_
) {}
698 /* Identifier index for the value. */
700 /* The captured value. */
702 virtual void gen_transform (FILE *f
, int, const char *, bool, int,
703 const char *, capture_info
*,
704 dt_operand
** = 0, int = 0);
709 struct if_expr
: public operand
711 if_expr (source_location loc
)
712 : operand (OP_IF
, loc
), cond (NULL
), trueexpr (NULL
), falseexpr (NULL
) {}
718 /* with expression. */
720 struct with_expr
: public operand
722 with_expr (source_location loc
)
723 : operand (OP_WITH
, loc
), with (NULL
), subexpr (NULL
) {}
731 is_a_helper
<capture
*>::test (operand
*op
)
733 return op
->type
== operand::OP_CAPTURE
;
739 is_a_helper
<predicate
*>::test (operand
*op
)
741 return op
->type
== operand::OP_PREDICATE
;
747 is_a_helper
<c_expr
*>::test (operand
*op
)
749 return op
->type
== operand::OP_C_EXPR
;
755 is_a_helper
<expr
*>::test (operand
*op
)
757 return op
->type
== operand::OP_EXPR
;
763 is_a_helper
<if_expr
*>::test (operand
*op
)
765 return op
->type
== operand::OP_IF
;
771 is_a_helper
<with_expr
*>::test (operand
*op
)
773 return op
->type
== operand::OP_WITH
;
776 /* The main class of a pattern and its transform. This is used to
777 represent both (simplify ...) and (match ...) kinds. The AST
778 duplicates all outer 'if' and 'for' expressions here so each
779 simplify can exist in isolation. */
783 enum simplify_kind
{ SIMPLIFY
, MATCH
};
785 simplify (simplify_kind kind_
, operand
*match_
, operand
*result_
,
786 vec
<vec
<user_id
*> > for_vec_
, cid_map_t
*capture_ids_
)
787 : kind (kind_
), match (match_
), result (result_
),
788 for_vec (for_vec_
), for_subst_vec (vNULL
),
789 capture_ids (capture_ids_
), capture_max (capture_ids_
->elements () - 1) {}
792 /* The expression that is matched against the GENERIC or GIMPLE IL. */
794 /* For a (simplify ...) an expression with ifs and withs with the expression
795 produced when the pattern applies in the leafs.
796 For a (match ...) the leafs are either empty if it is a simple predicate
797 or the single expression specifying the matched operands. */
798 struct operand
*result
;
799 /* Collected 'for' expression operators that have to be replaced
800 in the lowering phase. */
801 vec
<vec
<user_id
*> > for_vec
;
802 vec
<std::pair
<user_id
*, id_base
*> > for_subst_vec
;
803 /* A map of capture identifiers to indexes. */
804 cid_map_t
*capture_ids
;
808 /* Debugging routines for dumping the AST. */
811 print_operand (operand
*o
, FILE *f
= stderr
, bool flattened
= false)
813 if (capture
*c
= dyn_cast
<capture
*> (o
))
815 if (c
->what
&& flattened
== false)
816 print_operand (c
->what
, f
, flattened
);
817 fprintf (f
, "@%u", c
->where
);
820 else if (predicate
*p
= dyn_cast
<predicate
*> (o
))
821 fprintf (f
, "%s", p
->p
->id
);
823 else if (is_a
<c_expr
*> (o
))
824 fprintf (f
, "c_expr");
826 else if (expr
*e
= dyn_cast
<expr
*> (o
))
828 if (e
->ops
.length () == 0)
829 fprintf (f
, "%s", e
->operation
->id
);
832 fprintf (f
, "(%s", e
->operation
->id
);
834 if (flattened
== false)
836 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
839 print_operand (e
->ops
[i
], f
, flattened
);
851 print_matches (struct simplify
*s
, FILE *f
= stderr
)
853 fprintf (f
, "for expression: ");
854 print_operand (s
->match
, f
);
861 /* Lowering of commutative operators. */
864 cartesian_product (const vec
< vec
<operand
*> >& ops_vector
,
865 vec
< vec
<operand
*> >& result
, vec
<operand
*>& v
, unsigned n
)
867 if (n
== ops_vector
.length ())
869 vec
<operand
*> xv
= v
.copy ();
870 result
.safe_push (xv
);
874 for (unsigned i
= 0; i
< ops_vector
[n
].length (); ++i
)
876 v
[n
] = ops_vector
[n
][i
];
877 cartesian_product (ops_vector
, result
, v
, n
+ 1);
881 /* Lower OP to two operands in case it is marked as commutative. */
883 static vec
<operand
*>
884 commutate (operand
*op
, vec
<vec
<user_id
*> > &for_vec
)
886 vec
<operand
*> ret
= vNULL
;
888 if (capture
*c
= dyn_cast
<capture
*> (op
))
895 vec
<operand
*> v
= commutate (c
->what
, for_vec
);
896 for (unsigned i
= 0; i
< v
.length (); ++i
)
898 capture
*nc
= new capture (c
->location
, c
->where
, v
[i
]);
904 expr
*e
= dyn_cast
<expr
*> (op
);
905 if (!e
|| e
->ops
.length () == 0)
911 vec
< vec
<operand
*> > ops_vector
= vNULL
;
912 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
913 ops_vector
.safe_push (commutate (e
->ops
[i
], for_vec
));
915 auto_vec
< vec
<operand
*> > result
;
916 auto_vec
<operand
*> v (e
->ops
.length ());
917 v
.quick_grow_cleared (e
->ops
.length ());
918 cartesian_product (ops_vector
, result
, v
, 0);
921 for (unsigned i
= 0; i
< result
.length (); ++i
)
923 expr
*ne
= new expr (e
);
924 ne
->is_commutative
= false;
925 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
926 ne
->append_op (result
[i
][j
]);
930 if (!e
->is_commutative
)
933 for (unsigned i
= 0; i
< result
.length (); ++i
)
935 expr
*ne
= new expr (e
);
936 if (operator_id
*p
= dyn_cast
<operator_id
*> (ne
->operation
))
938 if (comparison_code_p (p
->code
))
939 ne
->operation
= swap_tree_comparison (p
);
941 else if (user_id
*p
= dyn_cast
<user_id
*> (ne
->operation
))
943 bool found_compare
= false;
944 for (unsigned j
= 0; j
< p
->substitutes
.length (); ++j
)
945 if (operator_id
*q
= dyn_cast
<operator_id
*> (p
->substitutes
[j
]))
947 if (comparison_code_p (q
->code
)
948 && swap_tree_comparison (q
) != q
)
950 found_compare
= true;
956 user_id
*newop
= new user_id ("<internal>");
957 for (unsigned j
= 0; j
< p
->substitutes
.length (); ++j
)
959 id_base
*subst
= p
->substitutes
[j
];
960 if (operator_id
*q
= dyn_cast
<operator_id
*> (subst
))
962 if (comparison_code_p (q
->code
))
963 subst
= swap_tree_comparison (q
);
965 newop
->substitutes
.safe_push (subst
);
967 ne
->operation
= newop
;
968 /* Search for 'p' inside the for vector and push 'newop'
969 to the same level. */
970 for (unsigned j
= 0; newop
&& j
< for_vec
.length (); ++j
)
971 for (unsigned k
= 0; k
< for_vec
[j
].length (); ++k
)
972 if (for_vec
[j
][k
] == p
)
974 for_vec
[j
].safe_push (newop
);
980 ne
->is_commutative
= false;
981 // result[i].length () is 2 since e->operation is binary
982 for (unsigned j
= result
[i
].length (); j
; --j
)
983 ne
->append_op (result
[i
][j
-1]);
990 /* Lower operations marked as commutative in the AST of S and push
991 the resulting patterns to SIMPLIFIERS. */
994 lower_commutative (simplify
*s
, vec
<simplify
*>& simplifiers
)
996 vec
<operand
*> matchers
= commutate (s
->match
, s
->for_vec
);
997 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
999 simplify
*ns
= new simplify (s
->kind
, matchers
[i
], s
->result
,
1000 s
->for_vec
, s
->capture_ids
);
1001 simplifiers
.safe_push (ns
);
1005 /* Strip conditional conversios using operator OPER from O and its
1006 children if STRIP, else replace them with an unconditional convert. */
1009 lower_opt_convert (operand
*o
, enum tree_code oper
,
1010 enum tree_code to_oper
, bool strip
)
1012 if (capture
*c
= dyn_cast
<capture
*> (o
))
1015 return new capture (c
->location
, c
->where
,
1016 lower_opt_convert (c
->what
, oper
, to_oper
, strip
));
1021 expr
*e
= dyn_cast
<expr
*> (o
);
1025 if (*e
->operation
== oper
)
1028 return lower_opt_convert (e
->ops
[0], oper
, to_oper
, strip
);
1030 expr
*ne
= new expr (e
);
1031 ne
->operation
= (to_oper
== CONVERT_EXPR
1032 ? get_operator ("CONVERT_EXPR")
1033 : get_operator ("VIEW_CONVERT_EXPR"));
1034 ne
->append_op (lower_opt_convert (e
->ops
[0], oper
, to_oper
, strip
));
1038 expr
*ne
= new expr (e
);
1039 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1040 ne
->append_op (lower_opt_convert (e
->ops
[i
], oper
, to_oper
, strip
));
1045 /* Determine whether O or its children uses the conditional conversion
1049 has_opt_convert (operand
*o
, enum tree_code oper
)
1051 if (capture
*c
= dyn_cast
<capture
*> (o
))
1054 return has_opt_convert (c
->what
, oper
);
1059 expr
*e
= dyn_cast
<expr
*> (o
);
1063 if (*e
->operation
== oper
)
1066 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1067 if (has_opt_convert (e
->ops
[i
], oper
))
1073 /* Lower conditional convert operators in O, expanding it to a vector
1076 static vec
<operand
*>
1077 lower_opt_convert (operand
*o
)
1079 vec
<operand
*> v1
= vNULL
, v2
;
1083 enum tree_code opers
[]
1084 = { CONVERT0
, CONVERT_EXPR
,
1085 CONVERT1
, CONVERT_EXPR
,
1086 CONVERT2
, CONVERT_EXPR
,
1087 VIEW_CONVERT0
, VIEW_CONVERT_EXPR
,
1088 VIEW_CONVERT1
, VIEW_CONVERT_EXPR
,
1089 VIEW_CONVERT2
, VIEW_CONVERT_EXPR
};
1091 /* Conditional converts are lowered to a pattern with the
1092 conversion and one without. The three different conditional
1093 convert codes are lowered separately. */
1095 for (unsigned i
= 0; i
< sizeof (opers
) / sizeof (enum tree_code
); i
+= 2)
1098 for (unsigned j
= 0; j
< v1
.length (); ++j
)
1099 if (has_opt_convert (v1
[j
], opers
[i
]))
1101 v2
.safe_push (lower_opt_convert (v1
[j
],
1102 opers
[i
], opers
[i
+1], false));
1103 v2
.safe_push (lower_opt_convert (v1
[j
],
1104 opers
[i
], opers
[i
+1], true));
1110 for (unsigned j
= 0; j
< v2
.length (); ++j
)
1111 v1
.safe_push (v2
[j
]);
1118 /* Lower conditional convert operators in the AST of S and push
1119 the resulting multiple patterns to SIMPLIFIERS. */
1122 lower_opt_convert (simplify
*s
, vec
<simplify
*>& simplifiers
)
1124 vec
<operand
*> matchers
= lower_opt_convert (s
->match
);
1125 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
1127 simplify
*ns
= new simplify (s
->kind
, matchers
[i
], s
->result
,
1128 s
->for_vec
, s
->capture_ids
);
1129 simplifiers
.safe_push (ns
);
1133 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
1134 GENERIC and a GIMPLE variant. */
1136 static vec
<operand
*>
1137 lower_cond (operand
*o
)
1139 vec
<operand
*> ro
= vNULL
;
1141 if (capture
*c
= dyn_cast
<capture
*> (o
))
1145 vec
<operand
*> lop
= vNULL
;
1146 lop
= lower_cond (c
->what
);
1148 for (unsigned i
= 0; i
< lop
.length (); ++i
)
1149 ro
.safe_push (new capture (c
->location
, c
->where
, lop
[i
]));
1154 expr
*e
= dyn_cast
<expr
*> (o
);
1155 if (!e
|| e
->ops
.length () == 0)
1161 vec
< vec
<operand
*> > ops_vector
= vNULL
;
1162 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1163 ops_vector
.safe_push (lower_cond (e
->ops
[i
]));
1165 auto_vec
< vec
<operand
*> > result
;
1166 auto_vec
<operand
*> v (e
->ops
.length ());
1167 v
.quick_grow_cleared (e
->ops
.length ());
1168 cartesian_product (ops_vector
, result
, v
, 0);
1170 for (unsigned i
= 0; i
< result
.length (); ++i
)
1172 expr
*ne
= new expr (e
);
1173 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
1174 ne
->append_op (result
[i
][j
]);
1176 /* If this is a COND with a captured expression or an
1177 expression with two operands then also match a GENERIC
1178 form on the compare. */
1179 if ((*e
->operation
== COND_EXPR
1180 || *e
->operation
== VEC_COND_EXPR
)
1181 && ((is_a
<capture
*> (e
->ops
[0])
1182 && as_a
<capture
*> (e
->ops
[0])->what
1183 && is_a
<expr
*> (as_a
<capture
*> (e
->ops
[0])->what
)
1185 (as_a
<capture
*> (e
->ops
[0])->what
)->ops
.length () == 2)
1186 || (is_a
<expr
*> (e
->ops
[0])
1187 && as_a
<expr
*> (e
->ops
[0])->ops
.length () == 2)))
1189 expr
*ne
= new expr (e
);
1190 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
1191 ne
->append_op (result
[i
][j
]);
1192 if (capture
*c
= dyn_cast
<capture
*> (ne
->ops
[0]))
1194 expr
*ocmp
= as_a
<expr
*> (c
->what
);
1195 expr
*cmp
= new expr (ocmp
);
1196 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
1197 cmp
->append_op (ocmp
->ops
[j
]);
1198 cmp
->is_generic
= true;
1199 ne
->ops
[0] = new capture (c
->location
, c
->where
, cmp
);
1203 expr
*ocmp
= as_a
<expr
*> (ne
->ops
[0]);
1204 expr
*cmp
= new expr (ocmp
);
1205 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
1206 cmp
->append_op (ocmp
->ops
[j
]);
1207 cmp
->is_generic
= true;
1217 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
1218 GENERIC and a GIMPLE variant. */
1221 lower_cond (simplify
*s
, vec
<simplify
*>& simplifiers
)
1223 vec
<operand
*> matchers
= lower_cond (s
->match
);
1224 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
1226 simplify
*ns
= new simplify (s
->kind
, matchers
[i
], s
->result
,
1227 s
->for_vec
, s
->capture_ids
);
1228 simplifiers
.safe_push (ns
);
1232 /* Return true if O refers to ID. */
1235 contains_id (operand
*o
, user_id
*id
)
1237 if (capture
*c
= dyn_cast
<capture
*> (o
))
1238 return c
->what
&& contains_id (c
->what
, id
);
1240 if (expr
*e
= dyn_cast
<expr
*> (o
))
1242 if (e
->operation
== id
)
1244 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1245 if (contains_id (e
->ops
[i
], id
))
1250 if (with_expr
*w
= dyn_cast
<with_expr
*> (o
))
1251 return (contains_id (w
->with
, id
)
1252 || contains_id (w
->subexpr
, id
));
1254 if (if_expr
*ife
= dyn_cast
<if_expr
*> (o
))
1255 return (contains_id (ife
->cond
, id
)
1256 || contains_id (ife
->trueexpr
, id
)
1257 || (ife
->falseexpr
&& contains_id (ife
->falseexpr
, id
)));
1259 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
1260 return ce
->capture_ids
&& ce
->capture_ids
->get (id
->id
);
1266 /* In AST operand O replace operator ID with operator WITH. */
1269 replace_id (operand
*o
, user_id
*id
, id_base
*with
)
1271 /* Deep-copy captures and expressions, replacing operations as
1273 if (capture
*c
= dyn_cast
<capture
*> (o
))
1277 return new capture (c
->location
, c
->where
,
1278 replace_id (c
->what
, id
, with
));
1280 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1282 expr
*ne
= new expr (e
);
1283 if (e
->operation
== id
)
1284 ne
->operation
= with
;
1285 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1286 ne
->append_op (replace_id (e
->ops
[i
], id
, with
));
1289 else if (with_expr
*w
= dyn_cast
<with_expr
*> (o
))
1291 with_expr
*nw
= new with_expr (w
->location
);
1292 nw
->with
= as_a
<c_expr
*> (replace_id (w
->with
, id
, with
));
1293 nw
->subexpr
= replace_id (w
->subexpr
, id
, with
);
1296 else if (if_expr
*ife
= dyn_cast
<if_expr
*> (o
))
1298 if_expr
*nife
= new if_expr (ife
->location
);
1299 nife
->cond
= as_a
<c_expr
*> (replace_id (ife
->cond
, id
, with
));
1300 nife
->trueexpr
= replace_id (ife
->trueexpr
, id
, with
);
1302 nife
->falseexpr
= replace_id (ife
->falseexpr
, id
, with
);
1306 /* For c_expr we simply record a string replacement table which is
1307 applied at code-generation time. */
1308 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
1310 vec
<c_expr::id_tab
> ids
= ce
->ids
.copy ();
1311 ids
.safe_push (c_expr::id_tab (id
->id
, with
->id
));
1312 return new c_expr (ce
->r
, ce
->location
,
1313 ce
->code
, ce
->nr_stmts
, ids
, ce
->capture_ids
);
1319 /* Return true if the binary operator OP is ok for delayed substitution
1320 during for lowering. */
1323 binary_ok (operator_id
*op
)
1330 case TRUNC_DIV_EXPR
:
1332 case FLOOR_DIV_EXPR
:
1333 case ROUND_DIV_EXPR
:
1334 case TRUNC_MOD_EXPR
:
1336 case FLOOR_MOD_EXPR
:
1337 case ROUND_MOD_EXPR
:
1339 case EXACT_DIV_EXPR
:
1351 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1354 lower_for (simplify
*sin
, vec
<simplify
*>& simplifiers
)
1356 vec
<vec
<user_id
*> >& for_vec
= sin
->for_vec
;
1357 unsigned worklist_start
= 0;
1358 auto_vec
<simplify
*> worklist
;
1359 worklist
.safe_push (sin
);
1361 /* Lower each recorded for separately, operating on the
1362 set of simplifiers created by the previous one.
1363 Lower inner-to-outer so inner for substitutes can refer
1364 to operators replaced by outer fors. */
1365 for (int fi
= for_vec
.length () - 1; fi
>= 0; --fi
)
1367 vec
<user_id
*>& ids
= for_vec
[fi
];
1368 unsigned n_ids
= ids
.length ();
1369 unsigned max_n_opers
= 0;
1370 bool can_delay_subst
= (sin
->kind
== simplify::SIMPLIFY
);
1371 for (unsigned i
= 0; i
< n_ids
; ++i
)
1373 if (ids
[i
]->substitutes
.length () > max_n_opers
)
1374 max_n_opers
= ids
[i
]->substitutes
.length ();
1375 /* Require that all substitutes are of the same kind so that
1376 if we delay substitution to the result op code generation
1377 can look at the first substitute for deciding things like
1378 types of operands. */
1379 enum id_base::id_kind kind
= ids
[i
]->substitutes
[0]->kind
;
1380 for (unsigned j
= 0; j
< ids
[i
]->substitutes
.length (); ++j
)
1381 if (ids
[i
]->substitutes
[j
]->kind
!= kind
)
1382 can_delay_subst
= false;
1383 else if (operator_id
*op
1384 = dyn_cast
<operator_id
*> (ids
[i
]->substitutes
[j
]))
1387 = as_a
<operator_id
*> (ids
[i
]->substitutes
[0]);
1388 if (strcmp (op
->tcc
, "tcc_comparison") == 0
1389 && strcmp (op0
->tcc
, "tcc_comparison") == 0)
1391 /* Unfortunately we can't just allow all tcc_binary. */
1392 else if (strcmp (op
->tcc
, "tcc_binary") == 0
1393 && strcmp (op0
->tcc
, "tcc_binary") == 0
1397 else if ((strcmp (op
->id
+ 1, "SHIFT_EXPR") == 0
1398 || strcmp (op
->id
+ 1, "ROTATE_EXPR") == 0)
1399 && (strcmp (op0
->id
+ 1, "SHIFT_EXPR") == 0
1400 || strcmp (op0
->id
+ 1, "ROTATE_EXPR") == 0))
1403 can_delay_subst
= false;
1405 else if (is_a
<fn_id
*> (ids
[i
]->substitutes
[j
]))
1408 can_delay_subst
= false;
1411 unsigned worklist_end
= worklist
.length ();
1412 for (unsigned si
= worklist_start
; si
< worklist_end
; ++si
)
1414 simplify
*s
= worklist
[si
];
1415 for (unsigned j
= 0; j
< max_n_opers
; ++j
)
1417 operand
*match_op
= s
->match
;
1418 operand
*result_op
= s
->result
;
1419 auto_vec
<std::pair
<user_id
*, id_base
*> > subst (n_ids
);
1421 for (unsigned i
= 0; i
< n_ids
; ++i
)
1423 user_id
*id
= ids
[i
];
1424 id_base
*oper
= id
->substitutes
[j
% id
->substitutes
.length ()];
1426 && (contains_id (match_op
, id
)
1427 || contains_id (result_op
, id
)))
1432 subst
.quick_push (std::make_pair (id
, oper
));
1433 match_op
= replace_id (match_op
, id
, oper
);
1435 && !can_delay_subst
)
1436 result_op
= replace_id (result_op
, id
, oper
);
1441 simplify
*ns
= new simplify (s
->kind
, match_op
, result_op
,
1442 vNULL
, s
->capture_ids
);
1443 ns
->for_subst_vec
.safe_splice (s
->for_subst_vec
);
1446 ns
->for_subst_vec
.safe_splice (subst
);
1448 worklist
.safe_push (ns
);
1451 worklist_start
= worklist_end
;
1454 /* Copy out the result from the last for lowering. */
1455 for (unsigned i
= worklist_start
; i
< worklist
.length (); ++i
)
1456 simplifiers
.safe_push (worklist
[i
]);
1459 /* Lower the AST for everything in SIMPLIFIERS. */
1462 lower (vec
<simplify
*>& simplifiers
, bool gimple
)
1464 auto_vec
<simplify
*> out_simplifiers
;
1465 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1466 lower_opt_convert (simplifiers
[i
], out_simplifiers
);
1468 simplifiers
.truncate (0);
1469 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1470 lower_commutative (out_simplifiers
[i
], simplifiers
);
1472 out_simplifiers
.truncate (0);
1474 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1475 lower_cond (simplifiers
[i
], out_simplifiers
);
1477 out_simplifiers
.safe_splice (simplifiers
);
1480 simplifiers
.truncate (0);
1481 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1482 lower_for (out_simplifiers
[i
], simplifiers
);
1488 /* The decision tree built for generating GIMPLE and GENERIC pattern
1489 matching code. It represents the 'match' expression of all
1490 simplifies and has those as its leafs. */
1494 /* A hash-map collecting semantically equivalent leafs in the decision
1495 tree for splitting out to separate functions. */
1504 struct sinfo_hashmap_traits
: simple_hashmap_traits
<pointer_hash
<dt_simplify
>,
1507 static inline hashval_t
hash (const key_type
&);
1508 static inline bool equal_keys (const key_type
&, const key_type
&);
1509 template <typename T
> static inline void remove (T
&) {}
1512 typedef hash_map
<void * /* unused */, sinfo
*, sinfo_hashmap_traits
>
1516 /* Decision tree base class, used for DT_TRUE and DT_NODE. */
1520 enum dt_type
{ DT_NODE
, DT_OPERAND
, DT_TRUE
, DT_MATCH
, DT_SIMPLIFY
};
1524 vec
<dt_node
*> kids
;
1528 unsigned total_size
;
1531 dt_node (enum dt_type type_
): type (type_
), level (0), kids (vNULL
) {}
1533 dt_node
*append_node (dt_node
*);
1534 dt_node
*append_op (operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1535 dt_node
*append_true_op (dt_node
*parent
= 0, unsigned pos
= 0);
1536 dt_node
*append_match_op (dt_operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1537 dt_node
*append_simplify (simplify
*, unsigned, dt_operand
**);
1539 virtual void gen (FILE *, int, bool) {}
1541 void gen_kids (FILE *, int, bool);
1542 void gen_kids_1 (FILE *, int, bool,
1543 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_operand
*>,
1544 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_node
*>);
1546 void analyze (sinfo_map_t
&);
1549 /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */
1551 struct dt_operand
: public dt_node
1554 dt_operand
*match_dop
;
1558 dt_operand (enum dt_type type
, operand
*op_
, dt_operand
*match_dop_
,
1559 dt_operand
*parent_
= 0, unsigned pos_
= 0)
1560 : dt_node (type
), op (op_
), match_dop (match_dop_
),
1561 parent (parent_
), pos (pos_
) {}
1563 void gen (FILE *, int, bool);
1564 unsigned gen_predicate (FILE *, int, const char *, bool);
1565 unsigned gen_match_op (FILE *, int, const char *, bool);
1567 unsigned gen_gimple_expr (FILE *, int);
1568 unsigned gen_generic_expr (FILE *, int, const char *);
1570 char *get_name (char *);
1571 void gen_opname (char *, unsigned);
1574 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1576 struct dt_simplify
: public dt_node
1579 unsigned pattern_no
;
1580 dt_operand
**indexes
;
1583 dt_simplify (simplify
*s_
, unsigned pattern_no_
, dt_operand
**indexes_
)
1584 : dt_node (DT_SIMPLIFY
), s (s_
), pattern_no (pattern_no_
),
1585 indexes (indexes_
), info (NULL
) {}
1587 void gen_1 (FILE *, int, bool, operand
*);
1588 void gen (FILE *f
, int, bool);
1594 is_a_helper
<dt_operand
*>::test (dt_node
*n
)
1596 return (n
->type
== dt_node::DT_OPERAND
1597 || n
->type
== dt_node::DT_MATCH
);
1603 is_a_helper
<dt_simplify
*>::test (dt_node
*n
)
1605 return n
->type
== dt_node::DT_SIMPLIFY
;
1610 /* A container for the actual decision tree. */
1612 struct decision_tree
1616 void insert (struct simplify
*, unsigned);
1617 void gen (FILE *f
, bool gimple
);
1618 void print (FILE *f
= stderr
);
1620 decision_tree () { root
= new dt_node (dt_node::DT_NODE
); }
1622 static dt_node
*insert_operand (dt_node
*, operand
*, dt_operand
**indexes
,
1623 unsigned pos
= 0, dt_node
*parent
= 0);
1624 static dt_node
*find_node (vec
<dt_node
*>&, dt_node
*);
1625 static bool cmp_node (dt_node
*, dt_node
*);
1626 static void print_node (dt_node
*, FILE *f
= stderr
, unsigned = 0);
1629 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1632 cmp_operand (operand
*o1
, operand
*o2
)
1634 if (!o1
|| !o2
|| o1
->type
!= o2
->type
)
1637 if (o1
->type
== operand::OP_PREDICATE
)
1639 predicate
*p1
= as_a
<predicate
*>(o1
);
1640 predicate
*p2
= as_a
<predicate
*>(o2
);
1641 return p1
->p
== p2
->p
;
1643 else if (o1
->type
== operand::OP_EXPR
)
1645 expr
*e1
= static_cast<expr
*>(o1
);
1646 expr
*e2
= static_cast<expr
*>(o2
);
1647 return (e1
->operation
== e2
->operation
1648 && e1
->is_generic
== e2
->is_generic
);
1654 /* Compare two decision tree nodes N1 and N2 and return true if they
1658 decision_tree::cmp_node (dt_node
*n1
, dt_node
*n2
)
1660 if (!n1
|| !n2
|| n1
->type
!= n2
->type
)
1666 if (n1
->type
== dt_node::DT_TRUE
)
1669 if (n1
->type
== dt_node::DT_OPERAND
)
1670 return cmp_operand ((as_a
<dt_operand
*> (n1
))->op
,
1671 (as_a
<dt_operand
*> (n2
))->op
);
1672 else if (n1
->type
== dt_node::DT_MATCH
)
1673 return ((as_a
<dt_operand
*> (n1
))->match_dop
1674 == (as_a
<dt_operand
*> (n2
))->match_dop
);
1678 /* Search OPS for a decision tree node like P and return it if found. */
1681 decision_tree::find_node (vec
<dt_node
*>& ops
, dt_node
*p
)
1683 /* We can merge adjacent DT_TRUE. */
1684 if (p
->type
== dt_node::DT_TRUE
1686 && ops
.last ()->type
== dt_node::DT_TRUE
)
1688 for (int i
= ops
.length () - 1; i
>= 0; --i
)
1690 /* But we can't merge across DT_TRUE nodes as they serve as
1691 pattern order barriers to make sure that patterns apply
1692 in order of appearance in case multiple matches are possible. */
1693 if (ops
[i
]->type
== dt_node::DT_TRUE
)
1695 if (decision_tree::cmp_node (ops
[i
], p
))
1701 /* Append N to the decision tree if it there is not already an existing
1705 dt_node::append_node (dt_node
*n
)
1709 kid
= decision_tree::find_node (kids
, n
);
1714 n
->level
= this->level
+ 1;
1719 /* Append OP to the decision tree. */
1722 dt_node::append_op (operand
*op
, dt_node
*parent
, unsigned pos
)
1724 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1725 dt_operand
*n
= new dt_operand (DT_OPERAND
, op
, 0, parent_
, pos
);
1726 return append_node (n
);
1729 /* Append a DT_TRUE decision tree node. */
1732 dt_node::append_true_op (dt_node
*parent
, unsigned pos
)
1734 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1735 dt_operand
*n
= new dt_operand (DT_TRUE
, 0, 0, parent_
, pos
);
1736 return append_node (n
);
1739 /* Append a DT_MATCH decision tree node. */
1742 dt_node::append_match_op (dt_operand
*match_dop
, dt_node
*parent
, unsigned pos
)
1744 dt_operand
*parent_
= as_a
<dt_operand
*> (parent
);
1745 dt_operand
*n
= new dt_operand (DT_MATCH
, 0, match_dop
, parent_
, pos
);
1746 return append_node (n
);
1749 /* Append S to the decision tree. */
1752 dt_node::append_simplify (simplify
*s
, unsigned pattern_no
,
1753 dt_operand
**indexes
)
1755 dt_simplify
*n
= new dt_simplify (s
, pattern_no
, indexes
);
1756 for (unsigned i
= 0; i
< kids
.length (); ++i
)
1757 if (dt_simplify
*s2
= dyn_cast
<dt_simplify
*> (kids
[i
]))
1759 warning_at (s
->match
->location
, "duplicate pattern");
1760 warning_at (s2
->s
->match
->location
, "previous pattern defined here");
1761 print_operand (s
->match
, stderr
);
1762 fprintf (stderr
, "\n");
1764 return append_node (n
);
1767 /* Analyze the node and its children. */
1770 dt_node::analyze (sinfo_map_t
&map
)
1776 if (type
== DT_SIMPLIFY
)
1778 /* Populate the map of equivalent simplifies. */
1779 dt_simplify
*s
= as_a
<dt_simplify
*> (this);
1781 sinfo
*&si
= map
.get_or_insert (s
, &existed
);
1796 for (unsigned i
= 0; i
< kids
.length (); ++i
)
1798 kids
[i
]->analyze (map
);
1799 num_leafs
+= kids
[i
]->num_leafs
;
1800 total_size
+= kids
[i
]->total_size
;
1801 max_level
= MAX (max_level
, kids
[i
]->max_level
);
1805 /* Insert O into the decision tree and return the decision tree node found
1809 decision_tree::insert_operand (dt_node
*p
, operand
*o
, dt_operand
**indexes
,
1810 unsigned pos
, dt_node
*parent
)
1812 dt_node
*q
, *elm
= 0;
1814 if (capture
*c
= dyn_cast
<capture
*> (o
))
1816 unsigned capt_index
= c
->where
;
1818 if (indexes
[capt_index
] == 0)
1821 q
= insert_operand (p
, c
->what
, indexes
, pos
, parent
);
1824 q
= elm
= p
->append_true_op (parent
, pos
);
1827 // get to the last capture
1828 for (operand
*what
= c
->what
;
1829 what
&& is_a
<capture
*> (what
);
1830 c
= as_a
<capture
*> (what
), what
= c
->what
)
1835 unsigned cc_index
= c
->where
;
1836 dt_operand
*match_op
= indexes
[cc_index
];
1838 dt_operand
temp (dt_node::DT_TRUE
, 0, 0);
1839 elm
= decision_tree::find_node (p
->kids
, &temp
);
1843 dt_operand
temp (dt_node::DT_MATCH
, 0, match_op
);
1844 elm
= decision_tree::find_node (p
->kids
, &temp
);
1849 dt_operand
temp (dt_node::DT_OPERAND
, c
->what
, 0);
1850 elm
= decision_tree::find_node (p
->kids
, &temp
);
1854 gcc_assert (elm
->type
== dt_node::DT_TRUE
1855 || elm
->type
== dt_node::DT_OPERAND
1856 || elm
->type
== dt_node::DT_MATCH
);
1857 indexes
[capt_index
] = static_cast<dt_operand
*> (elm
);
1862 p
= p
->append_match_op (indexes
[capt_index
], parent
, pos
);
1864 return insert_operand (p
, c
->what
, indexes
, 0, p
);
1869 p
= p
->append_op (o
, parent
, pos
);
1872 if (expr
*e
= dyn_cast
<expr
*>(o
))
1874 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1875 q
= decision_tree::insert_operand (q
, e
->ops
[i
], indexes
, i
, p
);
1881 /* Insert S into the decision tree. */
1884 decision_tree::insert (struct simplify
*s
, unsigned pattern_no
)
1886 dt_operand
**indexes
= XCNEWVEC (dt_operand
*, s
->capture_max
+ 1);
1887 dt_node
*p
= decision_tree::insert_operand (root
, s
->match
, indexes
);
1888 p
->append_simplify (s
, pattern_no
, indexes
);
1891 /* Debug functions to dump the decision tree. */
1894 decision_tree::print_node (dt_node
*p
, FILE *f
, unsigned indent
)
1896 if (p
->type
== dt_node::DT_NODE
)
1897 fprintf (f
, "root");
1901 for (unsigned i
= 0; i
< indent
; i
++)
1904 if (p
->type
== dt_node::DT_OPERAND
)
1906 dt_operand
*dop
= static_cast<dt_operand
*>(p
);
1907 print_operand (dop
->op
, f
, true);
1909 else if (p
->type
== dt_node::DT_TRUE
)
1910 fprintf (f
, "true");
1911 else if (p
->type
== dt_node::DT_MATCH
)
1912 fprintf (f
, "match (%p)", (void *)((as_a
<dt_operand
*>(p
))->match_dop
));
1913 else if (p
->type
== dt_node::DT_SIMPLIFY
)
1915 dt_simplify
*s
= static_cast<dt_simplify
*> (p
);
1916 fprintf (f
, "simplify_%u { ", s
->pattern_no
);
1917 for (int i
= 0; i
<= s
->s
->capture_max
; ++i
)
1918 fprintf (f
, "%p, ", (void *) s
->indexes
[i
]);
1923 fprintf (stderr
, " (%p), %u, %u\n", (void *) p
, p
->level
, p
->kids
.length ());
1925 for (unsigned i
= 0; i
< p
->kids
.length (); ++i
)
1926 decision_tree::print_node (p
->kids
[i
], f
, indent
+ 2);
1930 decision_tree::print (FILE *f
)
1932 return decision_tree::print_node (root
, f
);
1936 /* For GENERIC we have to take care of wrapping multiple-used
1937 expressions with side-effects in save_expr and preserve side-effects
1938 of expressions with omit_one_operand. Analyze captures in
1939 match, result and with expressions and perform early-outs
1940 on the outermost match expression operands for cases we cannot
1945 capture_info (simplify
*s
, operand
*, bool);
1946 void walk_match (operand
*o
, unsigned toplevel_arg
, bool, bool);
1947 bool walk_result (operand
*o
, bool, operand
*);
1948 void walk_c_expr (c_expr
*);
1954 bool force_no_side_effects_p
;
1955 bool force_single_use
;
1956 bool cond_expr_cond_p
;
1957 unsigned long toplevel_msk
;
1958 unsigned match_use_count
;
1959 unsigned result_use_count
;
1964 auto_vec
<cinfo
> info
;
1965 unsigned long force_no_side_effects
;
1969 /* Analyze captures in S. */
1971 capture_info::capture_info (simplify
*s
, operand
*result
, bool gimple_
)
1976 if (s
->kind
== simplify::MATCH
)
1978 force_no_side_effects
= -1;
1982 force_no_side_effects
= 0;
1983 info
.safe_grow_cleared (s
->capture_max
+ 1);
1984 for (int i
= 0; i
<= s
->capture_max
; ++i
)
1985 info
[i
].same_as
= i
;
1987 e
= as_a
<expr
*> (s
->match
);
1988 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1989 walk_match (e
->ops
[i
], i
,
1990 (i
!= 0 && *e
->operation
== COND_EXPR
)
1991 || *e
->operation
== TRUTH_ANDIF_EXPR
1992 || *e
->operation
== TRUTH_ORIF_EXPR
,
1994 && (*e
->operation
== COND_EXPR
1995 || *e
->operation
== VEC_COND_EXPR
));
1997 walk_result (s
->result
, false, result
);
2000 /* Analyze captures in the match expression piece O. */
2003 capture_info::walk_match (operand
*o
, unsigned toplevel_arg
,
2004 bool conditional_p
, bool cond_expr_cond_p
)
2006 if (capture
*c
= dyn_cast
<capture
*> (o
))
2008 unsigned where
= c
->where
;
2009 info
[where
].match_use_count
++;
2010 info
[where
].toplevel_msk
|= 1 << toplevel_arg
;
2011 info
[where
].force_no_side_effects_p
|= conditional_p
;
2012 info
[where
].cond_expr_cond_p
|= cond_expr_cond_p
;
2017 /* Recurse to exprs and captures. */
2018 if (is_a
<capture
*> (c
->what
)
2019 || is_a
<expr
*> (c
->what
))
2020 walk_match (c
->what
, toplevel_arg
, conditional_p
, false);
2021 /* We need to look past multiple captures to find a captured
2022 expression as with conditional converts two captures
2023 can be collapsed onto the same expression. Also collect
2024 what captures capture the same thing. */
2025 while (c
->what
&& is_a
<capture
*> (c
->what
))
2027 c
= as_a
<capture
*> (c
->what
);
2028 if (info
[c
->where
].same_as
!= c
->where
2029 && info
[c
->where
].same_as
!= info
[where
].same_as
)
2030 fatal_at (c
->location
, "cannot handle this collapsed capture");
2031 info
[c
->where
].same_as
= info
[where
].same_as
;
2033 /* Mark expr (non-leaf) captures and forced single-use exprs. */
2036 && (e
= dyn_cast
<expr
*> (c
->what
)))
2038 info
[where
].expr_p
= true;
2039 info
[where
].force_single_use
|= e
->force_single_use
;
2042 else if (expr
*e
= dyn_cast
<expr
*> (o
))
2044 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
2046 bool cond_p
= conditional_p
;
2047 bool cond_expr_cond_p
= false;
2048 if (i
!= 0 && *e
->operation
== COND_EXPR
)
2050 else if (*e
->operation
== TRUTH_ANDIF_EXPR
2051 || *e
->operation
== TRUTH_ORIF_EXPR
)
2054 && (*e
->operation
== COND_EXPR
2055 || *e
->operation
== VEC_COND_EXPR
))
2056 cond_expr_cond_p
= true;
2057 walk_match (e
->ops
[i
], toplevel_arg
, cond_p
, cond_expr_cond_p
);
2060 else if (is_a
<predicate
*> (o
))
2062 /* Mark non-captured leafs toplevel arg for checking. */
2063 force_no_side_effects
|= 1 << toplevel_arg
;
2066 warning_at (o
->location
,
2067 "forcing no side-effects on possibly lost leaf");
2073 /* Analyze captures in the result expression piece O. Return true
2074 if RESULT was visited in one of the children. Only visit
2075 non-if/with children if they are rooted on RESULT. */
2078 capture_info::walk_result (operand
*o
, bool conditional_p
, operand
*result
)
2080 if (capture
*c
= dyn_cast
<capture
*> (o
))
2082 unsigned where
= info
[c
->where
].same_as
;
2083 info
[where
].result_use_count
++;
2084 /* If we substitute an expression capture we don't know
2085 which captures this will end up using (well, we don't
2086 compute that). Force the uses to be side-effect free
2087 which means forcing the toplevels that reach the
2088 expression side-effect free. */
2089 if (info
[where
].expr_p
)
2090 force_no_side_effects
|= info
[where
].toplevel_msk
;
2091 /* Mark CSE capture uses as forced to have no side-effects. */
2093 && is_a
<expr
*> (c
->what
))
2095 info
[where
].cse_p
= true;
2096 walk_result (c
->what
, true, result
);
2099 else if (expr
*e
= dyn_cast
<expr
*> (o
))
2101 id_base
*opr
= e
->operation
;
2102 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
2103 opr
= uid
->substitutes
[0];
2104 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
2106 bool cond_p
= conditional_p
;
2107 if (i
!= 0 && *e
->operation
== COND_EXPR
)
2109 else if (*e
->operation
== TRUTH_ANDIF_EXPR
2110 || *e
->operation
== TRUTH_ORIF_EXPR
)
2112 walk_result (e
->ops
[i
], cond_p
, result
);
2115 else if (if_expr
*e
= dyn_cast
<if_expr
*> (o
))
2117 /* 'if' conditions should be all fine. */
2118 if (e
->trueexpr
== result
)
2120 walk_result (e
->trueexpr
, false, result
);
2123 if (e
->falseexpr
== result
)
2125 walk_result (e
->falseexpr
, false, result
);
2129 if (is_a
<if_expr
*> (e
->trueexpr
)
2130 || is_a
<with_expr
*> (e
->trueexpr
))
2131 res
|= walk_result (e
->trueexpr
, false, result
);
2133 && (is_a
<if_expr
*> (e
->falseexpr
)
2134 || is_a
<with_expr
*> (e
->falseexpr
)))
2135 res
|= walk_result (e
->falseexpr
, false, result
);
2138 else if (with_expr
*e
= dyn_cast
<with_expr
*> (o
))
2140 bool res
= (e
->subexpr
== result
);
2142 || is_a
<if_expr
*> (e
->subexpr
)
2143 || is_a
<with_expr
*> (e
->subexpr
))
2144 res
|= walk_result (e
->subexpr
, false, result
);
2146 walk_c_expr (e
->with
);
2149 else if (c_expr
*e
= dyn_cast
<c_expr
*> (o
))
2157 /* Look for captures in the C expr E. */
2160 capture_info::walk_c_expr (c_expr
*e
)
2162 /* Give up for C exprs mentioning captures not inside TREE_TYPE,
2163 TREE_REAL_CST, TREE_CODE or a predicate where they cannot
2164 really escape through. */
2165 unsigned p_depth
= 0;
2166 for (unsigned i
= 0; i
< e
->code
.length (); ++i
)
2168 const cpp_token
*t
= &e
->code
[i
];
2169 const cpp_token
*n
= i
< e
->code
.length () - 1 ? &e
->code
[i
+1] : NULL
;
2171 if (t
->type
== CPP_NAME
2172 && (strcmp ((const char *)CPP_HASHNODE
2173 (t
->val
.node
.node
)->ident
.str
, "TREE_TYPE") == 0
2174 || strcmp ((const char *)CPP_HASHNODE
2175 (t
->val
.node
.node
)->ident
.str
, "TREE_CODE") == 0
2176 || strcmp ((const char *)CPP_HASHNODE
2177 (t
->val
.node
.node
)->ident
.str
, "TREE_REAL_CST") == 0
2178 || ((id
= get_operator ((const char *)CPP_HASHNODE
2179 (t
->val
.node
.node
)->ident
.str
))
2180 && is_a
<predicate_id
*> (id
)))
2181 && n
->type
== CPP_OPEN_PAREN
)
2183 else if (t
->type
== CPP_CLOSE_PAREN
2186 else if (p_depth
== 0
2187 && t
->type
== CPP_ATSIGN
2188 && (n
->type
== CPP_NUMBER
2189 || n
->type
== CPP_NAME
)
2190 && !(n
->flags
& PREV_WHITE
))
2193 if (n
->type
== CPP_NUMBER
)
2194 id
= (const char *)n
->val
.str
.text
;
2196 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
2197 unsigned *where
= e
->capture_ids
->get(id
);
2199 fatal_at (n
, "unknown capture id '%s'", id
);
2200 info
[info
[*where
].same_as
].force_no_side_effects_p
= true;
2203 warning_at (t
, "capture escapes");
2209 /* Code generation off the decision tree and the refered AST nodes. */
2212 is_conversion (id_base
*op
)
2214 return (*op
== CONVERT_EXPR
2216 || *op
== FLOAT_EXPR
2217 || *op
== FIX_TRUNC_EXPR
2218 || *op
== VIEW_CONVERT_EXPR
);
2221 /* Get the type to be used for generating operand POS of OP from the
2225 get_operand_type (id_base
*op
, unsigned pos
,
2226 const char *in_type
,
2227 const char *expr_type
,
2228 const char *other_oprnd_type
)
2230 /* Generally operands whose type does not match the type of the
2231 expression generated need to know their types but match and
2232 thus can fall back to 'other_oprnd_type'. */
2233 if (is_conversion (op
))
2234 return other_oprnd_type
;
2235 else if (*op
== REALPART_EXPR
2236 || *op
== IMAGPART_EXPR
)
2237 return other_oprnd_type
;
2238 else if (is_a
<operator_id
*> (op
)
2239 && strcmp (as_a
<operator_id
*> (op
)->tcc
, "tcc_comparison") == 0)
2240 return other_oprnd_type
;
2241 else if (*op
== COND_EXPR
2243 return "boolean_type_node";
2246 /* Otherwise all types should match - choose one in order of
2253 return other_oprnd_type
;
2257 /* Generate transform code for an expression. */
2260 expr::gen_transform (FILE *f
, int indent
, const char *dest
, bool gimple
,
2261 int depth
, const char *in_type
, capture_info
*cinfo
,
2262 dt_operand
**indexes
, int)
2264 id_base
*opr
= operation
;
2265 /* When we delay operator substituting during lowering of fors we
2266 make sure that for code-gen purposes the effects of each substitute
2267 are the same. Thus just look at that. */
2268 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
2269 opr
= uid
->substitutes
[0];
2271 bool conversion_p
= is_conversion (opr
);
2272 const char *type
= expr_type
;
2275 /* If there was a type specification in the pattern use it. */
2277 else if (conversion_p
)
2278 /* For conversions we need to build the expression using the
2279 outer type passed in. */
2281 else if (*opr
== REALPART_EXPR
2282 || *opr
== IMAGPART_EXPR
)
2284 /* __real and __imag use the component type of its operand. */
2285 sprintf (optype
, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth
);
2288 else if (is_a
<operator_id
*> (opr
)
2289 && !strcmp (as_a
<operator_id
*> (opr
)->tcc
, "tcc_comparison"))
2291 /* comparisons use boolean_type_node (or what gets in), but
2292 their operands need to figure out the types themselves. */
2297 sprintf (optype
, "boolean_type_node");
2302 else if (*opr
== COND_EXPR
2303 || *opr
== VEC_COND_EXPR
)
2305 /* Conditions are of the same type as their first alternative. */
2306 sprintf (optype
, "TREE_TYPE (ops%d[1])", depth
);
2311 /* Other operations are of the same type as their first operand. */
2312 sprintf (optype
, "TREE_TYPE (ops%d[0])", depth
);
2316 fatal_at (location
, "cannot determine type of operand");
2318 fprintf_indent (f
, indent
, "{\n");
2320 fprintf_indent (f
, indent
, "tree ops%d[%u], res;\n", depth
, ops
.length ());
2322 snprintf (op0type
, 64, "TREE_TYPE (ops%d[0])", depth
);
2323 for (unsigned i
= 0; i
< ops
.length (); ++i
)
2326 snprintf (dest
, 32, "ops%d[%u]", depth
, i
);
2328 = get_operand_type (opr
, i
, in_type
, expr_type
,
2329 i
== 0 ? NULL
: op0type
);
2330 ops
[i
]->gen_transform (f
, indent
, dest
, gimple
, depth
+ 1, optype
,
2333 || *opr
== VEC_COND_EXPR
) && i
== 0 ? 1 : 2);
2336 const char *opr_name
;
2337 if (*operation
== CONVERT_EXPR
)
2338 opr_name
= "NOP_EXPR";
2340 opr_name
= operation
->id
;
2344 if (*opr
== CONVERT_EXPR
)
2346 fprintf_indent (f
, indent
,
2347 "if (%s != TREE_TYPE (ops%d[0])\n",
2349 fprintf_indent (f
, indent
,
2350 " && !useless_type_conversion_p (%s, TREE_TYPE (ops%d[0])))\n",
2352 fprintf_indent (f
, indent
+ 2, "{\n");
2355 /* ??? Building a stmt can fail for various reasons here, seq being
2356 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
2357 So if we fail here we should continue matching other patterns. */
2358 fprintf_indent (f
, indent
, "code_helper tem_code = %s;\n", opr_name
);
2359 fprintf_indent (f
, indent
, "tree tem_ops[3] = { ");
2360 for (unsigned i
= 0; i
< ops
.length (); ++i
)
2361 fprintf (f
, "ops%d[%u]%s", depth
, i
,
2362 i
== ops
.length () - 1 ? " };\n" : ", ");
2363 fprintf_indent (f
, indent
,
2364 "gimple_resimplify%d (lseq, &tem_code, %s, tem_ops, valueize);\n",
2365 ops
.length (), type
);
2366 fprintf_indent (f
, indent
,
2367 "res = maybe_push_res_to_seq (tem_code, %s, tem_ops, lseq);\n",
2369 fprintf_indent (f
, indent
,
2370 "if (!res) return false;\n");
2371 if (*opr
== CONVERT_EXPR
)
2374 fprintf_indent (f
, indent
, " }\n");
2375 fprintf_indent (f
, indent
, "else\n");
2376 fprintf_indent (f
, indent
, " res = ops%d[0];\n", depth
);
2381 if (*opr
== CONVERT_EXPR
)
2383 fprintf_indent (f
, indent
, "if (TREE_TYPE (ops%d[0]) != %s)\n",
2387 if (opr
->kind
== id_base::CODE
)
2388 fprintf_indent (f
, indent
, "res = fold_build%d_loc (loc, %s, %s",
2389 ops
.length(), opr_name
, type
);
2392 fprintf_indent (f
, indent
, "{\n");
2393 fprintf_indent (f
, indent
, " res = maybe_build_call_expr_loc (loc, "
2394 "%s, %s, %d", opr_name
, type
, ops
.length());
2396 for (unsigned i
= 0; i
< ops
.length (); ++i
)
2397 fprintf (f
, ", ops%d[%u]", depth
, i
);
2398 fprintf (f
, ");\n");
2399 if (opr
->kind
!= id_base::CODE
)
2401 fprintf_indent (f
, indent
, " if (!res)\n");
2402 fprintf_indent (f
, indent
, " return NULL_TREE;\n");
2403 fprintf_indent (f
, indent
, "}\n");
2405 if (*opr
== CONVERT_EXPR
)
2408 fprintf_indent (f
, indent
, "else\n");
2409 fprintf_indent (f
, indent
, " res = ops%d[0];\n", depth
);
2412 fprintf_indent (f
, indent
, "%s = res;\n", dest
);
2414 fprintf_indent (f
, indent
, "}\n");
2417 /* Generate code for a c_expr which is either the expression inside
2418 an if statement or a sequence of statements which computes a
2419 result to be stored to DEST. */
2422 c_expr::gen_transform (FILE *f
, int indent
, const char *dest
,
2423 bool, int, const char *, capture_info
*,
2426 if (dest
&& nr_stmts
== 1)
2427 fprintf_indent (f
, indent
, "%s = ", dest
);
2429 unsigned stmt_nr
= 1;
2430 for (unsigned i
= 0; i
< code
.length (); ++i
)
2432 const cpp_token
*token
= &code
[i
];
2434 /* Replace captures for code-gen. */
2435 if (token
->type
== CPP_ATSIGN
)
2437 const cpp_token
*n
= &code
[i
+1];
2438 if ((n
->type
== CPP_NUMBER
2439 || n
->type
== CPP_NAME
)
2440 && !(n
->flags
& PREV_WHITE
))
2442 if (token
->flags
& PREV_WHITE
)
2445 if (n
->type
== CPP_NUMBER
)
2446 id
= (const char *)n
->val
.str
.text
;
2448 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
2449 unsigned *cid
= capture_ids
->get (id
);
2451 fatal_at (token
, "unknown capture id");
2452 fprintf (f
, "captures[%u]", *cid
);
2458 if (token
->flags
& PREV_WHITE
)
2461 if (token
->type
== CPP_NAME
)
2463 const char *id
= (const char *) NODE_NAME (token
->val
.node
.node
);
2465 for (j
= 0; j
< ids
.length (); ++j
)
2467 if (strcmp (id
, ids
[j
].id
) == 0)
2469 fprintf (f
, "%s", ids
[j
].oper
);
2473 if (j
< ids
.length ())
2477 /* Output the token as string. */
2478 char *tk
= (char *)cpp_token_as_text (r
, token
);
2481 if (token
->type
== CPP_SEMICOLON
)
2485 if (dest
&& stmt_nr
== nr_stmts
)
2486 fprintf_indent (f
, indent
, "%s = ", dest
);
2491 /* Generate transform code for a capture. */
2494 capture::gen_transform (FILE *f
, int indent
, const char *dest
, bool gimple
,
2495 int depth
, const char *in_type
, capture_info
*cinfo
,
2496 dt_operand
**indexes
, int cond_handling
)
2498 if (what
&& is_a
<expr
*> (what
))
2500 if (indexes
[where
] == 0)
2503 sprintf (buf
, "captures[%u]", where
);
2504 what
->gen_transform (f
, indent
, buf
, gimple
, depth
, in_type
,
2509 fprintf_indent (f
, indent
, "%s = captures[%u];\n", dest
, where
);
2511 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2512 with substituting a capture of that. */
2514 && cond_handling
!= 0
2515 && cinfo
->info
[where
].cond_expr_cond_p
)
2517 /* If substituting into a cond_expr condition, unshare. */
2518 if (cond_handling
== 1)
2519 fprintf_indent (f
, indent
, "%s = unshare_expr (%s);\n", dest
, dest
);
2520 /* If substituting elsewhere we might need to decompose it. */
2521 else if (cond_handling
== 2)
2523 /* ??? Returning false here will also not allow any other patterns
2524 to match unless this generator was split out. */
2525 fprintf_indent (f
, indent
, "if (COMPARISON_CLASS_P (%s))\n", dest
);
2526 fprintf_indent (f
, indent
, " {\n");
2527 fprintf_indent (f
, indent
, " if (!seq) return false;\n");
2528 fprintf_indent (f
, indent
, " %s = gimple_build (seq,"
2530 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
2531 " TREE_OPERAND (%s, 1));\n",
2532 dest
, dest
, dest
, dest
, dest
);
2533 fprintf_indent (f
, indent
, " }\n");
2538 /* Return the name of the operand representing the decision tree node.
2539 Use NAME as space to generate it. */
2542 dt_operand::get_name (char *name
)
2545 sprintf (name
, "t");
2546 else if (parent
->level
== 1)
2547 sprintf (name
, "op%u", pos
);
2548 else if (parent
->type
== dt_node::DT_MATCH
)
2549 return parent
->get_name (name
);
2551 sprintf (name
, "o%u%u", parent
->level
, pos
);
2555 /* Fill NAME with the operand name at position POS. */
2558 dt_operand::gen_opname (char *name
, unsigned pos
)
2561 sprintf (name
, "op%u", pos
);
2563 sprintf (name
, "o%u%u", level
, pos
);
2566 /* Generate matching code for the decision tree operand which is
2570 dt_operand::gen_predicate (FILE *f
, int indent
, const char *opname
, bool gimple
)
2572 predicate
*p
= as_a
<predicate
*> (op
);
2574 if (p
->p
->matchers
.exists ())
2576 /* If this is a predicate generated from a pattern mangle its
2577 name and pass on the valueize hook. */
2579 fprintf_indent (f
, indent
, "if (gimple_%s (%s, valueize))\n",
2582 fprintf_indent (f
, indent
, "if (tree_%s (%s))\n", p
->p
->id
, opname
);
2585 fprintf_indent (f
, indent
, "if (%s (%s))\n", p
->p
->id
, opname
);
2586 fprintf_indent (f
, indent
+ 2, "{\n");
2590 /* Generate matching code for the decision tree operand which is
2594 dt_operand::gen_match_op (FILE *f
, int indent
, const char *opname
, bool gimple
)
2596 char match_opname
[20];
2597 match_dop
->get_name (match_opname
);
2599 fprintf_indent (f
, indent
, "if (%s == %s || (operand_equal_p (%s, %s, 0) "
2600 "&& types_match (%s, %s)))\n",
2601 opname
, match_opname
, opname
, match_opname
,
2602 opname
, match_opname
);
2604 fprintf_indent (f
, indent
, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
2605 opname
, match_opname
, opname
, match_opname
);
2606 fprintf_indent (f
, indent
+ 2, "{\n");
2610 /* Generate GIMPLE matching code for the decision tree operand. */
2613 dt_operand::gen_gimple_expr (FILE *f
, int indent
)
2615 expr
*e
= static_cast<expr
*> (op
);
2616 id_base
*id
= e
->operation
;
2617 unsigned n_ops
= e
->ops
.length ();
2619 for (unsigned i
= 0; i
< n_ops
; ++i
)
2621 char child_opname
[20];
2622 gen_opname (child_opname
, i
);
2624 if (id
->kind
== id_base::CODE
)
2627 || *id
== REALPART_EXPR
|| *id
== IMAGPART_EXPR
2628 || *id
== BIT_FIELD_REF
|| *id
== VIEW_CONVERT_EXPR
)
2630 /* ??? If this is a memory operation we can't (and should not)
2631 match this. The only sensible operand types are
2632 SSA names and invariants. */
2633 fprintf_indent (f
, indent
,
2634 "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def), %i);\n",
2636 fprintf_indent (f
, indent
,
2637 "if ((TREE_CODE (%s) == SSA_NAME\n",
2639 fprintf_indent (f
, indent
,
2640 " || is_gimple_min_invariant (%s))\n",
2642 fprintf_indent (f
, indent
,
2643 " && (%s = do_valueize (valueize, %s)))\n",
2644 child_opname
, child_opname
);
2645 fprintf_indent (f
, indent
,
2651 fprintf_indent (f
, indent
,
2652 "tree %s = gimple_assign_rhs%u (def);\n",
2653 child_opname
, i
+ 1);
2656 fprintf_indent (f
, indent
,
2657 "tree %s = gimple_call_arg (def, %u);\n",
2659 fprintf_indent (f
, indent
,
2660 "if ((%s = do_valueize (valueize, %s)))\n",
2661 child_opname
, child_opname
);
2662 fprintf_indent (f
, indent
, " {\n");
2665 /* While the toplevel operands are canonicalized by the caller
2666 after valueizing operands of sub-expressions we have to
2667 re-canonicalize operand order. */
2668 if (operator_id
*code
= dyn_cast
<operator_id
*> (id
))
2670 /* ??? We can't canonicalize tcc_comparison operands here
2671 because that requires changing the comparison code which
2672 we already matched... */
2673 if (commutative_tree_code (code
->code
)
2674 || commutative_ternary_tree_code (code
->code
))
2676 char child_opname0
[20], child_opname1
[20];
2677 gen_opname (child_opname0
, 0);
2678 gen_opname (child_opname1
, 1);
2679 fprintf_indent (f
, indent
,
2680 "if (tree_swap_operands_p (%s, %s, false))\n",
2681 child_opname0
, child_opname1
);
2682 fprintf_indent (f
, indent
,
2683 " std::swap (%s, %s);\n",
2684 child_opname0
, child_opname1
);
2691 /* Generate GENERIC matching code for the decision tree operand. */
2694 dt_operand::gen_generic_expr (FILE *f
, int indent
, const char *opname
)
2696 expr
*e
= static_cast<expr
*> (op
);
2697 unsigned n_ops
= e
->ops
.length ();
2699 for (unsigned i
= 0; i
< n_ops
; ++i
)
2701 char child_opname
[20];
2702 gen_opname (child_opname
, i
);
2704 if (e
->operation
->kind
== id_base::CODE
)
2705 fprintf_indent (f
, indent
, "tree %s = TREE_OPERAND (%s, %u);\n",
2706 child_opname
, opname
, i
);
2708 fprintf_indent (f
, indent
, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2709 child_opname
, opname
, i
);
2715 /* Generate matching code for the children of the decision tree node. */
2718 dt_node::gen_kids (FILE *f
, int indent
, bool gimple
)
2720 auto_vec
<dt_operand
*> gimple_exprs
;
2721 auto_vec
<dt_operand
*> generic_exprs
;
2722 auto_vec
<dt_operand
*> fns
;
2723 auto_vec
<dt_operand
*> generic_fns
;
2724 auto_vec
<dt_operand
*> preds
;
2725 auto_vec
<dt_node
*> others
;
2727 for (unsigned i
= 0; i
< kids
.length (); ++i
)
2729 if (kids
[i
]->type
== dt_node::DT_OPERAND
)
2731 dt_operand
*op
= as_a
<dt_operand
*> (kids
[i
]);
2732 if (expr
*e
= dyn_cast
<expr
*> (op
->op
))
2734 if (e
->ops
.length () == 0
2735 && (!gimple
|| !(*e
->operation
== CONSTRUCTOR
)))
2736 generic_exprs
.safe_push (op
);
2737 else if (e
->operation
->kind
== id_base::FN
)
2742 generic_fns
.safe_push (op
);
2744 else if (e
->operation
->kind
== id_base::PREDICATE
)
2745 preds
.safe_push (op
);
2748 if (gimple
&& !e
->is_generic
)
2749 gimple_exprs
.safe_push (op
);
2751 generic_exprs
.safe_push (op
);
2754 else if (op
->op
->type
== operand::OP_PREDICATE
)
2755 others
.safe_push (kids
[i
]);
2759 else if (kids
[i
]->type
== dt_node::DT_SIMPLIFY
)
2760 others
.safe_push (kids
[i
]);
2761 else if (kids
[i
]->type
== dt_node::DT_MATCH
2762 || kids
[i
]->type
== dt_node::DT_TRUE
)
2764 /* A DT_TRUE operand serves as a barrier - generate code now
2765 for what we have collected sofar.
2766 Like DT_TRUE, DT_MATCH serves as a barrier as it can cause
2767 dependent matches to get out-of-order. Generate code now
2768 for what we have collected sofar. */
2769 gen_kids_1 (f
, indent
, gimple
, gimple_exprs
, generic_exprs
,
2770 fns
, generic_fns
, preds
, others
);
2771 /* And output the true operand itself. */
2772 kids
[i
]->gen (f
, indent
, gimple
);
2773 gimple_exprs
.truncate (0);
2774 generic_exprs
.truncate (0);
2776 generic_fns
.truncate (0);
2778 others
.truncate (0);
2784 /* Generate code for the remains. */
2785 gen_kids_1 (f
, indent
, gimple
, gimple_exprs
, generic_exprs
,
2786 fns
, generic_fns
, preds
, others
);
2789 /* Generate matching code for the children of the decision tree node. */
2792 dt_node::gen_kids_1 (FILE *f
, int indent
, bool gimple
,
2793 vec
<dt_operand
*> gimple_exprs
,
2794 vec
<dt_operand
*> generic_exprs
,
2795 vec
<dt_operand
*> fns
,
2796 vec
<dt_operand
*> generic_fns
,
2797 vec
<dt_operand
*> preds
,
2798 vec
<dt_node
*> others
)
2801 char *kid_opname
= buf
;
2803 unsigned exprs_len
= gimple_exprs
.length ();
2804 unsigned gexprs_len
= generic_exprs
.length ();
2805 unsigned fns_len
= fns
.length ();
2806 unsigned gfns_len
= generic_fns
.length ();
2808 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2811 gimple_exprs
[0]->get_name (kid_opname
);
2813 fns
[0]->get_name (kid_opname
);
2815 generic_fns
[0]->get_name (kid_opname
);
2817 generic_exprs
[0]->get_name (kid_opname
);
2819 fprintf_indent (f
, indent
, "switch (TREE_CODE (%s))\n", kid_opname
);
2820 fprintf_indent (f
, indent
, " {\n");
2824 if (exprs_len
|| fns_len
)
2826 fprintf_indent (f
, indent
,
2827 "case SSA_NAME:\n");
2828 fprintf_indent (f
, indent
,
2829 " if (do_valueize (valueize, %s) != NULL_TREE)\n",
2831 fprintf_indent (f
, indent
,
2833 fprintf_indent (f
, indent
,
2834 " gimple *def_stmt = SSA_NAME_DEF_STMT (%s);\n",
2840 fprintf_indent (f
, indent
,
2841 "if (gassign *def = dyn_cast <gassign *> (def_stmt))\n");
2842 fprintf_indent (f
, indent
,
2843 " switch (gimple_assign_rhs_code (def))\n");
2845 fprintf_indent (f
, indent
, "{\n");
2846 for (unsigned i
= 0; i
< exprs_len
; ++i
)
2848 expr
*e
= as_a
<expr
*> (gimple_exprs
[i
]->op
);
2849 id_base
*op
= e
->operation
;
2850 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2851 fprintf_indent (f
, indent
, "CASE_CONVERT:\n");
2853 fprintf_indent (f
, indent
, "case %s:\n", op
->id
);
2854 fprintf_indent (f
, indent
, " {\n");
2855 gimple_exprs
[i
]->gen (f
, indent
+ 4, true);
2856 fprintf_indent (f
, indent
, " break;\n");
2857 fprintf_indent (f
, indent
, " }\n");
2859 fprintf_indent (f
, indent
, "default:;\n");
2860 fprintf_indent (f
, indent
, "}\n");
2866 fprintf_indent (f
, indent
,
2867 "%sif (gcall *def = dyn_cast <gcall *>"
2869 exprs_len
? "else " : "");
2870 fprintf_indent (f
, indent
,
2871 " switch (gimple_call_combined_fn (def))\n");
2874 fprintf_indent (f
, indent
, "{\n");
2875 for (unsigned i
= 0; i
< fns_len
; ++i
)
2877 expr
*e
= as_a
<expr
*>(fns
[i
]->op
);
2878 fprintf_indent (f
, indent
, "case %s:\n", e
->operation
->id
);
2879 fprintf_indent (f
, indent
, " {\n");
2880 fns
[i
]->gen (f
, indent
+ 4, true);
2881 fprintf_indent (f
, indent
, " break;\n");
2882 fprintf_indent (f
, indent
, " }\n");
2885 fprintf_indent (f
, indent
, "default:;\n");
2886 fprintf_indent (f
, indent
, "}\n");
2891 fprintf_indent (f
, indent
, " }\n");
2892 fprintf_indent (f
, indent
, " break;\n");
2895 for (unsigned i
= 0; i
< generic_exprs
.length (); ++i
)
2897 expr
*e
= as_a
<expr
*>(generic_exprs
[i
]->op
);
2898 id_base
*op
= e
->operation
;
2899 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2900 fprintf_indent (f
, indent
, "CASE_CONVERT:\n");
2902 fprintf_indent (f
, indent
, "case %s:\n", op
->id
);
2903 fprintf_indent (f
, indent
, " {\n");
2904 generic_exprs
[i
]->gen (f
, indent
+ 4, gimple
);
2905 fprintf_indent (f
, indent
, " break;\n");
2906 fprintf_indent (f
, indent
, " }\n");
2911 fprintf_indent (f
, indent
,
2912 "case CALL_EXPR:\n");
2913 fprintf_indent (f
, indent
,
2914 " switch (get_call_combined_fn (%s))\n",
2916 fprintf_indent (f
, indent
,
2920 for (unsigned j
= 0; j
< generic_fns
.length (); ++j
)
2922 expr
*e
= as_a
<expr
*>(generic_fns
[j
]->op
);
2923 gcc_assert (e
->operation
->kind
== id_base::FN
);
2925 fprintf_indent (f
, indent
, "case %s:\n", e
->operation
->id
);
2926 fprintf_indent (f
, indent
, " {\n");
2927 generic_fns
[j
]->gen (f
, indent
+ 4, false);
2928 fprintf_indent (f
, indent
, " break;\n");
2929 fprintf_indent (f
, indent
, " }\n");
2931 fprintf_indent (f
, indent
, "default:;\n");
2934 fprintf_indent (f
, indent
, " }\n");
2935 fprintf_indent (f
, indent
, " break;\n");
2938 /* Close switch (TREE_CODE ()). */
2939 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2942 fprintf_indent (f
, indent
, " default:;\n");
2943 fprintf_indent (f
, indent
, " }\n");
2946 for (unsigned i
= 0; i
< preds
.length (); ++i
)
2948 expr
*e
= as_a
<expr
*> (preds
[i
]->op
);
2949 predicate_id
*p
= as_a
<predicate_id
*> (e
->operation
);
2950 preds
[i
]->get_name (kid_opname
);
2951 fprintf_indent (f
, indent
, "tree %s_pops[%d];\n", kid_opname
, p
->nargs
);
2952 fprintf_indent (f
, indent
, "if (%s_%s (%s, %s_pops%s))\n",
2953 gimple
? "gimple" : "tree",
2954 p
->id
, kid_opname
, kid_opname
,
2955 gimple
? ", valueize" : "");
2956 fprintf_indent (f
, indent
, " {\n");
2957 for (int j
= 0; j
< p
->nargs
; ++j
)
2959 char child_opname
[20];
2960 preds
[i
]->gen_opname (child_opname
, j
);
2961 fprintf_indent (f
, indent
+ 4, "tree %s = %s_pops[%d];\n",
2962 child_opname
, kid_opname
, j
);
2964 preds
[i
]->gen_kids (f
, indent
+ 4, gimple
);
2968 for (unsigned i
= 0; i
< others
.length (); ++i
)
2969 others
[i
]->gen (f
, indent
, gimple
);
2972 /* Generate matching code for the decision tree operand. */
2975 dt_operand::gen (FILE *f
, int indent
, bool gimple
)
2980 unsigned n_braces
= 0;
2982 if (type
== DT_OPERAND
)
2985 case operand::OP_PREDICATE
:
2986 n_braces
= gen_predicate (f
, indent
, opname
, gimple
);
2989 case operand::OP_EXPR
:
2991 n_braces
= gen_gimple_expr (f
, indent
);
2993 n_braces
= gen_generic_expr (f
, indent
, opname
);
2999 else if (type
== DT_TRUE
)
3001 else if (type
== DT_MATCH
)
3002 n_braces
= gen_match_op (f
, indent
, opname
, gimple
);
3006 indent
+= 4 * n_braces
;
3007 gen_kids (f
, indent
, gimple
);
3009 for (unsigned i
= 0; i
< n_braces
; ++i
)
3014 fprintf_indent (f
, indent
, " }\n");
3019 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3020 step of a '(simplify ...)' or '(match ...)'. This handles everything
3021 that is not part of the decision tree (simplify->match).
3022 Main recursive worker. */
3025 dt_simplify::gen_1 (FILE *f
, int indent
, bool gimple
, operand
*result
)
3029 if (with_expr
*w
= dyn_cast
<with_expr
*> (result
))
3031 fprintf_indent (f
, indent
, "{\n");
3033 output_line_directive (f
, w
->location
);
3034 w
->with
->gen_transform (f
, indent
, NULL
, true, 1, "type", NULL
);
3035 gen_1 (f
, indent
, gimple
, w
->subexpr
);
3037 fprintf_indent (f
, indent
, "}\n");
3040 else if (if_expr
*ife
= dyn_cast
<if_expr
*> (result
))
3042 output_line_directive (f
, ife
->location
);
3043 fprintf_indent (f
, indent
, "if (");
3044 ife
->cond
->gen_transform (f
, indent
, NULL
, true, 1, "type", NULL
);
3046 fprintf_indent (f
, indent
+ 2, "{\n");
3048 gen_1 (f
, indent
, gimple
, ife
->trueexpr
);
3050 fprintf_indent (f
, indent
+ 2, "}\n");
3053 fprintf_indent (f
, indent
, "else\n");
3054 fprintf_indent (f
, indent
+ 2, "{\n");
3056 gen_1 (f
, indent
, gimple
, ife
->falseexpr
);
3058 fprintf_indent (f
, indent
+ 2, "}\n");
3064 /* Analyze captures and perform early-outs on the incoming arguments
3065 that cover cases we cannot handle. */
3066 capture_info
cinfo (s
, result
, gimple
);
3067 if (s
->kind
== simplify::SIMPLIFY
)
3071 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
3072 if (cinfo
.force_no_side_effects
& (1 << i
))
3074 fprintf_indent (f
, indent
,
3075 "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n",
3078 warning_at (as_a
<expr
*> (s
->match
)->ops
[i
]->location
,
3079 "forcing toplevel operand to have no "
3082 for (int i
= 0; i
<= s
->capture_max
; ++i
)
3083 if (cinfo
.info
[i
].cse_p
)
3085 else if (cinfo
.info
[i
].force_no_side_effects_p
3086 && (cinfo
.info
[i
].toplevel_msk
3087 & cinfo
.force_no_side_effects
) == 0)
3089 fprintf_indent (f
, indent
,
3090 "if (TREE_SIDE_EFFECTS (captures[%d])) "
3091 "return NULL_TREE;\n", i
);
3093 warning_at (cinfo
.info
[i
].c
->location
,
3094 "forcing captured operand to have no "
3097 else if ((cinfo
.info
[i
].toplevel_msk
3098 & cinfo
.force_no_side_effects
) != 0)
3099 /* Mark capture as having no side-effects if we had to verify
3100 that via forced toplevel operand checks. */
3101 cinfo
.info
[i
].force_no_side_effects_p
= true;
3105 /* Force single-use restriction by only allowing simple
3106 results via setting seq to NULL. */
3107 fprintf_indent (f
, indent
, "gimple_seq *lseq = seq;\n");
3108 bool first_p
= true;
3109 for (int i
= 0; i
<= s
->capture_max
; ++i
)
3110 if (cinfo
.info
[i
].force_single_use
)
3114 fprintf_indent (f
, indent
, "if (lseq\n");
3115 fprintf_indent (f
, indent
, " && (");
3121 fprintf_indent (f
, indent
, " || ");
3123 fprintf (f
, "!single_use (captures[%d])", i
);
3127 fprintf (f
, "))\n");
3128 fprintf_indent (f
, indent
, " lseq = NULL;\n");
3133 fprintf_indent (f
, indent
, "if (dump_file && (dump_flags & TDF_DETAILS)) "
3134 "fprintf (dump_file, \"Applying pattern ");
3135 output_line_directive (f
,
3136 result
? result
->location
: s
->match
->location
, true);
3137 fprintf (f
, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
3141 /* If there is no result then this is a predicate implementation. */
3142 fprintf_indent (f
, indent
, "return true;\n");
3146 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
3147 in outermost position). */
3148 if (result
->type
== operand::OP_EXPR
3149 && *as_a
<expr
*> (result
)->operation
== NON_LVALUE_EXPR
)
3150 result
= as_a
<expr
*> (result
)->ops
[0];
3151 if (result
->type
== operand::OP_EXPR
)
3153 expr
*e
= as_a
<expr
*> (result
);
3154 id_base
*opr
= e
->operation
;
3155 bool is_predicate
= false;
3156 /* When we delay operator substituting during lowering of fors we
3157 make sure that for code-gen purposes the effects of each substitute
3158 are the same. Thus just look at that. */
3159 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
3160 opr
= uid
->substitutes
[0];
3161 else if (is_a
<predicate_id
*> (opr
))
3162 is_predicate
= true;
3164 fprintf_indent (f
, indent
, "*res_code = %s;\n",
3165 *e
->operation
== CONVERT_EXPR
3166 ? "NOP_EXPR" : e
->operation
->id
);
3167 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
3170 snprintf (dest
, 32, "res_ops[%d]", j
);
3172 = get_operand_type (opr
, j
,
3173 "type", e
->expr_type
,
3174 j
== 0 ? NULL
: "TREE_TYPE (res_ops[0])");
3175 /* We need to expand GENERIC conditions we captured from
3176 COND_EXPRs and we need to unshare them when substituting
3178 int cond_handling
= 0;
3180 cond_handling
= ((*opr
== COND_EXPR
3181 || *opr
== VEC_COND_EXPR
) && j
== 0) ? 1 : 2;
3182 e
->ops
[j
]->gen_transform (f
, indent
, dest
, true, 1, optype
,
3183 &cinfo
, indexes
, cond_handling
);
3186 /* Re-fold the toplevel result. It's basically an embedded
3187 gimple_build w/o actually building the stmt. */
3189 fprintf_indent (f
, indent
,
3190 "gimple_resimplify%d (lseq, res_code, type, "
3191 "res_ops, valueize);\n", e
->ops
.length ());
3193 else if (result
->type
== operand::OP_CAPTURE
3194 || result
->type
== operand::OP_C_EXPR
)
3196 result
->gen_transform (f
, indent
, "res_ops[0]", true, 1, "type",
3198 fprintf_indent (f
, indent
, "*res_code = TREE_CODE (res_ops[0]);\n");
3199 if (is_a
<capture
*> (result
)
3200 && cinfo
.info
[as_a
<capture
*> (result
)->where
].cond_expr_cond_p
)
3202 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
3203 with substituting a capture of that. */
3204 fprintf_indent (f
, indent
,
3205 "if (COMPARISON_CLASS_P (res_ops[0]))\n");
3206 fprintf_indent (f
, indent
,
3208 fprintf_indent (f
, indent
,
3209 " tree tem = res_ops[0];\n");
3210 fprintf_indent (f
, indent
,
3211 " res_ops[0] = TREE_OPERAND (tem, 0);\n");
3212 fprintf_indent (f
, indent
,
3213 " res_ops[1] = TREE_OPERAND (tem, 1);\n");
3214 fprintf_indent (f
, indent
,
3220 fprintf_indent (f
, indent
, "return true;\n");
3224 bool is_predicate
= false;
3225 if (result
->type
== operand::OP_EXPR
)
3227 expr
*e
= as_a
<expr
*> (result
);
3228 id_base
*opr
= e
->operation
;
3229 /* When we delay operator substituting during lowering of fors we
3230 make sure that for code-gen purposes the effects of each substitute
3231 are the same. Thus just look at that. */
3232 if (user_id
*uid
= dyn_cast
<user_id
*> (opr
))
3233 opr
= uid
->substitutes
[0];
3234 else if (is_a
<predicate_id
*> (opr
))
3235 is_predicate
= true;
3236 /* Search for captures used multiple times in the result expression
3237 and wrap them in a SAVE_EXPR. Allow as many uses as in the
3238 original expression. */
3240 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
3242 if (cinfo
.info
[i
].same_as
!= (unsigned)i
3243 || cinfo
.info
[i
].cse_p
)
3245 if (cinfo
.info
[i
].result_use_count
3246 > cinfo
.info
[i
].match_use_count
)
3247 fprintf_indent (f
, indent
,
3248 "if (! tree_invariant_p (captures[%d])) "
3249 "return NULL_TREE;\n", i
);
3251 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
3255 snprintf (dest
, 32, "res_ops[%d]", j
);
3258 fprintf_indent (f
, indent
, "tree res_op%d;\n", j
);
3259 snprintf (dest
, 32, "res_op%d", j
);
3262 = get_operand_type (opr
, j
,
3263 "type", e
->expr_type
,
3265 ? NULL
: "TREE_TYPE (res_op0)");
3266 e
->ops
[j
]->gen_transform (f
, indent
, dest
, false, 1, optype
,
3270 fprintf_indent (f
, indent
, "return true;\n");
3273 fprintf_indent (f
, indent
, "tree res;\n");
3274 /* Re-fold the toplevel result. Use non_lvalue to
3275 build NON_LVALUE_EXPRs so they get properly
3276 ignored when in GIMPLE form. */
3277 if (*opr
== NON_LVALUE_EXPR
)
3278 fprintf_indent (f
, indent
,
3279 "res = non_lvalue_loc (loc, res_op0);\n");
3282 if (is_a
<operator_id
*> (opr
))
3283 fprintf_indent (f
, indent
,
3284 "res = fold_build%d_loc (loc, %s, type",
3286 *e
->operation
== CONVERT_EXPR
3287 ? "NOP_EXPR" : e
->operation
->id
);
3289 fprintf_indent (f
, indent
,
3290 "res = maybe_build_call_expr_loc (loc, "
3291 "%s, type, %d", e
->operation
->id
,
3293 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
3294 fprintf (f
, ", res_op%d", j
);
3295 fprintf (f
, ");\n");
3296 if (!is_a
<operator_id
*> (opr
))
3298 fprintf_indent (f
, indent
, "if (!res)\n");
3299 fprintf_indent (f
, indent
, " return NULL_TREE;\n");
3304 else if (result
->type
== operand::OP_CAPTURE
3305 || result
->type
== operand::OP_C_EXPR
)
3308 fprintf_indent (f
, indent
, "tree res;\n");
3309 result
->gen_transform (f
, indent
, "res", false, 1, "type",
3316 /* Search for captures not used in the result expression and dependent
3317 on TREE_SIDE_EFFECTS emit omit_one_operand. */
3318 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
3320 if (cinfo
.info
[i
].same_as
!= (unsigned)i
)
3322 if (!cinfo
.info
[i
].force_no_side_effects_p
3323 && !cinfo
.info
[i
].expr_p
3324 && cinfo
.info
[i
].result_use_count
== 0)
3326 fprintf_indent (f
, indent
,
3327 "if (TREE_SIDE_EFFECTS (captures[%d]))\n",
3329 fprintf_indent (f
, indent
+ 2,
3330 "res = build2_loc (loc, COMPOUND_EXPR, type, "
3331 "fold_ignored_result (captures[%d]), res);\n",
3335 fprintf_indent (f
, indent
, "return res;\n");
3340 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3341 step of a '(simplify ...)' or '(match ...)'. This handles everything
3342 that is not part of the decision tree (simplify->match). */
3345 dt_simplify::gen (FILE *f
, int indent
, bool gimple
)
3347 fprintf_indent (f
, indent
, "{\n");
3349 output_line_directive (f
,
3350 s
->result
? s
->result
->location
: s
->match
->location
);
3351 if (s
->capture_max
>= 0)
3354 fprintf_indent (f
, indent
, "tree captures[%u] ATTRIBUTE_UNUSED = { %s",
3355 s
->capture_max
+ 1, indexes
[0]->get_name (opname
));
3357 for (int i
= 1; i
<= s
->capture_max
; ++i
)
3361 fprintf (f
, ", %s", indexes
[i
]->get_name (opname
));
3363 fprintf (f
, " };\n");
3366 /* If we have a split-out function for the actual transform, call it. */
3367 if (info
&& info
->fname
)
3371 fprintf_indent (f
, indent
, "if (%s (res_code, res_ops, seq, "
3372 "valueize, type, captures", info
->fname
);
3373 for (unsigned i
= 0; i
< s
->for_subst_vec
.length (); ++i
)
3374 if (s
->for_subst_vec
[i
].first
->used
)
3375 fprintf (f
, ", %s", s
->for_subst_vec
[i
].second
->id
);
3376 fprintf (f
, "))\n");
3377 fprintf_indent (f
, indent
, " return true;\n");
3381 fprintf_indent (f
, indent
, "tree res = %s (loc, type",
3383 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
3384 fprintf (f
, ", op%d", i
);
3385 fprintf (f
, ", captures");
3386 for (unsigned i
= 0; i
< s
->for_subst_vec
.length (); ++i
)
3388 if (s
->for_subst_vec
[i
].first
->used
)
3389 fprintf (f
, ", %s", s
->for_subst_vec
[i
].second
->id
);
3391 fprintf (f
, ");\n");
3392 fprintf_indent (f
, indent
, "if (res) return res;\n");
3397 for (unsigned i
= 0; i
< s
->for_subst_vec
.length (); ++i
)
3399 if (! s
->for_subst_vec
[i
].first
->used
)
3401 if (is_a
<operator_id
*> (s
->for_subst_vec
[i
].second
))
3402 fprintf_indent (f
, indent
, "enum tree_code %s = %s;\n",
3403 s
->for_subst_vec
[i
].first
->id
,
3404 s
->for_subst_vec
[i
].second
->id
);
3405 else if (is_a
<fn_id
*> (s
->for_subst_vec
[i
].second
))
3406 fprintf_indent (f
, indent
, "combined_fn %s = %s;\n",
3407 s
->for_subst_vec
[i
].first
->id
,
3408 s
->for_subst_vec
[i
].second
->id
);
3412 gen_1 (f
, indent
, gimple
, s
->result
);
3416 fprintf_indent (f
, indent
, "}\n");
3420 /* Hash function for finding equivalent transforms. */
3423 sinfo_hashmap_traits::hash (const key_type
&v
)
3425 /* Only bother to compare those originating from the same source pattern. */
3426 return v
->s
->result
->location
;
3429 /* Compare function for finding equivalent transforms. */
3432 compare_op (operand
*o1
, simplify
*s1
, operand
*o2
, simplify
*s2
)
3434 if (o1
->type
!= o2
->type
)
3439 case operand::OP_IF
:
3441 if_expr
*if1
= as_a
<if_expr
*> (o1
);
3442 if_expr
*if2
= as_a
<if_expr
*> (o2
);
3443 /* ??? Properly compare c-exprs. */
3444 if (if1
->cond
!= if2
->cond
)
3446 if (!compare_op (if1
->trueexpr
, s1
, if2
->trueexpr
, s2
))
3448 if (if1
->falseexpr
!= if2
->falseexpr
3450 && !compare_op (if1
->falseexpr
, s1
, if2
->falseexpr
, s2
)))
3454 case operand::OP_WITH
:
3456 with_expr
*with1
= as_a
<with_expr
*> (o1
);
3457 with_expr
*with2
= as_a
<with_expr
*> (o2
);
3458 if (with1
->with
!= with2
->with
)
3460 return compare_op (with1
->subexpr
, s1
, with2
->subexpr
, s2
);
3465 /* We've hit a result. Time to compare capture-infos - this is required
3466 in addition to the conservative pointer-equivalency of the result IL. */
3467 capture_info
cinfo1 (s1
, o1
, true);
3468 capture_info
cinfo2 (s2
, o2
, true);
3470 if (cinfo1
.force_no_side_effects
!= cinfo2
.force_no_side_effects
3471 || cinfo1
.info
.length () != cinfo2
.info
.length ())
3474 for (unsigned i
= 0; i
< cinfo1
.info
.length (); ++i
)
3476 if (cinfo1
.info
[i
].expr_p
!= cinfo2
.info
[i
].expr_p
3477 || cinfo1
.info
[i
].cse_p
!= cinfo2
.info
[i
].cse_p
3478 || (cinfo1
.info
[i
].force_no_side_effects_p
3479 != cinfo2
.info
[i
].force_no_side_effects_p
)
3480 || cinfo1
.info
[i
].force_single_use
!= cinfo2
.info
[i
].force_single_use
3481 || cinfo1
.info
[i
].cond_expr_cond_p
!= cinfo2
.info
[i
].cond_expr_cond_p
3482 /* toplevel_msk is an optimization */
3483 || cinfo1
.info
[i
].result_use_count
!= cinfo2
.info
[i
].result_use_count
3484 || cinfo1
.info
[i
].same_as
!= cinfo2
.info
[i
].same_as
3485 /* the pointer back to the capture is for diagnostics only */)
3489 /* ??? Deep-compare the actual result. */
3494 sinfo_hashmap_traits::equal_keys (const key_type
&v
,
3495 const key_type
&candidate
)
3497 return compare_op (v
->s
->result
, v
->s
, candidate
->s
->result
, candidate
->s
);
3501 /* Main entry to generate code for matching GIMPLE IL off the decision
3505 decision_tree::gen (FILE *f
, bool gimple
)
3511 fprintf (stderr
, "%s decision tree has %u leafs, maximum depth %u and "
3512 "a total number of %u nodes\n",
3513 gimple
? "GIMPLE" : "GENERIC",
3514 root
->num_leafs
, root
->max_level
, root
->total_size
);
3516 /* First split out the transform part of equal leafs. */
3519 for (sinfo_map_t::iterator iter
= si
.begin ();
3520 iter
!= si
.end (); ++iter
)
3522 sinfo
*s
= (*iter
).second
;
3523 /* Do not split out single uses. */
3530 fprintf (stderr
, "found %u uses of", s
->cnt
);
3531 output_line_directive (stderr
, s
->s
->s
->result
->location
);
3534 /* Generate a split out function with the leaf transform code. */
3535 s
->fname
= xasprintf ("%s_simplify_%u", gimple
? "gimple" : "generic",
3538 fprintf (f
, "\nstatic bool\n"
3539 "%s (code_helper *res_code, tree *res_ops,\n"
3540 " gimple_seq *seq, tree (*valueize)(tree) "
3541 "ATTRIBUTE_UNUSED,\n"
3542 " tree ARG_UNUSED (type), tree *ARG_UNUSED "
3547 fprintf (f
, "\nstatic tree\n"
3548 "%s (location_t ARG_UNUSED (loc), tree ARG_UNUSED (type),\n",
3549 (*iter
).second
->fname
);
3550 for (unsigned i
= 0;
3551 i
< as_a
<expr
*>(s
->s
->s
->match
)->ops
.length (); ++i
)
3552 fprintf (f
, " tree ARG_UNUSED (op%d),", i
);
3553 fprintf (f
, " tree *captures\n");
3555 for (unsigned i
= 0; i
< s
->s
->s
->for_subst_vec
.length (); ++i
)
3557 if (! s
->s
->s
->for_subst_vec
[i
].first
->used
)
3559 if (is_a
<operator_id
*> (s
->s
->s
->for_subst_vec
[i
].second
))
3560 fprintf (f
, ", enum tree_code ARG_UNUSED (%s)",
3561 s
->s
->s
->for_subst_vec
[i
].first
->id
);
3562 else if (is_a
<fn_id
*> (s
->s
->s
->for_subst_vec
[i
].second
))
3563 fprintf (f
, ", combined_fn ARG_UNUSED (%s)",
3564 s
->s
->s
->for_subst_vec
[i
].first
->id
);
3567 fprintf (f
, ")\n{\n");
3568 s
->s
->gen_1 (f
, 2, gimple
, s
->s
->s
->result
);
3570 fprintf (f
, " return false;\n");
3572 fprintf (f
, " return NULL_TREE;\n");
3575 fprintf (stderr
, "removed %u duplicate tails\n", rcnt
);
3577 for (unsigned n
= 1; n
<= 3; ++n
)
3579 /* First generate split-out functions. */
3580 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
3582 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
3583 expr
*e
= static_cast<expr
*>(dop
->op
);
3584 if (e
->ops
.length () != n
3585 /* Builtin simplifications are somewhat premature on
3586 GENERIC. The following drops patterns with outermost
3587 calls. It's easy to emit overloads for function code
3588 though if necessary. */
3590 && e
->operation
->kind
!= id_base::CODE
))
3594 fprintf (f
, "\nstatic bool\n"
3595 "gimple_simplify_%s (code_helper *res_code, tree *res_ops,\n"
3596 " gimple_seq *seq, tree (*valueize)(tree) "
3597 "ATTRIBUTE_UNUSED,\n"
3598 " code_helper ARG_UNUSED (code), tree "
3599 "ARG_UNUSED (type)\n",
3602 fprintf (f
, "\nstatic tree\n"
3603 "generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
3604 "tree_code ARG_UNUSED (code), tree ARG_UNUSED (type)",
3606 for (unsigned i
= 0; i
< n
; ++i
)
3607 fprintf (f
, ", tree op%d", i
);
3610 dop
->gen_kids (f
, 2, gimple
);
3612 fprintf (f
, " return false;\n");
3614 fprintf (f
, " return NULL_TREE;\n");
3618 /* Then generate the main entry with the outermost switch and
3619 tail-calls to the split-out functions. */
3621 fprintf (f
, "\nstatic bool\n"
3622 "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
3623 " gimple_seq *seq, tree (*valueize)(tree),\n"
3624 " code_helper code, tree type");
3626 fprintf (f
, "\ntree\n"
3627 "generic_simplify (location_t loc, enum tree_code code, "
3628 "tree type ATTRIBUTE_UNUSED");
3629 for (unsigned i
= 0; i
< n
; ++i
)
3630 fprintf (f
, ", tree op%d", i
);
3635 fprintf (f
, " switch (code.get_rep())\n"
3638 fprintf (f
, " switch (code)\n"
3640 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
3642 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
3643 expr
*e
= static_cast<expr
*>(dop
->op
);
3644 if (e
->ops
.length () != n
3645 /* Builtin simplifications are somewhat premature on
3646 GENERIC. The following drops patterns with outermost
3647 calls. It's easy to emit overloads for function code
3648 though if necessary. */
3650 && e
->operation
->kind
!= id_base::CODE
))
3653 if (*e
->operation
== CONVERT_EXPR
3654 || *e
->operation
== NOP_EXPR
)
3655 fprintf (f
, " CASE_CONVERT:\n");
3657 fprintf (f
, " case %s%s:\n",
3658 is_a
<fn_id
*> (e
->operation
) ? "-" : "",
3661 fprintf (f
, " return gimple_simplify_%s (res_code, res_ops, "
3662 "seq, valueize, code, type", e
->operation
->id
);
3664 fprintf (f
, " return generic_simplify_%s (loc, code, type",
3666 for (unsigned i
= 0; i
< n
; ++i
)
3667 fprintf (f
, ", op%d", i
);
3668 fprintf (f
, ");\n");
3670 fprintf (f
, " default:;\n"
3674 fprintf (f
, " return false;\n");
3676 fprintf (f
, " return NULL_TREE;\n");
3681 /* Output code to implement the predicate P from the decision tree DT. */
3684 write_predicate (FILE *f
, predicate_id
*p
, decision_tree
&dt
, bool gimple
)
3686 fprintf (f
, "\nbool\n"
3687 "%s%s (tree t%s%s)\n"
3688 "{\n", gimple
? "gimple_" : "tree_", p
->id
,
3689 p
->nargs
> 0 ? ", tree *res_ops" : "",
3690 gimple
? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
3691 /* Conveniently make 'type' available. */
3692 fprintf_indent (f
, 2, "tree type = TREE_TYPE (t);\n");
3695 fprintf_indent (f
, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
3696 dt
.root
->gen_kids (f
, 2, gimple
);
3698 fprintf_indent (f
, 2, "return false;\n"
3702 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
3705 write_header (FILE *f
, const char *head
)
3707 fprintf (f
, "/* Generated automatically by the program `genmatch' from\n");
3708 fprintf (f
, " a IL pattern matching and simplification description. */\n");
3710 /* Include the header instead of writing it awkwardly quoted here. */
3711 fprintf (f
, "\n#include \"%s\"\n", head
);
3721 parser (cpp_reader
*);
3724 const cpp_token
*next ();
3725 const cpp_token
*peek (unsigned = 1);
3726 const cpp_token
*peek_ident (const char * = NULL
, unsigned = 1);
3727 const cpp_token
*expect (enum cpp_ttype
);
3728 const cpp_token
*eat_token (enum cpp_ttype
);
3729 const char *get_string ();
3730 const char *get_ident ();
3731 const cpp_token
*eat_ident (const char *);
3732 const char *get_number ();
3734 id_base
*parse_operation ();
3735 operand
*parse_capture (operand
*, bool);
3736 operand
*parse_expr ();
3737 c_expr
*parse_c_expr (cpp_ttype
);
3738 operand
*parse_op ();
3740 void record_operlist (source_location
, user_id
*);
3742 void parse_pattern ();
3743 operand
*parse_result (operand
*, predicate_id
*);
3744 void push_simplify (simplify::simplify_kind
,
3745 vec
<simplify
*>&, operand
*, operand
*);
3746 void parse_simplify (simplify::simplify_kind
,
3747 vec
<simplify
*>&, predicate_id
*, operand
*);
3748 void parse_for (source_location
);
3749 void parse_if (source_location
);
3750 void parse_predicates (source_location
);
3751 void parse_operator_list (source_location
);
3754 vec
<c_expr
*> active_ifs
;
3755 vec
<vec
<user_id
*> > active_fors
;
3756 hash_set
<user_id
*> *oper_lists_set
;
3757 vec
<user_id
*> oper_lists
;
3759 cid_map_t
*capture_ids
;
3762 vec
<simplify
*> simplifiers
;
3763 vec
<predicate_id
*> user_predicates
;
3764 bool parsing_match_operand
;
3767 /* Lexing helpers. */
3769 /* Read the next non-whitespace token from R. */
3774 const cpp_token
*token
;
3777 token
= cpp_get_token (r
);
3779 while (token
->type
== CPP_PADDING
3780 && token
->type
!= CPP_EOF
);
3784 /* Peek at the next non-whitespace token from R. */
3787 parser::peek (unsigned num
)
3789 const cpp_token
*token
;
3793 token
= cpp_peek_token (r
, i
++);
3795 while ((token
->type
== CPP_PADDING
3796 && token
->type
!= CPP_EOF
)
3798 /* If we peek at EOF this is a fatal error as it leaves the
3799 cpp_reader in unusable state. Assume we really wanted a
3800 token and thus this EOF is unexpected. */
3801 if (token
->type
== CPP_EOF
)
3802 fatal_at (token
, "unexpected end of file");
3806 /* Peek at the next identifier token (or return NULL if the next
3807 token is not an identifier or equal to ID if supplied). */
3810 parser::peek_ident (const char *id
, unsigned num
)
3812 const cpp_token
*token
= peek (num
);
3813 if (token
->type
!= CPP_NAME
)
3819 const char *t
= (const char *) CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
3820 if (strcmp (id
, t
) == 0)
3826 /* Read the next token from R and assert it is of type TK. */
3829 parser::expect (enum cpp_ttype tk
)
3831 const cpp_token
*token
= next ();
3832 if (token
->type
!= tk
)
3833 fatal_at (token
, "expected %s, got %s",
3834 cpp_type2name (tk
, 0), cpp_type2name (token
->type
, 0));
3839 /* Consume the next token from R and assert it is of type TK. */
3842 parser::eat_token (enum cpp_ttype tk
)
3847 /* Read the next token from R and assert it is of type CPP_STRING and
3848 return its value. */
3851 parser::get_string ()
3853 const cpp_token
*token
= expect (CPP_STRING
);
3854 return (const char *)token
->val
.str
.text
;
3857 /* Read the next token from R and assert it is of type CPP_NAME and
3858 return its value. */
3861 parser::get_ident ()
3863 const cpp_token
*token
= expect (CPP_NAME
);
3864 return (const char *)CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
3867 /* Eat an identifier token with value S from R. */
3870 parser::eat_ident (const char *s
)
3872 const cpp_token
*token
= peek ();
3873 const char *t
= get_ident ();
3874 if (strcmp (s
, t
) != 0)
3875 fatal_at (token
, "expected '%s' got '%s'\n", s
, t
);
3879 /* Read the next token from R and assert it is of type CPP_NUMBER and
3880 return its value. */
3883 parser::get_number ()
3885 const cpp_token
*token
= expect (CPP_NUMBER
);
3886 return (const char *)token
->val
.str
.text
;
3890 /* Record an operator-list use for transparent for handling. */
3893 parser::record_operlist (source_location loc
, user_id
*p
)
3895 if (!oper_lists_set
->add (p
))
3897 if (!oper_lists
.is_empty ()
3898 && oper_lists
[0]->substitutes
.length () != p
->substitutes
.length ())
3899 fatal_at (loc
, "User-defined operator list does not have the "
3900 "same number of entries as others used in the pattern");
3901 oper_lists
.safe_push (p
);
3905 /* Parse the operator ID, special-casing convert?, convert1? and
3909 parser::parse_operation ()
3911 const cpp_token
*id_tok
= peek ();
3912 const char *id
= get_ident ();
3913 const cpp_token
*token
= peek ();
3914 if (strcmp (id
, "convert0") == 0)
3915 fatal_at (id_tok
, "use 'convert?' here");
3916 else if (strcmp (id
, "view_convert0") == 0)
3917 fatal_at (id_tok
, "use 'view_convert?' here");
3918 if (token
->type
== CPP_QUERY
3919 && !(token
->flags
& PREV_WHITE
))
3921 if (strcmp (id
, "convert") == 0)
3923 else if (strcmp (id
, "convert1") == 0)
3925 else if (strcmp (id
, "convert2") == 0)
3927 else if (strcmp (id
, "view_convert") == 0)
3928 id
= "view_convert0";
3929 else if (strcmp (id
, "view_convert1") == 0)
3931 else if (strcmp (id
, "view_convert2") == 0)
3934 fatal_at (id_tok
, "non-convert operator conditionalized");
3936 if (!parsing_match_operand
)
3937 fatal_at (id_tok
, "conditional convert can only be used in "
3938 "match expression");
3939 eat_token (CPP_QUERY
);
3941 else if (strcmp (id
, "convert1") == 0
3942 || strcmp (id
, "convert2") == 0
3943 || strcmp (id
, "view_convert1") == 0
3944 || strcmp (id
, "view_convert2") == 0)
3945 fatal_at (id_tok
, "expected '?' after conditional operator");
3946 id_base
*op
= get_operator (id
);
3948 fatal_at (id_tok
, "unknown operator %s", id
);
3950 user_id
*p
= dyn_cast
<user_id
*> (op
);
3951 if (p
&& p
->is_oper_list
)
3953 if (active_fors
.length() == 0)
3954 record_operlist (id_tok
->src_loc
, p
);
3956 fatal_at (id_tok
, "operator-list %s cannot be exapnded inside 'for'", id
);
3962 capture = '@'<number> */
3965 parser::parse_capture (operand
*op
, bool require_existing
)
3967 source_location src_loc
= eat_token (CPP_ATSIGN
)->src_loc
;
3968 const cpp_token
*token
= peek ();
3969 const char *id
= NULL
;
3970 if (token
->type
== CPP_NUMBER
)
3972 else if (token
->type
== CPP_NAME
)
3975 fatal_at (token
, "expected number or identifier");
3976 unsigned next_id
= capture_ids
->elements ();
3978 unsigned &num
= capture_ids
->get_or_insert (id
, &existed
);
3981 if (require_existing
)
3982 fatal_at (src_loc
, "unknown capture id");
3985 return new capture (src_loc
, num
, op
);
3988 /* Parse an expression
3989 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
3992 parser::parse_expr ()
3994 const cpp_token
*token
= peek ();
3995 expr
*e
= new expr (parse_operation (), token
->src_loc
);
3998 bool is_commutative
= false;
3999 bool force_capture
= false;
4000 const char *expr_type
= NULL
;
4002 if (token
->type
== CPP_COLON
4003 && !(token
->flags
& PREV_WHITE
))
4005 eat_token (CPP_COLON
);
4007 if (token
->type
== CPP_NAME
4008 && !(token
->flags
& PREV_WHITE
))
4010 const char *s
= get_ident ();
4011 if (!parsing_match_operand
)
4021 = dyn_cast
<operator_id
*> (e
->operation
))
4023 if (!commutative_tree_code (p
->code
)
4024 && !comparison_code_p (p
->code
))
4025 fatal_at (token
, "operation is not commutative");
4027 else if (user_id
*p
= dyn_cast
<user_id
*> (e
->operation
))
4028 for (unsigned i
= 0;
4029 i
< p
->substitutes
.length (); ++i
)
4032 = dyn_cast
<operator_id
*> (p
->substitutes
[i
]))
4034 if (!commutative_tree_code (q
->code
)
4035 && !comparison_code_p (q
->code
))
4036 fatal_at (token
, "operation %s is not "
4037 "commutative", q
->id
);
4040 is_commutative
= true;
4042 else if (*sp
== 'C')
4043 is_commutative
= true;
4044 else if (*sp
== 's')
4046 e
->force_single_use
= true;
4047 force_capture
= true;
4050 fatal_at (token
, "flag %c not recognized", *sp
);
4057 fatal_at (token
, "expected flag or type specifying identifier");
4060 if (token
->type
== CPP_ATSIGN
4061 && !(token
->flags
& PREV_WHITE
))
4062 op
= parse_capture (e
, false);
4063 else if (force_capture
)
4065 unsigned num
= capture_ids
->elements ();
4066 /* Big enough for a 32-bit UINT_MAX plus prefix. */
4069 sprintf (id
, "__%u", num
);
4070 capture_ids
->get_or_insert (xstrdup (id
), &existed
);
4072 fatal_at (token
, "reserved capture id '%s' already used", id
);
4073 op
= new capture (token
->src_loc
, num
, e
);
4079 const cpp_token
*token
= peek ();
4080 if (token
->type
== CPP_CLOSE_PAREN
)
4082 if (e
->operation
->nargs
!= -1
4083 && e
->operation
->nargs
!= (int) e
->ops
.length ())
4084 fatal_at (token
, "'%s' expects %u operands, not %u",
4085 e
->operation
->id
, e
->operation
->nargs
, e
->ops
.length ());
4088 if (e
->ops
.length () == 2)
4089 e
->is_commutative
= true;
4091 fatal_at (token
, "only binary operators or function with "
4092 "two arguments can be marked commutative");
4094 e
->expr_type
= expr_type
;
4097 else if (!(token
->flags
& PREV_WHITE
))
4098 fatal_at (token
, "expected expression operand");
4100 e
->append_op (parse_op ());
4105 /* Lex native C code delimited by START recording the preprocessing tokens
4106 for later processing.
4107 c_expr = ('{'|'(') <pp token>... ('}'|')') */
4110 parser::parse_c_expr (cpp_ttype start
)
4112 const cpp_token
*token
;
4115 vec
<cpp_token
> code
= vNULL
;
4116 unsigned nr_stmts
= 0;
4117 source_location loc
= eat_token (start
)->src_loc
;
4118 if (start
== CPP_OPEN_PAREN
)
4119 end
= CPP_CLOSE_PAREN
;
4120 else if (start
== CPP_OPEN_BRACE
)
4121 end
= CPP_CLOSE_BRACE
;
4129 /* Count brace pairs to find the end of the expr to match. */
4130 if (token
->type
== start
)
4132 else if (token
->type
== end
4135 else if (token
->type
== CPP_EOF
)
4136 fatal_at (token
, "unexpected end of file");
4138 /* This is a lame way of counting the number of statements. */
4139 if (token
->type
== CPP_SEMICOLON
)
4142 /* If this is possibly a user-defined identifier mark it used. */
4143 if (token
->type
== CPP_NAME
)
4145 id_base
*idb
= get_operator ((const char *)CPP_HASHNODE
4146 (token
->val
.node
.node
)->ident
.str
);
4148 if (idb
&& (p
= dyn_cast
<user_id
*> (idb
)) && p
->is_oper_list
)
4149 record_operlist (token
->src_loc
, p
);
4152 /* Record the token. */
4153 code
.safe_push (*token
);
4156 return new c_expr (r
, loc
, code
, nr_stmts
, vNULL
, capture_ids
);
4159 /* Parse an operand which is either an expression, a predicate or
4160 a standalone capture.
4161 op = predicate | expr | c_expr | capture */
4166 const cpp_token
*token
= peek ();
4167 struct operand
*op
= NULL
;
4168 if (token
->type
== CPP_OPEN_PAREN
)
4170 eat_token (CPP_OPEN_PAREN
);
4172 eat_token (CPP_CLOSE_PAREN
);
4174 else if (token
->type
== CPP_OPEN_BRACE
)
4176 op
= parse_c_expr (CPP_OPEN_BRACE
);
4180 /* Remaining ops are either empty or predicates */
4181 if (token
->type
== CPP_NAME
)
4183 const char *id
= get_ident ();
4184 id_base
*opr
= get_operator (id
);
4186 fatal_at (token
, "expected predicate name");
4187 if (operator_id
*code
= dyn_cast
<operator_id
*> (opr
))
4189 if (code
->nargs
!= 0)
4190 fatal_at (token
, "using an operator with operands as predicate");
4191 /* Parse the zero-operand operator "predicates" as
4193 op
= new expr (opr
, token
->src_loc
);
4195 else if (user_id
*code
= dyn_cast
<user_id
*> (opr
))
4197 if (code
->nargs
!= 0)
4198 fatal_at (token
, "using an operator with operands as predicate");
4199 /* Parse the zero-operand operator "predicates" as
4201 op
= new expr (opr
, token
->src_loc
);
4203 else if (predicate_id
*p
= dyn_cast
<predicate_id
*> (opr
))
4204 op
= new predicate (p
, token
->src_loc
);
4206 fatal_at (token
, "using an unsupported operator as predicate");
4207 if (!parsing_match_operand
)
4208 fatal_at (token
, "predicates are only allowed in match expression");
4210 if (token
->flags
& PREV_WHITE
)
4213 else if (token
->type
!= CPP_COLON
4214 && token
->type
!= CPP_ATSIGN
)
4215 fatal_at (token
, "expected expression or predicate");
4216 /* optionally followed by a capture and a predicate. */
4217 if (token
->type
== CPP_COLON
)
4218 fatal_at (token
, "not implemented: predicate on leaf operand");
4219 if (token
->type
== CPP_ATSIGN
)
4220 op
= parse_capture (op
, !parsing_match_operand
);
4226 /* Create a new simplify from the current parsing state and MATCH,
4227 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
4230 parser::push_simplify (simplify::simplify_kind kind
,
4231 vec
<simplify
*>& simplifiers
,
4232 operand
*match
, operand
*result
)
4234 /* Build and push a temporary for operator list uses in expressions. */
4235 if (!oper_lists
.is_empty ())
4236 active_fors
.safe_push (oper_lists
);
4238 simplifiers
.safe_push
4239 (new simplify (kind
, match
, result
,
4240 active_fors
.copy (), capture_ids
));
4242 if (!oper_lists
.is_empty ())
4247 <result-op> = <op> | <if> | <with>
4248 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
4249 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
4253 parser::parse_result (operand
*result
, predicate_id
*matcher
)
4255 const cpp_token
*token
= peek ();
4256 if (token
->type
!= CPP_OPEN_PAREN
)
4259 eat_token (CPP_OPEN_PAREN
);
4260 if (peek_ident ("if"))
4263 if_expr
*ife
= new if_expr (token
->src_loc
);
4264 ife
->cond
= parse_c_expr (CPP_OPEN_PAREN
);
4265 if (peek ()->type
== CPP_OPEN_PAREN
)
4267 ife
->trueexpr
= parse_result (result
, matcher
);
4268 if (peek ()->type
== CPP_OPEN_PAREN
)
4269 ife
->falseexpr
= parse_result (result
, matcher
);
4270 else if (peek ()->type
!= CPP_CLOSE_PAREN
)
4271 ife
->falseexpr
= parse_op ();
4273 else if (peek ()->type
!= CPP_CLOSE_PAREN
)
4275 ife
->trueexpr
= parse_op ();
4276 if (peek ()->type
== CPP_OPEN_PAREN
)
4277 ife
->falseexpr
= parse_result (result
, matcher
);
4278 else if (peek ()->type
!= CPP_CLOSE_PAREN
)
4279 ife
->falseexpr
= parse_op ();
4281 /* If this if is immediately closed then it contains a
4282 manual matcher or is part of a predicate definition. */
4283 else /* if (peek ()->type == CPP_CLOSE_PAREN) */
4286 fatal_at (peek (), "manual transform not implemented");
4287 ife
->trueexpr
= result
;
4289 eat_token (CPP_CLOSE_PAREN
);
4292 else if (peek_ident ("with"))
4295 with_expr
*withe
= new with_expr (token
->src_loc
);
4296 /* Parse (with c-expr expr) as (if-with (true) expr). */
4297 withe
->with
= parse_c_expr (CPP_OPEN_BRACE
);
4298 withe
->with
->nr_stmts
= 0;
4299 withe
->subexpr
= parse_result (result
, matcher
);
4300 eat_token (CPP_CLOSE_PAREN
);
4303 else if (peek_ident ("switch"))
4305 token
= eat_ident ("switch");
4306 source_location ifloc
= eat_token (CPP_OPEN_PAREN
)->src_loc
;
4308 if_expr
*ife
= new if_expr (ifloc
);
4310 ife
->cond
= parse_c_expr (CPP_OPEN_PAREN
);
4311 if (peek ()->type
== CPP_OPEN_PAREN
)
4312 ife
->trueexpr
= parse_result (result
, matcher
);
4314 ife
->trueexpr
= parse_op ();
4315 eat_token (CPP_CLOSE_PAREN
);
4316 if (peek ()->type
!= CPP_OPEN_PAREN
4317 || !peek_ident ("if", 2))
4318 fatal_at (token
, "switch can be implemented with a single if");
4319 while (peek ()->type
!= CPP_CLOSE_PAREN
)
4321 if (peek ()->type
== CPP_OPEN_PAREN
)
4323 if (peek_ident ("if", 2))
4325 ifloc
= eat_token (CPP_OPEN_PAREN
)->src_loc
;
4327 ife
->falseexpr
= new if_expr (ifloc
);
4328 ife
= as_a
<if_expr
*> (ife
->falseexpr
);
4329 ife
->cond
= parse_c_expr (CPP_OPEN_PAREN
);
4330 if (peek ()->type
== CPP_OPEN_PAREN
)
4331 ife
->trueexpr
= parse_result (result
, matcher
);
4333 ife
->trueexpr
= parse_op ();
4334 eat_token (CPP_CLOSE_PAREN
);
4338 /* switch default clause */
4339 ife
->falseexpr
= parse_result (result
, matcher
);
4340 eat_token (CPP_CLOSE_PAREN
);
4346 /* switch default clause */
4347 ife
->falseexpr
= parse_op ();
4348 eat_token (CPP_CLOSE_PAREN
);
4352 eat_token (CPP_CLOSE_PAREN
);
4357 operand
*op
= result
;
4360 eat_token (CPP_CLOSE_PAREN
);
4366 simplify = 'simplify' <expr> <result-op>
4368 match = 'match' <ident> <expr> [<result-op>]
4369 and fill SIMPLIFIERS with the results. */
4372 parser::parse_simplify (simplify::simplify_kind kind
,
4373 vec
<simplify
*>& simplifiers
, predicate_id
*matcher
,
4376 /* Reset the capture map. */
4378 capture_ids
= new cid_map_t
;
4379 /* Reset oper_lists and set. */
4380 hash_set
<user_id
*> olist
;
4381 oper_lists_set
= &olist
;
4384 const cpp_token
*loc
= peek ();
4385 parsing_match_operand
= true;
4386 struct operand
*match
= parse_op ();
4387 parsing_match_operand
= false;
4388 if (match
->type
== operand::OP_CAPTURE
&& !matcher
)
4389 fatal_at (loc
, "outermost expression cannot be captured");
4390 if (match
->type
== operand::OP_EXPR
4391 && is_a
<predicate_id
*> (as_a
<expr
*> (match
)->operation
))
4392 fatal_at (loc
, "outermost expression cannot be a predicate");
4394 /* Splice active_ifs onto result and continue parsing the
4396 if_expr
*active_if
= NULL
;
4397 for (int i
= active_ifs
.length (); i
> 0; --i
)
4399 if_expr
*ifc
= new if_expr (active_ifs
[i
-1]->location
);
4400 ifc
->cond
= active_ifs
[i
-1];
4401 ifc
->trueexpr
= active_if
;
4404 if_expr
*outermost_if
= active_if
;
4405 while (active_if
&& active_if
->trueexpr
)
4406 active_if
= as_a
<if_expr
*> (active_if
->trueexpr
);
4408 const cpp_token
*token
= peek ();
4410 /* If this if is immediately closed then it is part of a predicate
4411 definition. Push it. */
4412 if (token
->type
== CPP_CLOSE_PAREN
)
4415 fatal_at (token
, "expected transform expression");
4418 active_if
->trueexpr
= result
;
4419 result
= outermost_if
;
4421 push_simplify (kind
, simplifiers
, match
, result
);
4425 operand
*tem
= parse_result (result
, matcher
);
4428 active_if
->trueexpr
= tem
;
4429 result
= outermost_if
;
4434 push_simplify (kind
, simplifiers
, match
, result
);
4437 /* Parsing of the outer control structures. */
4439 /* Parse a for expression
4440 for = '(' 'for' <subst>... <pattern> ')'
4441 subst = <ident> '(' <ident>... ')' */
4444 parser::parse_for (source_location
)
4446 auto_vec
<const cpp_token
*> user_id_tokens
;
4447 vec
<user_id
*> user_ids
= vNULL
;
4448 const cpp_token
*token
;
4449 unsigned min_n_opers
= 0, max_n_opers
= 0;
4454 if (token
->type
!= CPP_NAME
)
4457 /* Insert the user defined operators into the operator hash. */
4458 const char *id
= get_ident ();
4459 if (get_operator (id
, true) != NULL
)
4460 fatal_at (token
, "operator already defined");
4461 user_id
*op
= new user_id (id
);
4462 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
4464 user_ids
.safe_push (op
);
4465 user_id_tokens
.safe_push (token
);
4467 eat_token (CPP_OPEN_PAREN
);
4470 while ((token
= peek_ident ()) != 0)
4472 const char *oper
= get_ident ();
4473 id_base
*idb
= get_operator (oper
, true);
4475 fatal_at (token
, "no such operator '%s'", oper
);
4476 if (*idb
== CONVERT0
|| *idb
== CONVERT1
|| *idb
== CONVERT2
4477 || *idb
== VIEW_CONVERT0
|| *idb
== VIEW_CONVERT1
4478 || *idb
== VIEW_CONVERT2
)
4479 fatal_at (token
, "conditional operators cannot be used inside for");
4483 else if (idb
->nargs
== -1)
4485 else if (idb
->nargs
!= arity
)
4486 fatal_at (token
, "operator '%s' with arity %d does not match "
4487 "others with arity %d", oper
, idb
->nargs
, arity
);
4489 user_id
*p
= dyn_cast
<user_id
*> (idb
);
4492 if (p
->is_oper_list
)
4493 op
->substitutes
.safe_splice (p
->substitutes
);
4495 fatal_at (token
, "iterator cannot be used as operator-list");
4498 op
->substitutes
.safe_push (idb
);
4501 token
= expect (CPP_CLOSE_PAREN
);
4503 unsigned nsubstitutes
= op
->substitutes
.length ();
4504 if (nsubstitutes
== 0)
4505 fatal_at (token
, "A user-defined operator must have at least "
4506 "one substitution");
4507 if (max_n_opers
== 0)
4509 min_n_opers
= nsubstitutes
;
4510 max_n_opers
= nsubstitutes
;
4514 if (nsubstitutes
% min_n_opers
!= 0
4515 && min_n_opers
% nsubstitutes
!= 0)
4516 fatal_at (token
, "All user-defined identifiers must have a "
4517 "multiple number of operator substitutions of the "
4518 "smallest number of substitutions");
4519 if (nsubstitutes
< min_n_opers
)
4520 min_n_opers
= nsubstitutes
;
4521 else if (nsubstitutes
> max_n_opers
)
4522 max_n_opers
= nsubstitutes
;
4526 unsigned n_ids
= user_ids
.length ();
4528 fatal_at (token
, "for requires at least one user-defined identifier");
4531 if (token
->type
== CPP_CLOSE_PAREN
)
4532 fatal_at (token
, "no pattern defined in for");
4534 active_fors
.safe_push (user_ids
);
4538 if (token
->type
== CPP_CLOSE_PAREN
)
4544 /* Remove user-defined operators from the hash again. */
4545 for (unsigned i
= 0; i
< user_ids
.length (); ++i
)
4547 if (!user_ids
[i
]->used
)
4548 warning_at (user_id_tokens
[i
],
4549 "operator %s defined but not used", user_ids
[i
]->id
);
4550 operators
->remove_elt (user_ids
[i
]);
4554 /* Parse an identifier associated with a list of operators.
4555 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
4558 parser::parse_operator_list (source_location
)
4560 const cpp_token
*token
= peek ();
4561 const char *id
= get_ident ();
4563 if (get_operator (id
, true) != 0)
4564 fatal_at (token
, "operator %s already defined", id
);
4566 user_id
*op
= new user_id (id
, true);
4569 while ((token
= peek_ident ()) != 0)
4572 const char *oper
= get_ident ();
4573 id_base
*idb
= get_operator (oper
, true);
4576 fatal_at (token
, "no such operator '%s'", oper
);
4580 else if (idb
->nargs
== -1)
4582 else if (arity
!= idb
->nargs
)
4583 fatal_at (token
, "operator '%s' with arity %d does not match "
4584 "others with arity %d", oper
, idb
->nargs
, arity
);
4586 /* We allow composition of multiple operator lists. */
4587 if (user_id
*p
= dyn_cast
<user_id
*> (idb
))
4588 op
->substitutes
.safe_splice (p
->substitutes
);
4590 op
->substitutes
.safe_push (idb
);
4593 // Check that there is no junk after id-list
4595 if (token
->type
!= CPP_CLOSE_PAREN
)
4596 fatal_at (token
, "expected identifier got %s", cpp_type2name (token
->type
, 0));
4598 if (op
->substitutes
.length () == 0)
4599 fatal_at (token
, "operator-list cannot be empty");
4602 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
4606 /* Parse an outer if expression.
4607 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
4610 parser::parse_if (source_location
)
4612 c_expr
*ifexpr
= parse_c_expr (CPP_OPEN_PAREN
);
4614 const cpp_token
*token
= peek ();
4615 if (token
->type
== CPP_CLOSE_PAREN
)
4616 fatal_at (token
, "no pattern defined in if");
4618 active_ifs
.safe_push (ifexpr
);
4621 const cpp_token
*token
= peek ();
4622 if (token
->type
== CPP_CLOSE_PAREN
)
4630 /* Parse a list of predefined predicate identifiers.
4631 preds = '(' 'define_predicates' <ident>... ')' */
4634 parser::parse_predicates (source_location
)
4638 const cpp_token
*token
= peek ();
4639 if (token
->type
!= CPP_NAME
)
4642 add_predicate (get_ident ());
4647 /* Parse outer control structures.
4648 pattern = <preds>|<for>|<if>|<simplify>|<match> */
4651 parser::parse_pattern ()
4653 /* All clauses start with '('. */
4654 eat_token (CPP_OPEN_PAREN
);
4655 const cpp_token
*token
= peek ();
4656 const char *id
= get_ident ();
4657 if (strcmp (id
, "simplify") == 0)
4659 parse_simplify (simplify::SIMPLIFY
, simplifiers
, NULL
, NULL
);
4662 else if (strcmp (id
, "match") == 0)
4664 bool with_args
= false;
4665 source_location e_loc
= peek ()->src_loc
;
4666 if (peek ()->type
== CPP_OPEN_PAREN
)
4668 eat_token (CPP_OPEN_PAREN
);
4671 const char *name
= get_ident ();
4672 id_base
*id
= get_operator (name
);
4676 p
= add_predicate (name
);
4677 user_predicates
.safe_push (p
);
4679 else if ((p
= dyn_cast
<predicate_id
*> (id
)))
4682 fatal_at (token
, "cannot add a match to a non-predicate ID");
4683 /* Parse (match <id> <arg>... (match-expr)) here. */
4687 capture_ids
= new cid_map_t
;
4688 e
= new expr (p
, e_loc
);
4689 while (peek ()->type
== CPP_ATSIGN
)
4690 e
->append_op (parse_capture (NULL
, false));
4691 eat_token (CPP_CLOSE_PAREN
);
4694 && ((e
&& e
->ops
.length () != (unsigned)p
->nargs
)
4695 || (!e
&& p
->nargs
!= 0)))
4696 fatal_at (token
, "non-matching number of match operands");
4697 p
->nargs
= e
? e
->ops
.length () : 0;
4698 parse_simplify (simplify::MATCH
, p
->matchers
, p
, e
);
4701 else if (strcmp (id
, "for") == 0)
4702 parse_for (token
->src_loc
);
4703 else if (strcmp (id
, "if") == 0)
4704 parse_if (token
->src_loc
);
4705 else if (strcmp (id
, "define_predicates") == 0)
4707 if (active_ifs
.length () > 0
4708 || active_fors
.length () > 0)
4709 fatal_at (token
, "define_predicates inside if or for is not supported");
4710 parse_predicates (token
->src_loc
);
4712 else if (strcmp (id
, "define_operator_list") == 0)
4714 if (active_ifs
.length () > 0
4715 || active_fors
.length () > 0)
4716 fatal_at (token
, "operator-list inside if or for is not supported");
4717 parse_operator_list (token
->src_loc
);
4720 fatal_at (token
, "expected %s'simplify', 'match', 'for' or 'if'",
4721 active_ifs
.length () == 0 && active_fors
.length () == 0
4722 ? "'define_predicates', " : "");
4724 eat_token (CPP_CLOSE_PAREN
);
4727 /* Main entry of the parser. Repeatedly parse outer control structures. */
4729 parser::parser (cpp_reader
*r_
)
4733 active_fors
= vNULL
;
4734 simplifiers
= vNULL
;
4735 oper_lists_set
= NULL
;
4738 user_predicates
= vNULL
;
4739 parsing_match_operand
= false;
4741 const cpp_token
*token
= next ();
4742 while (token
->type
!= CPP_EOF
)
4744 _cpp_backup_tokens (r
, 1);
4751 /* Helper for the linemap code. */
4754 round_alloc_size (size_t s
)
4760 /* The genmatch generator progam. It reads from a pattern description
4761 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
4764 main (int argc
, char **argv
)
4768 progname
= "genmatch";
4774 char *input
= argv
[argc
-1];
4775 for (int i
= 1; i
< argc
- 1; ++i
)
4777 if (strcmp (argv
[i
], "--gimple") == 0)
4779 else if (strcmp (argv
[i
], "--generic") == 0)
4781 else if (strcmp (argv
[i
], "-v") == 0)
4783 else if (strcmp (argv
[i
], "-vv") == 0)
4787 fprintf (stderr
, "Usage: genmatch "
4788 "[--gimple] [--generic] [-v[v]] input\n");
4793 line_table
= XCNEW (struct line_maps
);
4794 linemap_init (line_table
, 0);
4795 line_table
->reallocator
= xrealloc
;
4796 line_table
->round_alloc_size
= round_alloc_size
;
4798 r
= cpp_create_reader (CLK_GNUC99
, NULL
, line_table
);
4799 cpp_callbacks
*cb
= cpp_get_callbacks (r
);
4800 cb
->error
= error_cb
;
4802 /* Add the build directory to the #include "" search path. */
4803 cpp_dir
*dir
= XCNEW (cpp_dir
);
4804 dir
->name
= getpwd ();
4806 dir
->name
= ASTRDUP (".");
4807 cpp_set_include_chains (r
, dir
, NULL
, false);
4809 if (!cpp_read_main_file (r
, input
))
4811 cpp_define (r
, gimple
? "GIMPLE=1": "GENERIC=1");
4812 cpp_define (r
, gimple
? "GENERIC=0": "GIMPLE=0");
4814 null_id
= new id_base (id_base::NULL_ID
, "null");
4816 /* Pre-seed operators. */
4817 operators
= new hash_table
<id_base
> (1024);
4818 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
4819 add_operator (SYM, # SYM, # TYPE, NARGS);
4820 #define END_OF_BASE_TREE_CODES
4822 add_operator (CONVERT0
, "convert0", "tcc_unary", 1);
4823 add_operator (CONVERT1
, "convert1", "tcc_unary", 1);
4824 add_operator (CONVERT2
, "convert2", "tcc_unary", 1);
4825 add_operator (VIEW_CONVERT0
, "view_convert0", "tcc_unary", 1);
4826 add_operator (VIEW_CONVERT1
, "view_convert1", "tcc_unary", 1);
4827 add_operator (VIEW_CONVERT2
, "view_convert2", "tcc_unary", 1);
4828 #undef END_OF_BASE_TREE_CODES
4831 /* Pre-seed builtin functions.
4832 ??? Cannot use N (name) as that is targetm.emultls.get_address
4833 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
4834 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
4835 add_function (ENUM, "CFN_" # ENUM);
4836 #include "builtins.def"
4838 #define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \
4839 add_function (IFN_##CODE, "CFN_" #CODE);
4840 #include "internal-fn.def"
4846 write_header (stdout
, "gimple-match-head.c");
4848 write_header (stdout
, "generic-match-head.c");
4850 /* Go over all predicates defined with patterns and perform
4851 lowering and code generation. */
4852 for (unsigned i
= 0; i
< p
.user_predicates
.length (); ++i
)
4854 predicate_id
*pred
= p
.user_predicates
[i
];
4855 lower (pred
->matchers
, gimple
);
4858 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
4859 print_matches (pred
->matchers
[i
]);
4862 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
4863 dt
.insert (pred
->matchers
[i
], i
);
4868 write_predicate (stdout
, pred
, dt
, gimple
);
4871 /* Lower the main simplifiers and generate code for them. */
4872 lower (p
.simplifiers
, gimple
);
4875 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
4876 print_matches (p
.simplifiers
[i
]);
4879 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
4880 dt
.insert (p
.simplifiers
[i
], i
);
4885 dt
.gen (stdout
, gimple
);
4888 cpp_finish (r
, NULL
);