1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
4 Copyright (C) 2014-2015 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/>. */
27 #include "coretypes.h"
30 #include "hash-table.h"
35 /* Stubs for GGC referenced through instantiations triggered by hash-map. */
36 void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
37 size_t, size_t MEM_STAT_DECL
)
41 void ggc_free (void *)
48 static struct line_maps
*line_table
;
51 #if GCC_VERSION >= 4001
52 __attribute__((format (printf
, 6, 0)))
54 error_cb (cpp_reader
*, int errtype
, int, source_location location
,
55 unsigned int, const char *msg
, va_list *ap
)
57 const line_map_ordinary
*map
;
58 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
59 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
60 fprintf (stderr
, "%s:%d:%d %s: ", loc
.file
, loc
.line
, loc
.column
,
61 (errtype
== CPP_DL_WARNING
) ? "warning" : "error");
62 vfprintf (stderr
, msg
, *ap
);
63 fprintf (stderr
, "\n");
64 FILE *f
= fopen (loc
.file
, "r");
70 if (!fgets (buf
, 128, f
))
72 if (buf
[strlen (buf
) - 1] != '\n')
79 fprintf (stderr
, "%s", buf
);
80 for (int i
= 0; i
< loc
.column
- 1; ++i
)
88 if (errtype
== CPP_DL_FATAL
)
94 #if GCC_VERSION >= 4001
95 __attribute__((format (printf
, 2, 3)))
97 fatal_at (const cpp_token
*tk
, const char *msg
, ...)
101 error_cb (NULL
, CPP_DL_FATAL
, 0, tk
->src_loc
, 0, msg
, &ap
);
106 #if GCC_VERSION >= 4001
107 __attribute__((format (printf
, 2, 3)))
109 fatal_at (source_location loc
, const char *msg
, ...)
113 error_cb (NULL
, CPP_DL_FATAL
, 0, loc
, 0, msg
, &ap
);
118 #if GCC_VERSION >= 4001
119 __attribute__((format (printf
, 2, 3)))
121 warning_at (const cpp_token
*tk
, const char *msg
, ...)
125 error_cb (NULL
, CPP_DL_WARNING
, 0, tk
->src_loc
, 0, msg
, &ap
);
130 output_line_directive (FILE *f
, source_location location
,
131 bool dumpfile
= false)
133 const line_map_ordinary
*map
;
134 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
135 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
138 /* When writing to a dumpfile only dump the filename. */
139 const char *file
= strrchr (loc
.file
, DIR_SEPARATOR
);
144 fprintf (f
, "%s:%d", file
, loc
.line
);
147 /* Other gen programs really output line directives here, at least for
148 development it's right now more convenient to have line information
149 from the generated file. Still keep the directives as comment for now
150 to easily back-point to the meta-description. */
151 fprintf (f
, "/* #line %d \"%s\" */\n", loc
.line
, loc
.file
);
155 /* Pull in tree codes and builtin function codes from their
158 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
171 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
172 enum built_in_function
{
173 #include "builtins.def"
179 /* Base class for all identifiers the parser knows. */
181 struct id_base
: nofree_ptr_hash
<id_base
>
183 enum id_kind
{ CODE
, FN
, PREDICATE
, USER
} kind
;
185 id_base (id_kind
, const char *, int = -1);
191 /* hash_table support. */
192 static inline hashval_t
hash (const id_base
*);
193 static inline int equal (const id_base
*, const id_base
*);
197 id_base::hash (const id_base
*op
)
203 id_base::equal (const id_base
*op1
,
206 return (op1
->hashval
== op2
->hashval
207 && strcmp (op1
->id
, op2
->id
) == 0);
210 /* Hashtable of known pattern operators. This is pre-seeded from
211 all known tree codes and all known builtin function ids. */
212 static hash_table
<id_base
> *operators
;
214 id_base::id_base (id_kind kind_
, const char *id_
, int nargs_
)
219 hashval
= htab_hash_string (id
);
222 /* Identifier that maps to a tree code. */
224 struct operator_id
: public id_base
226 operator_id (enum tree_code code_
, const char *id_
, unsigned nargs_
,
228 : id_base (id_base::CODE
, id_
, nargs_
), code (code_
), tcc (tcc_
) {}
233 /* Identifier that maps to a builtin function code. */
235 struct fn_id
: public id_base
237 fn_id (enum built_in_function fn_
, const char *id_
)
238 : id_base (id_base::FN
, id_
), fn (fn_
) {}
239 enum built_in_function fn
;
244 /* Identifier that maps to a user-defined predicate. */
246 struct predicate_id
: public id_base
248 predicate_id (const char *id_
)
249 : id_base (id_base::PREDICATE
, id_
), matchers (vNULL
) {}
250 vec
<simplify
*> matchers
;
253 /* Identifier that maps to a operator defined by a 'for' directive. */
255 struct user_id
: public id_base
257 user_id (const char *id_
, bool is_oper_list_
= false)
258 : id_base (id_base::USER
, id_
), substitutes (vNULL
),
259 used (false), is_oper_list (is_oper_list_
) {}
260 vec
<id_base
*> substitutes
;
268 is_a_helper
<fn_id
*>::test (id_base
*id
)
270 return id
->kind
== id_base::FN
;
276 is_a_helper
<operator_id
*>::test (id_base
*id
)
278 return id
->kind
== id_base::CODE
;
284 is_a_helper
<predicate_id
*>::test (id_base
*id
)
286 return id
->kind
== id_base::PREDICATE
;
292 is_a_helper
<user_id
*>::test (id_base
*id
)
294 return id
->kind
== id_base::USER
;
297 /* Add a predicate identifier to the hash. */
299 static predicate_id
*
300 add_predicate (const char *id
)
302 predicate_id
*p
= new predicate_id (id
);
303 id_base
**slot
= operators
->find_slot_with_hash (p
, p
->hashval
, INSERT
);
305 fatal ("duplicate id definition");
310 /* Add a tree code identifier to the hash. */
313 add_operator (enum tree_code code
, const char *id
,
314 const char *tcc
, unsigned nargs
)
316 if (strcmp (tcc
, "tcc_unary") != 0
317 && strcmp (tcc
, "tcc_binary") != 0
318 && strcmp (tcc
, "tcc_comparison") != 0
319 && strcmp (tcc
, "tcc_expression") != 0
320 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
321 && strcmp (tcc
, "tcc_reference") != 0
322 /* To have INTEGER_CST and friends as "predicate operators". */
323 && strcmp (tcc
, "tcc_constant") != 0
324 /* And allow CONSTRUCTOR for vector initializers. */
325 && !(code
== CONSTRUCTOR
))
327 /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
328 if (code
== ADDR_EXPR
)
330 operator_id
*op
= new operator_id (code
, id
, nargs
, tcc
);
331 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
333 fatal ("duplicate id definition");
337 /* Add a builtin identifier to the hash. */
340 add_builtin (enum built_in_function code
, const char *id
)
342 fn_id
*fn
= new fn_id (code
, id
);
343 id_base
**slot
= operators
->find_slot_with_hash (fn
, fn
->hashval
, INSERT
);
345 fatal ("duplicate id definition");
349 /* Helper for easy comparing ID with tree code CODE. */
352 operator==(id_base
&id
, enum tree_code code
)
354 if (operator_id
*oid
= dyn_cast
<operator_id
*> (&id
))
355 return oid
->code
== code
;
359 /* Lookup the identifier ID. */
362 get_operator (const char *id
)
364 id_base
tem (id_base::CODE
, id
);
366 id_base
*op
= operators
->find_with_hash (&tem
, tem
.hashval
);
369 /* If this is a user-defined identifier track whether it was used. */
370 if (user_id
*uid
= dyn_cast
<user_id
*> (op
))
375 /* Try all-uppercase. */
376 char *id2
= xstrdup (id
);
377 for (unsigned i
= 0; i
< strlen (id2
); ++i
)
378 id2
[i
] = TOUPPER (id2
[i
]);
379 new (&tem
) id_base (id_base::CODE
, id2
);
380 op
= operators
->find_with_hash (&tem
, tem
.hashval
);
387 /* Try _EXPR appended. */
388 id2
= (char *)xrealloc (id2
, strlen (id2
) + sizeof ("_EXPR") + 1);
389 strcat (id2
, "_EXPR");
390 new (&tem
) id_base (id_base::CODE
, id2
);
391 op
= operators
->find_with_hash (&tem
, tem
.hashval
);
401 typedef hash_map
<nofree_string_hash
, unsigned> cid_map_t
;
404 /* The AST produced by parsing of the pattern definitions. */
409 /* The base class for operands. */
412 enum op_type
{ OP_PREDICATE
, OP_EXPR
, OP_CAPTURE
, OP_C_EXPR
};
413 operand (enum op_type type_
) : type (type_
) {}
415 virtual void gen_transform (FILE *, const char *, bool, int,
416 const char *, capture_info
*,
419 { gcc_unreachable (); }
422 /* A predicate operand. Predicates are leafs in the AST. */
424 struct predicate
: public operand
426 predicate (predicate_id
*p_
) : operand (OP_PREDICATE
), p (p_
) {}
430 /* An operand that constitutes an expression. Expressions include
431 function calls and user-defined predicate invocations. */
433 struct expr
: public operand
435 expr (id_base
*operation_
, bool is_commutative_
= false)
436 : operand (OP_EXPR
), operation (operation_
),
437 ops (vNULL
), expr_type (NULL
), is_commutative (is_commutative_
),
438 is_generic (false) {}
439 void append_op (operand
*op
) { ops
.safe_push (op
); }
440 /* The operator and its operands. */
443 /* An explicitely specified type - used exclusively for conversions. */
444 const char *expr_type
;
445 /* Whether the operation is to be applied commutatively. This is
446 later lowered to two separate patterns. */
448 /* Whether the expression is expected to be in GENERIC form. */
450 virtual void gen_transform (FILE *f
, const char *, bool, int,
451 const char *, capture_info
*,
452 dt_operand
** = 0, bool = true);
455 /* An operator that is represented by native C code. This is always
456 a leaf operand in the AST. This class is also used to represent
457 the code to be generated for 'if' and 'with' expressions. */
459 struct c_expr
: public operand
461 /* A mapping of an identifier and its replacement. Used to apply
466 id_tab (const char *id_
, const char *oper_
): id (id_
), oper (oper_
) {}
469 c_expr (cpp_reader
*r_
, vec
<cpp_token
> code_
, unsigned nr_stmts_
,
470 vec
<id_tab
> ids_
, cid_map_t
*capture_ids_
)
471 : operand (OP_C_EXPR
), r (r_
), code (code_
), capture_ids (capture_ids_
),
472 nr_stmts (nr_stmts_
), ids (ids_
) {}
473 /* cpplib tokens and state to transform this back to source. */
476 cid_map_t
*capture_ids
;
477 /* The number of statements parsed (well, the number of ';'s). */
479 /* The identifier replacement vector. */
481 virtual void gen_transform (FILE *f
, const char *, bool, int,
482 const char *, capture_info
*,
483 dt_operand
** = 0, bool = true);
486 /* A wrapper around another operand that captures its value. */
488 struct capture
: public operand
490 capture (unsigned where_
, operand
*what_
)
491 : operand (OP_CAPTURE
), where (where_
), what (what_
) {}
492 /* Identifier index for the value. */
494 /* The captured value. */
496 virtual void gen_transform (FILE *f
, const char *, bool, int,
497 const char *, capture_info
*,
498 dt_operand
** = 0, bool = true);
504 is_a_helper
<capture
*>::test (operand
*op
)
506 return op
->type
== operand::OP_CAPTURE
;
512 is_a_helper
<predicate
*>::test (operand
*op
)
514 return op
->type
== operand::OP_PREDICATE
;
520 is_a_helper
<c_expr
*>::test (operand
*op
)
522 return op
->type
== operand::OP_C_EXPR
;
528 is_a_helper
<expr
*>::test (operand
*op
)
530 return op
->type
== operand::OP_EXPR
;
533 /* Helper to distinguish 'if' from 'with' expressions. */
537 if_or_with (operand
*cexpr_
, source_location location_
, bool is_with_
)
538 : location (location_
), cexpr (cexpr_
), is_with (is_with_
) {}
539 source_location location
;
544 /* The main class of a pattern and its transform. This is used to
545 represent both (simplify ...) and (match ...) kinds. The AST
546 duplicates all outer 'if' and 'for' expressions here so each
547 simplify can exist in isolation. */
551 simplify (operand
*match_
, source_location match_location_
,
552 struct operand
*result_
, source_location result_location_
,
553 vec
<if_or_with
> ifexpr_vec_
, vec
<vec
<user_id
*> > for_vec_
,
554 cid_map_t
*capture_ids_
)
555 : match (match_
), match_location (match_location_
),
556 result (result_
), result_location (result_location_
),
557 ifexpr_vec (ifexpr_vec_
), for_vec (for_vec_
),
558 capture_ids (capture_ids_
), capture_max (capture_ids_
->elements () - 1) {}
560 /* The expression that is matched against the GENERIC or GIMPLE IL. */
562 source_location match_location
;
563 /* For a (simplify ...) the expression produced when the pattern applies.
564 For a (match ...) either NULL if it is a simple predicate or the
565 single expression specifying the matched operands. */
566 struct operand
*result
;
567 source_location result_location
;
568 /* Collected 'if' expressions that need to evaluate to true to make
569 the pattern apply. */
570 vec
<if_or_with
> ifexpr_vec
;
571 /* Collected 'for' expression operators that have to be replaced
572 in the lowering phase. */
573 vec
<vec
<user_id
*> > for_vec
;
574 /* A map of capture identifiers to indexes. */
575 cid_map_t
*capture_ids
;
579 /* Debugging routines for dumping the AST. */
582 print_operand (operand
*o
, FILE *f
= stderr
, bool flattened
= false)
584 if (capture
*c
= dyn_cast
<capture
*> (o
))
586 fprintf (f
, "@%u", c
->where
);
587 if (c
->what
&& flattened
== false)
590 print_operand (c
->what
, f
, flattened
);
595 else if (predicate
*p
= dyn_cast
<predicate
*> (o
))
596 fprintf (f
, "%s", p
->p
->id
);
598 else if (is_a
<c_expr
*> (o
))
599 fprintf (f
, "c_expr");
601 else if (expr
*e
= dyn_cast
<expr
*> (o
))
603 fprintf (f
, "(%s", e
->operation
->id
);
605 if (flattened
== false)
608 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
610 print_operand (e
->ops
[i
], f
, flattened
);
622 print_matches (struct simplify
*s
, FILE *f
= stderr
)
624 fprintf (f
, "for expression: ");
625 print_operand (s
->match
, f
);
632 /* Lowering of commutative operators. */
635 cartesian_product (const vec
< vec
<operand
*> >& ops_vector
,
636 vec
< vec
<operand
*> >& result
, vec
<operand
*>& v
, unsigned n
)
638 if (n
== ops_vector
.length ())
640 vec
<operand
*> xv
= v
.copy ();
641 result
.safe_push (xv
);
645 for (unsigned i
= 0; i
< ops_vector
[n
].length (); ++i
)
647 v
[n
] = ops_vector
[n
][i
];
648 cartesian_product (ops_vector
, result
, v
, n
+ 1);
652 /* Lower OP to two operands in case it is marked as commutative. */
654 static vec
<operand
*>
655 commutate (operand
*op
)
657 vec
<operand
*> ret
= vNULL
;
659 if (capture
*c
= dyn_cast
<capture
*> (op
))
666 vec
<operand
*> v
= commutate (c
->what
);
667 for (unsigned i
= 0; i
< v
.length (); ++i
)
669 capture
*nc
= new capture (c
->where
, v
[i
]);
675 expr
*e
= dyn_cast
<expr
*> (op
);
676 if (!e
|| e
->ops
.length () == 0)
682 vec
< vec
<operand
*> > ops_vector
= vNULL
;
683 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
684 ops_vector
.safe_push (commutate (e
->ops
[i
]));
686 auto_vec
< vec
<operand
*> > result
;
687 auto_vec
<operand
*> v (e
->ops
.length ());
688 v
.quick_grow_cleared (e
->ops
.length ());
689 cartesian_product (ops_vector
, result
, v
, 0);
692 for (unsigned i
= 0; i
< result
.length (); ++i
)
694 expr
*ne
= new expr (e
->operation
);
695 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
696 ne
->append_op (result
[i
][j
]);
700 if (!e
->is_commutative
)
703 for (unsigned i
= 0; i
< result
.length (); ++i
)
705 expr
*ne
= new expr (e
->operation
);
706 // result[i].length () is 2 since e->operation is binary
707 for (unsigned j
= result
[i
].length (); j
; --j
)
708 ne
->append_op (result
[i
][j
-1]);
715 /* Lower operations marked as commutative in the AST of S and push
716 the resulting patterns to SIMPLIFIERS. */
719 lower_commutative (simplify
*s
, vec
<simplify
*>& simplifiers
)
721 vec
<operand
*> matchers
= commutate (s
->match
);
722 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
724 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
725 s
->result
, s
->result_location
, s
->ifexpr_vec
,
726 s
->for_vec
, s
->capture_ids
);
727 simplifiers
.safe_push (ns
);
731 /* Strip conditional conversios using operator OPER from O and its
732 children if STRIP, else replace them with an unconditional convert. */
735 lower_opt_convert (operand
*o
, enum tree_code oper
,
736 enum tree_code to_oper
, bool strip
)
738 if (capture
*c
= dyn_cast
<capture
*> (o
))
741 return new capture (c
->where
,
742 lower_opt_convert (c
->what
, oper
, to_oper
, strip
));
747 expr
*e
= dyn_cast
<expr
*> (o
);
751 if (*e
->operation
== oper
)
754 return lower_opt_convert (e
->ops
[0], oper
, to_oper
, strip
);
756 expr
*ne
= new expr (to_oper
== CONVERT_EXPR
757 ? get_operator ("CONVERT_EXPR")
758 : get_operator ("VIEW_CONVERT_EXPR"));
759 ne
->append_op (lower_opt_convert (e
->ops
[0], oper
, to_oper
, strip
));
763 expr
*ne
= new expr (e
->operation
, e
->is_commutative
);
764 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
765 ne
->append_op (lower_opt_convert (e
->ops
[i
], oper
, to_oper
, strip
));
770 /* Determine whether O or its children uses the conditional conversion
774 has_opt_convert (operand
*o
, enum tree_code oper
)
776 if (capture
*c
= dyn_cast
<capture
*> (o
))
779 return has_opt_convert (c
->what
, oper
);
784 expr
*e
= dyn_cast
<expr
*> (o
);
788 if (*e
->operation
== oper
)
791 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
792 if (has_opt_convert (e
->ops
[i
], oper
))
798 /* Lower conditional convert operators in O, expanding it to a vector
801 static vec
<operand
*>
802 lower_opt_convert (operand
*o
)
804 vec
<operand
*> v1
= vNULL
, v2
;
808 enum tree_code opers
[]
809 = { CONVERT0
, CONVERT_EXPR
,
810 CONVERT1
, CONVERT_EXPR
,
811 CONVERT2
, CONVERT_EXPR
,
812 VIEW_CONVERT0
, VIEW_CONVERT_EXPR
,
813 VIEW_CONVERT1
, VIEW_CONVERT_EXPR
,
814 VIEW_CONVERT2
, VIEW_CONVERT_EXPR
};
816 /* Conditional converts are lowered to a pattern with the
817 conversion and one without. The three different conditional
818 convert codes are lowered separately. */
820 for (unsigned i
= 0; i
< sizeof (opers
) / sizeof (enum tree_code
); i
+= 2)
823 for (unsigned j
= 0; j
< v1
.length (); ++j
)
824 if (has_opt_convert (v1
[j
], opers
[i
]))
826 v2
.safe_push (lower_opt_convert (v1
[j
],
827 opers
[i
], opers
[i
+1], false));
828 v2
.safe_push (lower_opt_convert (v1
[j
],
829 opers
[i
], opers
[i
+1], true));
835 for (unsigned j
= 0; j
< v2
.length (); ++j
)
836 v1
.safe_push (v2
[j
]);
843 /* Lower conditional convert operators in the AST of S and push
844 the resulting multiple patterns to SIMPLIFIERS. */
847 lower_opt_convert (simplify
*s
, vec
<simplify
*>& simplifiers
)
849 vec
<operand
*> matchers
= lower_opt_convert (s
->match
);
850 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
852 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
853 s
->result
, s
->result_location
, s
->ifexpr_vec
,
854 s
->for_vec
, s
->capture_ids
);
855 simplifiers
.safe_push (ns
);
859 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
860 GENERIC and a GIMPLE variant. */
862 static vec
<operand
*>
863 lower_cond (operand
*o
)
865 vec
<operand
*> ro
= vNULL
;
867 if (capture
*c
= dyn_cast
<capture
*> (o
))
871 vec
<operand
*> lop
= vNULL
;
872 lop
= lower_cond (c
->what
);
874 for (unsigned i
= 0; i
< lop
.length (); ++i
)
875 ro
.safe_push (new capture (c
->where
, lop
[i
]));
880 expr
*e
= dyn_cast
<expr
*> (o
);
881 if (!e
|| e
->ops
.length () == 0)
887 vec
< vec
<operand
*> > ops_vector
= vNULL
;
888 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
889 ops_vector
.safe_push (lower_cond (e
->ops
[i
]));
891 auto_vec
< vec
<operand
*> > result
;
892 auto_vec
<operand
*> v (e
->ops
.length ());
893 v
.quick_grow_cleared (e
->ops
.length ());
894 cartesian_product (ops_vector
, result
, v
, 0);
896 for (unsigned i
= 0; i
< result
.length (); ++i
)
898 expr
*ne
= new expr (e
->operation
);
899 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
900 ne
->append_op (result
[i
][j
]);
902 /* If this is a COND with a captured expression or an
903 expression with two operands then also match a GENERIC
904 form on the compare. */
905 if ((*e
->operation
== COND_EXPR
906 || *e
->operation
== VEC_COND_EXPR
)
907 && ((is_a
<capture
*> (e
->ops
[0])
908 && as_a
<capture
*> (e
->ops
[0])->what
909 && is_a
<expr
*> (as_a
<capture
*> (e
->ops
[0])->what
)
911 (as_a
<capture
*> (e
->ops
[0])->what
)->ops
.length () == 2)
912 || (is_a
<expr
*> (e
->ops
[0])
913 && as_a
<expr
*> (e
->ops
[0])->ops
.length () == 2)))
915 expr
*ne
= new expr (e
->operation
);
916 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
917 ne
->append_op (result
[i
][j
]);
918 if (capture
*c
= dyn_cast
<capture
*> (ne
->ops
[0]))
920 expr
*ocmp
= as_a
<expr
*> (c
->what
);
921 expr
*cmp
= new expr (ocmp
->operation
);
922 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
923 cmp
->append_op (ocmp
->ops
[j
]);
924 cmp
->is_generic
= true;
925 ne
->ops
[0] = new capture (c
->where
, cmp
);
929 expr
*ocmp
= as_a
<expr
*> (ne
->ops
[0]);
930 expr
*cmp
= new expr (ocmp
->operation
);
931 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
932 cmp
->append_op (ocmp
->ops
[j
]);
933 cmp
->is_generic
= true;
943 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
944 GENERIC and a GIMPLE variant. */
947 lower_cond (simplify
*s
, vec
<simplify
*>& simplifiers
)
949 vec
<operand
*> matchers
= lower_cond (s
->match
);
950 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
952 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
953 s
->result
, s
->result_location
, s
->ifexpr_vec
,
954 s
->for_vec
, s
->capture_ids
);
955 simplifiers
.safe_push (ns
);
959 /* In AST operand O replace operator ID with operator WITH. */
962 replace_id (operand
*o
, user_id
*id
, id_base
*with
)
964 /* Deep-copy captures and expressions, replacing operations as
966 if (capture
*c
= dyn_cast
<capture
*> (o
))
970 return new capture (c
->where
, replace_id (c
->what
, id
, with
));
972 else if (expr
*e
= dyn_cast
<expr
*> (o
))
974 expr
*ne
= new expr (e
->operation
== id
? with
: e
->operation
,
976 ne
->expr_type
= e
->expr_type
;
977 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
978 ne
->append_op (replace_id (e
->ops
[i
], id
, with
));
982 /* For c_expr we simply record a string replacement table which is
983 applied at code-generation time. */
984 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
986 vec
<c_expr::id_tab
> ids
= ce
->ids
.copy ();
987 ids
.safe_push (c_expr::id_tab (id
->id
, with
->id
));
988 return new c_expr (ce
->r
, ce
->code
, ce
->nr_stmts
, ids
, ce
->capture_ids
);
994 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
997 lower_for (simplify
*sin
, vec
<simplify
*>& simplifiers
)
999 vec
<vec
<user_id
*> >& for_vec
= sin
->for_vec
;
1000 unsigned worklist_start
= 0;
1001 auto_vec
<simplify
*> worklist
;
1002 worklist
.safe_push (sin
);
1004 /* Lower each recorded for separately, operating on the
1005 set of simplifiers created by the previous one.
1006 Lower inner-to-outer so inner for substitutes can refer
1007 to operators replaced by outer fors. */
1008 for (int fi
= for_vec
.length () - 1; fi
>= 0; --fi
)
1010 vec
<user_id
*>& ids
= for_vec
[fi
];
1011 unsigned n_ids
= ids
.length ();
1012 unsigned max_n_opers
= 0;
1013 for (unsigned i
= 0; i
< n_ids
; ++i
)
1014 if (ids
[i
]->substitutes
.length () > max_n_opers
)
1015 max_n_opers
= ids
[i
]->substitutes
.length ();
1017 unsigned worklist_end
= worklist
.length ();
1018 for (unsigned si
= worklist_start
; si
< worklist_end
; ++si
)
1020 simplify
*s
= worklist
[si
];
1021 for (unsigned j
= 0; j
< max_n_opers
; ++j
)
1023 operand
*match_op
= s
->match
;
1024 operand
*result_op
= s
->result
;
1025 vec
<if_or_with
> ifexpr_vec
= s
->ifexpr_vec
.copy ();
1027 for (unsigned i
= 0; i
< n_ids
; ++i
)
1029 user_id
*id
= ids
[i
];
1030 id_base
*oper
= id
->substitutes
[j
% id
->substitutes
.length ()];
1031 match_op
= replace_id (match_op
, id
, oper
);
1033 result_op
= replace_id (result_op
, id
, oper
);
1034 for (unsigned k
= 0; k
< s
->ifexpr_vec
.length (); ++k
)
1035 ifexpr_vec
[k
].cexpr
= replace_id (ifexpr_vec
[k
].cexpr
,
1038 simplify
*ns
= new simplify (match_op
, s
->match_location
,
1039 result_op
, s
->result_location
,
1040 ifexpr_vec
, vNULL
, s
->capture_ids
);
1041 worklist
.safe_push (ns
);
1044 worklist_start
= worklist_end
;
1047 /* Copy out the result from the last for lowering. */
1048 for (unsigned i
= worklist_start
; i
< worklist
.length (); ++i
)
1049 simplifiers
.safe_push (worklist
[i
]);
1052 /* Lower the AST for everything in SIMPLIFIERS. */
1055 lower (vec
<simplify
*>& simplifiers
, bool gimple
)
1057 auto_vec
<simplify
*> out_simplifiers
;
1058 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1059 lower_opt_convert (simplifiers
[i
], out_simplifiers
);
1061 simplifiers
.truncate (0);
1062 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1063 lower_commutative (out_simplifiers
[i
], simplifiers
);
1065 out_simplifiers
.truncate (0);
1067 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1068 lower_cond (simplifiers
[i
], out_simplifiers
);
1070 out_simplifiers
.safe_splice (simplifiers
);
1073 simplifiers
.truncate (0);
1074 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1075 lower_for (out_simplifiers
[i
], simplifiers
);
1081 /* The decision tree built for generating GIMPLE and GENERIC pattern
1082 matching code. It represents the 'match' expression of all
1083 simplifies and has those as its leafs. */
1085 /* Decision tree base class, used for DT_TRUE and DT_NODE. */
1089 enum dt_type
{ DT_NODE
, DT_OPERAND
, DT_TRUE
, DT_MATCH
, DT_SIMPLIFY
};
1093 vec
<dt_node
*> kids
;
1095 dt_node (enum dt_type type_
): type (type_
), level (0), kids (vNULL
) {}
1097 dt_node
*append_node (dt_node
*);
1098 dt_node
*append_op (operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1099 dt_node
*append_true_op (dt_node
*parent
= 0, unsigned pos
= 0);
1100 dt_node
*append_match_op (dt_operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1101 dt_node
*append_simplify (simplify
*, unsigned, dt_operand
**);
1103 virtual void gen (FILE *, bool) {}
1105 void gen_kids (FILE *, bool);
1106 void gen_kids_1 (FILE *, bool,
1107 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_operand
*>,
1108 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_node
*>);
1111 /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */
1113 struct dt_operand
: public dt_node
1116 dt_operand
*match_dop
;
1120 dt_operand (enum dt_type type
, operand
*op_
, dt_operand
*match_dop_
,
1121 dt_operand
*parent_
= 0, unsigned pos_
= 0)
1122 : dt_node (type
), op (op_
), match_dop (match_dop_
),
1123 parent (parent_
), pos (pos_
) {}
1125 void gen (FILE *, bool);
1126 unsigned gen_predicate (FILE *, const char *, bool);
1127 unsigned gen_match_op (FILE *, const char *);
1129 unsigned gen_gimple_expr (FILE *);
1130 unsigned gen_generic_expr (FILE *, const char *);
1132 char *get_name (char *);
1133 void gen_opname (char *, unsigned);
1136 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1138 struct dt_simplify
: public dt_node
1141 unsigned pattern_no
;
1142 dt_operand
**indexes
;
1144 dt_simplify (simplify
*s_
, unsigned pattern_no_
, dt_operand
**indexes_
)
1145 : dt_node (DT_SIMPLIFY
), s (s_
), pattern_no (pattern_no_
),
1146 indexes (indexes_
) {}
1148 void gen (FILE *f
, bool);
1154 is_a_helper
<dt_operand
*>::test (dt_node
*n
)
1156 return (n
->type
== dt_node::DT_OPERAND
1157 || n
->type
== dt_node::DT_MATCH
);
1160 /* A container for the actual decision tree. */
1162 struct decision_tree
1166 void insert (struct simplify
*, unsigned);
1167 void gen_gimple (FILE *f
= stderr
);
1168 void gen_generic (FILE *f
= stderr
);
1169 void print (FILE *f
= stderr
);
1171 decision_tree () { root
= new dt_node (dt_node::DT_NODE
); }
1173 static dt_node
*insert_operand (dt_node
*, operand
*, dt_operand
**indexes
,
1174 unsigned pos
= 0, dt_node
*parent
= 0);
1175 static dt_node
*find_node (vec
<dt_node
*>&, dt_node
*);
1176 static bool cmp_node (dt_node
*, dt_node
*);
1177 static void print_node (dt_node
*, FILE *f
= stderr
, unsigned = 0);
1180 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1183 cmp_operand (operand
*o1
, operand
*o2
)
1185 if (!o1
|| !o2
|| o1
->type
!= o2
->type
)
1188 if (o1
->type
== operand::OP_PREDICATE
)
1190 predicate
*p1
= as_a
<predicate
*>(o1
);
1191 predicate
*p2
= as_a
<predicate
*>(o2
);
1192 return p1
->p
== p2
->p
;
1194 else if (o1
->type
== operand::OP_EXPR
)
1196 expr
*e1
= static_cast<expr
*>(o1
);
1197 expr
*e2
= static_cast<expr
*>(o2
);
1198 return (e1
->operation
== e2
->operation
1199 && e1
->is_generic
== e2
->is_generic
);
1205 /* Compare two decision tree nodes N1 and N2 and return true if they
1209 decision_tree::cmp_node (dt_node
*n1
, dt_node
*n2
)
1211 if (!n1
|| !n2
|| n1
->type
!= n2
->type
)
1217 if (n1
->type
== dt_node::DT_TRUE
)
1220 if (n1
->type
== dt_node::DT_OPERAND
)
1221 return cmp_operand ((as_a
<dt_operand
*> (n1
))->op
,
1222 (as_a
<dt_operand
*> (n2
))->op
);
1223 else if (n1
->type
== dt_node::DT_MATCH
)
1224 return ((as_a
<dt_operand
*> (n1
))->match_dop
1225 == (as_a
<dt_operand
*> (n2
))->match_dop
);
1229 /* Search OPS for a decision tree node like P and return it if found. */
1232 decision_tree::find_node (vec
<dt_node
*>& ops
, dt_node
*p
)
1234 /* We can merge adjacent DT_TRUE. */
1235 if (p
->type
== dt_node::DT_TRUE
1237 && ops
.last ()->type
== dt_node::DT_TRUE
)
1239 for (int i
= ops
.length () - 1; i
>= 0; --i
)
1241 /* But we can't merge across DT_TRUE nodes as they serve as
1242 pattern order barriers to make sure that patterns apply
1243 in order of appearance in case multiple matches are possible. */
1244 if (ops
[i
]->type
== dt_node::DT_TRUE
)
1246 if (decision_tree::cmp_node (ops
[i
], p
))
1252 /* Append N to the decision tree if it there is not already an existing
1256 dt_node::append_node (dt_node
*n
)
1260 kid
= decision_tree::find_node (kids
, n
);
1265 n
->level
= this->level
+ 1;
1270 /* Append OP to the decision tree. */
1273 dt_node::append_op (operand
*op
, dt_node
*parent
, unsigned pos
)
1275 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1276 dt_operand
*n
= new dt_operand (DT_OPERAND
, op
, 0, parent_
, pos
);
1277 return append_node (n
);
1280 /* Append a DT_TRUE decision tree node. */
1283 dt_node::append_true_op (dt_node
*parent
, unsigned pos
)
1285 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1286 dt_operand
*n
= new dt_operand (DT_TRUE
, 0, 0, parent_
, pos
);
1287 return append_node (n
);
1290 /* Append a DT_MATCH decision tree node. */
1293 dt_node::append_match_op (dt_operand
*match_dop
, dt_node
*parent
, unsigned pos
)
1295 dt_operand
*parent_
= as_a
<dt_operand
*> (parent
);
1296 dt_operand
*n
= new dt_operand (DT_MATCH
, 0, match_dop
, parent_
, pos
);
1297 return append_node (n
);
1300 /* Append S to the decision tree. */
1303 dt_node::append_simplify (simplify
*s
, unsigned pattern_no
,
1304 dt_operand
**indexes
)
1306 dt_simplify
*n
= new dt_simplify (s
, pattern_no
, indexes
);
1307 return append_node (n
);
1310 /* Insert O into the decision tree and return the decision tree node found
1314 decision_tree::insert_operand (dt_node
*p
, operand
*o
, dt_operand
**indexes
,
1315 unsigned pos
, dt_node
*parent
)
1317 dt_node
*q
, *elm
= 0;
1319 if (capture
*c
= dyn_cast
<capture
*> (o
))
1321 unsigned capt_index
= c
->where
;
1323 if (indexes
[capt_index
] == 0)
1326 q
= insert_operand (p
, c
->what
, indexes
, pos
, parent
);
1329 q
= elm
= p
->append_true_op (parent
, pos
);
1332 // get to the last capture
1333 for (operand
*what
= c
->what
;
1334 what
&& is_a
<capture
*> (what
);
1335 c
= as_a
<capture
*> (what
), what
= c
->what
)
1340 unsigned cc_index
= c
->where
;
1341 dt_operand
*match_op
= indexes
[cc_index
];
1343 dt_operand
temp (dt_node::DT_TRUE
, 0, 0);
1344 elm
= decision_tree::find_node (p
->kids
, &temp
);
1348 dt_operand
temp (dt_node::DT_MATCH
, 0, match_op
);
1349 elm
= decision_tree::find_node (p
->kids
, &temp
);
1354 dt_operand
temp (dt_node::DT_OPERAND
, c
->what
, 0);
1355 elm
= decision_tree::find_node (p
->kids
, &temp
);
1359 gcc_assert (elm
->type
== dt_node::DT_TRUE
1360 || elm
->type
== dt_node::DT_OPERAND
1361 || elm
->type
== dt_node::DT_MATCH
);
1362 indexes
[capt_index
] = static_cast<dt_operand
*> (elm
);
1367 p
= p
->append_match_op (indexes
[capt_index
], parent
, pos
);
1369 return insert_operand (p
, c
->what
, indexes
, 0, p
);
1374 p
= p
->append_op (o
, parent
, pos
);
1377 if (expr
*e
= dyn_cast
<expr
*>(o
))
1379 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1380 q
= decision_tree::insert_operand (q
, e
->ops
[i
], indexes
, i
, p
);
1386 /* Insert S into the decision tree. */
1389 decision_tree::insert (struct simplify
*s
, unsigned pattern_no
)
1391 dt_operand
**indexes
= XCNEWVEC (dt_operand
*, s
->capture_max
+ 1);
1392 dt_node
*p
= decision_tree::insert_operand (root
, s
->match
, indexes
);
1393 p
->append_simplify (s
, pattern_no
, indexes
);
1396 /* Debug functions to dump the decision tree. */
1399 decision_tree::print_node (dt_node
*p
, FILE *f
, unsigned indent
)
1401 if (p
->type
== dt_node::DT_NODE
)
1402 fprintf (f
, "root");
1406 for (unsigned i
= 0; i
< indent
; i
++)
1409 if (p
->type
== dt_node::DT_OPERAND
)
1411 dt_operand
*dop
= static_cast<dt_operand
*>(p
);
1412 print_operand (dop
->op
, f
, true);
1414 else if (p
->type
== dt_node::DT_TRUE
)
1415 fprintf (f
, "true");
1416 else if (p
->type
== dt_node::DT_MATCH
)
1417 fprintf (f
, "match (%p)", (void *)((as_a
<dt_operand
*>(p
))->match_dop
));
1418 else if (p
->type
== dt_node::DT_SIMPLIFY
)
1420 dt_simplify
*s
= static_cast<dt_simplify
*> (p
);
1421 fprintf (f
, "simplify_%u { ", s
->pattern_no
);
1422 for (int i
= 0; i
<= s
->s
->capture_max
; ++i
)
1423 fprintf (f
, "%p, ", (void *) s
->indexes
[i
]);
1428 fprintf (stderr
, " (%p), %u, %u\n", (void *) p
, p
->level
, p
->kids
.length ());
1430 for (unsigned i
= 0; i
< p
->kids
.length (); ++i
)
1431 decision_tree::print_node (p
->kids
[i
], f
, indent
+ 2);
1435 decision_tree::print (FILE *f
)
1437 return decision_tree::print_node (root
, f
);
1441 /* For GENERIC we have to take care of wrapping multiple-used
1442 expressions with side-effects in save_expr and preserve side-effects
1443 of expressions with omit_one_operand. Analyze captures in
1444 match, result and with expressions and perform early-outs
1445 on the outermost match expression operands for cases we cannot
1450 capture_info (simplify
*s
);
1451 void walk_match (operand
*o
, unsigned toplevel_arg
, bool, bool);
1452 void walk_result (operand
*o
, bool);
1453 void walk_c_expr (c_expr
*);
1459 bool force_no_side_effects_p
;
1460 bool cond_expr_cond_p
;
1461 unsigned long toplevel_msk
;
1462 int result_use_count
;
1465 auto_vec
<cinfo
> info
;
1466 unsigned long force_no_side_effects
;
1469 /* Analyze captures in S. */
1471 capture_info::capture_info (simplify
*s
)
1475 || ((e
= dyn_cast
<expr
*> (s
->result
))
1476 && is_a
<predicate_id
*> (e
->operation
)))
1478 force_no_side_effects
= -1;
1482 force_no_side_effects
= 0;
1483 info
.safe_grow_cleared (s
->capture_max
+ 1);
1484 e
= as_a
<expr
*> (s
->match
);
1485 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1486 walk_match (e
->ops
[i
], i
,
1487 (i
!= 0 && *e
->operation
== COND_EXPR
)
1488 || *e
->operation
== TRUTH_ANDIF_EXPR
1489 || *e
->operation
== TRUTH_ORIF_EXPR
,
1491 && (*e
->operation
== COND_EXPR
1492 || *e
->operation
== VEC_COND_EXPR
));
1494 walk_result (s
->result
, false);
1496 for (unsigned i
= 0; i
< s
->ifexpr_vec
.length (); ++i
)
1497 if (s
->ifexpr_vec
[i
].is_with
)
1498 walk_c_expr (as_a
<c_expr
*>(s
->ifexpr_vec
[i
].cexpr
));
1501 /* Analyze captures in the match expression piece O. */
1504 capture_info::walk_match (operand
*o
, unsigned toplevel_arg
,
1505 bool conditional_p
, bool cond_expr_cond_p
)
1507 if (capture
*c
= dyn_cast
<capture
*> (o
))
1509 info
[c
->where
].toplevel_msk
|= 1 << toplevel_arg
;
1510 info
[c
->where
].force_no_side_effects_p
|= conditional_p
;
1511 info
[c
->where
].cond_expr_cond_p
|= cond_expr_cond_p
;
1512 /* Mark expr (non-leaf) captures and recurse. */
1514 && is_a
<expr
*> (c
->what
))
1516 info
[c
->where
].expr_p
= true;
1517 walk_match (c
->what
, toplevel_arg
, conditional_p
, false);
1520 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1522 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1524 bool cond_p
= conditional_p
;
1525 bool cond_expr_cond_p
= false;
1526 if (i
!= 0 && *e
->operation
== COND_EXPR
)
1528 else if (*e
->operation
== TRUTH_ANDIF_EXPR
1529 || *e
->operation
== TRUTH_ORIF_EXPR
)
1532 && (*e
->operation
== COND_EXPR
1533 || *e
->operation
== VEC_COND_EXPR
))
1534 cond_expr_cond_p
= true;
1535 walk_match (e
->ops
[i
], toplevel_arg
, cond_p
, cond_expr_cond_p
);
1538 else if (is_a
<predicate
*> (o
))
1540 /* Mark non-captured leafs toplevel arg for checking. */
1541 force_no_side_effects
|= 1 << toplevel_arg
;
1547 /* Analyze captures in the result expression piece O. */
1550 capture_info::walk_result (operand
*o
, bool conditional_p
)
1552 if (capture
*c
= dyn_cast
<capture
*> (o
))
1554 info
[c
->where
].result_use_count
++;
1555 /* If we substitute an expression capture we don't know
1556 which captures this will end up using (well, we don't
1557 compute that). Force the uses to be side-effect free
1558 which means forcing the toplevels that reach the
1559 expression side-effect free. */
1560 if (info
[c
->where
].expr_p
)
1561 force_no_side_effects
|= info
[c
->where
].toplevel_msk
;
1562 /* Mark CSE capture capture uses as forced to have
1565 && is_a
<expr
*> (c
->what
))
1567 info
[c
->where
].cse_p
= true;
1568 walk_result (c
->what
, true);
1571 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1573 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1575 bool cond_p
= conditional_p
;
1576 if (i
!= 0 && *e
->operation
== COND_EXPR
)
1578 else if (*e
->operation
== TRUTH_ANDIF_EXPR
1579 || *e
->operation
== TRUTH_ORIF_EXPR
)
1581 walk_result (e
->ops
[i
], cond_p
);
1584 else if (c_expr
*e
= dyn_cast
<c_expr
*> (o
))
1590 /* Look for captures in the C expr E. */
1593 capture_info::walk_c_expr (c_expr
*e
)
1595 /* Give up for C exprs mentioning captures not inside TREE_TYPE (). */
1596 unsigned p_depth
= 0;
1597 for (unsigned i
= 0; i
< e
->code
.length (); ++i
)
1599 const cpp_token
*t
= &e
->code
[i
];
1600 const cpp_token
*n
= i
< e
->code
.length () - 1 ? &e
->code
[i
+1] : NULL
;
1601 if (t
->type
== CPP_NAME
1602 && strcmp ((const char *)CPP_HASHNODE
1603 (t
->val
.node
.node
)->ident
.str
, "TREE_TYPE") == 0
1604 && n
->type
== CPP_OPEN_PAREN
)
1606 else if (t
->type
== CPP_CLOSE_PAREN
1609 else if (p_depth
== 0
1610 && t
->type
== CPP_ATSIGN
1611 && (n
->type
== CPP_NUMBER
1612 || n
->type
== CPP_NAME
)
1613 && !(n
->flags
& PREV_WHITE
))
1616 if (n
->type
== CPP_NUMBER
)
1617 id
= (const char *)n
->val
.str
.text
;
1619 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
1620 info
[*e
->capture_ids
->get(id
)].force_no_side_effects_p
= true;
1626 /* Code generation off the decision tree and the refered AST nodes. */
1629 is_conversion (id_base
*op
)
1631 return (*op
== CONVERT_EXPR
1633 || *op
== FLOAT_EXPR
1634 || *op
== FIX_TRUNC_EXPR
1635 || *op
== VIEW_CONVERT_EXPR
);
1638 /* Get the type to be used for generating operands of OP from the
1642 get_operand_type (id_base
*op
, const char *in_type
,
1643 const char *expr_type
,
1644 const char *other_oprnd_type
)
1646 /* Generally operands whose type does not match the type of the
1647 expression generated need to know their types but match and
1648 thus can fall back to 'other_oprnd_type'. */
1649 if (is_conversion (op
))
1650 return other_oprnd_type
;
1651 else if (*op
== REALPART_EXPR
1652 || *op
== IMAGPART_EXPR
)
1653 return other_oprnd_type
;
1654 else if (is_a
<operator_id
*> (op
)
1655 && strcmp (as_a
<operator_id
*> (op
)->tcc
, "tcc_comparison") == 0)
1656 return other_oprnd_type
;
1659 /* Otherwise all types should match - choose one in order of
1666 return other_oprnd_type
;
1670 /* Generate transform code for an expression. */
1673 expr::gen_transform (FILE *f
, const char *dest
, bool gimple
, int depth
,
1674 const char *in_type
, capture_info
*cinfo
,
1675 dt_operand
**indexes
, bool)
1677 bool conversion_p
= is_conversion (operation
);
1678 const char *type
= expr_type
;
1681 /* If there was a type specification in the pattern use it. */
1683 else if (conversion_p
)
1684 /* For conversions we need to build the expression using the
1685 outer type passed in. */
1687 else if (*operation
== REALPART_EXPR
1688 || *operation
== IMAGPART_EXPR
)
1690 /* __real and __imag use the component type of its operand. */
1691 sprintf (optype
, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth
);
1694 else if (is_a
<operator_id
*> (operation
)
1695 && !strcmp (as_a
<operator_id
*> (operation
)->tcc
, "tcc_comparison"))
1697 /* comparisons use boolean_type_node (or what gets in), but
1698 their operands need to figure out the types themselves. */
1699 sprintf (optype
, "boolean_type_node");
1702 else if (*operation
== COND_EXPR
1703 || *operation
== VEC_COND_EXPR
)
1705 /* Conditions are of the same type as their first alternative. */
1706 sprintf (optype
, "TREE_TYPE (ops%d[1])", depth
);
1711 /* Other operations are of the same type as their first operand. */
1712 sprintf (optype
, "TREE_TYPE (ops%d[0])", depth
);
1716 fatal ("two conversions in a row");
1719 fprintf (f
, " tree ops%d[%u], res;\n", depth
, ops
.length ());
1721 snprintf (op0type
, 64, "TREE_TYPE (ops%d[0])", depth
);
1722 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1725 snprintf (dest
, 32, " ops%d[%u]", depth
, i
);
1727 = get_operand_type (operation
, in_type
, expr_type
,
1728 i
== 0 ? NULL
: op0type
);
1729 ops
[i
]->gen_transform (f
, dest
, gimple
, depth
+ 1, optype
, cinfo
, indexes
,
1730 ((!(*operation
== COND_EXPR
)
1731 && !(*operation
== VEC_COND_EXPR
))
1736 if (*operation
== CONVERT_EXPR
)
1739 opr
= operation
->id
;
1743 if (*operation
== CONVERT_EXPR
)
1744 fprintf (f
, " if (%s != TREE_TYPE (ops%d[0])\n"
1745 " && !useless_type_conversion_p (%s, TREE_TYPE (ops%d[0])))\n"
1746 " {\n", type
, depth
, type
, depth
);
1747 /* ??? Building a stmt can fail for various reasons here, seq being
1748 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
1749 So if we fail here we should continue matching other patterns. */
1750 fprintf (f
, " code_helper tem_code = %s;\n"
1751 " tree tem_ops[3] = { ", opr
);
1752 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1753 fprintf (f
, "ops%d[%u]%s", depth
, i
,
1754 i
== ops
.length () - 1 ? " };\n" : ", ");
1755 fprintf (f
, " gimple_resimplify%d (seq, &tem_code, %s, tem_ops, valueize);\n",
1756 ops
.length (), type
);
1757 fprintf (f
, " res = maybe_push_res_to_seq (tem_code, %s, tem_ops, seq);\n"
1758 " if (!res) return false;\n", type
);
1759 if (*operation
== CONVERT_EXPR
)
1762 " res = ops%d[0];\n", depth
);
1766 if (*operation
== CONVERT_EXPR
)
1767 fprintf (f
, " if (TREE_TYPE (ops%d[0]) != %s)\n", depth
, type
);
1768 if (operation
->kind
== id_base::CODE
)
1769 fprintf (f
, " res = fold_build%d_loc (loc, %s, %s",
1770 ops
.length(), opr
, type
);
1772 fprintf (f
, " res = build_call_expr_loc (loc, "
1773 "builtin_decl_implicit (%s), %d", opr
, ops
.length());
1774 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1775 fprintf (f
, ", ops%d[%u]", depth
, i
);
1776 fprintf (f
, ");\n");
1777 if (*operation
== CONVERT_EXPR
)
1778 fprintf (f
, " else\n"
1779 " res = ops%d[0];\n", depth
);
1781 fprintf (f
, "%s = res;\n", dest
);
1785 /* Generate code for a c_expr which is either the expression inside
1786 an if statement or a sequence of statements which computes a
1787 result to be stored to DEST. */
1790 c_expr::gen_transform (FILE *f
, const char *dest
,
1791 bool, int, const char *, capture_info
*,
1792 dt_operand
**, bool)
1794 if (dest
&& nr_stmts
== 1)
1795 fprintf (f
, "%s = ", dest
);
1797 unsigned stmt_nr
= 1;
1798 for (unsigned i
= 0; i
< code
.length (); ++i
)
1800 const cpp_token
*token
= &code
[i
];
1802 /* Replace captures for code-gen. */
1803 if (token
->type
== CPP_ATSIGN
)
1805 const cpp_token
*n
= &code
[i
+1];
1806 if ((n
->type
== CPP_NUMBER
1807 || n
->type
== CPP_NAME
)
1808 && !(n
->flags
& PREV_WHITE
))
1810 if (token
->flags
& PREV_WHITE
)
1813 if (n
->type
== CPP_NUMBER
)
1814 id
= (const char *)n
->val
.str
.text
;
1816 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
1817 fprintf (f
, "captures[%u]", *capture_ids
->get(id
));
1823 if (token
->flags
& PREV_WHITE
)
1826 if (token
->type
== CPP_NAME
)
1828 const char *id
= (const char *) NODE_NAME (token
->val
.node
.node
);
1830 for (j
= 0; j
< ids
.length (); ++j
)
1832 if (strcmp (id
, ids
[j
].id
) == 0)
1834 fprintf (f
, "%s", ids
[j
].oper
);
1838 if (j
< ids
.length ())
1842 /* Output the token as string. */
1843 char *tk
= (char *)cpp_token_as_text (r
, token
);
1846 if (token
->type
== CPP_SEMICOLON
)
1849 if (dest
&& stmt_nr
== nr_stmts
)
1850 fprintf (f
, "\n %s = ", dest
);
1857 /* Generate transform code for a capture. */
1860 capture::gen_transform (FILE *f
, const char *dest
, bool gimple
, int depth
,
1861 const char *in_type
, capture_info
*cinfo
,
1862 dt_operand
**indexes
, bool expand_compares
)
1864 if (what
&& is_a
<expr
*> (what
))
1866 if (indexes
[where
] == 0)
1869 sprintf (buf
, "captures[%u]", where
);
1870 what
->gen_transform (f
, buf
, gimple
, depth
, in_type
, cinfo
, NULL
);
1874 fprintf (f
, "%s = captures[%u];\n", dest
, where
);
1876 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
1877 with substituting a capture of that.
1878 ??? Returning false here will also not allow any other patterns
1880 if (gimple
&& expand_compares
1881 && cinfo
->info
[where
].cond_expr_cond_p
)
1882 fprintf (f
, "if (COMPARISON_CLASS_P (%s))\n"
1884 " if (!seq) return false;\n"
1885 " %s = gimple_build (seq, TREE_CODE (%s),"
1886 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
1887 " TREE_OPERAND (%s, 1));\n"
1888 " }\n", dest
, dest
, dest
, dest
, dest
, dest
);
1891 /* Return the name of the operand representing the decision tree node.
1892 Use NAME as space to generate it. */
1895 dt_operand::get_name (char *name
)
1898 sprintf (name
, "t");
1899 else if (parent
->level
== 1)
1900 sprintf (name
, "op%u", pos
);
1901 else if (parent
->type
== dt_node::DT_MATCH
)
1902 return parent
->get_name (name
);
1904 sprintf (name
, "o%u%u", parent
->level
, pos
);
1908 /* Fill NAME with the operand name at position POS. */
1911 dt_operand::gen_opname (char *name
, unsigned pos
)
1914 sprintf (name
, "op%u", pos
);
1916 sprintf (name
, "o%u%u", level
, pos
);
1919 /* Generate matching code for the decision tree operand which is
1923 dt_operand::gen_predicate (FILE *f
, const char *opname
, bool gimple
)
1925 predicate
*p
= as_a
<predicate
*> (op
);
1927 if (p
->p
->matchers
.exists ())
1929 /* If this is a predicate generated from a pattern mangle its
1930 name and pass on the valueize hook. */
1932 fprintf (f
, "if (gimple_%s (%s, valueize))\n", p
->p
->id
, opname
);
1934 fprintf (f
, "if (tree_%s (%s))\n", p
->p
->id
, opname
);
1937 fprintf (f
, "if (%s (%s))\n", p
->p
->id
, opname
);
1942 /* Generate matching code for the decision tree operand which is
1946 dt_operand::gen_match_op (FILE *f
, const char *opname
)
1948 char match_opname
[20];
1949 match_dop
->get_name (match_opname
);
1950 fprintf (f
, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
1951 opname
, match_opname
, opname
, match_opname
);
1956 /* Generate GIMPLE matching code for the decision tree operand. */
1959 dt_operand::gen_gimple_expr (FILE *f
)
1961 expr
*e
= static_cast<expr
*> (op
);
1962 id_base
*id
= e
->operation
;
1963 unsigned n_ops
= e
->ops
.length ();
1965 for (unsigned i
= 0; i
< n_ops
; ++i
)
1967 char child_opname
[20];
1968 gen_opname (child_opname
, i
);
1970 if (id
->kind
== id_base::CODE
)
1973 || *id
== REALPART_EXPR
|| *id
== IMAGPART_EXPR
1974 || *id
== BIT_FIELD_REF
|| *id
== VIEW_CONVERT_EXPR
)
1976 /* ??? If this is a memory operation we can't (and should not)
1977 match this. The only sensible operand types are
1978 SSA names and invariants. */
1979 fprintf (f
, "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), %i);\n",
1981 fprintf (f
, "if ((TREE_CODE (%s) == SSA_NAME\n"
1982 "|| is_gimple_min_invariant (%s))\n"
1983 "&& (%s = do_valueize (valueize, %s)))\n"
1984 "{\n", child_opname
, child_opname
, child_opname
,
1989 fprintf (f
, "tree %s = gimple_assign_rhs%u (def_stmt);\n",
1990 child_opname
, i
+ 1);
1993 fprintf (f
, "tree %s = gimple_call_arg (def_stmt, %u);\n",
1995 fprintf (f
, "if ((%s = do_valueize (valueize, %s)))\n",
1996 child_opname
, child_opname
);
2003 /* Generate GENERIC matching code for the decision tree operand. */
2006 dt_operand::gen_generic_expr (FILE *f
, const char *opname
)
2008 expr
*e
= static_cast<expr
*> (op
);
2009 unsigned n_ops
= e
->ops
.length ();
2011 for (unsigned i
= 0; i
< n_ops
; ++i
)
2013 char child_opname
[20];
2014 gen_opname (child_opname
, i
);
2016 if (e
->operation
->kind
== id_base::CODE
)
2017 fprintf (f
, "tree %s = TREE_OPERAND (%s, %u);\n",
2018 child_opname
, opname
, i
);
2020 fprintf (f
, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2021 child_opname
, opname
, i
);
2027 /* Generate matching code for the children of the decision tree node. */
2030 dt_node::gen_kids (FILE *f
, bool gimple
)
2032 auto_vec
<dt_operand
*> gimple_exprs
;
2033 auto_vec
<dt_operand
*> generic_exprs
;
2034 auto_vec
<dt_operand
*> fns
;
2035 auto_vec
<dt_operand
*> generic_fns
;
2036 auto_vec
<dt_operand
*> preds
;
2037 auto_vec
<dt_node
*> others
;
2039 for (unsigned i
= 0; i
< kids
.length (); ++i
)
2041 if (kids
[i
]->type
== dt_node::DT_OPERAND
)
2043 dt_operand
*op
= as_a
<dt_operand
*> (kids
[i
]);
2044 if (expr
*e
= dyn_cast
<expr
*> (op
->op
))
2046 if (e
->ops
.length () == 0
2047 && (!gimple
|| !(*e
->operation
== CONSTRUCTOR
)))
2048 generic_exprs
.safe_push (op
);
2049 else if (e
->operation
->kind
== id_base::FN
)
2054 generic_fns
.safe_push (op
);
2056 else if (e
->operation
->kind
== id_base::PREDICATE
)
2057 preds
.safe_push (op
);
2061 gimple_exprs
.safe_push (op
);
2063 generic_exprs
.safe_push (op
);
2066 else if (op
->op
->type
== operand::OP_PREDICATE
)
2067 others
.safe_push (kids
[i
]);
2071 else if (kids
[i
]->type
== dt_node::DT_MATCH
2072 || kids
[i
]->type
== dt_node::DT_SIMPLIFY
)
2073 others
.safe_push (kids
[i
]);
2074 else if (kids
[i
]->type
== dt_node::DT_TRUE
)
2076 /* A DT_TRUE operand serves as a barrier - generate code now
2077 for what we have collected sofar. */
2078 gen_kids_1 (f
, gimple
, gimple_exprs
, generic_exprs
,
2079 fns
, generic_fns
, preds
, others
);
2080 /* And output the true operand itself. */
2081 kids
[i
]->gen (f
, gimple
);
2082 gimple_exprs
.truncate (0);
2083 generic_exprs
.truncate (0);
2085 generic_fns
.truncate (0);
2087 others
.truncate (0);
2093 /* Generate code for the remains. */
2094 gen_kids_1 (f
, gimple
, gimple_exprs
, generic_exprs
,
2095 fns
, generic_fns
, preds
, others
);
2098 /* Generate matching code for the children of the decision tree node. */
2101 dt_node::gen_kids_1 (FILE *f
, bool gimple
,
2102 vec
<dt_operand
*> gimple_exprs
,
2103 vec
<dt_operand
*> generic_exprs
,
2104 vec
<dt_operand
*> fns
,
2105 vec
<dt_operand
*> generic_fns
,
2106 vec
<dt_operand
*> preds
,
2107 vec
<dt_node
*> others
)
2110 char *kid_opname
= buf
;
2112 unsigned exprs_len
= gimple_exprs
.length ();
2113 unsigned gexprs_len
= generic_exprs
.length ();
2114 unsigned fns_len
= fns
.length ();
2115 unsigned gfns_len
= generic_fns
.length ();
2117 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2120 gimple_exprs
[0]->get_name (kid_opname
);
2122 fns
[0]->get_name (kid_opname
);
2124 generic_fns
[0]->get_name (kid_opname
);
2126 generic_exprs
[0]->get_name (kid_opname
);
2128 fprintf (f
, "switch (TREE_CODE (%s))\n"
2132 if (exprs_len
|| fns_len
)
2134 fprintf (f
, "case SSA_NAME:\n");
2135 fprintf (f
, "if (do_valueize (valueize, %s) != NULL_TREE)\n", kid_opname
);
2137 fprintf (f
, "gimple def_stmt = SSA_NAME_DEF_STMT (%s);\n", kid_opname
);
2141 fprintf (f
, "if (is_gimple_assign (def_stmt))\n");
2142 fprintf (f
, "switch (gimple_assign_rhs_code (def_stmt))\n"
2144 for (unsigned i
= 0; i
< exprs_len
; ++i
)
2146 expr
*e
= as_a
<expr
*> (gimple_exprs
[i
]->op
);
2147 id_base
*op
= e
->operation
;
2148 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2149 fprintf (f
, "CASE_CONVERT:\n");
2151 fprintf (f
, "case %s:\n", op
->id
);
2153 gimple_exprs
[i
]->gen (f
, true);
2154 fprintf (f
, "break;\n"
2157 fprintf (f
, "default:;\n"
2164 fprintf (f
, "else ");
2166 fprintf (f
, "if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL))\n"
2168 "tree fndecl = gimple_call_fndecl (def_stmt);\n"
2169 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2172 for (unsigned i
= 0; i
< fns_len
; ++i
)
2174 expr
*e
= as_a
<expr
*>(fns
[i
]->op
);
2175 fprintf (f
, "case %s:\n"
2176 "{\n", e
->operation
->id
);
2177 fns
[i
]->gen (f
, true);
2178 fprintf (f
, "break;\n"
2182 fprintf (f
, "default:;\n"
2191 for (unsigned i
= 0; i
< generic_exprs
.length (); ++i
)
2193 expr
*e
= as_a
<expr
*>(generic_exprs
[i
]->op
);
2194 id_base
*op
= e
->operation
;
2195 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2196 fprintf (f
, "CASE_CONVERT:\n");
2198 fprintf (f
, "case %s:\n", op
->id
);
2200 generic_exprs
[i
]->gen (f
, gimple
);
2201 fprintf (f
, "break;\n"
2207 fprintf (f
, "case CALL_EXPR:\n"
2209 "tree fndecl = get_callee_fndecl (%s);\n"
2210 "if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)\n"
2211 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2214 for (unsigned j
= 0; j
< generic_fns
.length (); ++j
)
2216 expr
*e
= as_a
<expr
*>(generic_fns
[j
]->op
);
2217 gcc_assert (e
->operation
->kind
== id_base::FN
);
2219 fprintf (f
, "case %s:\n"
2220 "{\n", e
->operation
->id
);
2221 generic_fns
[j
]->gen (f
, false);
2222 fprintf (f
, "break;\n"
2226 fprintf (f
, "default:;\n"
2232 /* Close switch (TREE_CODE ()). */
2233 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2234 fprintf (f
, "default:;\n"
2237 for (unsigned i
= 0; i
< preds
.length (); ++i
)
2239 expr
*e
= as_a
<expr
*> (preds
[i
]->op
);
2240 predicate_id
*p
= as_a
<predicate_id
*> (e
->operation
);
2241 preds
[i
]->get_name (kid_opname
);
2242 fprintf (f
, "tree %s_pops[%d];\n", kid_opname
, p
->nargs
);
2243 fprintf (f
, "if (%s_%s (%s, %s_pops%s))\n",
2244 gimple
? "gimple" : "tree",
2245 p
->id
, kid_opname
, kid_opname
,
2246 gimple
? ", valueize" : "");
2248 for (int j
= 0; j
< p
->nargs
; ++j
)
2250 char child_opname
[20];
2251 preds
[i
]->gen_opname (child_opname
, j
);
2252 fprintf (f
, "tree %s = %s_pops[%d];\n", child_opname
, kid_opname
, j
);
2254 preds
[i
]->gen_kids (f
, gimple
);
2258 for (unsigned i
= 0; i
< others
.length (); ++i
)
2259 others
[i
]->gen (f
, gimple
);
2262 /* Generate matching code for the decision tree operand. */
2265 dt_operand::gen (FILE *f
, bool gimple
)
2270 unsigned n_braces
= 0;
2272 if (type
== DT_OPERAND
)
2275 case operand::OP_PREDICATE
:
2276 n_braces
= gen_predicate (f
, opname
, gimple
);
2279 case operand::OP_EXPR
:
2281 n_braces
= gen_gimple_expr (f
);
2283 n_braces
= gen_generic_expr (f
, opname
);
2289 else if (type
== DT_TRUE
)
2291 else if (type
== DT_MATCH
)
2292 n_braces
= gen_match_op (f
, opname
);
2296 gen_kids (f
, gimple
);
2298 for (unsigned i
= 0; i
< n_braces
; ++i
)
2304 /* Generate code for the '(if ...)', '(with ..)' and actual transform
2305 step of a '(simplify ...)' or '(match ...)'. This handles everything
2306 that is not part of the decision tree (simplify->match). */
2309 dt_simplify::gen (FILE *f
, bool gimple
)
2312 output_line_directive (f
, s
->result_location
);
2313 if (s
->capture_max
>= 0)
2314 fprintf (f
, "tree captures[%u] ATTRIBUTE_UNUSED = {};\n",
2315 s
->capture_max
+ 1);
2317 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2321 fprintf (f
, "captures[%u] = %s;\n", i
, indexes
[i
]->get_name (opname
));
2324 unsigned n_braces
= 0;
2325 if (s
->ifexpr_vec
!= vNULL
)
2327 for (unsigned i
= 0; i
< s
->ifexpr_vec
.length (); ++i
)
2329 if_or_with
&w
= s
->ifexpr_vec
[i
];
2333 output_line_directive (f
, w
.location
);
2334 w
.cexpr
->gen_transform (f
, NULL
, true, 1, "type", NULL
);
2339 output_line_directive (f
, w
.location
);
2340 fprintf (f
, "if (");
2341 if (i
== s
->ifexpr_vec
.length () - 1
2342 || s
->ifexpr_vec
[i
+1].is_with
)
2343 w
.cexpr
->gen_transform (f
, NULL
, true, 1, "type", NULL
);
2352 output_line_directive (f
, s
->ifexpr_vec
[j
].location
);
2356 s
->ifexpr_vec
[j
].cexpr
->gen_transform (f
, NULL
,
2362 while (j
< s
->ifexpr_vec
.length ()
2363 && !s
->ifexpr_vec
[j
].is_with
);
2373 /* Analyze captures and perform early-outs on the incoming arguments
2374 that cover cases we cannot handle. */
2375 capture_info
cinfo (s
);
2379 && !((e
= dyn_cast
<expr
*> (s
->result
))
2380 && is_a
<predicate_id
*> (e
->operation
)))
2382 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
2383 if (cinfo
.force_no_side_effects
& (1 << i
))
2384 fprintf (f
, "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n", i
);
2385 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2386 if (cinfo
.info
[i
].cse_p
)
2388 else if (cinfo
.info
[i
].force_no_side_effects_p
2389 && (cinfo
.info
[i
].toplevel_msk
2390 & cinfo
.force_no_side_effects
) == 0)
2391 fprintf (f
, "if (TREE_SIDE_EFFECTS (captures[%d])) "
2392 "return NULL_TREE;\n", i
);
2393 else if ((cinfo
.info
[i
].toplevel_msk
2394 & cinfo
.force_no_side_effects
) != 0)
2395 /* Mark capture as having no side-effects if we had to verify
2396 that via forced toplevel operand checks. */
2397 cinfo
.info
[i
].force_no_side_effects_p
= true;
2400 fprintf (f
, "if (dump_file && (dump_flags & TDF_DETAILS)) "
2401 "fprintf (dump_file, \"Applying pattern ");
2402 output_line_directive (f
, s
->result_location
, true);
2403 fprintf (f
, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
2405 operand
*result
= s
->result
;
2408 /* If there is no result then this is a predicate implementation. */
2409 fprintf (f
, "return true;\n");
2413 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
2414 in outermost position). */
2415 if (result
->type
== operand::OP_EXPR
2416 && *as_a
<expr
*> (result
)->operation
== NON_LVALUE_EXPR
)
2417 result
= as_a
<expr
*> (result
)->ops
[0];
2418 if (result
->type
== operand::OP_EXPR
)
2420 expr
*e
= as_a
<expr
*> (result
);
2421 bool is_predicate
= is_a
<predicate_id
*> (e
->operation
);
2423 fprintf (f
, "*res_code = %s;\n",
2424 *e
->operation
== CONVERT_EXPR
2425 ? "NOP_EXPR" : e
->operation
->id
);
2426 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2429 snprintf (dest
, 32, " res_ops[%d]", j
);
2431 = get_operand_type (e
->operation
,
2432 "type", e
->expr_type
,
2434 ? NULL
: "TREE_TYPE (res_ops[0])");
2435 /* We need to expand GENERIC conditions we captured from
2437 bool expand_generic_cond_exprs_p
2439 /* But avoid doing that if the GENERIC condition is
2440 valid - which it is in the first operand of COND_EXPRs
2441 and VEC_COND_EXRPs. */
2442 && ((!(*e
->operation
== COND_EXPR
)
2443 && !(*e
->operation
== VEC_COND_EXPR
))
2445 e
->ops
[j
]->gen_transform (f
, dest
, true, 1, optype
, &cinfo
,
2446 indexes
, expand_generic_cond_exprs_p
);
2449 /* Re-fold the toplevel result. It's basically an embedded
2450 gimple_build w/o actually building the stmt. */
2452 fprintf (f
, "gimple_resimplify%d (seq, res_code, type, "
2453 "res_ops, valueize);\n", e
->ops
.length ());
2455 else if (result
->type
== operand::OP_CAPTURE
2456 || result
->type
== operand::OP_C_EXPR
)
2458 result
->gen_transform (f
, "res_ops[0]", true, 1, "type",
2459 &cinfo
, indexes
, false);
2460 fprintf (f
, "*res_code = TREE_CODE (res_ops[0]);\n");
2461 if (is_a
<capture
*> (result
)
2462 && cinfo
.info
[as_a
<capture
*> (result
)->where
].cond_expr_cond_p
)
2464 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2465 with substituting a capture of that. */
2466 fprintf (f
, "if (COMPARISON_CLASS_P (res_ops[0]))\n"
2468 " tree tem = res_ops[0];\n"
2469 " res_ops[0] = TREE_OPERAND (tem, 0);\n"
2470 " res_ops[1] = TREE_OPERAND (tem, 1);\n"
2476 fprintf (f
, "return true;\n");
2480 bool is_predicate
= false;
2481 if (result
->type
== operand::OP_EXPR
)
2483 expr
*e
= as_a
<expr
*> (result
);
2484 is_predicate
= is_a
<predicate_id
*> (e
->operation
);
2485 /* Search for captures used multiple times in the result expression
2486 and dependent on TREE_SIDE_EFFECTS emit a SAVE_EXPR. */
2488 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
2490 if (!cinfo
.info
[i
].force_no_side_effects_p
2491 && cinfo
.info
[i
].result_use_count
> 1)
2492 fprintf (f
, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2493 " captures[%d] = save_expr (captures[%d]);\n",
2496 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2500 snprintf (dest
, 32, "res_ops[%d]", j
);
2503 fprintf (f
, " tree res_op%d;\n", j
);
2504 snprintf (dest
, 32, " res_op%d", j
);
2507 = get_operand_type (e
->operation
,
2508 "type", e
->expr_type
,
2510 ? NULL
: "TREE_TYPE (res_op0)");
2511 e
->ops
[j
]->gen_transform (f
, dest
, false, 1, optype
,
2515 fprintf (f
, "return true;\n");
2518 fprintf (f
, " tree res;\n");
2519 /* Re-fold the toplevel result. Use non_lvalue to
2520 build NON_LVALUE_EXPRs so they get properly
2521 ignored when in GIMPLE form. */
2522 if (*e
->operation
== NON_LVALUE_EXPR
)
2523 fprintf (f
, " res = non_lvalue_loc (loc, res_op0);\n");
2526 if (e
->operation
->kind
== id_base::CODE
)
2527 fprintf (f
, " res = fold_build%d_loc (loc, %s, type",
2529 *e
->operation
== CONVERT_EXPR
2530 ? "NOP_EXPR" : e
->operation
->id
);
2532 fprintf (f
, " res = build_call_expr_loc "
2533 "(loc, builtin_decl_implicit (%s), %d",
2534 e
->operation
->id
, e
->ops
.length());
2535 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2536 fprintf (f
, ", res_op%d", j
);
2537 fprintf (f
, ");\n");
2541 else if (result
->type
== operand::OP_CAPTURE
2542 || result
->type
== operand::OP_C_EXPR
)
2545 fprintf (f
, " tree res;\n");
2546 s
->result
->gen_transform (f
, " res", false, 1, "type",
2553 /* Search for captures not used in the result expression and dependent
2554 on TREE_SIDE_EFFECTS emit omit_one_operand. */
2555 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
2557 if (!cinfo
.info
[i
].force_no_side_effects_p
2558 && !cinfo
.info
[i
].expr_p
2559 && cinfo
.info
[i
].result_use_count
== 0)
2560 fprintf (f
, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2561 " res = build2_loc (loc, COMPOUND_EXPR, type,"
2562 " fold_ignored_result (captures[%d]), res);\n",
2565 fprintf (f
, " return res;\n");
2569 for (unsigned i
= 0; i
< n_braces
; ++i
)
2575 /* Main entry to generate code for matching GIMPLE IL off the decision
2579 decision_tree::gen_gimple (FILE *f
)
2581 for (unsigned n
= 1; n
<= 3; ++n
)
2583 fprintf (f
, "\nstatic bool\n"
2584 "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
2585 " gimple_seq *seq, tree (*valueize)(tree),\n"
2586 " code_helper code, tree type");
2587 for (unsigned i
= 0; i
< n
; ++i
)
2588 fprintf (f
, ", tree op%d", i
);
2592 fprintf (f
, "switch (code.get_rep())\n"
2594 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
2596 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
2597 expr
*e
= static_cast<expr
*>(dop
->op
);
2598 if (e
->ops
.length () != n
)
2601 if (*e
->operation
== CONVERT_EXPR
2602 || *e
->operation
== NOP_EXPR
)
2603 fprintf (f
, "CASE_CONVERT:\n");
2605 fprintf (f
, "case %s%s:\n",
2606 is_a
<fn_id
*> (e
->operation
) ? "-" : "",
2609 dop
->gen_kids (f
, true);
2610 fprintf (f
, "break;\n");
2613 fprintf (f
, "default:;\n"
2616 fprintf (f
, "return false;\n");
2621 /* Main entry to generate code for matching GENERIC IL off the decision
2625 decision_tree::gen_generic (FILE *f
)
2627 for (unsigned n
= 1; n
<= 3; ++n
)
2629 fprintf (f
, "\ntree\n"
2630 "generic_simplify (location_t loc, enum tree_code code, "
2631 "tree type ATTRIBUTE_UNUSED");
2632 for (unsigned i
= 0; i
< n
; ++i
)
2633 fprintf (f
, ", tree op%d", i
);
2637 fprintf (f
, "switch (code)\n"
2639 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
2641 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
2642 expr
*e
= static_cast<expr
*>(dop
->op
);
2643 if (e
->ops
.length () != n
2644 /* Builtin simplifications are somewhat premature on
2645 GENERIC. The following drops patterns with outermost
2646 calls. It's easy to emit overloads for function code
2647 though if necessary. */
2648 || e
->operation
->kind
!= id_base::CODE
)
2651 operator_id
*op_id
= static_cast <operator_id
*> (e
->operation
);
2652 if (op_id
->code
== NOP_EXPR
|| op_id
->code
== CONVERT_EXPR
)
2653 fprintf (f
, "CASE_CONVERT:\n");
2655 fprintf (f
, "case %s:\n", e
->operation
->id
);
2657 dop
->gen_kids (f
, false);
2658 fprintf (f
, "break;\n"
2661 fprintf (f
, "default:;\n"
2664 fprintf (f
, "return NULL_TREE;\n");
2669 /* Output code to implement the predicate P from the decision tree DT. */
2672 write_predicate (FILE *f
, predicate_id
*p
, decision_tree
&dt
, bool gimple
)
2674 fprintf (f
, "\nbool\n"
2675 "%s%s (tree t%s%s)\n"
2676 "{\n", gimple
? "gimple_" : "tree_", p
->id
,
2677 p
->nargs
> 0 ? ", tree *res_ops" : "",
2678 gimple
? ", tree (*valueize)(tree)" : "");
2679 /* Conveniently make 'type' available. */
2680 fprintf (f
, "tree type = TREE_TYPE (t);\n");
2683 fprintf (f
, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
2684 dt
.root
->gen_kids (f
, gimple
);
2686 fprintf (f
, "return false;\n"
2690 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
2693 write_header (FILE *f
, const char *head
)
2695 fprintf (f
, "/* Generated automatically by the program `genmatch' from\n");
2696 fprintf (f
, " a IL pattern matching and simplification description. */\n");
2698 /* Include the header instead of writing it awkwardly quoted here. */
2699 fprintf (f
, "\n#include \"%s\"\n", head
);
2709 parser (cpp_reader
*);
2712 const cpp_token
*next ();
2713 const cpp_token
*peek ();
2714 const cpp_token
*peek_ident (const char * = NULL
);
2715 const cpp_token
*expect (enum cpp_ttype
);
2716 void eat_token (enum cpp_ttype
);
2717 const char *get_string ();
2718 const char *get_ident ();
2719 void eat_ident (const char *);
2720 const char *get_number ();
2722 id_base
*parse_operation ();
2723 operand
*parse_capture (operand
*);
2724 operand
*parse_expr ();
2725 c_expr
*parse_c_expr (cpp_ttype
);
2726 operand
*parse_op ();
2728 void record_operlist (source_location
, user_id
*);
2730 void parse_pattern ();
2731 void push_simplify (vec
<simplify
*>&, operand
*, source_location
,
2732 operand
*, source_location
);
2733 void parse_simplify (source_location
, vec
<simplify
*>&, predicate_id
*,
2735 void parse_for (source_location
);
2736 void parse_if (source_location
);
2737 void parse_predicates (source_location
);
2738 void parse_operator_list (source_location
);
2741 vec
<if_or_with
> active_ifs
;
2742 vec
<vec
<user_id
*> > active_fors
;
2743 hash_set
<user_id
*> *oper_lists_set
;
2744 vec
<user_id
*> oper_lists
;
2746 cid_map_t
*capture_ids
;
2749 vec
<simplify
*> simplifiers
;
2750 vec
<predicate_id
*> user_predicates
;
2751 bool parsing_match_operand
;
2754 /* Lexing helpers. */
2756 /* Read the next non-whitespace token from R. */
2761 const cpp_token
*token
;
2764 token
= cpp_get_token (r
);
2766 while (token
->type
== CPP_PADDING
2767 && token
->type
!= CPP_EOF
);
2771 /* Peek at the next non-whitespace token from R. */
2776 const cpp_token
*token
;
2780 token
= cpp_peek_token (r
, i
++);
2782 while (token
->type
== CPP_PADDING
2783 && token
->type
!= CPP_EOF
);
2784 /* If we peek at EOF this is a fatal error as it leaves the
2785 cpp_reader in unusable state. Assume we really wanted a
2786 token and thus this EOF is unexpected. */
2787 if (token
->type
== CPP_EOF
)
2788 fatal_at (token
, "unexpected end of file");
2792 /* Peek at the next identifier token (or return NULL if the next
2793 token is not an identifier or equal to ID if supplied). */
2796 parser::peek_ident (const char *id
)
2798 const cpp_token
*token
= peek ();
2799 if (token
->type
!= CPP_NAME
)
2805 const char *t
= (const char *) CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
2806 if (strcmp (id
, t
) == 0)
2812 /* Read the next token from R and assert it is of type TK. */
2815 parser::expect (enum cpp_ttype tk
)
2817 const cpp_token
*token
= next ();
2818 if (token
->type
!= tk
)
2819 fatal_at (token
, "expected %s, got %s",
2820 cpp_type2name (tk
, 0), cpp_type2name (token
->type
, 0));
2825 /* Consume the next token from R and assert it is of type TK. */
2828 parser::eat_token (enum cpp_ttype tk
)
2833 /* Read the next token from R and assert it is of type CPP_STRING and
2834 return its value. */
2837 parser::get_string ()
2839 const cpp_token
*token
= expect (CPP_STRING
);
2840 return (const char *)token
->val
.str
.text
;
2843 /* Read the next token from R and assert it is of type CPP_NAME and
2844 return its value. */
2847 parser::get_ident ()
2849 const cpp_token
*token
= expect (CPP_NAME
);
2850 return (const char *)CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
2853 /* Eat an identifier token with value S from R. */
2856 parser::eat_ident (const char *s
)
2858 const cpp_token
*token
= peek ();
2859 const char *t
= get_ident ();
2860 if (strcmp (s
, t
) != 0)
2861 fatal_at (token
, "expected '%s' got '%s'\n", s
, t
);
2864 /* Read the next token from R and assert it is of type CPP_NUMBER and
2865 return its value. */
2868 parser::get_number ()
2870 const cpp_token
*token
= expect (CPP_NUMBER
);
2871 return (const char *)token
->val
.str
.text
;
2875 /* Record an operator-list use for transparent for handling. */
2878 parser::record_operlist (source_location loc
, user_id
*p
)
2880 if (!oper_lists_set
->add (p
))
2882 if (!oper_lists
.is_empty ()
2883 && oper_lists
[0]->substitutes
.length () != p
->substitutes
.length ())
2884 fatal_at (loc
, "User-defined operator list does not have the "
2885 "same number of entries as others used in the pattern");
2886 oper_lists
.safe_push (p
);
2890 /* Parse the operator ID, special-casing convert?, convert1? and
2894 parser::parse_operation ()
2896 const cpp_token
*id_tok
= peek ();
2897 const char *id
= get_ident ();
2898 const cpp_token
*token
= peek ();
2899 if (strcmp (id
, "convert0") == 0)
2900 fatal_at (id_tok
, "use 'convert?' here");
2901 else if (strcmp (id
, "view_convert0") == 0)
2902 fatal_at (id_tok
, "use 'view_convert?' here");
2903 if (token
->type
== CPP_QUERY
2904 && !(token
->flags
& PREV_WHITE
))
2906 if (strcmp (id
, "convert") == 0)
2908 else if (strcmp (id
, "convert1") == 0)
2910 else if (strcmp (id
, "convert2") == 0)
2912 else if (strcmp (id
, "view_convert") == 0)
2913 id
= "view_convert0";
2914 else if (strcmp (id
, "view_convert1") == 0)
2916 else if (strcmp (id
, "view_convert2") == 0)
2919 fatal_at (id_tok
, "non-convert operator conditionalized");
2921 if (!parsing_match_operand
)
2922 fatal_at (id_tok
, "conditional convert can only be used in "
2923 "match expression");
2924 eat_token (CPP_QUERY
);
2926 else if (strcmp (id
, "convert1") == 0
2927 || strcmp (id
, "convert2") == 0
2928 || strcmp (id
, "view_convert1") == 0
2929 || strcmp (id
, "view_convert2") == 0)
2930 fatal_at (id_tok
, "expected '?' after conditional operator");
2931 id_base
*op
= get_operator (id
);
2933 fatal_at (id_tok
, "unknown operator %s", id
);
2935 user_id
*p
= dyn_cast
<user_id
*> (op
);
2936 if (p
&& p
->is_oper_list
)
2938 if (active_fors
.length() == 0)
2939 record_operlist (id_tok
->src_loc
, p
);
2941 fatal_at (id_tok
, "operator-list %s cannot be exapnded inside 'for'", id
);
2947 capture = '@'<number> */
2950 parser::parse_capture (operand
*op
)
2952 eat_token (CPP_ATSIGN
);
2953 const cpp_token
*token
= peek ();
2954 const char *id
= NULL
;
2955 if (token
->type
== CPP_NUMBER
)
2957 else if (token
->type
== CPP_NAME
)
2960 fatal_at (token
, "expected number or identifier");
2961 unsigned next_id
= capture_ids
->elements ();
2963 unsigned &num
= capture_ids
->get_or_insert (id
, &existed
);
2966 return new capture (num
, op
);
2969 /* Parse an expression
2970 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
2973 parser::parse_expr ()
2975 expr
*e
= new expr (parse_operation ());
2976 const cpp_token
*token
= peek ();
2978 bool is_commutative
= false;
2979 const char *expr_type
= NULL
;
2981 if (token
->type
== CPP_COLON
2982 && !(token
->flags
& PREV_WHITE
))
2984 eat_token (CPP_COLON
);
2986 if (token
->type
== CPP_NAME
2987 && !(token
->flags
& PREV_WHITE
))
2989 const char *s
= get_ident ();
2990 if (s
[0] == 'c' && !s
[1])
2992 if (!parsing_match_operand
)
2994 "flag 'c' can only be used in match expression");
2995 is_commutative
= true;
2997 else if (s
[1] != '\0')
2999 if (parsing_match_operand
)
3000 fatal_at (token
, "type can only be used in result expression");
3004 fatal_at (token
, "flag %s not recognized", s
);
3009 fatal_at (token
, "expected flag or type specifying identifier");
3012 if (token
->type
== CPP_ATSIGN
3013 && !(token
->flags
& PREV_WHITE
))
3014 op
= parse_capture (e
);
3019 const cpp_token
*token
= peek ();
3020 if (token
->type
== CPP_CLOSE_PAREN
)
3022 if (e
->operation
->nargs
!= -1
3023 && e
->operation
->nargs
!= (int) e
->ops
.length ())
3024 fatal_at (token
, "'%s' expects %u operands, not %u",
3025 e
->operation
->id
, e
->operation
->nargs
, e
->ops
.length ());
3028 if (e
->ops
.length () == 2)
3029 e
->is_commutative
= true;
3031 fatal_at (token
, "only binary operators or function with "
3032 "two arguments can be marked commutative");
3034 e
->expr_type
= expr_type
;
3037 e
->append_op (parse_op ());
3042 /* Lex native C code delimited by START recording the preprocessing tokens
3043 for later processing.
3044 c_expr = ('{'|'(') <pp token>... ('}'|')') */
3047 parser::parse_c_expr (cpp_ttype start
)
3049 const cpp_token
*token
;
3052 vec
<cpp_token
> code
= vNULL
;
3053 unsigned nr_stmts
= 0;
3055 if (start
== CPP_OPEN_PAREN
)
3056 end
= CPP_CLOSE_PAREN
;
3057 else if (start
== CPP_OPEN_BRACE
)
3058 end
= CPP_CLOSE_BRACE
;
3066 /* Count brace pairs to find the end of the expr to match. */
3067 if (token
->type
== start
)
3069 else if (token
->type
== end
3073 /* This is a lame way of counting the number of statements. */
3074 if (token
->type
== CPP_SEMICOLON
)
3077 /* If this is possibly a user-defined identifier mark it used. */
3078 if (token
->type
== CPP_NAME
)
3080 id_base
*idb
= get_operator ((const char *)CPP_HASHNODE
3081 (token
->val
.node
.node
)->ident
.str
);
3083 if (idb
&& (p
= dyn_cast
<user_id
*> (idb
)) && p
->is_oper_list
)
3084 record_operlist (token
->src_loc
, p
);
3087 /* Record the token. */
3088 code
.safe_push (*token
);
3091 return new c_expr (r
, code
, nr_stmts
, vNULL
, capture_ids
);
3094 /* Parse an operand which is either an expression, a predicate or
3095 a standalone capture.
3096 op = predicate | expr | c_expr | capture */
3101 const cpp_token
*token
= peek ();
3102 struct operand
*op
= NULL
;
3103 if (token
->type
== CPP_OPEN_PAREN
)
3105 eat_token (CPP_OPEN_PAREN
);
3107 eat_token (CPP_CLOSE_PAREN
);
3109 else if (token
->type
== CPP_OPEN_BRACE
)
3111 op
= parse_c_expr (CPP_OPEN_BRACE
);
3115 /* Remaining ops are either empty or predicates */
3116 if (token
->type
== CPP_NAME
)
3118 const char *id
= get_ident ();
3119 id_base
*opr
= get_operator (id
);
3121 fatal_at (token
, "expected predicate name");
3122 if (operator_id
*code
= dyn_cast
<operator_id
*> (opr
))
3124 if (code
->nargs
!= 0)
3125 fatal_at (token
, "using an operator with operands as predicate");
3126 /* Parse the zero-operand operator "predicates" as
3128 op
= new expr (opr
);
3130 else if (user_id
*code
= dyn_cast
<user_id
*> (opr
))
3132 if (code
->nargs
!= 0)
3133 fatal_at (token
, "using an operator with operands as predicate");
3134 /* Parse the zero-operand operator "predicates" as
3136 op
= new expr (opr
);
3138 else if (predicate_id
*p
= dyn_cast
<predicate_id
*> (opr
))
3139 op
= new predicate (p
);
3141 fatal_at (token
, "using an unsupported operator as predicate");
3142 if (!parsing_match_operand
)
3143 fatal_at (token
, "predicates are only allowed in match expression");
3145 if (token
->flags
& PREV_WHITE
)
3148 else if (token
->type
!= CPP_COLON
3149 && token
->type
!= CPP_ATSIGN
)
3150 fatal_at (token
, "expected expression or predicate");
3151 /* optionally followed by a capture and a predicate. */
3152 if (token
->type
== CPP_COLON
)
3153 fatal_at (token
, "not implemented: predicate on leaf operand");
3154 if (token
->type
== CPP_ATSIGN
)
3155 op
= parse_capture (op
);
3161 /* Create a new simplify from the current parsing state and MATCH,
3162 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
3165 parser::push_simplify (vec
<simplify
*>& simplifiers
,
3166 operand
*match
, source_location match_loc
,
3167 operand
*result
, source_location result_loc
)
3169 /* Build and push a temporary for for operator list uses in expressions. */
3170 if (!oper_lists
.is_empty ())
3171 active_fors
.safe_push (oper_lists
);
3173 simplifiers
.safe_push
3174 (new simplify (match
, match_loc
, result
, result_loc
,
3175 active_ifs
.copy (), active_fors
.copy (), capture_ids
));
3177 if (!oper_lists
.is_empty ())
3182 simplify = 'simplify' <expr> <result-op>
3184 match = 'match' <ident> <expr> [<result-op>]
3186 <result-op> = <op> | <if> | <with>
3187 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
3188 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
3189 and fill SIMPLIFIERS with the results. */
3192 parser::parse_simplify (source_location match_location
,
3193 vec
<simplify
*>& simplifiers
, predicate_id
*matcher
,
3196 /* Reset the capture map. */
3198 capture_ids
= new cid_map_t
;
3199 /* Reset oper_lists and set. */
3200 hash_set
<user_id
*> olist
;
3201 oper_lists_set
= &olist
;
3204 const cpp_token
*loc
= peek ();
3205 parsing_match_operand
= true;
3206 struct operand
*match
= parse_op ();
3207 parsing_match_operand
= false;
3208 if (match
->type
== operand::OP_CAPTURE
&& !matcher
)
3209 fatal_at (loc
, "outermost expression cannot be captured");
3210 if (match
->type
== operand::OP_EXPR
3211 && is_a
<predicate_id
*> (as_a
<expr
*> (match
)->operation
))
3212 fatal_at (loc
, "outermost expression cannot be a predicate");
3214 const cpp_token
*token
= peek ();
3216 /* If this if is immediately closed then it is part of a predicate
3217 definition. Push it. */
3218 if (token
->type
== CPP_CLOSE_PAREN
)
3221 fatal_at (token
, "expected transform expression");
3222 push_simplify (simplifiers
, match
, match_location
,
3223 result
, token
->src_loc
);
3227 unsigned active_ifs_len
= active_ifs
.length ();
3230 if (token
->type
== CPP_OPEN_PAREN
)
3232 source_location paren_loc
= token
->src_loc
;
3233 eat_token (CPP_OPEN_PAREN
);
3234 if (peek_ident ("if"))
3237 active_ifs
.safe_push (if_or_with (parse_c_expr (CPP_OPEN_PAREN
),
3238 token
->src_loc
, false));
3239 /* If this if is immediately closed then it contains a
3240 manual matcher or is part of a predicate definition.
3242 if (peek ()->type
== CPP_CLOSE_PAREN
)
3245 fatal_at (token
, "manual transform not implemented");
3246 push_simplify (simplifiers
, match
, match_location
,
3250 else if (peek_ident ("with"))
3253 /* Parse (with c-expr expr) as (if-with (true) expr). */
3254 c_expr
*e
= parse_c_expr (CPP_OPEN_BRACE
);
3256 active_ifs
.safe_push (if_or_with (e
, token
->src_loc
, true));
3260 operand
*op
= result
;
3263 push_simplify (simplifiers
, match
, match_location
,
3264 op
, token
->src_loc
);
3265 eat_token (CPP_CLOSE_PAREN
);
3266 /* A "default" result closes the enclosing scope. */
3267 if (active_ifs
.length () > active_ifs_len
)
3269 eat_token (CPP_CLOSE_PAREN
);
3276 else if (token
->type
== CPP_CLOSE_PAREN
)
3278 /* Close a scope if requested. */
3279 if (active_ifs
.length () > active_ifs_len
)
3281 eat_token (CPP_CLOSE_PAREN
);
3291 fatal_at (token
, "expected match operand expression");
3292 push_simplify (simplifiers
, match
, match_location
,
3293 matcher
? result
: parse_op (), token
->src_loc
);
3294 /* A "default" result closes the enclosing scope. */
3295 if (active_ifs
.length () > active_ifs_len
)
3297 eat_token (CPP_CLOSE_PAREN
);
3307 /* Parsing of the outer control structures. */
3309 /* Parse a for expression
3310 for = '(' 'for' <subst>... <pattern> ')'
3311 subst = <ident> '(' <ident>... ')' */
3314 parser::parse_for (source_location
)
3316 auto_vec
<const cpp_token
*> user_id_tokens
;
3317 vec
<user_id
*> user_ids
= vNULL
;
3318 const cpp_token
*token
;
3319 unsigned min_n_opers
= 0, max_n_opers
= 0;
3324 if (token
->type
!= CPP_NAME
)
3327 /* Insert the user defined operators into the operator hash. */
3328 const char *id
= get_ident ();
3329 if (get_operator (id
) != NULL
)
3330 fatal_at (token
, "operator already defined");
3331 user_id
*op
= new user_id (id
);
3332 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
3334 user_ids
.safe_push (op
);
3335 user_id_tokens
.safe_push (token
);
3337 eat_token (CPP_OPEN_PAREN
);
3340 while ((token
= peek_ident ()) != 0)
3342 const char *oper
= get_ident ();
3343 id_base
*idb
= get_operator (oper
);
3345 fatal_at (token
, "no such operator '%s'", oper
);
3346 if (*idb
== CONVERT0
|| *idb
== CONVERT1
|| *idb
== CONVERT2
3347 || *idb
== VIEW_CONVERT0
|| *idb
== VIEW_CONVERT1
3348 || *idb
== VIEW_CONVERT2
)
3349 fatal_at (token
, "conditional operators cannot be used inside for");
3353 else if (idb
->nargs
== -1)
3355 else if (idb
->nargs
!= arity
)
3356 fatal_at (token
, "operator '%s' with arity %d does not match "
3357 "others with arity %d", oper
, idb
->nargs
, arity
);
3359 user_id
*p
= dyn_cast
<user_id
*> (idb
);
3362 if (p
->is_oper_list
)
3363 op
->substitutes
.safe_splice (p
->substitutes
);
3365 fatal_at (token
, "iterator cannot be used as operator-list");
3368 op
->substitutes
.safe_push (idb
);
3371 token
= expect (CPP_CLOSE_PAREN
);
3373 unsigned nsubstitutes
= op
->substitutes
.length ();
3374 if (nsubstitutes
== 0)
3375 fatal_at (token
, "A user-defined operator must have at least "
3376 "one substitution");
3377 if (max_n_opers
== 0)
3379 min_n_opers
= nsubstitutes
;
3380 max_n_opers
= nsubstitutes
;
3384 if (nsubstitutes
% min_n_opers
!= 0
3385 && min_n_opers
% nsubstitutes
!= 0)
3386 fatal_at (token
, "All user-defined identifiers must have a "
3387 "multiple number of operator substitutions of the "
3388 "smallest number of substitutions");
3389 if (nsubstitutes
< min_n_opers
)
3390 min_n_opers
= nsubstitutes
;
3391 else if (nsubstitutes
> max_n_opers
)
3392 max_n_opers
= nsubstitutes
;
3396 unsigned n_ids
= user_ids
.length ();
3398 fatal_at (token
, "for requires at least one user-defined identifier");
3401 if (token
->type
== CPP_CLOSE_PAREN
)
3402 fatal_at (token
, "no pattern defined in for");
3404 active_fors
.safe_push (user_ids
);
3408 if (token
->type
== CPP_CLOSE_PAREN
)
3414 /* Remove user-defined operators from the hash again. */
3415 for (unsigned i
= 0; i
< user_ids
.length (); ++i
)
3417 if (!user_ids
[i
]->used
)
3418 warning_at (user_id_tokens
[i
],
3419 "operator %s defined but not used", user_ids
[i
]->id
);
3420 operators
->remove_elt (user_ids
[i
]);
3424 /* Parse an identifier associated with a list of operators.
3425 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
3428 parser::parse_operator_list (source_location
)
3430 const cpp_token
*token
= peek ();
3431 const char *id
= get_ident ();
3433 if (get_operator (id
) != 0)
3434 fatal_at (token
, "operator %s already defined", id
);
3436 user_id
*op
= new user_id (id
, true);
3439 while ((token
= peek_ident ()) != 0)
3442 const char *oper
= get_ident ();
3443 id_base
*idb
= get_operator (oper
);
3446 fatal_at (token
, "no such operator '%s'", oper
);
3450 else if (idb
->nargs
== -1)
3452 else if (arity
!= idb
->nargs
)
3453 fatal_at (token
, "operator '%s' with arity %d does not match "
3454 "others with arity %d", oper
, idb
->nargs
, arity
);
3456 /* We allow composition of multiple operator lists. */
3457 if (user_id
*p
= dyn_cast
<user_id
*> (idb
))
3458 op
->substitutes
.safe_splice (p
->substitutes
);
3460 op
->substitutes
.safe_push (idb
);
3463 // Check that there is no junk after id-list
3465 if (token
->type
!= CPP_CLOSE_PAREN
)
3466 fatal_at (token
, "expected identifier got %s", cpp_type2name (token
->type
, 0));
3468 if (op
->substitutes
.length () == 0)
3469 fatal_at (token
, "operator-list cannot be empty");
3472 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
3476 /* Parse an outer if expression.
3477 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
3480 parser::parse_if (source_location loc
)
3482 operand
*ifexpr
= parse_c_expr (CPP_OPEN_PAREN
);
3484 const cpp_token
*token
= peek ();
3485 if (token
->type
== CPP_CLOSE_PAREN
)
3486 fatal_at (token
, "no pattern defined in if");
3488 active_ifs
.safe_push (if_or_with (ifexpr
, loc
, false));
3491 const cpp_token
*token
= peek ();
3492 if (token
->type
== CPP_CLOSE_PAREN
)
3500 /* Parse a list of predefined predicate identifiers.
3501 preds = '(' 'define_predicates' <ident>... ')' */
3504 parser::parse_predicates (source_location
)
3508 const cpp_token
*token
= peek ();
3509 if (token
->type
!= CPP_NAME
)
3512 add_predicate (get_ident ());
3517 /* Parse outer control structures.
3518 pattern = <preds>|<for>|<if>|<simplify>|<match> */
3521 parser::parse_pattern ()
3523 /* All clauses start with '('. */
3524 eat_token (CPP_OPEN_PAREN
);
3525 const cpp_token
*token
= peek ();
3526 const char *id
= get_ident ();
3527 if (strcmp (id
, "simplify") == 0)
3529 parse_simplify (token
->src_loc
, simplifiers
, NULL
, NULL
);
3532 else if (strcmp (id
, "match") == 0)
3534 bool with_args
= false;
3535 if (peek ()->type
== CPP_OPEN_PAREN
)
3537 eat_token (CPP_OPEN_PAREN
);
3540 const char *name
= get_ident ();
3541 id_base
*id
= get_operator (name
);
3545 p
= add_predicate (name
);
3546 user_predicates
.safe_push (p
);
3548 else if ((p
= dyn_cast
<predicate_id
*> (id
)))
3551 fatal_at (token
, "cannot add a match to a non-predicate ID");
3552 /* Parse (match <id> <arg>... (match-expr)) here. */
3556 capture_ids
= new cid_map_t
;
3558 while (peek ()->type
== CPP_ATSIGN
)
3559 e
->append_op (parse_capture (NULL
));
3560 eat_token (CPP_CLOSE_PAREN
);
3563 && ((e
&& e
->ops
.length () != (unsigned)p
->nargs
)
3564 || (!e
&& p
->nargs
!= 0)))
3565 fatal_at (token
, "non-matching number of match operands");
3566 p
->nargs
= e
? e
->ops
.length () : 0;
3567 parse_simplify (token
->src_loc
, p
->matchers
, p
, e
);
3570 else if (strcmp (id
, "for") == 0)
3571 parse_for (token
->src_loc
);
3572 else if (strcmp (id
, "if") == 0)
3573 parse_if (token
->src_loc
);
3574 else if (strcmp (id
, "define_predicates") == 0)
3576 if (active_ifs
.length () > 0
3577 || active_fors
.length () > 0)
3578 fatal_at (token
, "define_predicates inside if or for is not supported");
3579 parse_predicates (token
->src_loc
);
3581 else if (strcmp (id
, "define_operator_list") == 0)
3583 if (active_ifs
.length () > 0
3584 || active_fors
.length () > 0)
3585 fatal_at (token
, "operator-list inside if or for is not supported");
3586 parse_operator_list (token
->src_loc
);
3589 fatal_at (token
, "expected %s'simplify', 'match', 'for' or 'if'",
3590 active_ifs
.length () == 0 && active_fors
.length () == 0
3591 ? "'define_predicates', " : "");
3593 eat_token (CPP_CLOSE_PAREN
);
3596 /* Main entry of the parser. Repeatedly parse outer control structures. */
3598 parser::parser (cpp_reader
*r_
)
3602 active_fors
= vNULL
;
3603 simplifiers
= vNULL
;
3604 oper_lists_set
= NULL
;
3607 user_predicates
= vNULL
;
3608 parsing_match_operand
= false;
3610 const cpp_token
*token
= next ();
3611 while (token
->type
!= CPP_EOF
)
3613 _cpp_backup_tokens (r
, 1);
3620 /* Helper for the linemap code. */
3623 round_alloc_size (size_t s
)
3629 /* The genmatch generator progam. It reads from a pattern description
3630 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
3633 main (int argc
, char **argv
)
3637 progname
= "genmatch";
3643 bool verbose
= false;
3644 char *input
= argv
[argc
-1];
3645 for (int i
= 1; i
< argc
- 1; ++i
)
3647 if (strcmp (argv
[i
], "--gimple") == 0)
3649 else if (strcmp (argv
[i
], "--generic") == 0)
3651 else if (strcmp (argv
[i
], "-v") == 0)
3655 fprintf (stderr
, "Usage: genmatch "
3656 "[--gimple] [--generic] [-v] input\n");
3661 line_table
= XCNEW (struct line_maps
);
3662 linemap_init (line_table
, 0);
3663 line_table
->reallocator
= xrealloc
;
3664 line_table
->round_alloc_size
= round_alloc_size
;
3666 r
= cpp_create_reader (CLK_GNUC99
, NULL
, line_table
);
3667 cpp_callbacks
*cb
= cpp_get_callbacks (r
);
3668 cb
->error
= error_cb
;
3670 if (!cpp_read_main_file (r
, input
))
3672 cpp_define (r
, gimple
? "GIMPLE=1": "GENERIC=1");
3673 cpp_define (r
, gimple
? "GENERIC=0": "GIMPLE=0");
3675 /* Pre-seed operators. */
3676 operators
= new hash_table
<id_base
> (1024);
3677 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
3678 add_operator (SYM, # SYM, # TYPE, NARGS);
3679 #define END_OF_BASE_TREE_CODES
3681 add_operator (CONVERT0
, "CONVERT0", "tcc_unary", 1);
3682 add_operator (CONVERT1
, "CONVERT1", "tcc_unary", 1);
3683 add_operator (CONVERT2
, "CONVERT2", "tcc_unary", 1);
3684 add_operator (VIEW_CONVERT0
, "VIEW_CONVERT0", "tcc_unary", 1);
3685 add_operator (VIEW_CONVERT1
, "VIEW_CONVERT1", "tcc_unary", 1);
3686 add_operator (VIEW_CONVERT2
, "VIEW_CONVERT2", "tcc_unary", 1);
3687 #undef END_OF_BASE_TREE_CODES
3690 /* Pre-seed builtin functions.
3691 ??? Cannot use N (name) as that is targetm.emultls.get_address
3692 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
3693 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
3694 add_builtin (ENUM, # ENUM);
3695 #include "builtins.def"
3702 write_header (stdout
, "gimple-match-head.c");
3704 write_header (stdout
, "generic-match-head.c");
3706 /* Go over all predicates defined with patterns and perform
3707 lowering and code generation. */
3708 for (unsigned i
= 0; i
< p
.user_predicates
.length (); ++i
)
3710 predicate_id
*pred
= p
.user_predicates
[i
];
3711 lower (pred
->matchers
, gimple
);
3714 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
3715 print_matches (pred
->matchers
[i
]);
3718 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
3719 dt
.insert (pred
->matchers
[i
], i
);
3724 write_predicate (stdout
, pred
, dt
, gimple
);
3727 /* Lower the main simplifiers and generate code for them. */
3728 lower (p
.simplifiers
, gimple
);
3731 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
3732 print_matches (p
.simplifiers
[i
]);
3735 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
3736 dt
.insert (p
.simplifiers
[i
], i
);
3742 dt
.gen_gimple (stdout
);
3744 dt
.gen_generic (stdout
);
3747 cpp_finish (r
, NULL
);