c++: class NTTP and nested anon union [PR108566]
[official-gcc.git] / gcc / genmatch.cc
blob43bd0212d0e205a8b73ba95fe44d83a193ae922e
1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
4 Copyright (C) 2014-2023 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
13 version.
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
18 for more details.
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/>. */
24 #include "bconfig.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include <cpplib.h>
28 #include "errors.h"
29 #include "hash-table.h"
30 #include "hash-set.h"
31 #include "is-a.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)
38 return NULL;
40 void ggc_free (void *)
45 /* Global state. */
47 /* Verboseness. 0 is quiet, 1 adds some warnings, 2 is for debugging. */
48 unsigned verbose;
51 /* libccp helpers. */
53 static class line_maps *line_table;
55 /* The rich_location class within libcpp requires a way to expand
56 location_t instances, and relies on the client code
57 providing a symbol named
58 linemap_client_expand_location_to_spelling_point
59 to do this.
61 This is the implementation for genmatch. */
63 expanded_location
64 linemap_client_expand_location_to_spelling_point (location_t loc,
65 enum location_aspect)
67 const struct line_map_ordinary *map;
68 loc = linemap_resolve_location (line_table, loc, LRK_SPELLING_LOCATION, &map);
69 return linemap_expand_location (line_table, map, loc);
72 static bool
73 #if GCC_VERSION >= 4001
74 __attribute__((format (printf, 5, 0)))
75 #endif
76 diagnostic_cb (cpp_reader *, enum cpp_diagnostic_level errtype,
77 enum cpp_warning_reason, rich_location *richloc,
78 const char *msg, va_list *ap)
80 const line_map_ordinary *map;
81 location_t location = richloc->get_loc ();
82 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
83 expanded_location loc = linemap_expand_location (line_table, map, location);
84 fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column,
85 (errtype == CPP_DL_WARNING) ? "warning" : "error");
86 vfprintf (stderr, msg, *ap);
87 fprintf (stderr, "\n");
88 FILE *f = fopen (loc.file, "r");
89 if (f)
91 char buf[128];
92 while (loc.line > 0)
94 if (!fgets (buf, 128, f))
95 goto notfound;
96 if (buf[strlen (buf) - 1] != '\n')
98 if (loc.line > 1)
99 loc.line++;
101 loc.line--;
103 fprintf (stderr, "%s", buf);
104 for (int i = 0; i < loc.column - 1; ++i)
105 fputc (' ', stderr);
106 fputc ('^', stderr);
107 fputc ('\n', stderr);
108 notfound:
109 fclose (f);
112 if (errtype == CPP_DL_FATAL)
113 exit (1);
114 return false;
117 static void
118 #if GCC_VERSION >= 4001
119 __attribute__((format (printf, 2, 3)))
120 #endif
121 fatal_at (const cpp_token *tk, const char *msg, ...)
123 rich_location richloc (line_table, tk->src_loc);
124 va_list ap;
125 va_start (ap, msg);
126 diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
127 va_end (ap);
130 static void
131 #if GCC_VERSION >= 4001
132 __attribute__((format (printf, 2, 3)))
133 #endif
134 fatal_at (location_t loc, const char *msg, ...)
136 rich_location richloc (line_table, loc);
137 va_list ap;
138 va_start (ap, msg);
139 diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
140 va_end (ap);
143 static void
144 #if GCC_VERSION >= 4001
145 __attribute__((format (printf, 2, 3)))
146 #endif
147 warning_at (const cpp_token *tk, const char *msg, ...)
149 rich_location richloc (line_table, tk->src_loc);
150 va_list ap;
151 va_start (ap, msg);
152 diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
153 va_end (ap);
156 static void
157 #if GCC_VERSION >= 4001
158 __attribute__((format (printf, 2, 3)))
159 #endif
160 warning_at (location_t loc, const char *msg, ...)
162 rich_location richloc (line_table, loc);
163 va_list ap;
164 va_start (ap, msg);
165 diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
166 va_end (ap);
169 /* Like fprintf, but print INDENT spaces at the beginning. */
171 static void
172 #if GCC_VERSION >= 4001
173 __attribute__((format (printf, 3, 4)))
174 #endif
175 fprintf_indent (FILE *f, unsigned int indent, const char *format, ...)
177 va_list ap;
178 for (; indent >= 8; indent -= 8)
179 fputc ('\t', f);
180 fprintf (f, "%*s", indent, "");
181 va_start (ap, format);
182 vfprintf (f, format, ap);
183 va_end (ap);
186 static void
187 output_line_directive (FILE *f, location_t location,
188 bool dumpfile = false, bool fnargs = false)
190 const line_map_ordinary *map;
191 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
192 expanded_location loc = linemap_expand_location (line_table, map, location);
193 if (dumpfile)
195 /* When writing to a dumpfile only dump the filename. */
196 const char *file = strrchr (loc.file, DIR_SEPARATOR);
197 #if defined(DIR_SEPARATOR_2)
198 const char *pos2 = strrchr (loc.file, DIR_SEPARATOR_2);
199 if (pos2 && (!file || (pos2 > file)))
200 file = pos2;
201 #endif
202 if (!file)
203 file = loc.file;
204 else
205 ++file;
207 if (fnargs)
208 fprintf (f, "\"%s\", %d", file, loc.line);
209 else
210 fprintf (f, "%s:%d", file, loc.line);
212 else
213 /* Other gen programs really output line directives here, at least for
214 development it's right now more convenient to have line information
215 from the generated file. Still keep the directives as comment for now
216 to easily back-point to the meta-description. */
217 fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file);
221 /* Pull in tree codes and builtin function codes from their
222 definition files. */
224 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
225 enum tree_code {
226 #include "tree.def"
227 MAX_TREE_CODES
229 #undef DEFTREECODE
231 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
232 enum built_in_function {
233 #include "builtins.def"
234 END_BUILTINS
237 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE,
238 enum internal_fn {
239 #include "internal-fn.def"
240 IFN_LAST
243 enum combined_fn {
244 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
245 CFN_##ENUM = int (ENUM),
246 #include "builtins.def"
248 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
249 CFN_##CODE = int (END_BUILTINS) + int (IFN_##CODE),
250 #include "internal-fn.def"
252 CFN_LAST
255 #include "case-cfn-macros.h"
257 /* Return true if CODE represents a commutative tree code. Otherwise
258 return false. */
259 bool
260 commutative_tree_code (enum tree_code code)
262 switch (code)
264 case PLUS_EXPR:
265 case MULT_EXPR:
266 case MULT_HIGHPART_EXPR:
267 case MIN_EXPR:
268 case MAX_EXPR:
269 case BIT_IOR_EXPR:
270 case BIT_XOR_EXPR:
271 case BIT_AND_EXPR:
272 case NE_EXPR:
273 case EQ_EXPR:
274 case UNORDERED_EXPR:
275 case ORDERED_EXPR:
276 case UNEQ_EXPR:
277 case LTGT_EXPR:
278 case TRUTH_AND_EXPR:
279 case TRUTH_XOR_EXPR:
280 case TRUTH_OR_EXPR:
281 case WIDEN_MULT_EXPR:
282 case VEC_WIDEN_MULT_HI_EXPR:
283 case VEC_WIDEN_MULT_LO_EXPR:
284 case VEC_WIDEN_MULT_EVEN_EXPR:
285 case VEC_WIDEN_MULT_ODD_EXPR:
286 return true;
288 default:
289 break;
291 return false;
294 /* Return true if CODE represents a ternary tree code for which the
295 first two operands are commutative. Otherwise return false. */
296 bool
297 commutative_ternary_tree_code (enum tree_code code)
299 switch (code)
301 case WIDEN_MULT_PLUS_EXPR:
302 case WIDEN_MULT_MINUS_EXPR:
303 case DOT_PROD_EXPR:
304 return true;
306 default:
307 break;
309 return false;
312 /* Return true if CODE is a comparison. */
314 bool
315 comparison_code_p (enum tree_code code)
317 switch (code)
319 case EQ_EXPR:
320 case NE_EXPR:
321 case ORDERED_EXPR:
322 case UNORDERED_EXPR:
323 case LTGT_EXPR:
324 case UNEQ_EXPR:
325 case GT_EXPR:
326 case GE_EXPR:
327 case LT_EXPR:
328 case LE_EXPR:
329 case UNGT_EXPR:
330 case UNGE_EXPR:
331 case UNLT_EXPR:
332 case UNLE_EXPR:
333 return true;
335 default:
336 break;
338 return false;
342 /* Base class for all identifiers the parser knows. */
344 class id_base : public nofree_ptr_hash<id_base>
346 public:
347 enum id_kind { CODE, FN, PREDICATE, USER, NULL_ID } kind;
349 id_base (id_kind, const char *, int = -1);
351 hashval_t hashval;
352 int nargs;
353 const char *id;
355 /* hash_table support. */
356 static inline hashval_t hash (const id_base *);
357 static inline int equal (const id_base *, const id_base *);
360 inline hashval_t
361 id_base::hash (const id_base *op)
363 return op->hashval;
366 inline int
367 id_base::equal (const id_base *op1,
368 const id_base *op2)
370 return (op1->hashval == op2->hashval
371 && strcmp (op1->id, op2->id) == 0);
374 /* The special id "null", which matches nothing. */
375 static id_base *null_id;
377 /* Hashtable of known pattern operators. This is pre-seeded from
378 all known tree codes and all known builtin function ids. */
379 static hash_table<id_base> *operators;
381 id_base::id_base (id_kind kind_, const char *id_, int nargs_)
383 kind = kind_;
384 id = id_;
385 nargs = nargs_;
386 hashval = htab_hash_string (id);
389 /* Identifier that maps to a tree code. */
391 class operator_id : public id_base
393 public:
394 operator_id (enum tree_code code_, const char *id_, unsigned nargs_,
395 const char *tcc_)
396 : id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {}
397 enum tree_code code;
398 const char *tcc;
401 /* Identifier that maps to a builtin or internal function code. */
403 class fn_id : public id_base
405 public:
406 fn_id (enum built_in_function fn_, const char *id_)
407 : id_base (id_base::FN, id_), fn (fn_) {}
408 fn_id (enum internal_fn fn_, const char *id_)
409 : id_base (id_base::FN, id_), fn (int (END_BUILTINS) + int (fn_)) {}
410 unsigned int fn;
413 class simplify;
415 /* Identifier that maps to a user-defined predicate. */
417 class predicate_id : public id_base
419 public:
420 predicate_id (const char *id_)
421 : id_base (id_base::PREDICATE, id_), matchers (vNULL) {}
422 vec<simplify *> matchers;
425 /* Identifier that maps to a operator defined by a 'for' directive. */
427 class user_id : public id_base
429 public:
430 user_id (const char *id_, bool is_oper_list_ = false)
431 : id_base (id_base::USER, id_), substitutes (vNULL),
432 used (false), is_oper_list (is_oper_list_) {}
433 vec<id_base *> substitutes;
434 bool used;
435 bool is_oper_list;
438 template<>
439 template<>
440 inline bool
441 is_a_helper <fn_id *>::test (id_base *id)
443 return id->kind == id_base::FN;
446 template<>
447 template<>
448 inline bool
449 is_a_helper <operator_id *>::test (id_base *id)
451 return id->kind == id_base::CODE;
454 template<>
455 template<>
456 inline bool
457 is_a_helper <predicate_id *>::test (id_base *id)
459 return id->kind == id_base::PREDICATE;
462 template<>
463 template<>
464 inline bool
465 is_a_helper <user_id *>::test (id_base *id)
467 return id->kind == id_base::USER;
470 /* If ID has a pair of consecutive, commutative operands, return the
471 index of the first, otherwise return -1. */
473 static int
474 commutative_op (id_base *id)
476 if (operator_id *code = dyn_cast <operator_id *> (id))
478 if (commutative_tree_code (code->code)
479 || commutative_ternary_tree_code (code->code))
480 return 0;
481 return -1;
483 if (fn_id *fn = dyn_cast <fn_id *> (id))
484 switch (fn->fn)
486 CASE_CFN_FMA:
487 case CFN_FMS:
488 case CFN_FNMA:
489 case CFN_FNMS:
490 return 0;
492 case CFN_COND_ADD:
493 case CFN_COND_MUL:
494 case CFN_COND_MIN:
495 case CFN_COND_MAX:
496 case CFN_COND_FMIN:
497 case CFN_COND_FMAX:
498 case CFN_COND_AND:
499 case CFN_COND_IOR:
500 case CFN_COND_XOR:
501 case CFN_COND_FMA:
502 case CFN_COND_FMS:
503 case CFN_COND_FNMA:
504 case CFN_COND_FNMS:
505 return 1;
507 default:
508 return -1;
510 if (user_id *uid = dyn_cast<user_id *> (id))
512 int res = commutative_op (uid->substitutes[0]);
513 if (res < 0)
514 return -1;
515 for (unsigned i = 1; i < uid->substitutes.length (); ++i)
516 if (res != commutative_op (uid->substitutes[i]))
517 return -1;
518 return res;
520 return -1;
523 /* Add a predicate identifier to the hash. */
525 static predicate_id *
526 add_predicate (const char *id)
528 predicate_id *p = new predicate_id (id);
529 id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT);
530 if (*slot)
531 fatal ("duplicate id definition");
532 *slot = p;
533 return p;
536 /* Add a tree code identifier to the hash. */
538 static void
539 add_operator (enum tree_code code, const char *id,
540 const char *tcc, unsigned nargs)
542 if (strcmp (tcc, "tcc_unary") != 0
543 && strcmp (tcc, "tcc_binary") != 0
544 && strcmp (tcc, "tcc_comparison") != 0
545 && strcmp (tcc, "tcc_expression") != 0
546 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
547 && strcmp (tcc, "tcc_reference") != 0
548 /* To have INTEGER_CST and friends as "predicate operators". */
549 && strcmp (tcc, "tcc_constant") != 0
550 /* And allow CONSTRUCTOR for vector initializers. */
551 && !(code == CONSTRUCTOR)
552 /* Allow SSA_NAME as predicate operator. */
553 && !(code == SSA_NAME))
554 return;
555 /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
556 if (code == ADDR_EXPR)
557 nargs = 0;
558 operator_id *op = new operator_id (code, id, nargs, tcc);
559 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
560 if (*slot)
561 fatal ("duplicate id definition");
562 *slot = op;
565 /* Add a built-in or internal function identifier to the hash. ID is
566 the name of its CFN_* enumeration value. */
568 template <typename T>
569 static void
570 add_function (T code, const char *id)
572 fn_id *fn = new fn_id (code, id);
573 id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT);
574 if (*slot)
575 fatal ("duplicate id definition");
576 *slot = fn;
579 /* Helper for easy comparing ID with tree code CODE. */
581 static bool
582 operator==(id_base &id, enum tree_code code)
584 if (operator_id *oid = dyn_cast <operator_id *> (&id))
585 return oid->code == code;
586 return false;
589 /* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */
591 id_base *
592 get_operator (const char *id, bool allow_null = false)
594 if (allow_null && strcmp (id, "null") == 0)
595 return null_id;
597 id_base tem (id_base::CODE, id);
599 id_base *op = operators->find_with_hash (&tem, tem.hashval);
600 if (op)
602 /* If this is a user-defined identifier track whether it was used. */
603 if (user_id *uid = dyn_cast<user_id *> (op))
604 uid->used = true;
605 return op;
608 char *id2;
609 bool all_upper = true;
610 bool all_lower = true;
611 for (unsigned int i = 0; id[i]; ++i)
612 if (ISUPPER (id[i]))
613 all_lower = false;
614 else if (ISLOWER (id[i]))
615 all_upper = false;
616 if (all_lower)
618 /* Try in caps with _EXPR appended. */
619 id2 = ACONCAT ((id, "_EXPR", NULL));
620 for (unsigned int i = 0; id2[i]; ++i)
621 id2[i] = TOUPPER (id2[i]);
623 else if (all_upper && startswith (id, "IFN_"))
624 /* Try CFN_ instead of IFN_. */
625 id2 = ACONCAT (("CFN_", id + 4, NULL));
626 else if (all_upper && startswith (id, "BUILT_IN_"))
627 /* Try prepending CFN_. */
628 id2 = ACONCAT (("CFN_", id, NULL));
629 else
630 return NULL;
632 new (&tem) id_base (id_base::CODE, id2);
633 return operators->find_with_hash (&tem, tem.hashval);
636 /* Return the comparison operators that results if the operands are
637 swapped. This is safe for floating-point. */
639 id_base *
640 swap_tree_comparison (operator_id *p)
642 switch (p->code)
644 case EQ_EXPR:
645 case NE_EXPR:
646 case ORDERED_EXPR:
647 case UNORDERED_EXPR:
648 case LTGT_EXPR:
649 case UNEQ_EXPR:
650 return p;
651 case GT_EXPR:
652 return get_operator ("LT_EXPR");
653 case GE_EXPR:
654 return get_operator ("LE_EXPR");
655 case LT_EXPR:
656 return get_operator ("GT_EXPR");
657 case LE_EXPR:
658 return get_operator ("GE_EXPR");
659 case UNGT_EXPR:
660 return get_operator ("UNLT_EXPR");
661 case UNGE_EXPR:
662 return get_operator ("UNLE_EXPR");
663 case UNLT_EXPR:
664 return get_operator ("UNGT_EXPR");
665 case UNLE_EXPR:
666 return get_operator ("UNGE_EXPR");
667 default:
668 gcc_unreachable ();
672 typedef hash_map<nofree_string_hash, unsigned> cid_map_t;
675 /* The AST produced by parsing of the pattern definitions. */
677 class dt_operand;
678 class capture_info;
680 /* The base class for operands. */
682 class operand {
683 public:
684 enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR, OP_IF, OP_WITH };
685 operand (enum op_type type_, location_t loc_)
686 : type (type_), location (loc_) {}
687 enum op_type type;
688 location_t location;
689 virtual void gen_transform (FILE *, int, const char *, bool, int,
690 const char *, capture_info *,
691 dt_operand ** = 0,
692 int = 0)
693 { gcc_unreachable (); }
696 /* A predicate operand. Predicates are leafs in the AST. */
698 class predicate : public operand
700 public:
701 predicate (predicate_id *p_, location_t loc)
702 : operand (OP_PREDICATE, loc), p (p_) {}
703 predicate_id *p;
706 /* An operand that constitutes an expression. Expressions include
707 function calls and user-defined predicate invocations. */
709 class expr : public operand
711 public:
712 expr (id_base *operation_, location_t loc, bool is_commutative_ = false)
713 : operand (OP_EXPR, loc), operation (operation_),
714 ops (vNULL), expr_type (NULL), is_commutative (is_commutative_),
715 is_generic (false), force_single_use (false), force_leaf (false),
716 opt_grp (0) {}
717 expr (expr *e)
718 : operand (OP_EXPR, e->location), operation (e->operation),
719 ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commutative),
720 is_generic (e->is_generic), force_single_use (e->force_single_use),
721 force_leaf (e->force_leaf), opt_grp (e->opt_grp) {}
722 void append_op (operand *op) { ops.safe_push (op); }
723 /* The operator and its operands. */
724 id_base *operation;
725 vec<operand *> ops;
726 /* An explicitely specified type - used exclusively for conversions. */
727 const char *expr_type;
728 /* Whether the operation is to be applied commutatively. This is
729 later lowered to two separate patterns. */
730 bool is_commutative;
731 /* Whether the expression is expected to be in GENERIC form. */
732 bool is_generic;
733 /* Whether pushing any stmt to the sequence should be conditional
734 on this expression having a single-use. */
735 bool force_single_use;
736 /* Whether in the result expression this should be a leaf node
737 with any children simplified down to simple operands. */
738 bool force_leaf;
739 /* If non-zero, the group for optional handling. */
740 unsigned char opt_grp;
741 void gen_transform (FILE *f, int, const char *, bool, int,
742 const char *, capture_info *,
743 dt_operand ** = 0, int = 0) override;
746 /* An operator that is represented by native C code. This is always
747 a leaf operand in the AST. This class is also used to represent
748 the code to be generated for 'if' and 'with' expressions. */
750 class c_expr : public operand
752 public:
753 /* A mapping of an identifier and its replacement. Used to apply
754 'for' lowering. */
755 class id_tab {
756 public:
757 const char *id;
758 const char *oper;
759 id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {}
762 c_expr (cpp_reader *r_, location_t loc,
763 vec<cpp_token> code_, unsigned nr_stmts_,
764 vec<id_tab> ids_, cid_map_t *capture_ids_)
765 : operand (OP_C_EXPR, loc), r (r_), code (code_),
766 capture_ids (capture_ids_), nr_stmts (nr_stmts_), ids (ids_) {}
767 /* cpplib tokens and state to transform this back to source. */
768 cpp_reader *r;
769 vec<cpp_token> code;
770 cid_map_t *capture_ids;
771 /* The number of statements parsed (well, the number of ';'s). */
772 unsigned nr_stmts;
773 /* The identifier replacement vector. */
774 vec<id_tab> ids;
775 void gen_transform (FILE *f, int, const char *, bool, int,
776 const char *, capture_info *,
777 dt_operand ** = 0, int = 0) final override;
780 /* A wrapper around another operand that captures its value. */
782 class capture : public operand
784 public:
785 capture (location_t loc, unsigned where_, operand *what_, bool value_)
786 : operand (OP_CAPTURE, loc), where (where_), value_match (value_),
787 what (what_) {}
788 /* Identifier index for the value. */
789 unsigned where;
790 /* Whether in a match of two operands the compare should be for
791 equal values rather than equal atoms (boils down to a type
792 check or not). */
793 bool value_match;
794 /* The captured value. */
795 operand *what;
796 void gen_transform (FILE *f, int, const char *, bool, int,
797 const char *, capture_info *,
798 dt_operand ** = 0, int = 0) final override;
801 /* if expression. */
803 class if_expr : public operand
805 public:
806 if_expr (location_t loc)
807 : operand (OP_IF, loc), cond (NULL), trueexpr (NULL), falseexpr (NULL) {}
808 c_expr *cond;
809 operand *trueexpr;
810 operand *falseexpr;
813 /* with expression. */
815 class with_expr : public operand
817 public:
818 with_expr (location_t loc)
819 : operand (OP_WITH, loc), with (NULL), subexpr (NULL) {}
820 c_expr *with;
821 operand *subexpr;
824 template<>
825 template<>
826 inline bool
827 is_a_helper <capture *>::test (operand *op)
829 return op->type == operand::OP_CAPTURE;
832 template<>
833 template<>
834 inline bool
835 is_a_helper <predicate *>::test (operand *op)
837 return op->type == operand::OP_PREDICATE;
840 template<>
841 template<>
842 inline bool
843 is_a_helper <c_expr *>::test (operand *op)
845 return op->type == operand::OP_C_EXPR;
848 template<>
849 template<>
850 inline bool
851 is_a_helper <expr *>::test (operand *op)
853 return op->type == operand::OP_EXPR;
856 template<>
857 template<>
858 inline bool
859 is_a_helper <if_expr *>::test (operand *op)
861 return op->type == operand::OP_IF;
864 template<>
865 template<>
866 inline bool
867 is_a_helper <with_expr *>::test (operand *op)
869 return op->type == operand::OP_WITH;
872 /* The main class of a pattern and its transform. This is used to
873 represent both (simplify ...) and (match ...) kinds. The AST
874 duplicates all outer 'if' and 'for' expressions here so each
875 simplify can exist in isolation. */
877 class simplify
879 public:
880 enum simplify_kind { SIMPLIFY, MATCH };
882 simplify (simplify_kind kind_, unsigned id_, operand *match_,
883 operand *result_, vec<vec<user_id *> > for_vec_,
884 cid_map_t *capture_ids_)
885 : kind (kind_), id (id_), match (match_), result (result_),
886 for_vec (for_vec_), for_subst_vec (vNULL),
887 capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
889 simplify_kind kind;
890 /* ID. This is kept to easily associate related simplifies expanded
891 from the same original one. */
892 unsigned id;
893 /* The expression that is matched against the GENERIC or GIMPLE IL. */
894 operand *match;
895 /* For a (simplify ...) an expression with ifs and withs with the expression
896 produced when the pattern applies in the leafs.
897 For a (match ...) the leafs are either empty if it is a simple predicate
898 or the single expression specifying the matched operands. */
899 class operand *result;
900 /* Collected 'for' expression operators that have to be replaced
901 in the lowering phase. */
902 vec<vec<user_id *> > for_vec;
903 vec<std::pair<user_id *, id_base *> > for_subst_vec;
904 /* A map of capture identifiers to indexes. */
905 cid_map_t *capture_ids;
906 int capture_max;
909 /* Debugging routines for dumping the AST. */
911 DEBUG_FUNCTION void
912 print_operand (operand *o, FILE *f = stderr, bool flattened = false)
914 if (capture *c = dyn_cast<capture *> (o))
916 if (c->what && flattened == false)
917 print_operand (c->what, f, flattened);
918 fprintf (f, "@%u", c->where);
921 else if (predicate *p = dyn_cast<predicate *> (o))
922 fprintf (f, "%s", p->p->id);
924 else if (is_a<c_expr *> (o))
925 fprintf (f, "c_expr");
927 else if (expr *e = dyn_cast<expr *> (o))
929 if (e->ops.length () == 0)
930 fprintf (f, "%s", e->operation->id);
931 else
933 fprintf (f, "(%s", e->operation->id);
935 if (flattened == false)
937 for (unsigned i = 0; i < e->ops.length (); ++i)
939 putc (' ', f);
940 print_operand (e->ops[i], f, flattened);
943 putc (')', f);
947 else
948 gcc_unreachable ();
951 DEBUG_FUNCTION void
952 print_matches (class simplify *s, FILE *f = stderr)
954 fprintf (f, "for expression: ");
955 print_operand (s->match, f);
956 putc ('\n', f);
960 /* AST lowering. */
962 /* Lowering of commutative operators. */
964 static void
965 cartesian_product (const vec< vec<operand *> >& ops_vector,
966 vec< vec<operand *> >& result, vec<operand *>& v, unsigned n)
968 if (n == ops_vector.length ())
970 vec<operand *> xv = v.copy ();
971 result.safe_push (xv);
972 return;
975 for (unsigned i = 0; i < ops_vector[n].length (); ++i)
977 v[n] = ops_vector[n][i];
978 cartesian_product (ops_vector, result, v, n + 1);
982 /* Lower OP to two operands in case it is marked as commutative. */
984 static vec<operand *>
985 commutate (operand *op, vec<vec<user_id *> > &for_vec)
987 vec<operand *> ret = vNULL;
989 if (capture *c = dyn_cast <capture *> (op))
991 if (!c->what)
993 ret.safe_push (op);
994 return ret;
996 vec<operand *> v = commutate (c->what, for_vec);
997 for (unsigned i = 0; i < v.length (); ++i)
999 capture *nc = new capture (c->location, c->where, v[i],
1000 c->value_match);
1001 ret.safe_push (nc);
1003 return ret;
1006 expr *e = dyn_cast <expr *> (op);
1007 if (!e || e->ops.length () == 0)
1009 ret.safe_push (op);
1010 return ret;
1013 vec< vec<operand *> > ops_vector = vNULL;
1014 for (unsigned i = 0; i < e->ops.length (); ++i)
1015 ops_vector.safe_push (commutate (e->ops[i], for_vec));
1017 auto_vec< vec<operand *> > result;
1018 auto_vec<operand *> v (e->ops.length ());
1019 v.quick_grow_cleared (e->ops.length ());
1020 cartesian_product (ops_vector, result, v, 0);
1023 for (unsigned i = 0; i < result.length (); ++i)
1025 expr *ne = new expr (e);
1026 ne->is_commutative = false;
1027 for (unsigned j = 0; j < result[i].length (); ++j)
1028 ne->append_op (result[i][j]);
1029 ret.safe_push (ne);
1032 if (!e->is_commutative)
1033 return ret;
1035 /* The operation is always binary if it isn't inherently commutative. */
1036 int natural_opno = commutative_op (e->operation);
1037 unsigned int opno = natural_opno >= 0 ? natural_opno : 0;
1038 for (unsigned i = 0; i < result.length (); ++i)
1040 expr *ne = new expr (e);
1041 if (operator_id *r = dyn_cast <operator_id *> (ne->operation))
1043 if (comparison_code_p (r->code))
1044 ne->operation = swap_tree_comparison (r);
1046 else if (user_id *p = dyn_cast <user_id *> (ne->operation))
1048 bool found_compare = false;
1049 for (unsigned j = 0; j < p->substitutes.length (); ++j)
1050 if (operator_id *q = dyn_cast <operator_id *> (p->substitutes[j]))
1052 if (comparison_code_p (q->code)
1053 && swap_tree_comparison (q) != q)
1055 found_compare = true;
1056 break;
1059 if (found_compare)
1061 user_id *newop = new user_id ("<internal>");
1062 for (unsigned j = 0; j < p->substitutes.length (); ++j)
1064 id_base *subst = p->substitutes[j];
1065 if (operator_id *q = dyn_cast <operator_id *> (subst))
1067 if (comparison_code_p (q->code))
1068 subst = swap_tree_comparison (q);
1070 newop->substitutes.safe_push (subst);
1072 ne->operation = newop;
1073 /* Search for 'p' inside the for vector and push 'newop'
1074 to the same level. */
1075 for (unsigned j = 0; newop && j < for_vec.length (); ++j)
1076 for (unsigned k = 0; k < for_vec[j].length (); ++k)
1077 if (for_vec[j][k] == p)
1079 for_vec[j].safe_push (newop);
1080 newop = NULL;
1081 break;
1085 ne->is_commutative = false;
1086 for (unsigned j = 0; j < result[i].length (); ++j)
1088 int old_j = (j == opno ? opno + 1 : j == opno + 1 ? opno : j);
1089 ne->append_op (result[i][old_j]);
1091 ret.safe_push (ne);
1094 return ret;
1097 /* Lower operations marked as commutative in the AST of S and push
1098 the resulting patterns to SIMPLIFIERS. */
1100 static void
1101 lower_commutative (simplify *s, vec<simplify *>& simplifiers)
1103 vec<operand *> matchers = commutate (s->match, s->for_vec);
1104 for (unsigned i = 0; i < matchers.length (); ++i)
1106 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
1107 s->for_vec, s->capture_ids);
1108 simplifiers.safe_push (ns);
1112 /* Strip conditional operations using group GRP from O and its
1113 children if STRIP, else replace them with an unconditional operation. */
1115 operand *
1116 lower_opt (operand *o, unsigned char grp, bool strip)
1118 if (capture *c = dyn_cast<capture *> (o))
1120 if (c->what)
1121 return new capture (c->location, c->where,
1122 lower_opt (c->what, grp, strip),
1123 c->value_match);
1124 else
1125 return c;
1128 expr *e = dyn_cast<expr *> (o);
1129 if (!e)
1130 return o;
1132 if (e->opt_grp == grp)
1134 if (strip)
1135 return lower_opt (e->ops[0], grp, strip);
1137 expr *ne = new expr (e);
1138 ne->opt_grp = 0;
1139 ne->append_op (lower_opt (e->ops[0], grp, strip));
1140 return ne;
1143 expr *ne = new expr (e);
1144 for (unsigned i = 0; i < e->ops.length (); ++i)
1145 ne->append_op (lower_opt (e->ops[i], grp, strip));
1147 return ne;
1150 /* Determine whether O or its children uses the conditional operation
1151 group GRP. */
1153 static bool
1154 has_opt (operand *o, unsigned char grp)
1156 if (capture *c = dyn_cast<capture *> (o))
1158 if (c->what)
1159 return has_opt (c->what, grp);
1160 else
1161 return false;
1164 expr *e = dyn_cast<expr *> (o);
1165 if (!e)
1166 return false;
1168 if (e->opt_grp == grp)
1169 return true;
1171 for (unsigned i = 0; i < e->ops.length (); ++i)
1172 if (has_opt (e->ops[i], grp))
1173 return true;
1175 return false;
1178 /* Lower conditional convert operators in O, expanding it to a vector
1179 if required. */
1181 static vec<operand *>
1182 lower_opt (operand *o)
1184 vec<operand *> v1 = vNULL, v2;
1186 v1.safe_push (o);
1188 /* Conditional operations are lowered to a pattern with the
1189 operation and one without. All different conditional operation
1190 groups are lowered separately. */
1192 for (unsigned i = 1; i <= 10; ++i)
1194 v2 = vNULL;
1195 for (unsigned j = 0; j < v1.length (); ++j)
1196 if (has_opt (v1[j], i))
1198 v2.safe_push (lower_opt (v1[j], i, false));
1199 v2.safe_push (lower_opt (v1[j], i, true));
1202 if (v2 != vNULL)
1204 v1 = vNULL;
1205 for (unsigned j = 0; j < v2.length (); ++j)
1206 v1.safe_push (v2[j]);
1210 return v1;
1213 /* Lower conditional convert operators in the AST of S and push
1214 the resulting multiple patterns to SIMPLIFIERS. */
1216 static void
1217 lower_opt (simplify *s, vec<simplify *>& simplifiers)
1219 vec<operand *> matchers = lower_opt (s->match);
1220 for (unsigned i = 0; i < matchers.length (); ++i)
1222 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
1223 s->for_vec, s->capture_ids);
1224 simplifiers.safe_push (ns);
1228 /* Lower the compare operand of COND_EXPRs to a
1229 GENERIC and a GIMPLE variant. */
1231 static vec<operand *>
1232 lower_cond (operand *o)
1234 vec<operand *> ro = vNULL;
1236 if (capture *c = dyn_cast<capture *> (o))
1238 if (c->what)
1240 vec<operand *> lop = vNULL;
1241 lop = lower_cond (c->what);
1243 for (unsigned i = 0; i < lop.length (); ++i)
1244 ro.safe_push (new capture (c->location, c->where, lop[i],
1245 c->value_match));
1246 return ro;
1250 expr *e = dyn_cast<expr *> (o);
1251 if (!e || e->ops.length () == 0)
1253 ro.safe_push (o);
1254 return ro;
1257 vec< vec<operand *> > ops_vector = vNULL;
1258 for (unsigned i = 0; i < e->ops.length (); ++i)
1259 ops_vector.safe_push (lower_cond (e->ops[i]));
1261 auto_vec< vec<operand *> > result;
1262 auto_vec<operand *> v (e->ops.length ());
1263 v.quick_grow_cleared (e->ops.length ());
1264 cartesian_product (ops_vector, result, v, 0);
1266 for (unsigned i = 0; i < result.length (); ++i)
1268 expr *ne = new expr (e);
1269 for (unsigned j = 0; j < result[i].length (); ++j)
1270 ne->append_op (result[i][j]);
1271 ro.safe_push (ne);
1272 /* If this is a COND with a captured expression or an
1273 expression with two operands then also match a GENERIC
1274 form on the compare. */
1275 if (*e->operation == COND_EXPR
1276 && ((is_a <capture *> (e->ops[0])
1277 && as_a <capture *> (e->ops[0])->what
1278 && is_a <expr *> (as_a <capture *> (e->ops[0])->what)
1279 && as_a <expr *>
1280 (as_a <capture *> (e->ops[0])->what)->ops.length () == 2)
1281 || (is_a <expr *> (e->ops[0])
1282 && as_a <expr *> (e->ops[0])->ops.length () == 2)))
1284 ne = new expr (e);
1285 for (unsigned j = 0; j < result[i].length (); ++j)
1286 ne->append_op (result[i][j]);
1287 if (capture *c = dyn_cast <capture *> (ne->ops[0]))
1289 expr *ocmp = as_a <expr *> (c->what);
1290 expr *cmp = new expr (ocmp);
1291 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
1292 cmp->append_op (ocmp->ops[j]);
1293 cmp->is_generic = true;
1294 ne->ops[0] = new capture (c->location, c->where, cmp,
1295 c->value_match);
1297 else
1299 expr *ocmp = as_a <expr *> (ne->ops[0]);
1300 expr *cmp = new expr (ocmp);
1301 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
1302 cmp->append_op (ocmp->ops[j]);
1303 cmp->is_generic = true;
1304 ne->ops[0] = cmp;
1306 ro.safe_push (ne);
1310 return ro;
1313 /* Lower the compare operand of COND_EXPRs to a
1314 GENERIC and a GIMPLE variant. */
1316 static void
1317 lower_cond (simplify *s, vec<simplify *>& simplifiers)
1319 vec<operand *> matchers = lower_cond (s->match);
1320 for (unsigned i = 0; i < matchers.length (); ++i)
1322 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
1323 s->for_vec, s->capture_ids);
1324 ns->for_subst_vec.safe_splice (s->for_subst_vec);
1325 simplifiers.safe_push (ns);
1329 /* Return true if O refers to ID. */
1331 bool
1332 contains_id (operand *o, user_id *id)
1334 if (capture *c = dyn_cast<capture *> (o))
1335 return c->what && contains_id (c->what, id);
1337 if (expr *e = dyn_cast<expr *> (o))
1339 if (e->operation == id)
1340 return true;
1341 for (unsigned i = 0; i < e->ops.length (); ++i)
1342 if (contains_id (e->ops[i], id))
1343 return true;
1344 return false;
1347 if (with_expr *w = dyn_cast <with_expr *> (o))
1348 return (contains_id (w->with, id)
1349 || contains_id (w->subexpr, id));
1351 if (if_expr *ife = dyn_cast <if_expr *> (o))
1352 return (contains_id (ife->cond, id)
1353 || contains_id (ife->trueexpr, id)
1354 || (ife->falseexpr && contains_id (ife->falseexpr, id)));
1356 if (c_expr *ce = dyn_cast<c_expr *> (o))
1357 return ce->capture_ids && ce->capture_ids->get (id->id);
1359 return false;
1363 /* In AST operand O replace operator ID with operator WITH. */
1365 operand *
1366 replace_id (operand *o, user_id *id, id_base *with)
1368 /* Deep-copy captures and expressions, replacing operations as
1369 needed. */
1370 if (capture *c = dyn_cast<capture *> (o))
1372 if (!c->what)
1373 return c;
1374 return new capture (c->location, c->where,
1375 replace_id (c->what, id, with), c->value_match);
1377 else if (expr *e = dyn_cast<expr *> (o))
1379 expr *ne = new expr (e);
1380 if (e->operation == id)
1381 ne->operation = with;
1382 for (unsigned i = 0; i < e->ops.length (); ++i)
1383 ne->append_op (replace_id (e->ops[i], id, with));
1384 return ne;
1386 else if (with_expr *w = dyn_cast <with_expr *> (o))
1388 with_expr *nw = new with_expr (w->location);
1389 nw->with = as_a <c_expr *> (replace_id (w->with, id, with));
1390 nw->subexpr = replace_id (w->subexpr, id, with);
1391 return nw;
1393 else if (if_expr *ife = dyn_cast <if_expr *> (o))
1395 if_expr *nife = new if_expr (ife->location);
1396 nife->cond = as_a <c_expr *> (replace_id (ife->cond, id, with));
1397 nife->trueexpr = replace_id (ife->trueexpr, id, with);
1398 if (ife->falseexpr)
1399 nife->falseexpr = replace_id (ife->falseexpr, id, with);
1400 return nife;
1403 /* For c_expr we simply record a string replacement table which is
1404 applied at code-generation time. */
1405 if (c_expr *ce = dyn_cast<c_expr *> (o))
1407 vec<c_expr::id_tab> ids = ce->ids.copy ();
1408 ids.safe_push (c_expr::id_tab (id->id, with->id));
1409 return new c_expr (ce->r, ce->location,
1410 ce->code, ce->nr_stmts, ids, ce->capture_ids);
1413 return o;
1416 /* Return true if the binary operator OP is ok for delayed substitution
1417 during for lowering. */
1419 static bool
1420 binary_ok (operator_id *op)
1422 switch (op->code)
1424 case PLUS_EXPR:
1425 case MINUS_EXPR:
1426 case MULT_EXPR:
1427 case TRUNC_DIV_EXPR:
1428 case CEIL_DIV_EXPR:
1429 case FLOOR_DIV_EXPR:
1430 case ROUND_DIV_EXPR:
1431 case TRUNC_MOD_EXPR:
1432 case CEIL_MOD_EXPR:
1433 case FLOOR_MOD_EXPR:
1434 case ROUND_MOD_EXPR:
1435 case RDIV_EXPR:
1436 case EXACT_DIV_EXPR:
1437 case MIN_EXPR:
1438 case MAX_EXPR:
1439 case BIT_IOR_EXPR:
1440 case BIT_XOR_EXPR:
1441 case BIT_AND_EXPR:
1442 return true;
1443 default:
1444 return false;
1448 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1450 static void
1451 lower_for (simplify *sin, vec<simplify *>& simplifiers)
1453 vec<vec<user_id *> >& for_vec = sin->for_vec;
1454 unsigned worklist_start = 0;
1455 auto_vec<simplify *> worklist;
1456 worklist.safe_push (sin);
1458 /* Lower each recorded for separately, operating on the
1459 set of simplifiers created by the previous one.
1460 Lower inner-to-outer so inner for substitutes can refer
1461 to operators replaced by outer fors. */
1462 for (int fi = for_vec.length () - 1; fi >= 0; --fi)
1464 vec<user_id *>& ids = for_vec[fi];
1465 unsigned n_ids = ids.length ();
1466 unsigned max_n_opers = 0;
1467 bool can_delay_subst = (sin->kind == simplify::SIMPLIFY);
1468 for (unsigned i = 0; i < n_ids; ++i)
1470 if (ids[i]->substitutes.length () > max_n_opers)
1471 max_n_opers = ids[i]->substitutes.length ();
1472 /* Require that all substitutes are of the same kind so that
1473 if we delay substitution to the result op code generation
1474 can look at the first substitute for deciding things like
1475 types of operands. */
1476 enum id_base::id_kind kind = ids[i]->substitutes[0]->kind;
1477 for (unsigned j = 0; j < ids[i]->substitutes.length (); ++j)
1478 if (ids[i]->substitutes[j]->kind != kind)
1479 can_delay_subst = false;
1480 else if (operator_id *op
1481 = dyn_cast <operator_id *> (ids[i]->substitutes[j]))
1483 operator_id *op0
1484 = as_a <operator_id *> (ids[i]->substitutes[0]);
1485 if (strcmp (op->tcc, "tcc_comparison") == 0
1486 && strcmp (op0->tcc, "tcc_comparison") == 0)
1488 /* Unfortunately we can't just allow all tcc_binary. */
1489 else if (strcmp (op->tcc, "tcc_binary") == 0
1490 && strcmp (op0->tcc, "tcc_binary") == 0
1491 && binary_ok (op)
1492 && binary_ok (op0))
1494 else if ((strcmp (op->id + 1, "SHIFT_EXPR") == 0
1495 || strcmp (op->id + 1, "ROTATE_EXPR") == 0)
1496 && (strcmp (op0->id + 1, "SHIFT_EXPR") == 0
1497 || strcmp (op0->id + 1, "ROTATE_EXPR") == 0))
1499 else
1500 can_delay_subst = false;
1502 else if (is_a <fn_id *> (ids[i]->substitutes[j]))
1504 else
1505 can_delay_subst = false;
1508 unsigned worklist_end = worklist.length ();
1509 for (unsigned si = worklist_start; si < worklist_end; ++si)
1511 simplify *s = worklist[si];
1512 for (unsigned j = 0; j < max_n_opers; ++j)
1514 operand *match_op = s->match;
1515 operand *result_op = s->result;
1516 auto_vec<std::pair<user_id *, id_base *> > subst (n_ids);
1517 bool skip = false;
1518 for (unsigned i = 0; i < n_ids; ++i)
1520 user_id *id = ids[i];
1521 id_base *oper = id->substitutes[j % id->substitutes.length ()];
1522 if (oper == null_id
1523 && (contains_id (match_op, id)
1524 || contains_id (result_op, id)))
1526 skip = true;
1527 break;
1529 subst.quick_push (std::make_pair (id, oper));
1530 match_op = replace_id (match_op, id, oper);
1531 if (result_op
1532 && !can_delay_subst)
1533 result_op = replace_id (result_op, id, oper);
1535 if (skip)
1536 continue;
1538 simplify *ns = new simplify (s->kind, s->id, match_op, result_op,
1539 vNULL, s->capture_ids);
1540 ns->for_subst_vec.safe_splice (s->for_subst_vec);
1541 if (result_op
1542 && can_delay_subst)
1543 ns->for_subst_vec.safe_splice (subst);
1545 worklist.safe_push (ns);
1548 worklist_start = worklist_end;
1551 /* Copy out the result from the last for lowering. */
1552 for (unsigned i = worklist_start; i < worklist.length (); ++i)
1553 simplifiers.safe_push (worklist[i]);
1556 /* Lower the AST for everything in SIMPLIFIERS. */
1558 static void
1559 lower (vec<simplify *>& simplifiers, bool gimple)
1561 auto_vec<simplify *> out_simplifiers;
1562 for (auto s: simplifiers)
1563 lower_opt (s, out_simplifiers);
1565 simplifiers.truncate (0);
1566 for (auto s: out_simplifiers)
1567 lower_commutative (s, simplifiers);
1569 /* Lower for needs to happen before lowering cond
1570 to support (for cnd (cond vec_cond)). This is
1571 safe as substitution delay does not happen for
1572 cond or vec_cond. */
1573 out_simplifiers.truncate (0);
1574 for (auto s: simplifiers)
1575 lower_for (s, out_simplifiers);
1577 simplifiers.truncate (0);
1578 if (gimple)
1579 for (auto s: out_simplifiers)
1580 lower_cond (s, simplifiers);
1581 else
1582 simplifiers.safe_splice (out_simplifiers);
1588 /* The decision tree built for generating GIMPLE and GENERIC pattern
1589 matching code. It represents the 'match' expression of all
1590 simplifies and has those as its leafs. */
1592 class dt_simplify;
1594 /* A hash-map collecting semantically equivalent leafs in the decision
1595 tree for splitting out to separate functions. */
1596 struct sinfo
1598 dt_simplify *s;
1600 const char *fname;
1601 unsigned cnt;
1604 struct sinfo_hashmap_traits : simple_hashmap_traits<pointer_hash<dt_simplify>,
1605 sinfo *>
1607 static inline hashval_t hash (const key_type &);
1608 static inline bool equal_keys (const key_type &, const key_type &);
1609 template <typename T> static inline void remove (T &) {}
1612 typedef hash_map<void * /* unused */, sinfo *, sinfo_hashmap_traits>
1613 sinfo_map_t;
1615 /* Current simplifier ID we are processing during insertion into the
1616 decision tree. */
1617 static unsigned current_id;
1619 /* Decision tree base class, used for DT_NODE. */
1621 class dt_node
1623 public:
1624 enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY };
1626 enum dt_type type;
1627 unsigned level;
1628 dt_node *parent;
1629 vec<dt_node *> kids;
1631 /* Statistics. */
1632 unsigned num_leafs;
1633 unsigned total_size;
1634 unsigned max_level;
1636 dt_node (enum dt_type type_, dt_node *parent_)
1637 : type (type_), level (0), parent (parent_), kids (vNULL) {}
1639 dt_node *append_node (dt_node *);
1640 dt_node *append_op (operand *, dt_node *parent, unsigned pos);
1641 dt_node *append_true_op (operand *, dt_node *parent, unsigned pos);
1642 dt_node *append_match_op (operand *, dt_operand *, dt_node *parent,
1643 unsigned pos);
1644 dt_node *append_simplify (simplify *, unsigned, dt_operand **);
1646 virtual void gen (FILE *, int, bool, int) {}
1648 void gen_kids (FILE *, int, bool, int);
1649 void gen_kids_1 (FILE *, int, bool, int,
1650 const vec<dt_operand *> &, const vec<dt_operand *> &,
1651 const vec<dt_operand *> &, const vec<dt_operand *> &,
1652 const vec<dt_operand *> &, const vec<dt_node *> &);
1654 void analyze (sinfo_map_t &);
1657 /* Generic decision tree node used for DT_OPERAND, DT_MATCH and DT_TRUE. */
1659 class dt_operand : public dt_node
1661 public:
1662 operand *op;
1663 dt_operand *match_dop;
1664 unsigned pos;
1665 bool value_match;
1666 unsigned for_id;
1668 dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_,
1669 dt_operand *parent_, unsigned pos_)
1670 : dt_node (type, parent_), op (op_), match_dop (match_dop_),
1671 pos (pos_), value_match (false), for_id (current_id) {}
1673 void gen (FILE *, int, bool, int) final override;
1674 unsigned gen_predicate (FILE *, int, const char *, bool);
1675 unsigned gen_match_op (FILE *, int, const char *, bool);
1677 unsigned gen_gimple_expr (FILE *, int, int);
1678 unsigned gen_generic_expr (FILE *, int, const char *);
1680 char *get_name (char *);
1681 void gen_opname (char *, unsigned);
1684 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1686 class dt_simplify : public dt_node
1688 public:
1689 simplify *s;
1690 unsigned pattern_no;
1691 dt_operand **indexes;
1692 sinfo *info;
1694 dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_)
1695 : dt_node (DT_SIMPLIFY, NULL), s (s_), pattern_no (pattern_no_),
1696 indexes (indexes_), info (NULL) {}
1698 void gen_1 (FILE *, int, bool, operand *);
1699 void gen (FILE *f, int, bool, int) final override;
1702 template<>
1703 template<>
1704 inline bool
1705 is_a_helper <dt_operand *>::test (dt_node *n)
1707 return (n->type == dt_node::DT_OPERAND
1708 || n->type == dt_node::DT_MATCH
1709 || n->type == dt_node::DT_TRUE);
1712 template<>
1713 template<>
1714 inline bool
1715 is_a_helper <dt_simplify *>::test (dt_node *n)
1717 return n->type == dt_node::DT_SIMPLIFY;
1722 /* A container for the actual decision tree. */
1724 class decision_tree
1726 public:
1727 dt_node *root;
1729 void insert (class simplify *, unsigned);
1730 void gen (FILE *f, bool gimple);
1731 void print (FILE *f = stderr);
1733 decision_tree () { root = new dt_node (dt_node::DT_NODE, NULL); }
1735 static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes,
1736 unsigned pos = 0, dt_node *parent = 0);
1737 static dt_node *find_node (vec<dt_node *>&, dt_node *);
1738 static bool cmp_node (dt_node *, dt_node *);
1739 static void print_node (dt_node *, FILE *f = stderr, unsigned = 0);
1742 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1744 bool
1745 cmp_operand (operand *o1, operand *o2)
1747 if (!o1 || !o2 || o1->type != o2->type)
1748 return false;
1750 if (o1->type == operand::OP_PREDICATE)
1752 predicate *p1 = as_a<predicate *>(o1);
1753 predicate *p2 = as_a<predicate *>(o2);
1754 return p1->p == p2->p;
1756 else if (o1->type == operand::OP_EXPR)
1758 expr *e1 = static_cast<expr *>(o1);
1759 expr *e2 = static_cast<expr *>(o2);
1760 return (e1->operation == e2->operation
1761 && e1->is_generic == e2->is_generic);
1763 else
1764 return false;
1767 /* Compare two decision tree nodes N1 and N2 and return true if they
1768 are equal. */
1770 bool
1771 decision_tree::cmp_node (dt_node *n1, dt_node *n2)
1773 if (!n1 || !n2 || n1->type != n2->type)
1774 return false;
1776 if (n1 == n2)
1777 return true;
1779 if (n1->type == dt_node::DT_TRUE)
1780 return false;
1782 if (n1->type == dt_node::DT_OPERAND)
1783 return cmp_operand ((as_a<dt_operand *> (n1))->op,
1784 (as_a<dt_operand *> (n2))->op);
1785 else if (n1->type == dt_node::DT_MATCH)
1786 return (((as_a<dt_operand *> (n1))->match_dop
1787 == (as_a<dt_operand *> (n2))->match_dop)
1788 && ((as_a<dt_operand *> (n1))->value_match
1789 == (as_a<dt_operand *> (n2))->value_match));
1790 return false;
1793 /* Search OPS for a decision tree node like P and return it if found. */
1795 dt_node *
1796 decision_tree::find_node (vec<dt_node *>& ops, dt_node *p)
1798 /* We can merge adjacent DT_TRUE. */
1799 if (p->type == dt_node::DT_TRUE
1800 && !ops.is_empty ()
1801 && ops.last ()->type == dt_node::DT_TRUE)
1802 return ops.last ();
1803 dt_operand *true_node = NULL;
1804 for (int i = ops.length () - 1; i >= 0; --i)
1806 /* But we can't merge across DT_TRUE nodes as they serve as
1807 pattern order barriers to make sure that patterns apply
1808 in order of appearance in case multiple matches are possible. */
1809 if (ops[i]->type == dt_node::DT_TRUE)
1811 if (! true_node
1812 || as_a <dt_operand *> (ops[i])->for_id > true_node->for_id)
1813 true_node = as_a <dt_operand *> (ops[i]);
1815 if (decision_tree::cmp_node (ops[i], p))
1817 /* Unless we are processing the same pattern or the blocking
1818 pattern is before the one we are going to merge with. */
1819 if (true_node
1820 && true_node->for_id != current_id
1821 && true_node->for_id > as_a <dt_operand *> (ops[i])->for_id)
1823 if (verbose >= 1)
1825 location_t p_loc = 0;
1826 if (p->type == dt_node::DT_OPERAND)
1827 p_loc = as_a <dt_operand *> (p)->op->location;
1828 location_t op_loc = 0;
1829 if (ops[i]->type == dt_node::DT_OPERAND)
1830 op_loc = as_a <dt_operand *> (ops[i])->op->location;
1831 location_t true_loc = 0;
1832 true_loc = true_node->op->location;
1833 warning_at (p_loc,
1834 "failed to merge decision tree node");
1835 warning_at (op_loc,
1836 "with the following");
1837 warning_at (true_loc,
1838 "because of the following which serves as ordering "
1839 "barrier");
1841 return NULL;
1843 return ops[i];
1846 return NULL;
1849 /* Append N to the decision tree if it there is not already an existing
1850 identical child. */
1852 dt_node *
1853 dt_node::append_node (dt_node *n)
1855 dt_node *kid;
1857 kid = decision_tree::find_node (kids, n);
1858 if (kid)
1859 return kid;
1861 kids.safe_push (n);
1862 n->level = this->level + 1;
1864 return n;
1867 /* Append OP to the decision tree. */
1869 dt_node *
1870 dt_node::append_op (operand *op, dt_node *parent, unsigned pos)
1872 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
1873 dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos);
1874 return append_node (n);
1877 /* Append a DT_TRUE decision tree node. */
1879 dt_node *
1880 dt_node::append_true_op (operand *op, dt_node *parent, unsigned pos)
1882 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
1883 dt_operand *n = new dt_operand (DT_TRUE, op, 0, parent_, pos);
1884 return append_node (n);
1887 /* Append a DT_MATCH decision tree node. */
1889 dt_node *
1890 dt_node::append_match_op (operand *op, dt_operand *match_dop,
1891 dt_node *parent, unsigned pos)
1893 dt_operand *parent_ = as_a<dt_operand *> (parent);
1894 dt_operand *n = new dt_operand (DT_MATCH, op, match_dop, parent_, pos);
1895 return append_node (n);
1898 /* Append S to the decision tree. */
1900 dt_node *
1901 dt_node::append_simplify (simplify *s, unsigned pattern_no,
1902 dt_operand **indexes)
1904 dt_simplify *s2;
1905 dt_simplify *n = new dt_simplify (s, pattern_no, indexes);
1906 for (unsigned i = 0; i < kids.length (); ++i)
1907 if ((s2 = dyn_cast <dt_simplify *> (kids[i]))
1908 && (verbose >= 1
1909 || s->match->location != s2->s->match->location))
1911 /* With a nested patters, it's hard to avoid these in order
1912 to keep match.pd rules relatively small. */
1913 warning_at (s->match->location, "duplicate pattern");
1914 warning_at (s2->s->match->location, "previous pattern defined here");
1915 print_operand (s->match, stderr);
1916 fprintf (stderr, "\n");
1918 return append_node (n);
1921 /* Analyze the node and its children. */
1923 void
1924 dt_node::analyze (sinfo_map_t &map)
1926 num_leafs = 0;
1927 total_size = 1;
1928 max_level = level;
1930 if (type == DT_SIMPLIFY)
1932 /* Populate the map of equivalent simplifies. */
1933 dt_simplify *s = as_a <dt_simplify *> (this);
1934 bool existed;
1935 sinfo *&si = map.get_or_insert (s, &existed);
1936 if (!existed)
1938 si = new sinfo;
1939 si->s = s;
1940 si->cnt = 1;
1941 si->fname = NULL;
1943 else
1944 si->cnt++;
1945 s->info = si;
1946 num_leafs = 1;
1947 return;
1950 for (unsigned i = 0; i < kids.length (); ++i)
1952 kids[i]->analyze (map);
1953 num_leafs += kids[i]->num_leafs;
1954 total_size += kids[i]->total_size;
1955 max_level = MAX (max_level, kids[i]->max_level);
1959 /* Insert O into the decision tree and return the decision tree node found
1960 or created. */
1962 dt_node *
1963 decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes,
1964 unsigned pos, dt_node *parent)
1966 dt_node *q, *elm = 0;
1968 if (capture *c = dyn_cast<capture *> (o))
1970 unsigned capt_index = c->where;
1972 if (indexes[capt_index] == 0)
1974 if (c->what)
1975 q = insert_operand (p, c->what, indexes, pos, parent);
1976 else
1978 q = elm = p->append_true_op (o, parent, pos);
1979 goto at_assert_elm;
1981 // get to the last capture
1982 for (operand *what = c->what;
1983 what && is_a<capture *> (what);
1984 c = as_a<capture *> (what), what = c->what)
1987 if (!c->what)
1989 unsigned cc_index = c->where;
1990 dt_operand *match_op = indexes[cc_index];
1992 dt_operand temp (dt_node::DT_TRUE, 0, 0, 0, 0);
1993 elm = decision_tree::find_node (p->kids, &temp);
1995 if (elm == 0)
1997 dt_operand match (dt_node::DT_MATCH, 0, match_op, 0, 0);
1998 match.value_match = c->value_match;
1999 elm = decision_tree::find_node (p->kids, &match);
2002 else
2004 dt_operand temp (dt_node::DT_OPERAND, c->what, 0, 0, 0);
2005 elm = decision_tree::find_node (p->kids, &temp);
2008 at_assert_elm:
2009 gcc_assert (elm->type == dt_node::DT_TRUE
2010 || elm->type == dt_node::DT_OPERAND
2011 || elm->type == dt_node::DT_MATCH);
2012 indexes[capt_index] = static_cast<dt_operand *> (elm);
2013 return q;
2015 else
2017 p = p->append_match_op (o, indexes[capt_index], parent, pos);
2018 as_a <dt_operand *>(p)->value_match = c->value_match;
2019 if (c->what)
2020 return insert_operand (p, c->what, indexes, 0, p);
2021 else
2022 return p;
2025 p = p->append_op (o, parent, pos);
2026 q = p;
2028 if (expr *e = dyn_cast <expr *>(o))
2030 for (unsigned i = 0; i < e->ops.length (); ++i)
2031 q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p);
2034 return q;
2037 /* Insert S into the decision tree. */
2039 void
2040 decision_tree::insert (class simplify *s, unsigned pattern_no)
2042 current_id = s->id;
2043 dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1);
2044 dt_node *p = decision_tree::insert_operand (root, s->match, indexes);
2045 p->append_simplify (s, pattern_no, indexes);
2048 /* Debug functions to dump the decision tree. */
2050 DEBUG_FUNCTION void
2051 decision_tree::print_node (dt_node *p, FILE *f, unsigned indent)
2053 if (p->type == dt_node::DT_NODE)
2054 fprintf (f, "root");
2055 else
2057 fprintf (f, "|");
2058 for (unsigned i = 0; i < indent; i++)
2059 fprintf (f, "-");
2061 if (p->type == dt_node::DT_OPERAND)
2063 dt_operand *dop = static_cast<dt_operand *>(p);
2064 print_operand (dop->op, f, true);
2066 else if (p->type == dt_node::DT_TRUE)
2067 fprintf (f, "true");
2068 else if (p->type == dt_node::DT_MATCH)
2069 fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop));
2070 else if (p->type == dt_node::DT_SIMPLIFY)
2072 dt_simplify *s = static_cast<dt_simplify *> (p);
2073 fprintf (f, "simplify_%u { ", s->pattern_no);
2074 for (int i = 0; i <= s->s->capture_max; ++i)
2075 fprintf (f, "%p, ", (void *) s->indexes[i]);
2076 fprintf (f, " } ");
2078 if (is_a <dt_operand *> (p))
2079 fprintf (f, " [%u]", as_a <dt_operand *> (p)->for_id);
2082 fprintf (stderr, " (%p, %p), %u, %u\n",
2083 (void *) p, (void *) p->parent, p->level, p->kids.length ());
2085 for (unsigned i = 0; i < p->kids.length (); ++i)
2086 decision_tree::print_node (p->kids[i], f, indent + 2);
2089 DEBUG_FUNCTION void
2090 decision_tree::print (FILE *f)
2092 return decision_tree::print_node (root, f);
2096 /* For GENERIC we have to take care of wrapping multiple-used
2097 expressions with side-effects in save_expr and preserve side-effects
2098 of expressions with omit_one_operand. Analyze captures in
2099 match, result and with expressions and perform early-outs
2100 on the outermost match expression operands for cases we cannot
2101 handle. */
2103 class capture_info
2105 public:
2106 capture_info (simplify *s, operand *, bool);
2107 void walk_match (operand *o, unsigned toplevel_arg, bool, bool);
2108 bool walk_result (operand *o, bool, operand *);
2109 void walk_c_expr (c_expr *);
2111 struct cinfo
2113 bool expr_p;
2114 bool cse_p;
2115 bool force_no_side_effects_p;
2116 bool force_single_use;
2117 bool cond_expr_cond_p;
2118 unsigned long toplevel_msk;
2119 unsigned match_use_count;
2120 unsigned result_use_count;
2121 unsigned same_as;
2122 capture *c;
2125 auto_vec<cinfo> info;
2126 unsigned long force_no_side_effects;
2127 bool gimple;
2130 /* Analyze captures in S. */
2132 capture_info::capture_info (simplify *s, operand *result, bool gimple_)
2134 gimple = gimple_;
2136 expr *e;
2137 if (s->kind == simplify::MATCH)
2139 force_no_side_effects = -1;
2140 return;
2143 force_no_side_effects = 0;
2144 info.safe_grow_cleared (s->capture_max + 1, true);
2145 for (int i = 0; i <= s->capture_max; ++i)
2146 info[i].same_as = i;
2148 e = as_a <expr *> (s->match);
2149 for (unsigned i = 0; i < e->ops.length (); ++i)
2150 walk_match (e->ops[i], i,
2151 (i != 0 && *e->operation == COND_EXPR)
2152 || *e->operation == TRUTH_ANDIF_EXPR
2153 || *e->operation == TRUTH_ORIF_EXPR,
2154 i == 0 && *e->operation == COND_EXPR);
2156 walk_result (s->result, false, result);
2159 /* Analyze captures in the match expression piece O. */
2161 void
2162 capture_info::walk_match (operand *o, unsigned toplevel_arg,
2163 bool conditional_p, bool cond_expr_cond_p)
2165 if (capture *c = dyn_cast <capture *> (o))
2167 unsigned where = c->where;
2168 info[where].match_use_count++;
2169 info[where].toplevel_msk |= 1 << toplevel_arg;
2170 info[where].force_no_side_effects_p |= conditional_p;
2171 info[where].cond_expr_cond_p |= cond_expr_cond_p;
2172 if (!info[where].c)
2173 info[where].c = c;
2174 if (!c->what)
2175 return;
2176 /* Recurse to exprs and captures. */
2177 if (is_a <capture *> (c->what)
2178 || is_a <expr *> (c->what))
2179 walk_match (c->what, toplevel_arg, conditional_p, false);
2180 /* We need to look past multiple captures to find a captured
2181 expression as with conditional converts two captures
2182 can be collapsed onto the same expression. Also collect
2183 what captures capture the same thing. */
2184 while (c->what && is_a <capture *> (c->what))
2186 c = as_a <capture *> (c->what);
2187 if (info[c->where].same_as != c->where
2188 && info[c->where].same_as != info[where].same_as)
2189 fatal_at (c->location, "cannot handle this collapsed capture");
2190 info[c->where].same_as = info[where].same_as;
2192 /* Mark expr (non-leaf) captures and forced single-use exprs. */
2193 expr *e;
2194 if (c->what
2195 && (e = dyn_cast <expr *> (c->what)))
2197 /* Zero-operand expression captures like ADDR_EXPR@0 are
2198 similar as predicates -- if they are not mentioned in
2199 the result we have to force them to have no side-effects. */
2200 if (e->ops.length () != 0)
2201 info[where].expr_p = true;
2202 info[where].force_single_use |= e->force_single_use;
2205 else if (expr *e = dyn_cast <expr *> (o))
2207 for (unsigned i = 0; i < e->ops.length (); ++i)
2209 bool cond_p = conditional_p;
2210 bool expr_cond_p = false;
2211 if (i != 0 && *e->operation == COND_EXPR)
2212 cond_p = true;
2213 else if (*e->operation == TRUTH_ANDIF_EXPR
2214 || *e->operation == TRUTH_ORIF_EXPR)
2215 cond_p = true;
2216 if (i == 0
2217 && *e->operation == COND_EXPR)
2218 expr_cond_p = true;
2219 walk_match (e->ops[i], toplevel_arg, cond_p, expr_cond_p);
2222 else if (is_a <predicate *> (o))
2224 /* Mark non-captured leafs toplevel arg for checking. */
2225 force_no_side_effects |= 1 << toplevel_arg;
2226 if (verbose >= 1
2227 && !gimple)
2228 warning_at (o->location,
2229 "forcing no side-effects on possibly lost leaf");
2231 else
2232 gcc_unreachable ();
2235 /* Analyze captures in the result expression piece O. Return true
2236 if RESULT was visited in one of the children. Only visit
2237 non-if/with children if they are rooted on RESULT. */
2239 bool
2240 capture_info::walk_result (operand *o, bool conditional_p, operand *result)
2242 if (capture *c = dyn_cast <capture *> (o))
2244 unsigned where = info[c->where].same_as;
2245 info[where].result_use_count++;
2246 /* If we substitute an expression capture we don't know
2247 which captures this will end up using (well, we don't
2248 compute that). Force the uses to be side-effect free
2249 which means forcing the toplevels that reach the
2250 expression side-effect free. */
2251 if (info[where].expr_p)
2252 force_no_side_effects |= info[where].toplevel_msk;
2253 /* Mark CSE capture uses as forced to have no side-effects. */
2254 if (c->what
2255 && is_a <expr *> (c->what))
2257 info[where].cse_p = true;
2258 walk_result (c->what, true, result);
2261 else if (expr *e = dyn_cast <expr *> (o))
2263 id_base *opr = e->operation;
2264 if (user_id *uid = dyn_cast <user_id *> (opr))
2265 opr = uid->substitutes[0];
2266 for (unsigned i = 0; i < e->ops.length (); ++i)
2268 bool cond_p = conditional_p;
2269 if (i != 0 && *e->operation == COND_EXPR)
2270 cond_p = true;
2271 else if (*e->operation == TRUTH_ANDIF_EXPR
2272 || *e->operation == TRUTH_ORIF_EXPR)
2273 cond_p = true;
2274 walk_result (e->ops[i], cond_p, result);
2277 else if (if_expr *ie = dyn_cast <if_expr *> (o))
2279 /* 'if' conditions should be all fine. */
2280 if (ie->trueexpr == result)
2282 walk_result (ie->trueexpr, false, result);
2283 return true;
2285 if (ie->falseexpr == result)
2287 walk_result (ie->falseexpr, false, result);
2288 return true;
2290 bool res = false;
2291 if (is_a <if_expr *> (ie->trueexpr)
2292 || is_a <with_expr *> (ie->trueexpr))
2293 res |= walk_result (ie->trueexpr, false, result);
2294 if (ie->falseexpr
2295 && (is_a <if_expr *> (ie->falseexpr)
2296 || is_a <with_expr *> (ie->falseexpr)))
2297 res |= walk_result (ie->falseexpr, false, result);
2298 return res;
2300 else if (with_expr *we = dyn_cast <with_expr *> (o))
2302 bool res = (we->subexpr == result);
2303 if (res
2304 || is_a <if_expr *> (we->subexpr)
2305 || is_a <with_expr *> (we->subexpr))
2306 res |= walk_result (we->subexpr, false, result);
2307 if (res)
2308 walk_c_expr (we->with);
2309 return res;
2311 else if (c_expr *ce = dyn_cast <c_expr *> (o))
2312 walk_c_expr (ce);
2313 else
2314 gcc_unreachable ();
2316 return false;
2319 /* Look for captures in the C expr E. */
2321 void
2322 capture_info::walk_c_expr (c_expr *e)
2324 /* Give up for C exprs mentioning captures not inside TREE_TYPE,
2325 TREE_REAL_CST, TREE_CODE or a predicate where they cannot
2326 really escape through. */
2327 unsigned p_depth = 0;
2328 for (unsigned i = 0; i < e->code.length (); ++i)
2330 const cpp_token *t = &e->code[i];
2331 const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL;
2332 id_base *id;
2333 if (t->type == CPP_NAME
2334 && (strcmp ((const char *)CPP_HASHNODE
2335 (t->val.node.node)->ident.str, "TREE_TYPE") == 0
2336 || strcmp ((const char *)CPP_HASHNODE
2337 (t->val.node.node)->ident.str, "TREE_CODE") == 0
2338 || strcmp ((const char *)CPP_HASHNODE
2339 (t->val.node.node)->ident.str, "TREE_REAL_CST") == 0
2340 || ((id = get_operator ((const char *)CPP_HASHNODE
2341 (t->val.node.node)->ident.str))
2342 && is_a <predicate_id *> (id)))
2343 && n->type == CPP_OPEN_PAREN)
2344 p_depth++;
2345 else if (t->type == CPP_CLOSE_PAREN
2346 && p_depth > 0)
2347 p_depth--;
2348 else if (p_depth == 0
2349 && t->type == CPP_ATSIGN
2350 && (n->type == CPP_NUMBER
2351 || n->type == CPP_NAME)
2352 && !(n->flags & PREV_WHITE))
2354 const char *id1;
2355 if (n->type == CPP_NUMBER)
2356 id1 = (const char *)n->val.str.text;
2357 else
2358 id1 = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
2359 unsigned *where = e->capture_ids->get(id1);
2360 if (! where)
2361 fatal_at (n, "unknown capture id '%s'", id1);
2362 info[info[*where].same_as].force_no_side_effects_p = true;
2363 if (verbose >= 1
2364 && !gimple)
2365 warning_at (t, "capture escapes");
2371 /* The current label failing the current matched pattern during
2372 code generation. */
2373 static char *fail_label;
2375 /* Code generation off the decision tree and the refered AST nodes. */
2377 bool
2378 is_conversion (id_base *op)
2380 return (*op == CONVERT_EXPR
2381 || *op == NOP_EXPR
2382 || *op == FLOAT_EXPR
2383 || *op == FIX_TRUNC_EXPR
2384 || *op == VIEW_CONVERT_EXPR);
2387 /* Get the type to be used for generating operand POS of OP from the
2388 various sources. */
2390 static const char *
2391 get_operand_type (id_base *op, unsigned pos,
2392 const char *in_type,
2393 const char *expr_type,
2394 const char *other_oprnd_type)
2396 /* Generally operands whose type does not match the type of the
2397 expression generated need to know their types but match and
2398 thus can fall back to 'other_oprnd_type'. */
2399 if (is_conversion (op))
2400 return other_oprnd_type;
2401 else if (*op == REALPART_EXPR
2402 || *op == IMAGPART_EXPR)
2403 return other_oprnd_type;
2404 else if (is_a <operator_id *> (op)
2405 && strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0)
2406 return other_oprnd_type;
2407 else if (*op == COND_EXPR
2408 && pos == 0)
2409 return "boolean_type_node";
2410 else if (startswith (op->id, "CFN_COND_"))
2412 /* IFN_COND_* operands 1 and later by default have the same type
2413 as the result. The type of operand 0 needs to be specified
2414 explicitly. */
2415 if (pos > 0 && expr_type)
2416 return expr_type;
2417 else if (pos > 0 && in_type)
2418 return in_type;
2419 else
2420 return NULL;
2422 else
2424 /* Otherwise all types should match - choose one in order of
2425 preference. */
2426 if (expr_type)
2427 return expr_type;
2428 else if (in_type)
2429 return in_type;
2430 else
2431 return other_oprnd_type;
2435 /* Generate transform code for an expression. */
2437 void
2438 expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
2439 int depth, const char *in_type, capture_info *cinfo,
2440 dt_operand **indexes, int)
2442 id_base *opr = operation;
2443 /* When we delay operator substituting during lowering of fors we
2444 make sure that for code-gen purposes the effects of each substitute
2445 are the same. Thus just look at that. */
2446 if (user_id *uid = dyn_cast <user_id *> (opr))
2447 opr = uid->substitutes[0];
2449 bool conversion_p = is_conversion (opr);
2450 const char *type = expr_type;
2451 char optype[64];
2452 if (type)
2453 /* If there was a type specification in the pattern use it. */
2455 else if (conversion_p)
2456 /* For conversions we need to build the expression using the
2457 outer type passed in. */
2458 type = in_type;
2459 else if (*opr == REALPART_EXPR
2460 || *opr == IMAGPART_EXPR)
2462 /* __real and __imag use the component type of its operand. */
2463 snprintf (optype, sizeof (optype), "TREE_TYPE (TREE_TYPE (_o%d[0]))",
2464 depth);
2465 type = optype;
2467 else if (is_a <operator_id *> (opr)
2468 && !strcmp (as_a <operator_id *> (opr)->tcc, "tcc_comparison"))
2470 /* comparisons use boolean_type_node (or what gets in), but
2471 their operands need to figure out the types themselves. */
2472 if (in_type)
2473 type = in_type;
2474 else
2476 snprintf (optype, sizeof (optype), "boolean_type_node");
2477 type = optype;
2479 in_type = NULL;
2481 else if (*opr == COND_EXPR
2482 || *opr == VEC_COND_EXPR
2483 || startswith (opr->id, "CFN_COND_"))
2485 /* Conditions are of the same type as their first alternative. */
2486 snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[1])", depth);
2487 type = optype;
2489 else
2491 /* Other operations are of the same type as their first operand. */
2492 snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[0])", depth);
2493 type = optype;
2495 if (!type)
2496 fatal_at (location, "cannot determine type of operand");
2498 fprintf_indent (f, indent, "{\n");
2499 indent += 2;
2500 fprintf_indent (f, indent,
2501 "tree _o%d[%u], _r%d;\n", depth, ops.length (), depth);
2502 char op0type[64];
2503 snprintf (op0type, sizeof (op0type), "TREE_TYPE (_o%d[0])", depth);
2504 for (unsigned i = 0; i < ops.length (); ++i)
2506 char dest1[32];
2507 snprintf (dest1, sizeof (dest1), "_o%d[%u]", depth, i);
2508 const char *optype1
2509 = get_operand_type (opr, i, in_type, expr_type,
2510 i == 0 ? NULL : op0type);
2511 ops[i]->gen_transform (f, indent, dest1, gimple, depth + 1, optype1,
2512 cinfo, indexes,
2513 *opr == COND_EXPR && i == 0 ? 1 : 2);
2516 const char *opr_name;
2517 if (*operation == CONVERT_EXPR)
2518 opr_name = "NOP_EXPR";
2519 else
2520 opr_name = operation->id;
2522 if (gimple)
2524 if (*opr == CONVERT_EXPR)
2526 fprintf_indent (f, indent,
2527 "if (%s != TREE_TYPE (_o%d[0])\n",
2528 type, depth);
2529 fprintf_indent (f, indent,
2530 " && !useless_type_conversion_p (%s, TREE_TYPE "
2531 "(_o%d[0])))\n",
2532 type, depth);
2533 fprintf_indent (f, indent + 2, "{\n");
2534 indent += 4;
2536 /* ??? Building a stmt can fail for various reasons here, seq being
2537 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
2538 So if we fail here we should continue matching other patterns. */
2539 fprintf_indent (f, indent, "gimple_match_op tem_op "
2540 "(res_op->cond.any_else (), %s, %s", opr_name, type);
2541 for (unsigned i = 0; i < ops.length (); ++i)
2542 fprintf (f, ", _o%d[%u]", depth, i);
2543 fprintf (f, ");\n");
2544 fprintf_indent (f, indent, "tem_op.resimplify (%s, valueize);\n",
2545 !force_leaf ? "lseq" : "NULL");
2546 fprintf_indent (f, indent,
2547 "_r%d = maybe_push_res_to_seq (&tem_op, %s);\n", depth,
2548 !force_leaf ? "lseq" : "NULL");
2549 fprintf_indent (f, indent,
2550 "if (!_r%d) goto %s;\n",
2551 depth, fail_label);
2552 if (*opr == CONVERT_EXPR)
2554 indent -= 4;
2555 fprintf_indent (f, indent, " }\n");
2556 fprintf_indent (f, indent, "else\n");
2557 fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
2560 else
2562 if (*opr == CONVERT_EXPR)
2564 fprintf_indent (f, indent, "if (TREE_TYPE (_o%d[0]) != %s)\n",
2565 depth, type);
2566 indent += 2;
2568 if (opr->kind == id_base::CODE)
2569 fprintf_indent (f, indent, "_r%d = fold_build%d_loc (loc, %s, %s",
2570 depth, ops.length(), opr_name, type);
2571 else
2572 fprintf_indent (f, indent, "_r%d = maybe_build_call_expr_loc (loc, "
2573 "%s, %s, %d", depth, opr_name, type, ops.length());
2574 for (unsigned i = 0; i < ops.length (); ++i)
2575 fprintf (f, ", _o%d[%u]", depth, i);
2576 fprintf (f, ");\n");
2577 if (opr->kind != id_base::CODE)
2579 fprintf_indent (f, indent, "if (!_r%d)\n", depth);
2580 fprintf_indent (f, indent, " goto %s;\n", fail_label);
2582 if (force_leaf)
2584 fprintf_indent (f, indent, "if (EXPR_P (_r%d))\n", depth);
2585 fprintf_indent (f, indent, " goto %s;\n", fail_label);
2587 if (*opr == CONVERT_EXPR)
2589 indent -= 2;
2590 fprintf_indent (f, indent, "else\n");
2591 fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
2594 fprintf_indent (f, indent, "%s = _r%d;\n", dest, depth);
2595 indent -= 2;
2596 fprintf_indent (f, indent, "}\n");
2599 /* Generate code for a c_expr which is either the expression inside
2600 an if statement or a sequence of statements which computes a
2601 result to be stored to DEST. */
2603 void
2604 c_expr::gen_transform (FILE *f, int indent, const char *dest,
2605 bool, int, const char *, capture_info *,
2606 dt_operand **, int)
2608 if (dest && nr_stmts == 1)
2609 fprintf_indent (f, indent, "%s = ", dest);
2611 unsigned stmt_nr = 1;
2612 int prev_line = -1;
2613 for (unsigned i = 0; i < code.length (); ++i)
2615 const cpp_token *token = &code[i];
2617 /* We can't recover from all lexing losses but we can roughly restore line
2618 breaks from location info. */
2619 const line_map_ordinary *map;
2620 linemap_resolve_location (line_table, token->src_loc,
2621 LRK_SPELLING_LOCATION, &map);
2622 expanded_location loc = linemap_expand_location (line_table, map,
2623 token->src_loc);
2624 if (prev_line != -1 && loc.line != prev_line)
2625 fputc ('\n', f);
2626 prev_line = loc.line;
2628 /* Replace captures for code-gen. */
2629 if (token->type == CPP_ATSIGN)
2631 const cpp_token *n = &code[i+1];
2632 if ((n->type == CPP_NUMBER
2633 || n->type == CPP_NAME)
2634 && !(n->flags & PREV_WHITE))
2636 if (token->flags & PREV_WHITE)
2637 fputc (' ', f);
2638 const char *id;
2639 if (n->type == CPP_NUMBER)
2640 id = (const char *)n->val.str.text;
2641 else
2642 id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
2643 unsigned *cid = capture_ids->get (id);
2644 if (!cid)
2645 fatal_at (token, "unknown capture id");
2646 fprintf (f, "captures[%u]", *cid);
2647 ++i;
2648 continue;
2652 if (token->flags & PREV_WHITE)
2653 fputc (' ', f);
2655 if (token->type == CPP_NAME)
2657 const char *id = (const char *) NODE_NAME (token->val.node.node);
2658 unsigned j;
2659 for (j = 0; j < ids.length (); ++j)
2661 if (strcmp (id, ids[j].id) == 0)
2663 fprintf (f, "%s", ids[j].oper);
2664 break;
2667 if (j < ids.length ())
2668 continue;
2671 /* Output the token as string. */
2672 char *tk = (char *)cpp_token_as_text (r, token);
2673 fputs (tk, f);
2675 if (token->type == CPP_SEMICOLON)
2677 stmt_nr++;
2678 if (dest && stmt_nr == nr_stmts)
2679 fprintf_indent (f, indent, "%s = ", dest);
2682 fputc ('\n', f);
2685 /* Generate transform code for a capture. */
2687 void
2688 capture::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
2689 int depth, const char *in_type, capture_info *cinfo,
2690 dt_operand **indexes, int cond_handling)
2692 if (what && is_a<expr *> (what))
2694 if (indexes[where] == 0)
2696 char buf[20];
2697 snprintf (buf, sizeof (buf), "captures[%u]", where);
2698 what->gen_transform (f, indent, buf, gimple, depth, in_type,
2699 cinfo, NULL);
2703 /* If in GENERIC some capture is used multiple times, unshare it except
2704 when emitting the last use. */
2705 if (!gimple
2706 && cinfo->info.exists ()
2707 && cinfo->info[cinfo->info[where].same_as].result_use_count > 1)
2709 fprintf_indent (f, indent, "%s = unshare_expr (captures[%u]);\n",
2710 dest, where);
2711 cinfo->info[cinfo->info[where].same_as].result_use_count--;
2713 else
2714 fprintf_indent (f, indent, "%s = captures[%u];\n", dest, where);
2716 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2717 with substituting a capture of that. */
2718 if (gimple
2719 && cond_handling != 0
2720 && cinfo->info[where].cond_expr_cond_p)
2722 /* If substituting into a cond_expr condition, unshare. */
2723 if (cond_handling == 1)
2724 fprintf_indent (f, indent, "%s = unshare_expr (%s);\n", dest, dest);
2725 /* If substituting elsewhere we might need to decompose it. */
2726 else if (cond_handling == 2)
2728 /* ??? Returning false here will also not allow any other patterns
2729 to match unless this generator was split out. */
2730 fprintf_indent (f, indent, "if (COMPARISON_CLASS_P (%s))\n", dest);
2731 fprintf_indent (f, indent, " {\n");
2732 fprintf_indent (f, indent, " if (!seq) return false;\n");
2733 fprintf_indent (f, indent, " %s = gimple_build (seq,"
2734 " TREE_CODE (%s),"
2735 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
2736 " TREE_OPERAND (%s, 1));\n",
2737 dest, dest, dest, dest, dest);
2738 fprintf_indent (f, indent, " }\n");
2743 /* Return the name of the operand representing the decision tree node.
2744 Use NAME as space to generate it. */
2746 char *
2747 dt_operand::get_name (char *name)
2749 if (! parent)
2750 sprintf (name, "t");
2751 else if (parent->level == 1)
2752 sprintf (name, "_p%u", pos);
2753 else if (parent->type == dt_node::DT_MATCH)
2754 return as_a <dt_operand *> (parent)->get_name (name);
2755 else
2756 sprintf (name, "_q%u%u", parent->level, pos);
2757 return name;
2760 /* Fill NAME with the operand name at position POS. */
2762 void
2763 dt_operand::gen_opname (char *name, unsigned pos)
2765 if (! parent)
2766 sprintf (name, "_p%u", pos);
2767 else
2768 sprintf (name, "_q%u%u", level, pos);
2771 /* Generate matching code for the decision tree operand which is
2772 a predicate. */
2774 unsigned
2775 dt_operand::gen_predicate (FILE *f, int indent, const char *opname, bool gimple)
2777 predicate *p = as_a <predicate *> (op);
2779 if (p->p->matchers.exists ())
2781 /* If this is a predicate generated from a pattern mangle its
2782 name and pass on the valueize hook. */
2783 if (gimple)
2784 fprintf_indent (f, indent, "if (gimple_%s (%s, valueize))\n",
2785 p->p->id, opname);
2786 else
2787 fprintf_indent (f, indent, "if (tree_%s (%s))\n", p->p->id, opname);
2789 else
2790 fprintf_indent (f, indent, "if (%s (%s))\n", p->p->id, opname);
2791 fprintf_indent (f, indent + 2, "{\n");
2792 return 1;
2795 /* Generate matching code for the decision tree operand which is
2796 a capture-match. */
2798 unsigned
2799 dt_operand::gen_match_op (FILE *f, int indent, const char *opname, bool)
2801 char match_opname[20];
2802 match_dop->get_name (match_opname);
2803 if (value_match)
2804 fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
2805 "|| operand_equal_p (%s, %s, 0))\n",
2806 opname, match_opname, opname, opname, match_opname);
2807 else
2808 fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
2809 "|| (operand_equal_p (%s, %s, 0) "
2810 "&& types_match (%s, %s)))\n",
2811 opname, match_opname, opname, opname, match_opname,
2812 opname, match_opname);
2813 fprintf_indent (f, indent + 2, "{\n");
2814 return 1;
2817 /* Generate GIMPLE matching code for the decision tree operand. */
2819 unsigned
2820 dt_operand::gen_gimple_expr (FILE *f, int indent, int depth)
2822 expr *e = static_cast<expr *> (op);
2823 id_base *id = e->operation;
2824 unsigned n_ops = e->ops.length ();
2825 unsigned n_braces = 0;
2827 for (unsigned i = 0; i < n_ops; ++i)
2829 char child_opname[20];
2830 gen_opname (child_opname, i);
2832 if (id->kind == id_base::CODE)
2834 if (e->is_generic
2835 || *id == REALPART_EXPR || *id == IMAGPART_EXPR
2836 || *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR)
2838 /* ??? If this is a memory operation we can't (and should not)
2839 match this. The only sensible operand types are
2840 SSA names and invariants. */
2841 if (e->is_generic)
2843 char opname[20];
2844 get_name (opname);
2845 fprintf_indent (f, indent,
2846 "tree %s = TREE_OPERAND (%s, %i);\n",
2847 child_opname, opname, i);
2849 else
2850 fprintf_indent (f, indent,
2851 "tree %s = TREE_OPERAND "
2852 "(gimple_assign_rhs1 (_a%d), %i);\n",
2853 child_opname, depth, i);
2854 fprintf_indent (f, indent,
2855 "if ((TREE_CODE (%s) == SSA_NAME\n",
2856 child_opname);
2857 fprintf_indent (f, indent,
2858 " || is_gimple_min_invariant (%s)))\n",
2859 child_opname);
2860 fprintf_indent (f, indent,
2861 " {\n");
2862 indent += 4;
2863 n_braces++;
2864 fprintf_indent (f, indent,
2865 "%s = do_valueize (valueize, %s);\n",
2866 child_opname, child_opname);
2867 continue;
2869 else
2870 fprintf_indent (f, indent,
2871 "tree %s = gimple_assign_rhs%u (_a%d);\n",
2872 child_opname, i + 1, depth);
2874 else
2875 fprintf_indent (f, indent,
2876 "tree %s = gimple_call_arg (_c%d, %u);\n",
2877 child_opname, depth, i);
2878 fprintf_indent (f, indent,
2879 "%s = do_valueize (valueize, %s);\n",
2880 child_opname, child_opname);
2882 /* While the toplevel operands are canonicalized by the caller
2883 after valueizing operands of sub-expressions we have to
2884 re-canonicalize operand order. */
2885 int opno = commutative_op (id);
2886 if (opno >= 0)
2888 char child_opname0[20], child_opname1[20];
2889 gen_opname (child_opname0, opno);
2890 gen_opname (child_opname1, opno + 1);
2891 fprintf_indent (f, indent,
2892 "if (tree_swap_operands_p (%s, %s))\n",
2893 child_opname0, child_opname1);
2894 fprintf_indent (f, indent,
2895 " std::swap (%s, %s);\n",
2896 child_opname0, child_opname1);
2899 return n_braces;
2902 /* Generate GENERIC matching code for the decision tree operand. */
2904 unsigned
2905 dt_operand::gen_generic_expr (FILE *f, int indent, const char *opname)
2907 expr *e = static_cast<expr *> (op);
2908 unsigned n_ops = e->ops.length ();
2910 for (unsigned i = 0; i < n_ops; ++i)
2912 char child_opname[20];
2913 gen_opname (child_opname, i);
2915 if (e->operation->kind == id_base::CODE)
2916 fprintf_indent (f, indent, "tree %s = TREE_OPERAND (%s, %u);\n",
2917 child_opname, opname, i);
2918 else
2919 fprintf_indent (f, indent, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2920 child_opname, opname, i);
2923 return 0;
2926 /* Generate matching code for the children of the decision tree node. */
2928 void
2929 dt_node::gen_kids (FILE *f, int indent, bool gimple, int depth)
2931 auto_vec<dt_operand *> gimple_exprs;
2932 auto_vec<dt_operand *> generic_exprs;
2933 auto_vec<dt_operand *> fns;
2934 auto_vec<dt_operand *> generic_fns;
2935 auto_vec<dt_operand *> preds;
2936 auto_vec<dt_node *> others;
2938 for (unsigned i = 0; i < kids.length (); ++i)
2940 if (kids[i]->type == dt_node::DT_OPERAND)
2942 dt_operand *op = as_a<dt_operand *> (kids[i]);
2943 if (expr *e = dyn_cast <expr *> (op->op))
2945 if (e->ops.length () == 0
2946 /* In GIMPLE a CONSTRUCTOR always appears in a
2947 separate definition. */
2948 && (!gimple || !(*e->operation == CONSTRUCTOR)))
2950 generic_exprs.safe_push (op);
2951 /* But ADDR_EXPRs can appear directly when invariant
2952 and in a separate definition when not. */
2953 if (gimple && *e->operation == ADDR_EXPR)
2954 gimple_exprs.safe_push (op);
2956 else if (e->operation->kind == id_base::FN)
2958 if (gimple)
2959 fns.safe_push (op);
2960 else
2961 generic_fns.safe_push (op);
2963 else if (e->operation->kind == id_base::PREDICATE)
2964 preds.safe_push (op);
2965 else
2967 if (gimple && !e->is_generic)
2968 gimple_exprs.safe_push (op);
2969 else
2970 generic_exprs.safe_push (op);
2973 else if (op->op->type == operand::OP_PREDICATE)
2974 others.safe_push (kids[i]);
2975 else
2976 gcc_unreachable ();
2978 else if (kids[i]->type == dt_node::DT_SIMPLIFY)
2979 others.safe_push (kids[i]);
2980 else if (kids[i]->type == dt_node::DT_MATCH
2981 || kids[i]->type == dt_node::DT_TRUE)
2983 /* A DT_TRUE operand serves as a barrier - generate code now
2984 for what we have collected sofar.
2985 Like DT_TRUE, DT_MATCH serves as a barrier as it can cause
2986 dependent matches to get out-of-order. Generate code now
2987 for what we have collected sofar. */
2988 gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
2989 fns, generic_fns, preds, others);
2990 /* And output the true operand itself. */
2991 kids[i]->gen (f, indent, gimple, depth);
2992 gimple_exprs.truncate (0);
2993 generic_exprs.truncate (0);
2994 fns.truncate (0);
2995 generic_fns.truncate (0);
2996 preds.truncate (0);
2997 others.truncate (0);
2999 else
3000 gcc_unreachable ();
3003 /* Generate code for the remains. */
3004 gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
3005 fns, generic_fns, preds, others);
3008 /* Generate matching code for the children of the decision tree node. */
3010 void
3011 dt_node::gen_kids_1 (FILE *f, int indent, bool gimple, int depth,
3012 const vec<dt_operand *> &gimple_exprs,
3013 const vec<dt_operand *> &generic_exprs,
3014 const vec<dt_operand *> &fns,
3015 const vec<dt_operand *> &generic_fns,
3016 const vec<dt_operand *> &preds,
3017 const vec<dt_node *> &others)
3019 char buf[128];
3020 char *kid_opname = buf;
3022 unsigned exprs_len = gimple_exprs.length ();
3023 unsigned gexprs_len = generic_exprs.length ();
3024 unsigned fns_len = fns.length ();
3025 unsigned gfns_len = generic_fns.length ();
3027 if (exprs_len || fns_len || gexprs_len || gfns_len)
3029 if (exprs_len)
3030 gimple_exprs[0]->get_name (kid_opname);
3031 else if (fns_len)
3032 fns[0]->get_name (kid_opname);
3033 else if (gfns_len)
3034 generic_fns[0]->get_name (kid_opname);
3035 else
3036 generic_exprs[0]->get_name (kid_opname);
3038 fprintf_indent (f, indent, "switch (TREE_CODE (%s))\n", kid_opname);
3039 fprintf_indent (f, indent, " {\n");
3040 indent += 2;
3043 if (exprs_len || fns_len)
3045 depth++;
3046 fprintf_indent (f, indent,
3047 "case SSA_NAME:\n");
3048 fprintf_indent (f, indent,
3049 " if (gimple *_d%d = get_def (valueize, %s))\n",
3050 depth, kid_opname);
3051 fprintf_indent (f, indent,
3052 " {\n");
3053 indent += 6;
3054 if (exprs_len)
3056 fprintf_indent (f, indent,
3057 "if (gassign *_a%d = dyn_cast <gassign *> (_d%d))\n",
3058 depth, depth);
3059 fprintf_indent (f, indent,
3060 " switch (gimple_assign_rhs_code (_a%d))\n",
3061 depth);
3062 indent += 4;
3063 fprintf_indent (f, indent, "{\n");
3064 for (unsigned i = 0; i < exprs_len; ++i)
3066 expr *e = as_a <expr *> (gimple_exprs[i]->op);
3067 id_base *op = e->operation;
3068 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
3069 fprintf_indent (f, indent, "CASE_CONVERT:\n");
3070 else
3071 fprintf_indent (f, indent, "case %s:\n", op->id);
3072 fprintf_indent (f, indent, " {\n");
3073 gimple_exprs[i]->gen (f, indent + 4, true, depth);
3074 fprintf_indent (f, indent, " break;\n");
3075 fprintf_indent (f, indent, " }\n");
3077 fprintf_indent (f, indent, "default:;\n");
3078 fprintf_indent (f, indent, "}\n");
3079 indent -= 4;
3082 if (fns_len)
3084 fprintf_indent (f, indent,
3085 "%sif (gcall *_c%d = dyn_cast <gcall *> (_d%d))\n",
3086 exprs_len ? "else " : "", depth, depth);
3087 fprintf_indent (f, indent,
3088 " switch (gimple_call_combined_fn (_c%d))\n",
3089 depth);
3091 indent += 4;
3092 fprintf_indent (f, indent, "{\n");
3093 for (unsigned i = 0; i < fns_len; ++i)
3095 expr *e = as_a <expr *>(fns[i]->op);
3096 fprintf_indent (f, indent, "case %s:\n", e->operation->id);
3097 /* We need to be defensive against bogus prototypes allowing
3098 calls with not enough arguments. */
3099 fprintf_indent (f, indent,
3100 " if (gimple_call_num_args (_c%d) == %d)\n",
3101 depth, e->ops.length ());
3102 fprintf_indent (f, indent, " {\n");
3103 fns[i]->gen (f, indent + 6, true, depth);
3104 fprintf_indent (f, indent, " }\n");
3105 fprintf_indent (f, indent, " break;\n");
3108 fprintf_indent (f, indent, "default:;\n");
3109 fprintf_indent (f, indent, "}\n");
3110 indent -= 4;
3113 indent -= 6;
3114 depth--;
3115 fprintf_indent (f, indent, " }\n");
3116 /* See if there is SSA_NAME among generic_exprs and if yes, emit it
3117 here rather than in the next loop. */
3118 for (unsigned i = 0; i < generic_exprs.length (); ++i)
3120 expr *e = as_a <expr *>(generic_exprs[i]->op);
3121 id_base *op = e->operation;
3122 if (*op == SSA_NAME && (exprs_len || fns_len))
3124 fprintf_indent (f, indent + 4, "{\n");
3125 generic_exprs[i]->gen (f, indent + 6, gimple, depth);
3126 fprintf_indent (f, indent + 4, "}\n");
3130 fprintf_indent (f, indent, " break;\n");
3133 for (unsigned i = 0; i < generic_exprs.length (); ++i)
3135 expr *e = as_a <expr *>(generic_exprs[i]->op);
3136 id_base *op = e->operation;
3137 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
3138 fprintf_indent (f, indent, "CASE_CONVERT:\n");
3139 else if (*op == SSA_NAME && (exprs_len || fns_len))
3140 /* Already handled above. */
3141 continue;
3142 else
3143 fprintf_indent (f, indent, "case %s:\n", op->id);
3144 fprintf_indent (f, indent, " {\n");
3145 generic_exprs[i]->gen (f, indent + 4, gimple, depth);
3146 fprintf_indent (f, indent, " break;\n");
3147 fprintf_indent (f, indent, " }\n");
3150 if (gfns_len)
3152 fprintf_indent (f, indent,
3153 "case CALL_EXPR:\n");
3154 fprintf_indent (f, indent,
3155 " switch (get_call_combined_fn (%s))\n",
3156 kid_opname);
3157 fprintf_indent (f, indent,
3158 " {\n");
3159 indent += 4;
3161 for (unsigned j = 0; j < generic_fns.length (); ++j)
3163 expr *e = as_a <expr *>(generic_fns[j]->op);
3164 gcc_assert (e->operation->kind == id_base::FN);
3166 fprintf_indent (f, indent, "case %s:\n", e->operation->id);
3167 fprintf_indent (f, indent, " if (call_expr_nargs (%s) == %d)\n"
3168 " {\n", kid_opname, e->ops.length ());
3169 generic_fns[j]->gen (f, indent + 6, false, depth);
3170 fprintf_indent (f, indent, " }\n"
3171 " break;\n");
3173 fprintf_indent (f, indent, "default:;\n");
3175 indent -= 4;
3176 fprintf_indent (f, indent, " }\n");
3177 fprintf_indent (f, indent, " break;\n");
3180 /* Close switch (TREE_CODE ()). */
3181 if (exprs_len || fns_len || gexprs_len || gfns_len)
3183 indent -= 4;
3184 fprintf_indent (f, indent, " default:;\n");
3185 fprintf_indent (f, indent, " }\n");
3188 for (unsigned i = 0; i < preds.length (); ++i)
3190 expr *e = as_a <expr *> (preds[i]->op);
3191 predicate_id *p = as_a <predicate_id *> (e->operation);
3192 preds[i]->get_name (kid_opname);
3193 fprintf_indent (f, indent, "{\n");
3194 indent += 2;
3195 fprintf_indent (f, indent, "tree %s_pops[%d];\n", kid_opname, p->nargs);
3196 fprintf_indent (f, indent, "if (%s_%s (%s, %s_pops%s))\n",
3197 gimple ? "gimple" : "tree",
3198 p->id, kid_opname, kid_opname,
3199 gimple ? ", valueize" : "");
3200 fprintf_indent (f, indent, " {\n");
3201 for (int j = 0; j < p->nargs; ++j)
3203 char child_opname[20];
3204 preds[i]->gen_opname (child_opname, j);
3205 fprintf_indent (f, indent + 4, "tree %s = %s_pops[%d];\n",
3206 child_opname, kid_opname, j);
3208 preds[i]->gen_kids (f, indent + 4, gimple, depth);
3209 fprintf (f, "}\n");
3210 indent -= 2;
3211 fprintf_indent (f, indent, "}\n");
3214 for (unsigned i = 0; i < others.length (); ++i)
3215 others[i]->gen (f, indent, gimple, depth);
3218 /* Generate matching code for the decision tree operand. */
3220 void
3221 dt_operand::gen (FILE *f, int indent, bool gimple, int depth)
3223 char opname[20];
3224 get_name (opname);
3226 unsigned n_braces = 0;
3228 if (type == DT_OPERAND)
3229 switch (op->type)
3231 case operand::OP_PREDICATE:
3232 n_braces = gen_predicate (f, indent, opname, gimple);
3233 break;
3235 case operand::OP_EXPR:
3236 if (gimple)
3237 n_braces = gen_gimple_expr (f, indent, depth);
3238 else
3239 n_braces = gen_generic_expr (f, indent, opname);
3240 break;
3242 default:
3243 gcc_unreachable ();
3245 else if (type == DT_TRUE)
3247 else if (type == DT_MATCH)
3248 n_braces = gen_match_op (f, indent, opname, gimple);
3249 else
3250 gcc_unreachable ();
3252 indent += 4 * n_braces;
3253 gen_kids (f, indent, gimple, depth);
3255 for (unsigned i = 0; i < n_braces; ++i)
3257 indent -= 4;
3258 if (indent < 0)
3259 indent = 0;
3260 fprintf_indent (f, indent, " }\n");
3265 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3266 step of a '(simplify ...)' or '(match ...)'. This handles everything
3267 that is not part of the decision tree (simplify->match).
3268 Main recursive worker. */
3270 void
3271 dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
3273 if (result)
3275 if (with_expr *w = dyn_cast <with_expr *> (result))
3277 fprintf_indent (f, indent, "{\n");
3278 indent += 4;
3279 output_line_directive (f, w->location);
3280 w->with->gen_transform (f, indent, NULL, true, 1, "type", NULL);
3281 gen_1 (f, indent, gimple, w->subexpr);
3282 indent -= 4;
3283 fprintf_indent (f, indent, "}\n");
3284 return;
3286 else if (if_expr *ife = dyn_cast <if_expr *> (result))
3288 output_line_directive (f, ife->location);
3289 fprintf_indent (f, indent, "if (");
3290 ife->cond->gen_transform (f, indent, NULL, true, 1, "type", NULL);
3291 fprintf (f, ")\n");
3292 fprintf_indent (f, indent + 2, "{\n");
3293 indent += 4;
3294 gen_1 (f, indent, gimple, ife->trueexpr);
3295 indent -= 4;
3296 fprintf_indent (f, indent + 2, "}\n");
3297 if (ife->falseexpr)
3299 fprintf_indent (f, indent, "else\n");
3300 fprintf_indent (f, indent + 2, "{\n");
3301 indent += 4;
3302 gen_1 (f, indent, gimple, ife->falseexpr);
3303 indent -= 4;
3304 fprintf_indent (f, indent + 2, "}\n");
3306 return;
3310 static unsigned fail_label_cnt;
3311 char local_fail_label[256];
3312 snprintf (local_fail_label, 256, "next_after_fail%u", ++fail_label_cnt);
3313 fail_label = local_fail_label;
3315 /* Analyze captures and perform early-outs on the incoming arguments
3316 that cover cases we cannot handle. */
3317 capture_info cinfo (s, result, gimple);
3318 if (s->kind == simplify::SIMPLIFY)
3320 if (!gimple)
3322 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
3323 if (cinfo.force_no_side_effects & (1 << i))
3325 fprintf_indent (f, indent,
3326 "if (TREE_SIDE_EFFECTS (_p%d)) goto %s;\n",
3327 i, fail_label);
3328 if (verbose >= 1)
3329 warning_at (as_a <expr *> (s->match)->ops[i]->location,
3330 "forcing toplevel operand to have no "
3331 "side-effects");
3333 for (int i = 0; i <= s->capture_max; ++i)
3334 if (cinfo.info[i].cse_p)
3336 else if (cinfo.info[i].force_no_side_effects_p
3337 && (cinfo.info[i].toplevel_msk
3338 & cinfo.force_no_side_effects) == 0)
3340 fprintf_indent (f, indent,
3341 "if (TREE_SIDE_EFFECTS (captures[%d])) "
3342 "goto %s;\n", i, fail_label);
3343 if (verbose >= 1)
3344 warning_at (cinfo.info[i].c->location,
3345 "forcing captured operand to have no "
3346 "side-effects");
3348 else if ((cinfo.info[i].toplevel_msk
3349 & cinfo.force_no_side_effects) != 0)
3350 /* Mark capture as having no side-effects if we had to verify
3351 that via forced toplevel operand checks. */
3352 cinfo.info[i].force_no_side_effects_p = true;
3354 if (gimple)
3356 /* Force single-use restriction by only allowing simple
3357 results via setting seq to NULL. */
3358 fprintf_indent (f, indent, "gimple_seq *lseq = seq;\n");
3359 bool first_p = true;
3360 for (int i = 0; i <= s->capture_max; ++i)
3361 if (cinfo.info[i].force_single_use)
3363 if (first_p)
3365 fprintf_indent (f, indent, "if (lseq\n");
3366 fprintf_indent (f, indent, " && (");
3367 first_p = false;
3369 else
3371 fprintf (f, "\n");
3372 fprintf_indent (f, indent, " || ");
3374 fprintf (f, "!single_use (captures[%d])", i);
3376 if (!first_p)
3378 fprintf (f, "))\n");
3379 fprintf_indent (f, indent, " lseq = NULL;\n");
3384 if (s->kind == simplify::SIMPLIFY)
3385 fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s;\n", fail_label);
3387 fprintf_indent (f, indent, "if (UNLIKELY (dump_file && (dump_flags & TDF_FOLDING))) "
3388 "fprintf (dump_file, \"%s ",
3389 s->kind == simplify::SIMPLIFY
3390 ? "Applying pattern" : "Matching expression");
3391 fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
3392 output_line_directive (f,
3393 result ? result->location : s->match->location, true,
3394 true);
3395 fprintf (f, ", __FILE__, __LINE__);\n");
3397 fprintf_indent (f, indent, "{\n");
3398 indent += 2;
3399 if (!result)
3401 /* If there is no result then this is a predicate implementation. */
3402 fprintf_indent (f, indent, "return true;\n");
3404 else if (gimple)
3406 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
3407 in outermost position). */
3408 if (result->type == operand::OP_EXPR
3409 && *as_a <expr *> (result)->operation == NON_LVALUE_EXPR)
3410 result = as_a <expr *> (result)->ops[0];
3411 if (result->type == operand::OP_EXPR)
3413 expr *e = as_a <expr *> (result);
3414 id_base *opr = e->operation;
3415 bool is_predicate = false;
3416 /* When we delay operator substituting during lowering of fors we
3417 make sure that for code-gen purposes the effects of each substitute
3418 are the same. Thus just look at that. */
3419 if (user_id *uid = dyn_cast <user_id *> (opr))
3420 opr = uid->substitutes[0];
3421 else if (is_a <predicate_id *> (opr))
3422 is_predicate = true;
3423 if (!is_predicate)
3424 fprintf_indent (f, indent, "res_op->set_op (%s, type, %d);\n",
3425 *e->operation == CONVERT_EXPR
3426 ? "NOP_EXPR" : e->operation->id,
3427 e->ops.length ());
3428 for (unsigned j = 0; j < e->ops.length (); ++j)
3430 char dest[32];
3431 if (is_predicate)
3432 snprintf (dest, sizeof (dest), "res_ops[%d]", j);
3433 else
3434 snprintf (dest, sizeof (dest), "res_op->ops[%d]", j);
3435 const char *optype
3436 = get_operand_type (opr, j,
3437 "type", e->expr_type,
3438 j == 0 ? NULL
3439 : "TREE_TYPE (res_op->ops[0])");
3440 /* We need to expand GENERIC conditions we captured from
3441 COND_EXPRs and we need to unshare them when substituting
3442 into COND_EXPRs. */
3443 int cond_handling = 0;
3444 if (!is_predicate)
3445 cond_handling = (*opr == COND_EXPR && j == 0) ? 1 : 2;
3446 e->ops[j]->gen_transform (f, indent, dest, true, 1, optype,
3447 &cinfo, indexes, cond_handling);
3450 /* Re-fold the toplevel result. It's basically an embedded
3451 gimple_build w/o actually building the stmt. */
3452 if (!is_predicate)
3454 fprintf_indent (f, indent,
3455 "res_op->resimplify (%s, valueize);\n",
3456 !e->force_leaf ? "lseq" : "NULL");
3457 if (e->force_leaf)
3458 fprintf_indent (f, indent,
3459 "if (!maybe_push_res_to_seq (res_op, NULL)) "
3460 "goto %s;\n", fail_label);
3463 else if (result->type == operand::OP_CAPTURE
3464 || result->type == operand::OP_C_EXPR)
3466 fprintf_indent (f, indent, "tree tem;\n");
3467 result->gen_transform (f, indent, "tem", true, 1, "type",
3468 &cinfo, indexes);
3469 fprintf_indent (f, indent, "res_op->set_value (tem);\n");
3470 if (is_a <capture *> (result)
3471 && cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p)
3473 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
3474 with substituting a capture of that. */
3475 fprintf_indent (f, indent,
3476 "if (COMPARISON_CLASS_P (tem))\n");
3477 fprintf_indent (f, indent,
3478 " {\n");
3479 fprintf_indent (f, indent,
3480 " res_op->ops[0] = TREE_OPERAND (tem, 0);\n");
3481 fprintf_indent (f, indent,
3482 " res_op->ops[1] = TREE_OPERAND (tem, 1);\n");
3483 fprintf_indent (f, indent,
3484 " }\n");
3487 else
3488 gcc_unreachable ();
3489 fprintf_indent (f, indent, "return true;\n");
3491 else /* GENERIC */
3493 bool is_predicate = false;
3494 if (result->type == operand::OP_EXPR)
3496 expr *e = as_a <expr *> (result);
3497 id_base *opr = e->operation;
3498 /* When we delay operator substituting during lowering of fors we
3499 make sure that for code-gen purposes the effects of each substitute
3500 are the same. Thus just look at that. */
3501 if (user_id *uid = dyn_cast <user_id *> (opr))
3502 opr = uid->substitutes[0];
3503 else if (is_a <predicate_id *> (opr))
3504 is_predicate = true;
3505 /* Search for captures used multiple times in the result expression
3506 and wrap them in a SAVE_EXPR. Allow as many uses as in the
3507 original expression. */
3508 if (!is_predicate)
3509 for (int i = 0; i < s->capture_max + 1; ++i)
3511 if (cinfo.info[i].same_as != (unsigned)i
3512 || cinfo.info[i].cse_p)
3513 continue;
3514 if (cinfo.info[i].result_use_count
3515 > cinfo.info[i].match_use_count)
3516 fprintf_indent (f, indent,
3517 "if (! tree_invariant_p (captures[%d])) "
3518 "goto %s;\n", i, fail_label);
3520 for (unsigned j = 0; j < e->ops.length (); ++j)
3522 char dest[32];
3523 if (is_predicate)
3524 snprintf (dest, sizeof (dest), "res_ops[%d]", j);
3525 else
3527 fprintf_indent (f, indent, "tree res_op%d;\n", j);
3528 snprintf (dest, sizeof (dest), "res_op%d", j);
3530 const char *optype
3531 = get_operand_type (opr, j,
3532 "type", e->expr_type,
3533 j == 0
3534 ? NULL : "TREE_TYPE (res_op0)");
3535 e->ops[j]->gen_transform (f, indent, dest, false, 1, optype,
3536 &cinfo, indexes);
3538 if (is_predicate)
3539 fprintf_indent (f, indent, "return true;\n");
3540 else
3542 fprintf_indent (f, indent, "tree _r;\n");
3543 /* Re-fold the toplevel result. Use non_lvalue to
3544 build NON_LVALUE_EXPRs so they get properly
3545 ignored when in GIMPLE form. */
3546 if (*opr == NON_LVALUE_EXPR)
3547 fprintf_indent (f, indent,
3548 "_r = non_lvalue_loc (loc, res_op0);\n");
3549 else
3551 if (is_a <operator_id *> (opr))
3552 fprintf_indent (f, indent,
3553 "_r = fold_build%d_loc (loc, %s, type",
3554 e->ops.length (),
3555 *e->operation == CONVERT_EXPR
3556 ? "NOP_EXPR" : e->operation->id);
3557 else
3558 fprintf_indent (f, indent,
3559 "_r = maybe_build_call_expr_loc (loc, "
3560 "%s, type, %d", e->operation->id,
3561 e->ops.length());
3562 for (unsigned j = 0; j < e->ops.length (); ++j)
3563 fprintf (f, ", res_op%d", j);
3564 fprintf (f, ");\n");
3565 if (!is_a <operator_id *> (opr))
3567 fprintf_indent (f, indent, "if (!_r)\n");
3568 fprintf_indent (f, indent, " goto %s;\n", fail_label);
3573 else if (result->type == operand::OP_CAPTURE
3574 || result->type == operand::OP_C_EXPR)
3577 fprintf_indent (f, indent, "tree _r;\n");
3578 result->gen_transform (f, indent, "_r", false, 1, "type",
3579 &cinfo, indexes);
3581 else
3582 gcc_unreachable ();
3583 if (!is_predicate)
3585 /* Search for captures not used in the result expression and dependent
3586 on TREE_SIDE_EFFECTS emit omit_one_operand. */
3587 for (int i = 0; i < s->capture_max + 1; ++i)
3589 if (cinfo.info[i].same_as != (unsigned)i)
3590 continue;
3591 if (!cinfo.info[i].force_no_side_effects_p
3592 && !cinfo.info[i].expr_p
3593 && cinfo.info[i].result_use_count == 0)
3595 fprintf_indent (f, indent,
3596 "if (TREE_SIDE_EFFECTS (captures[%d]))\n",
3598 fprintf_indent (f, indent + 2,
3599 "_r = build2_loc (loc, COMPOUND_EXPR, type, "
3600 "fold_ignored_result (captures[%d]), _r);\n",
3604 fprintf_indent (f, indent, "return _r;\n");
3607 indent -= 2;
3608 fprintf_indent (f, indent, "}\n");
3609 fprintf (f, "%s:;\n", fail_label);
3610 fail_label = NULL;
3613 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3614 step of a '(simplify ...)' or '(match ...)'. This handles everything
3615 that is not part of the decision tree (simplify->match). */
3617 void
3618 dt_simplify::gen (FILE *f, int indent, bool gimple, int depth ATTRIBUTE_UNUSED)
3620 fprintf_indent (f, indent, "{\n");
3621 indent += 2;
3622 output_line_directive (f,
3623 s->result ? s->result->location : s->match->location);
3624 if (s->capture_max >= 0)
3626 char opname[20];
3627 fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = { %s",
3628 s->capture_max + 1, indexes[0]->get_name (opname));
3630 for (int i = 1; i <= s->capture_max; ++i)
3632 if (!indexes[i])
3633 break;
3634 fprintf (f, ", %s", indexes[i]->get_name (opname));
3636 fprintf (f, " };\n");
3639 /* If we have a split-out function for the actual transform, call it. */
3640 if (info && info->fname)
3642 if (gimple)
3644 fprintf_indent (f, indent, "if (%s (res_op, seq, "
3645 "valueize, type, captures", info->fname);
3646 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
3647 if (s->for_subst_vec[i].first->used)
3648 fprintf (f, ", %s", s->for_subst_vec[i].second->id);
3649 fprintf (f, "))\n");
3650 fprintf_indent (f, indent, " return true;\n");
3652 else
3654 fprintf_indent (f, indent, "tree res = %s (loc, type",
3655 info->fname);
3656 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
3657 fprintf (f, ", _p%d", i);
3658 fprintf (f, ", captures");
3659 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
3661 if (s->for_subst_vec[i].first->used)
3662 fprintf (f, ", %s", s->for_subst_vec[i].second->id);
3664 fprintf (f, ");\n");
3665 fprintf_indent (f, indent, "if (res) return res;\n");
3668 else
3670 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
3672 if (! s->for_subst_vec[i].first->used)
3673 continue;
3674 if (is_a <operator_id *> (s->for_subst_vec[i].second))
3675 fprintf_indent (f, indent, "const enum tree_code %s = %s;\n",
3676 s->for_subst_vec[i].first->id,
3677 s->for_subst_vec[i].second->id);
3678 else if (is_a <fn_id *> (s->for_subst_vec[i].second))
3679 fprintf_indent (f, indent, "const combined_fn %s = %s;\n",
3680 s->for_subst_vec[i].first->id,
3681 s->for_subst_vec[i].second->id);
3682 else
3683 gcc_unreachable ();
3685 gen_1 (f, indent, gimple, s->result);
3688 indent -= 2;
3689 fprintf_indent (f, indent, "}\n");
3693 /* Hash function for finding equivalent transforms. */
3695 hashval_t
3696 sinfo_hashmap_traits::hash (const key_type &v)
3698 /* Only bother to compare those originating from the same source pattern. */
3699 return v->s->result->location;
3702 /* Compare function for finding equivalent transforms. */
3704 static bool
3705 compare_op (operand *o1, simplify *s1, operand *o2, simplify *s2)
3707 if (o1->type != o2->type)
3708 return false;
3710 switch (o1->type)
3712 case operand::OP_IF:
3714 if_expr *if1 = as_a <if_expr *> (o1);
3715 if_expr *if2 = as_a <if_expr *> (o2);
3716 /* ??? Properly compare c-exprs. */
3717 if (if1->cond != if2->cond)
3718 return false;
3719 if (!compare_op (if1->trueexpr, s1, if2->trueexpr, s2))
3720 return false;
3721 if (if1->falseexpr != if2->falseexpr
3722 || (if1->falseexpr
3723 && !compare_op (if1->falseexpr, s1, if2->falseexpr, s2)))
3724 return false;
3725 return true;
3727 case operand::OP_WITH:
3729 with_expr *with1 = as_a <with_expr *> (o1);
3730 with_expr *with2 = as_a <with_expr *> (o2);
3731 if (with1->with != with2->with)
3732 return false;
3733 return compare_op (with1->subexpr, s1, with2->subexpr, s2);
3735 default:;
3738 /* We've hit a result. Time to compare capture-infos - this is required
3739 in addition to the conservative pointer-equivalency of the result IL. */
3740 capture_info cinfo1 (s1, o1, true);
3741 capture_info cinfo2 (s2, o2, true);
3743 if (cinfo1.force_no_side_effects != cinfo2.force_no_side_effects
3744 || cinfo1.info.length () != cinfo2.info.length ())
3745 return false;
3747 for (unsigned i = 0; i < cinfo1.info.length (); ++i)
3749 if (cinfo1.info[i].expr_p != cinfo2.info[i].expr_p
3750 || cinfo1.info[i].cse_p != cinfo2.info[i].cse_p
3751 || (cinfo1.info[i].force_no_side_effects_p
3752 != cinfo2.info[i].force_no_side_effects_p)
3753 || cinfo1.info[i].force_single_use != cinfo2.info[i].force_single_use
3754 || cinfo1.info[i].cond_expr_cond_p != cinfo2.info[i].cond_expr_cond_p
3755 /* toplevel_msk is an optimization */
3756 || cinfo1.info[i].result_use_count != cinfo2.info[i].result_use_count
3757 || cinfo1.info[i].same_as != cinfo2.info[i].same_as
3758 /* the pointer back to the capture is for diagnostics only */)
3759 return false;
3762 /* ??? Deep-compare the actual result. */
3763 return o1 == o2;
3766 bool
3767 sinfo_hashmap_traits::equal_keys (const key_type &v,
3768 const key_type &candidate)
3770 return compare_op (v->s->result, v->s, candidate->s->result, candidate->s);
3774 /* Main entry to generate code for matching GIMPLE IL off the decision
3775 tree. */
3777 void
3778 decision_tree::gen (FILE *f, bool gimple)
3780 sinfo_map_t si;
3782 root->analyze (si);
3784 fprintf (stderr, "%s decision tree has %u leafs, maximum depth %u and "
3785 "a total number of %u nodes\n",
3786 gimple ? "GIMPLE" : "GENERIC",
3787 root->num_leafs, root->max_level, root->total_size);
3789 /* First split out the transform part of equal leafs. */
3790 unsigned rcnt = 0;
3791 unsigned fcnt = 1;
3792 for (sinfo_map_t::iterator iter = si.begin ();
3793 iter != si.end (); ++iter)
3795 sinfo *s = (*iter).second;
3796 /* Do not split out single uses. */
3797 if (s->cnt <= 1)
3798 continue;
3800 rcnt += s->cnt - 1;
3801 if (verbose >= 1)
3803 fprintf (stderr, "found %u uses of", s->cnt);
3804 output_line_directive (stderr, s->s->s->result->location);
3807 /* Generate a split out function with the leaf transform code. */
3808 s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic",
3809 fcnt++);
3810 if (gimple)
3811 fprintf (f, "\nstatic bool\n"
3812 "%s (gimple_match_op *res_op, gimple_seq *seq,\n"
3813 " tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
3814 " const tree ARG_UNUSED (type), tree *ARG_UNUSED "
3815 "(captures)\n",
3816 s->fname);
3817 else
3819 fprintf (f, "\nstatic tree\n"
3820 "%s (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type),\n",
3821 (*iter).second->fname);
3822 for (unsigned i = 0;
3823 i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
3824 fprintf (f, " tree ARG_UNUSED (_p%d),", i);
3825 fprintf (f, " tree *captures\n");
3827 for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
3829 if (! s->s->s->for_subst_vec[i].first->used)
3830 continue;
3831 if (is_a <operator_id *> (s->s->s->for_subst_vec[i].second))
3832 fprintf (f, ", const enum tree_code ARG_UNUSED (%s)",
3833 s->s->s->for_subst_vec[i].first->id);
3834 else if (is_a <fn_id *> (s->s->s->for_subst_vec[i].second))
3835 fprintf (f, ", const combined_fn ARG_UNUSED (%s)",
3836 s->s->s->for_subst_vec[i].first->id);
3839 fprintf (f, ")\n{\n");
3840 s->s->gen_1 (f, 2, gimple, s->s->s->result);
3841 if (gimple)
3842 fprintf (f, " return false;\n");
3843 else
3844 fprintf (f, " return NULL_TREE;\n");
3845 fprintf (f, "}\n");
3847 fprintf (stderr, "removed %u duplicate tails\n", rcnt);
3849 for (unsigned n = 1; n <= 5; ++n)
3851 bool has_kids_p = false;
3853 /* First generate split-out functions. */
3854 for (unsigned j = 0; j < root->kids.length (); j++)
3856 dt_operand *dop = static_cast<dt_operand *>(root->kids[j]);
3857 expr *e = static_cast<expr *>(dop->op);
3858 if (e->ops.length () != n
3859 /* Builtin simplifications are somewhat premature on
3860 GENERIC. The following drops patterns with outermost
3861 calls. It's easy to emit overloads for function code
3862 though if necessary. */
3863 || (!gimple
3864 && e->operation->kind != id_base::CODE))
3865 continue;
3867 if (gimple)
3868 fprintf (f, "\nstatic bool\n"
3869 "gimple_simplify_%s (gimple_match_op *res_op,"
3870 " gimple_seq *seq,\n"
3871 " tree (*valueize)(tree) "
3872 "ATTRIBUTE_UNUSED,\n"
3873 " code_helper ARG_UNUSED (code), tree "
3874 "ARG_UNUSED (type)\n",
3875 e->operation->id);
3876 else
3877 fprintf (f, "\nstatic tree\n"
3878 "generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
3879 "tree_code ARG_UNUSED (code), const tree ARG_UNUSED (type)",
3880 e->operation->id);
3881 for (unsigned i = 0; i < n; ++i)
3882 fprintf (f, ", tree _p%d", i);
3883 fprintf (f, ")\n");
3884 fprintf (f, "{\n");
3885 dop->gen_kids (f, 2, gimple, 0);
3886 if (gimple)
3887 fprintf (f, " return false;\n");
3888 else
3889 fprintf (f, " return NULL_TREE;\n");
3890 fprintf (f, "}\n");
3891 has_kids_p = true;
3894 /* If this main entry has no children, avoid generating code
3895 with compiler warnings, by generating a simple stub. */
3896 if (! has_kids_p)
3898 if (gimple)
3899 fprintf (f, "\nstatic bool\n"
3900 "gimple_simplify (gimple_match_op*, gimple_seq*,\n"
3901 " tree (*)(tree), code_helper,\n"
3902 " const tree");
3903 else
3904 fprintf (f, "\ntree\n"
3905 "generic_simplify (location_t, enum tree_code,\n"
3906 " const tree");
3907 for (unsigned i = 0; i < n; ++i)
3908 fprintf (f, ", tree");
3909 fprintf (f, ")\n");
3910 fprintf (f, "{\n");
3911 if (gimple)
3912 fprintf (f, " return false;\n");
3913 else
3914 fprintf (f, " return NULL_TREE;\n");
3915 fprintf (f, "}\n");
3916 continue;
3919 /* Then generate the main entry with the outermost switch and
3920 tail-calls to the split-out functions. */
3921 if (gimple)
3922 fprintf (f, "\nstatic bool\n"
3923 "gimple_simplify (gimple_match_op *res_op, gimple_seq *seq,\n"
3924 " tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
3925 " code_helper code, const tree type");
3926 else
3927 fprintf (f, "\ntree\n"
3928 "generic_simplify (location_t loc, enum tree_code code, "
3929 "const tree type ATTRIBUTE_UNUSED");
3930 for (unsigned i = 0; i < n; ++i)
3931 fprintf (f, ", tree _p%d", i);
3932 fprintf (f, ")\n");
3933 fprintf (f, "{\n");
3935 if (gimple)
3936 fprintf (f, " switch (code.get_rep())\n"
3937 " {\n");
3938 else
3939 fprintf (f, " switch (code)\n"
3940 " {\n");
3941 for (unsigned i = 0; i < root->kids.length (); i++)
3943 dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
3944 expr *e = static_cast<expr *>(dop->op);
3945 if (e->ops.length () != n
3946 /* Builtin simplifications are somewhat premature on
3947 GENERIC. The following drops patterns with outermost
3948 calls. It's easy to emit overloads for function code
3949 though if necessary. */
3950 || (!gimple
3951 && e->operation->kind != id_base::CODE))
3952 continue;
3954 if (*e->operation == CONVERT_EXPR
3955 || *e->operation == NOP_EXPR)
3956 fprintf (f, " CASE_CONVERT:\n");
3957 else
3958 fprintf (f, " case %s%s:\n",
3959 is_a <fn_id *> (e->operation) ? "-" : "",
3960 e->operation->id);
3961 if (gimple)
3962 fprintf (f, " return gimple_simplify_%s (res_op, "
3963 "seq, valueize, code, type", e->operation->id);
3964 else
3965 fprintf (f, " return generic_simplify_%s (loc, code, type",
3966 e->operation->id);
3967 for (unsigned j = 0; j < n; ++j)
3968 fprintf (f, ", _p%d", j);
3969 fprintf (f, ");\n");
3971 fprintf (f, " default:;\n"
3972 " }\n");
3974 if (gimple)
3975 fprintf (f, " return false;\n");
3976 else
3977 fprintf (f, " return NULL_TREE;\n");
3978 fprintf (f, "}\n");
3982 /* Output code to implement the predicate P from the decision tree DT. */
3984 void
3985 write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
3987 fprintf (f, "\nbool\n"
3988 "%s%s (tree t%s%s)\n"
3989 "{\n", gimple ? "gimple_" : "tree_", p->id,
3990 p->nargs > 0 ? ", tree *res_ops" : "",
3991 gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
3992 /* Conveniently make 'type' available. */
3993 fprintf_indent (f, 2, "const tree type = TREE_TYPE (t);\n");
3995 if (!gimple)
3996 fprintf_indent (f, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
3997 dt.root->gen_kids (f, 2, gimple, 0);
3999 fprintf_indent (f, 2, "return false;\n"
4000 "}\n");
4003 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
4005 static void
4006 write_header (FILE *f, const char *head)
4008 fprintf (f, "/* Generated automatically by the program `genmatch' from\n");
4009 fprintf (f, " a IL pattern matching and simplification description. */\n");
4011 /* Include the header instead of writing it awkwardly quoted here. */
4012 fprintf (f, "\n#include \"%s\"\n", head);
4017 /* AST parsing. */
4019 class parser
4021 public:
4022 parser (cpp_reader *, bool gimple);
4024 private:
4025 const cpp_token *next ();
4026 const cpp_token *peek (unsigned = 1);
4027 const cpp_token *peek_ident (const char * = NULL, unsigned = 1);
4028 const cpp_token *expect (enum cpp_ttype);
4029 const cpp_token *eat_token (enum cpp_ttype);
4030 const char *get_string ();
4031 const char *get_ident ();
4032 const cpp_token *eat_ident (const char *);
4033 const char *get_number ();
4035 unsigned get_internal_capture_id ();
4037 id_base *parse_operation (unsigned char &);
4038 operand *parse_capture (operand *, bool);
4039 operand *parse_expr ();
4040 c_expr *parse_c_expr (cpp_ttype);
4041 operand *parse_op ();
4043 void record_operlist (location_t, user_id *);
4045 void parse_pattern ();
4046 operand *parse_result (operand *, predicate_id *);
4047 void push_simplify (simplify::simplify_kind,
4048 vec<simplify *>&, operand *, operand *);
4049 void parse_simplify (simplify::simplify_kind,
4050 vec<simplify *>&, predicate_id *, operand *);
4051 void parse_for (location_t);
4052 void parse_if (location_t);
4053 void parse_predicates (location_t);
4054 void parse_operator_list (location_t);
4056 void finish_match_operand (operand *);
4058 cpp_reader *r;
4059 bool gimple;
4060 vec<c_expr *> active_ifs;
4061 vec<vec<user_id *> > active_fors;
4062 hash_set<user_id *> *oper_lists_set;
4063 vec<user_id *> oper_lists;
4065 cid_map_t *capture_ids;
4066 unsigned last_id;
4068 public:
4069 vec<simplify *> simplifiers;
4070 vec<predicate_id *> user_predicates;
4071 bool parsing_match_operand;
4074 /* Lexing helpers. */
4076 /* Read the next non-whitespace token from R. */
4078 const cpp_token *
4079 parser::next ()
4081 const cpp_token *token;
4084 token = cpp_get_token (r);
4086 while (token->type == CPP_PADDING);
4087 return token;
4090 /* Peek at the next non-whitespace token from R. */
4092 const cpp_token *
4093 parser::peek (unsigned num)
4095 const cpp_token *token;
4096 unsigned i = 0;
4099 token = cpp_peek_token (r, i++);
4101 while (token->type == CPP_PADDING
4102 || (--num > 0));
4103 /* If we peek at EOF this is a fatal error as it leaves the
4104 cpp_reader in unusable state. Assume we really wanted a
4105 token and thus this EOF is unexpected. */
4106 if (token->type == CPP_EOF)
4107 fatal_at (token, "unexpected end of file");
4108 return token;
4111 /* Peek at the next identifier token (or return NULL if the next
4112 token is not an identifier or equal to ID if supplied). */
4114 const cpp_token *
4115 parser::peek_ident (const char *id, unsigned num)
4117 const cpp_token *token = peek (num);
4118 if (token->type != CPP_NAME)
4119 return 0;
4121 if (id == 0)
4122 return token;
4124 const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
4125 if (strcmp (id, t) == 0)
4126 return token;
4128 return 0;
4131 /* Read the next token from R and assert it is of type TK. */
4133 const cpp_token *
4134 parser::expect (enum cpp_ttype tk)
4136 const cpp_token *token = next ();
4137 if (token->type != tk)
4138 fatal_at (token, "expected %s, got %s",
4139 cpp_type2name (tk, 0), cpp_type2name (token->type, 0));
4141 return token;
4144 /* Consume the next token from R and assert it is of type TK. */
4146 const cpp_token *
4147 parser::eat_token (enum cpp_ttype tk)
4149 return expect (tk);
4152 /* Read the next token from R and assert it is of type CPP_STRING and
4153 return its value. */
4155 const char *
4156 parser::get_string ()
4158 const cpp_token *token = expect (CPP_STRING);
4159 return (const char *)token->val.str.text;
4162 /* Read the next token from R and assert it is of type CPP_NAME and
4163 return its value. */
4165 const char *
4166 parser::get_ident ()
4168 const cpp_token *token = expect (CPP_NAME);
4169 return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
4172 /* Eat an identifier token with value S from R. */
4174 const cpp_token *
4175 parser::eat_ident (const char *s)
4177 const cpp_token *token = peek ();
4178 const char *t = get_ident ();
4179 if (strcmp (s, t) != 0)
4180 fatal_at (token, "expected '%s' got '%s'\n", s, t);
4181 return token;
4184 /* Read the next token from R and assert it is of type CPP_NUMBER and
4185 return its value. */
4187 const char *
4188 parser::get_number ()
4190 const cpp_token *token = expect (CPP_NUMBER);
4191 return (const char *)token->val.str.text;
4194 /* Return a capture ID that can be used internally. */
4196 unsigned
4197 parser::get_internal_capture_id ()
4199 unsigned newid = capture_ids->elements ();
4200 /* Big enough for a 32-bit UINT_MAX plus prefix. */
4201 char id[13];
4202 bool existed;
4203 snprintf (id, sizeof (id), "__%u", newid);
4204 capture_ids->get_or_insert (xstrdup (id), &existed);
4205 if (existed)
4206 fatal ("reserved capture id '%s' already used", id);
4207 return newid;
4210 /* Record an operator-list use for transparent for handling. */
4212 void
4213 parser::record_operlist (location_t loc, user_id *p)
4215 if (!oper_lists_set->add (p))
4217 if (!oper_lists.is_empty ()
4218 && oper_lists[0]->substitutes.length () != p->substitutes.length ())
4219 fatal_at (loc, "User-defined operator list does not have the "
4220 "same number of entries as others used in the pattern");
4221 oper_lists.safe_push (p);
4225 /* Parse the operator ID, special-casing convert?, convert1? and
4226 convert2? */
4228 id_base *
4229 parser::parse_operation (unsigned char &opt_grp)
4231 const cpp_token *id_tok = peek ();
4232 char *alt_id = NULL;
4233 const char *id = get_ident ();
4234 const cpp_token *token = peek ();
4235 opt_grp = 0;
4236 if (token->type == CPP_QUERY
4237 && !(token->flags & PREV_WHITE))
4239 if (!parsing_match_operand)
4240 fatal_at (id_tok, "conditional convert can only be used in "
4241 "match expression");
4242 if (ISDIGIT (id[strlen (id) - 1]))
4244 opt_grp = id[strlen (id) - 1] - '0' + 1;
4245 alt_id = xstrdup (id);
4246 alt_id[strlen (id) - 1] = '\0';
4247 if (opt_grp == 1)
4248 fatal_at (id_tok, "use '%s?' here", alt_id);
4250 else
4251 opt_grp = 1;
4252 eat_token (CPP_QUERY);
4254 id_base *op = get_operator (alt_id ? alt_id : id);
4255 if (!op)
4256 fatal_at (id_tok, "unknown operator %s", alt_id ? alt_id : id);
4257 if (alt_id)
4258 free (alt_id);
4259 user_id *p = dyn_cast<user_id *> (op);
4260 if (p && p->is_oper_list)
4262 if (active_fors.length() == 0)
4263 record_operlist (id_tok->src_loc, p);
4264 else
4265 fatal_at (id_tok, "operator-list %s cannot be expanded inside 'for'", id);
4267 return op;
4270 /* Parse a capture.
4271 capture = '@'<number> */
4273 class operand *
4274 parser::parse_capture (operand *op, bool require_existing)
4276 location_t src_loc = eat_token (CPP_ATSIGN)->src_loc;
4277 const cpp_token *token = peek ();
4278 const char *id = NULL;
4279 bool value_match = false;
4280 /* For matches parse @@ as a value-match denoting the prevailing operand. */
4281 if (token->type == CPP_ATSIGN
4282 && ! (token->flags & PREV_WHITE)
4283 && parsing_match_operand)
4285 eat_token (CPP_ATSIGN);
4286 token = peek ();
4287 value_match = true;
4289 if (token->type == CPP_NUMBER)
4290 id = get_number ();
4291 else if (token->type == CPP_NAME)
4292 id = get_ident ();
4293 else
4294 fatal_at (token, "expected number or identifier");
4295 unsigned next_id = capture_ids->elements ();
4296 bool existed;
4297 unsigned &num = capture_ids->get_or_insert (id, &existed);
4298 if (!existed)
4300 if (require_existing)
4301 fatal_at (src_loc, "unknown capture id");
4302 num = next_id;
4304 return new capture (src_loc, num, op, value_match);
4307 /* Parse an expression
4308 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
4310 class operand *
4311 parser::parse_expr ()
4313 const cpp_token *token = peek ();
4314 unsigned char opt_grp;
4315 expr *e = new expr (parse_operation (opt_grp), token->src_loc);
4316 token = peek ();
4317 operand *op;
4318 bool is_commutative = false;
4319 bool force_capture = false;
4320 const char *expr_type = NULL;
4322 if (!parsing_match_operand
4323 && token->type == CPP_NOT
4324 && !(token->flags & PREV_WHITE))
4326 eat_token (CPP_NOT);
4327 e->force_leaf = true;
4330 if (token->type == CPP_COLON
4331 && !(token->flags & PREV_WHITE))
4333 eat_token (CPP_COLON);
4334 token = peek ();
4335 if (token->type == CPP_NAME
4336 && !(token->flags & PREV_WHITE))
4338 const char *s = get_ident ();
4339 if (!parsing_match_operand)
4340 expr_type = s;
4341 else
4343 const char *sp = s;
4344 while (*sp)
4346 if (*sp == 'c')
4348 if (operator_id *o
4349 = dyn_cast<operator_id *> (e->operation))
4351 if (!commutative_tree_code (o->code)
4352 && !comparison_code_p (o->code))
4353 fatal_at (token, "operation is not commutative");
4355 else if (user_id *p = dyn_cast<user_id *> (e->operation))
4356 for (unsigned i = 0;
4357 i < p->substitutes.length (); ++i)
4359 if (operator_id *q
4360 = dyn_cast<operator_id *> (p->substitutes[i]))
4362 if (!commutative_tree_code (q->code)
4363 && !comparison_code_p (q->code))
4364 fatal_at (token, "operation %s is not "
4365 "commutative", q->id);
4368 is_commutative = true;
4370 else if (*sp == 'C')
4371 is_commutative = true;
4372 else if (*sp == 's')
4374 e->force_single_use = true;
4375 force_capture = true;
4377 else
4378 fatal_at (token, "flag %c not recognized", *sp);
4379 sp++;
4382 token = peek ();
4384 else
4385 fatal_at (token, "expected flag or type specifying identifier");
4388 if (token->type == CPP_ATSIGN
4389 && !(token->flags & PREV_WHITE))
4390 op = parse_capture (e, false);
4391 else if (force_capture)
4393 unsigned num = get_internal_capture_id ();
4394 op = new capture (token->src_loc, num, e, false);
4396 else
4397 op = e;
4400 token = peek ();
4401 if (token->type == CPP_CLOSE_PAREN)
4403 if (e->operation->nargs != -1
4404 && e->operation->nargs != (int) e->ops.length ())
4405 fatal_at (token, "'%s' expects %u operands, not %u",
4406 e->operation->id, e->operation->nargs, e->ops.length ());
4407 if (is_commutative)
4409 if (e->ops.length () == 2
4410 || commutative_op (e->operation) >= 0)
4411 e->is_commutative = true;
4412 else
4413 fatal_at (token, "only binary operators or functions with "
4414 "two arguments can be marked commutative, "
4415 "unless the operation is known to be inherently "
4416 "commutative");
4418 e->expr_type = expr_type;
4419 if (opt_grp != 0)
4421 if (e->ops.length () != 1)
4422 fatal_at (token, "only unary operations can be conditional");
4423 e->opt_grp = opt_grp;
4425 return op;
4427 else if (!(token->flags & PREV_WHITE))
4428 fatal_at (token, "expected expression operand");
4430 e->append_op (parse_op ());
4432 while (1);
4435 /* Lex native C code delimited by START recording the preprocessing tokens
4436 for later processing.
4437 c_expr = ('{'|'(') <pp token>... ('}'|')') */
4439 c_expr *
4440 parser::parse_c_expr (cpp_ttype start)
4442 const cpp_token *token;
4443 cpp_ttype end;
4444 unsigned opencnt;
4445 vec<cpp_token> code = vNULL;
4446 unsigned nr_stmts = 0;
4447 location_t loc = eat_token (start)->src_loc;
4448 if (start == CPP_OPEN_PAREN)
4449 end = CPP_CLOSE_PAREN;
4450 else if (start == CPP_OPEN_BRACE)
4451 end = CPP_CLOSE_BRACE;
4452 else
4453 gcc_unreachable ();
4454 opencnt = 1;
4457 token = next ();
4459 /* Count brace pairs to find the end of the expr to match. */
4460 if (token->type == start)
4461 opencnt++;
4462 else if (token->type == end
4463 && --opencnt == 0)
4464 break;
4465 else if (token->type == CPP_EOF)
4466 fatal_at (token, "unexpected end of file");
4468 /* This is a lame way of counting the number of statements. */
4469 if (token->type == CPP_SEMICOLON)
4470 nr_stmts++;
4472 /* If this is possibly a user-defined identifier mark it used. */
4473 if (token->type == CPP_NAME)
4475 const char *str
4476 = (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
4477 if (strcmp (str, "return") == 0)
4478 fatal_at (token, "return statement not allowed in C expression");
4479 id_base *idb = get_operator (str);
4480 user_id *p;
4481 if (idb && (p = dyn_cast<user_id *> (idb)) && p->is_oper_list)
4482 record_operlist (token->src_loc, p);
4485 /* Record the token. */
4486 code.safe_push (*token);
4488 while (1);
4489 return new c_expr (r, loc, code, nr_stmts, vNULL, capture_ids);
4492 /* Parse an operand which is either an expression, a predicate or
4493 a standalone capture.
4494 op = predicate | expr | c_expr | capture */
4496 class operand *
4497 parser::parse_op ()
4499 const cpp_token *token = peek ();
4500 class operand *op = NULL;
4501 if (token->type == CPP_OPEN_PAREN)
4503 eat_token (CPP_OPEN_PAREN);
4504 op = parse_expr ();
4505 eat_token (CPP_CLOSE_PAREN);
4507 else if (token->type == CPP_OPEN_BRACE)
4509 op = parse_c_expr (CPP_OPEN_BRACE);
4511 else
4513 /* Remaining ops are either empty or predicates */
4514 if (token->type == CPP_NAME)
4516 const char *id = get_ident ();
4517 id_base *opr = get_operator (id);
4518 if (!opr)
4519 fatal_at (token, "expected predicate name");
4520 if (operator_id *code1 = dyn_cast <operator_id *> (opr))
4522 if (code1->nargs != 0)
4523 fatal_at (token, "using an operator with operands as predicate");
4524 /* Parse the zero-operand operator "predicates" as
4525 expression. */
4526 op = new expr (opr, token->src_loc);
4528 else if (user_id *code2 = dyn_cast <user_id *> (opr))
4530 if (code2->nargs != 0)
4531 fatal_at (token, "using an operator with operands as predicate");
4532 /* Parse the zero-operand operator "predicates" as
4533 expression. */
4534 op = new expr (opr, token->src_loc);
4536 else if (predicate_id *p = dyn_cast <predicate_id *> (opr))
4537 op = new predicate (p, token->src_loc);
4538 else
4539 fatal_at (token, "using an unsupported operator as predicate");
4540 if (!parsing_match_operand)
4541 fatal_at (token, "predicates are only allowed in match expression");
4542 token = peek ();
4543 if (token->flags & PREV_WHITE)
4544 return op;
4546 else if (token->type != CPP_COLON
4547 && token->type != CPP_ATSIGN)
4548 fatal_at (token, "expected expression or predicate");
4549 /* optionally followed by a capture and a predicate. */
4550 if (token->type == CPP_COLON)
4551 fatal_at (token, "not implemented: predicate on leaf operand");
4552 if (token->type == CPP_ATSIGN)
4553 op = parse_capture (op, !parsing_match_operand);
4556 return op;
4559 /* Create a new simplify from the current parsing state and MATCH,
4560 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
4562 void
4563 parser::push_simplify (simplify::simplify_kind kind,
4564 vec<simplify *>& simplifiers,
4565 operand *match, operand *result)
4567 /* Build and push a temporary for operator list uses in expressions. */
4568 if (!oper_lists.is_empty ())
4569 active_fors.safe_push (oper_lists);
4571 simplifiers.safe_push
4572 (new simplify (kind, last_id++, match, result,
4573 active_fors.copy (), capture_ids));
4575 if (!oper_lists.is_empty ())
4576 active_fors.pop ();
4579 /* Parse
4580 <result-op> = <op> | <if> | <with>
4581 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
4582 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
4583 and return it. */
4585 operand *
4586 parser::parse_result (operand *result, predicate_id *matcher)
4588 const cpp_token *token = peek ();
4589 if (token->type != CPP_OPEN_PAREN)
4590 return parse_op ();
4592 eat_token (CPP_OPEN_PAREN);
4593 if (peek_ident ("if"))
4595 eat_ident ("if");
4596 if_expr *ife = new if_expr (token->src_loc);
4597 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4598 if (peek ()->type == CPP_OPEN_PAREN)
4600 ife->trueexpr = parse_result (result, matcher);
4601 if (peek ()->type == CPP_OPEN_PAREN)
4602 ife->falseexpr = parse_result (result, matcher);
4603 else if (peek ()->type != CPP_CLOSE_PAREN)
4604 ife->falseexpr = parse_op ();
4606 else if (peek ()->type != CPP_CLOSE_PAREN)
4608 ife->trueexpr = parse_op ();
4609 if (peek ()->type == CPP_OPEN_PAREN)
4610 ife->falseexpr = parse_result (result, matcher);
4611 else if (peek ()->type != CPP_CLOSE_PAREN)
4612 ife->falseexpr = parse_op ();
4614 /* If this if is immediately closed then it contains a
4615 manual matcher or is part of a predicate definition. */
4616 else /* if (peek ()->type == CPP_CLOSE_PAREN) */
4618 if (!matcher)
4619 fatal_at (peek (), "manual transform not implemented");
4620 ife->trueexpr = result;
4622 eat_token (CPP_CLOSE_PAREN);
4623 return ife;
4625 else if (peek_ident ("with"))
4627 eat_ident ("with");
4628 with_expr *withe = new with_expr (token->src_loc);
4629 /* Parse (with c-expr expr) as (if-with (true) expr). */
4630 withe->with = parse_c_expr (CPP_OPEN_BRACE);
4631 withe->with->nr_stmts = 0;
4632 withe->subexpr = parse_result (result, matcher);
4633 eat_token (CPP_CLOSE_PAREN);
4634 return withe;
4636 else if (peek_ident ("switch"))
4638 token = eat_ident ("switch");
4639 location_t ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
4640 eat_ident ("if");
4641 if_expr *ife = new if_expr (ifloc);
4642 operand *res = ife;
4643 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4644 if (peek ()->type == CPP_OPEN_PAREN)
4645 ife->trueexpr = parse_result (result, matcher);
4646 else
4647 ife->trueexpr = parse_op ();
4648 eat_token (CPP_CLOSE_PAREN);
4649 if (peek ()->type != CPP_OPEN_PAREN
4650 || !peek_ident ("if", 2))
4651 fatal_at (token, "switch can be implemented with a single if");
4652 while (peek ()->type != CPP_CLOSE_PAREN)
4654 if (peek ()->type == CPP_OPEN_PAREN)
4656 if (peek_ident ("if", 2))
4658 ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
4659 eat_ident ("if");
4660 ife->falseexpr = new if_expr (ifloc);
4661 ife = as_a <if_expr *> (ife->falseexpr);
4662 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4663 if (peek ()->type == CPP_OPEN_PAREN)
4664 ife->trueexpr = parse_result (result, matcher);
4665 else
4666 ife->trueexpr = parse_op ();
4667 eat_token (CPP_CLOSE_PAREN);
4669 else
4671 /* switch default clause */
4672 ife->falseexpr = parse_result (result, matcher);
4673 eat_token (CPP_CLOSE_PAREN);
4674 return res;
4677 else
4679 /* switch default clause */
4680 ife->falseexpr = parse_op ();
4681 eat_token (CPP_CLOSE_PAREN);
4682 return res;
4685 eat_token (CPP_CLOSE_PAREN);
4686 return res;
4688 else
4690 operand *op = result;
4691 if (!matcher)
4692 op = parse_expr ();
4693 eat_token (CPP_CLOSE_PAREN);
4694 return op;
4698 /* Parse
4699 simplify = 'simplify' <expr> <result-op>
4701 match = 'match' <ident> <expr> [<result-op>]
4702 and fill SIMPLIFIERS with the results. */
4704 void
4705 parser::parse_simplify (simplify::simplify_kind kind,
4706 vec<simplify *>& simplifiers, predicate_id *matcher,
4707 operand *result)
4709 /* Reset the capture map. */
4710 if (!capture_ids)
4711 capture_ids = new cid_map_t;
4712 /* Reset oper_lists and set. */
4713 hash_set <user_id *> olist;
4714 oper_lists_set = &olist;
4715 oper_lists = vNULL;
4717 const cpp_token *loc = peek ();
4718 parsing_match_operand = true;
4719 class operand *match = parse_op ();
4720 finish_match_operand (match);
4721 parsing_match_operand = false;
4722 if (match->type == operand::OP_CAPTURE && !matcher)
4723 fatal_at (loc, "outermost expression cannot be captured");
4724 if (match->type == operand::OP_EXPR
4725 && is_a <predicate_id *> (as_a <expr *> (match)->operation))
4726 fatal_at (loc, "outermost expression cannot be a predicate");
4728 /* Splice active_ifs onto result and continue parsing the
4729 "then" expr. */
4730 if_expr *active_if = NULL;
4731 for (int i = active_ifs.length (); i > 0; --i)
4733 if_expr *ifc = new if_expr (active_ifs[i-1]->location);
4734 ifc->cond = active_ifs[i-1];
4735 ifc->trueexpr = active_if;
4736 active_if = ifc;
4738 if_expr *outermost_if = active_if;
4739 while (active_if && active_if->trueexpr)
4740 active_if = as_a <if_expr *> (active_if->trueexpr);
4742 const cpp_token *token = peek ();
4744 /* If this if is immediately closed then it is part of a predicate
4745 definition. Push it. */
4746 if (token->type == CPP_CLOSE_PAREN)
4748 if (!matcher)
4749 fatal_at (token, "expected transform expression");
4750 if (active_if)
4752 active_if->trueexpr = result;
4753 result = outermost_if;
4755 push_simplify (kind, simplifiers, match, result);
4756 return;
4759 operand *tem = parse_result (result, matcher);
4760 if (active_if)
4762 active_if->trueexpr = tem;
4763 result = outermost_if;
4765 else
4766 result = tem;
4768 push_simplify (kind, simplifiers, match, result);
4771 /* Parsing of the outer control structures. */
4773 /* Parse a for expression
4774 for = '(' 'for' <subst>... <pattern> ')'
4775 subst = <ident> '(' <ident>... ')' */
4777 void
4778 parser::parse_for (location_t)
4780 auto_vec<const cpp_token *> user_id_tokens;
4781 vec<user_id *> user_ids = vNULL;
4782 const cpp_token *token;
4783 unsigned min_n_opers = 0, max_n_opers = 0;
4785 while (1)
4787 token = peek ();
4788 if (token->type != CPP_NAME)
4789 break;
4791 /* Insert the user defined operators into the operator hash. */
4792 const char *id = get_ident ();
4793 if (get_operator (id, true) != NULL)
4794 fatal_at (token, "operator already defined");
4795 user_id *op = new user_id (id);
4796 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
4797 *slot = op;
4798 user_ids.safe_push (op);
4799 user_id_tokens.safe_push (token);
4801 eat_token (CPP_OPEN_PAREN);
4803 int arity = -1;
4804 while ((token = peek_ident ()) != 0)
4806 const char *oper = get_ident ();
4807 id_base *idb = get_operator (oper, true);
4808 if (idb == NULL)
4809 fatal_at (token, "no such operator '%s'", oper);
4811 if (arity == -1)
4812 arity = idb->nargs;
4813 else if (idb->nargs == -1)
4815 else if (idb->nargs != arity)
4816 fatal_at (token, "operator '%s' with arity %d does not match "
4817 "others with arity %d", oper, idb->nargs, arity);
4819 user_id *p = dyn_cast<user_id *> (idb);
4820 if (p)
4822 if (p->is_oper_list)
4823 op->substitutes.safe_splice (p->substitutes);
4824 else
4825 fatal_at (token, "iterator cannot be used as operator-list");
4827 else
4828 op->substitutes.safe_push (idb);
4830 op->nargs = arity;
4831 token = expect (CPP_CLOSE_PAREN);
4833 unsigned nsubstitutes = op->substitutes.length ();
4834 if (nsubstitutes == 0)
4835 fatal_at (token, "A user-defined operator must have at least "
4836 "one substitution");
4837 if (max_n_opers == 0)
4839 min_n_opers = nsubstitutes;
4840 max_n_opers = nsubstitutes;
4842 else
4844 if (nsubstitutes % min_n_opers != 0
4845 && min_n_opers % nsubstitutes != 0)
4846 fatal_at (token, "All user-defined identifiers must have a "
4847 "multiple number of operator substitutions of the "
4848 "smallest number of substitutions");
4849 if (nsubstitutes < min_n_opers)
4850 min_n_opers = nsubstitutes;
4851 else if (nsubstitutes > max_n_opers)
4852 max_n_opers = nsubstitutes;
4856 unsigned n_ids = user_ids.length ();
4857 if (n_ids == 0)
4858 fatal_at (token, "for requires at least one user-defined identifier");
4860 token = peek ();
4861 if (token->type == CPP_CLOSE_PAREN)
4862 fatal_at (token, "no pattern defined in for");
4864 active_fors.safe_push (user_ids);
4865 while (1)
4867 token = peek ();
4868 if (token->type == CPP_CLOSE_PAREN)
4869 break;
4870 parse_pattern ();
4872 active_fors.pop ();
4874 /* Remove user-defined operators from the hash again. */
4875 for (unsigned i = 0; i < user_ids.length (); ++i)
4877 if (!user_ids[i]->used)
4878 warning_at (user_id_tokens[i],
4879 "operator %s defined but not used", user_ids[i]->id);
4880 operators->remove_elt (user_ids[i]);
4884 /* Parse an identifier associated with a list of operators.
4885 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
4887 void
4888 parser::parse_operator_list (location_t)
4890 const cpp_token *token = peek ();
4891 const char *id = get_ident ();
4893 if (get_operator (id, true) != 0)
4894 fatal_at (token, "operator %s already defined", id);
4896 user_id *op = new user_id (id, true);
4897 int arity = -1;
4899 while ((token = peek_ident ()) != 0)
4901 token = peek ();
4902 const char *oper = get_ident ();
4903 id_base *idb = get_operator (oper, true);
4905 if (idb == 0)
4906 fatal_at (token, "no such operator '%s'", oper);
4908 if (arity == -1)
4909 arity = idb->nargs;
4910 else if (idb->nargs == -1)
4912 else if (arity != idb->nargs)
4913 fatal_at (token, "operator '%s' with arity %d does not match "
4914 "others with arity %d", oper, idb->nargs, arity);
4916 /* We allow composition of multiple operator lists. */
4917 if (user_id *p = dyn_cast<user_id *> (idb))
4918 op->substitutes.safe_splice (p->substitutes);
4919 else
4920 op->substitutes.safe_push (idb);
4923 // Check that there is no junk after id-list
4924 token = peek();
4925 if (token->type != CPP_CLOSE_PAREN)
4926 fatal_at (token, "expected identifier got %s", cpp_type2name (token->type, 0));
4928 if (op->substitutes.length () == 0)
4929 fatal_at (token, "operator-list cannot be empty");
4931 op->nargs = arity;
4932 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
4933 *slot = op;
4936 /* Parse an outer if expression.
4937 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
4939 void
4940 parser::parse_if (location_t)
4942 c_expr *ifexpr = parse_c_expr (CPP_OPEN_PAREN);
4944 const cpp_token *token = peek ();
4945 if (token->type == CPP_CLOSE_PAREN)
4946 fatal_at (token, "no pattern defined in if");
4948 active_ifs.safe_push (ifexpr);
4949 while (1)
4951 token = peek ();
4952 if (token->type == CPP_CLOSE_PAREN)
4953 break;
4955 parse_pattern ();
4957 active_ifs.pop ();
4960 /* Parse a list of predefined predicate identifiers.
4961 preds = '(' 'define_predicates' <ident>... ')' */
4963 void
4964 parser::parse_predicates (location_t)
4968 const cpp_token *token = peek ();
4969 if (token->type != CPP_NAME)
4970 break;
4972 add_predicate (get_ident ());
4974 while (1);
4977 /* Parse outer control structures.
4978 pattern = <preds>|<for>|<if>|<simplify>|<match> */
4980 void
4981 parser::parse_pattern ()
4983 /* All clauses start with '('. */
4984 eat_token (CPP_OPEN_PAREN);
4985 const cpp_token *token = peek ();
4986 const char *id = get_ident ();
4987 if (strcmp (id, "simplify") == 0)
4989 parse_simplify (simplify::SIMPLIFY, simplifiers, NULL, NULL);
4990 capture_ids = NULL;
4992 else if (strcmp (id, "match") == 0)
4994 bool with_args = false;
4995 location_t e_loc = peek ()->src_loc;
4996 if (peek ()->type == CPP_OPEN_PAREN)
4998 eat_token (CPP_OPEN_PAREN);
4999 with_args = true;
5001 const char *name = get_ident ();
5002 id_base *id1 = get_operator (name);
5003 predicate_id *p;
5004 if (!id1)
5006 p = add_predicate (name);
5007 user_predicates.safe_push (p);
5009 else if ((p = dyn_cast <predicate_id *> (id1)))
5011 else
5012 fatal_at (token, "cannot add a match to a non-predicate ID");
5013 /* Parse (match <id> <arg>... (match-expr)) here. */
5014 expr *e = NULL;
5015 if (with_args)
5017 capture_ids = new cid_map_t;
5018 e = new expr (p, e_loc);
5019 while (peek ()->type == CPP_ATSIGN)
5020 e->append_op (parse_capture (NULL, false));
5021 eat_token (CPP_CLOSE_PAREN);
5023 if (p->nargs != -1
5024 && ((e && e->ops.length () != (unsigned)p->nargs)
5025 || (!e && p->nargs != 0)))
5026 fatal_at (token, "non-matching number of match operands");
5027 p->nargs = e ? e->ops.length () : 0;
5028 parse_simplify (simplify::MATCH, p->matchers, p, e);
5029 capture_ids = NULL;
5031 else if (strcmp (id, "for") == 0)
5032 parse_for (token->src_loc);
5033 else if (strcmp (id, "if") == 0)
5034 parse_if (token->src_loc);
5035 else if (strcmp (id, "define_predicates") == 0)
5037 if (active_ifs.length () > 0
5038 || active_fors.length () > 0)
5039 fatal_at (token, "define_predicates inside if or for is not supported");
5040 parse_predicates (token->src_loc);
5042 else if (strcmp (id, "define_operator_list") == 0)
5044 if (active_ifs.length () > 0
5045 || active_fors.length () > 0)
5046 fatal_at (token, "operator-list inside if or for is not supported");
5047 parse_operator_list (token->src_loc);
5049 else
5050 fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'",
5051 active_ifs.length () == 0 && active_fors.length () == 0
5052 ? "'define_predicates', " : "");
5054 eat_token (CPP_CLOSE_PAREN);
5057 /* Helper for finish_match_operand, collecting captures of OP in CPTS
5058 recursively. */
5060 static void
5061 walk_captures (operand *op, vec<vec<capture *> > &cpts)
5063 if (! op)
5064 return;
5066 if (capture *c = dyn_cast <capture *> (op))
5068 cpts[c->where].safe_push (c);
5069 walk_captures (c->what, cpts);
5071 else if (expr *e = dyn_cast <expr *> (op))
5072 for (unsigned i = 0; i < e->ops.length (); ++i)
5073 walk_captures (e->ops[i], cpts);
5076 /* Finish up OP which is a match operand. */
5078 void
5079 parser::finish_match_operand (operand *op)
5081 /* Look for matching captures, diagnose mis-uses of @@ and apply
5082 early lowering and distribution of value_match. */
5083 auto_vec<vec<capture *> > cpts;
5084 cpts.safe_grow_cleared (capture_ids->elements (), true);
5085 walk_captures (op, cpts);
5086 for (unsigned i = 0; i < cpts.length (); ++i)
5088 capture *value_match = NULL;
5089 for (unsigned j = 0; j < cpts[i].length (); ++j)
5091 if (cpts[i][j]->value_match)
5093 if (value_match)
5094 fatal_at (cpts[i][j]->location, "duplicate @@");
5095 value_match = cpts[i][j];
5098 if (cpts[i].length () == 1 && value_match)
5099 fatal_at (value_match->location, "@@ without a matching capture");
5100 if (value_match)
5102 /* Duplicate prevailing capture with the existing ID, create
5103 a fake ID and rewrite all captures to use it. This turns
5104 @@1 into @__<newid>@1 and @1 into @__<newid>. */
5105 value_match->what = new capture (value_match->location,
5106 value_match->where,
5107 value_match->what, false);
5108 /* Create a fake ID and rewrite all captures to use it. */
5109 unsigned newid = get_internal_capture_id ();
5110 for (unsigned j = 0; j < cpts[i].length (); ++j)
5112 cpts[i][j]->where = newid;
5113 cpts[i][j]->value_match = true;
5116 cpts[i].release ();
5120 /* Main entry of the parser. Repeatedly parse outer control structures. */
5122 parser::parser (cpp_reader *r_, bool gimple_)
5124 r = r_;
5125 gimple = gimple_;
5126 active_ifs = vNULL;
5127 active_fors = vNULL;
5128 simplifiers = vNULL;
5129 oper_lists_set = NULL;
5130 oper_lists = vNULL;
5131 capture_ids = NULL;
5132 user_predicates = vNULL;
5133 parsing_match_operand = false;
5134 last_id = 0;
5136 const cpp_token *token = next ();
5137 while (token->type != CPP_EOF)
5139 _cpp_backup_tokens (r, 1);
5140 parse_pattern ();
5141 token = next ();
5146 /* Helper for the linemap code. */
5148 static size_t
5149 round_alloc_size (size_t s)
5151 return s;
5155 /* The genmatch generator program. It reads from a pattern description
5156 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
5159 main (int argc, char **argv)
5161 cpp_reader *r;
5163 progname = "genmatch";
5165 if (argc < 2)
5166 return 1;
5168 bool gimple = true;
5169 char *input = argv[argc-1];
5170 for (int i = 1; i < argc - 1; ++i)
5172 if (strcmp (argv[i], "--gimple") == 0)
5173 gimple = true;
5174 else if (strcmp (argv[i], "--generic") == 0)
5175 gimple = false;
5176 else if (strcmp (argv[i], "-v") == 0)
5177 verbose = 1;
5178 else if (strcmp (argv[i], "-vv") == 0)
5179 verbose = 2;
5180 else
5182 fprintf (stderr, "Usage: genmatch "
5183 "[--gimple] [--generic] [-v[v]] input\n");
5184 return 1;
5188 line_table = XCNEW (class line_maps);
5189 linemap_init (line_table, 0);
5190 line_table->reallocator = xrealloc;
5191 line_table->round_alloc_size = round_alloc_size;
5193 r = cpp_create_reader (CLK_GNUC99, NULL, line_table);
5194 cpp_callbacks *cb = cpp_get_callbacks (r);
5195 cb->diagnostic = diagnostic_cb;
5197 /* Add the build directory to the #include "" search path. */
5198 cpp_dir *dir = XCNEW (cpp_dir);
5199 dir->name = getpwd ();
5200 if (!dir->name)
5201 dir->name = ASTRDUP (".");
5202 cpp_set_include_chains (r, dir, NULL, false);
5204 if (!cpp_read_main_file (r, input))
5205 return 1;
5206 cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1");
5207 cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0");
5209 null_id = new id_base (id_base::NULL_ID, "null");
5211 /* Pre-seed operators. */
5212 operators = new hash_table<id_base> (1024);
5213 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
5214 add_operator (SYM, # SYM, # TYPE, NARGS);
5215 #define END_OF_BASE_TREE_CODES
5216 #include "tree.def"
5217 #undef END_OF_BASE_TREE_CODES
5218 #undef DEFTREECODE
5220 /* Pre-seed builtin functions.
5221 ??? Cannot use N (name) as that is targetm.emultls.get_address
5222 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
5223 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
5224 add_function (ENUM, "CFN_" # ENUM);
5225 #include "builtins.def"
5227 #define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \
5228 add_function (IFN_##CODE, "CFN_" #CODE);
5229 #include "internal-fn.def"
5231 /* Parse ahead! */
5232 parser p (r, gimple);
5234 if (gimple)
5235 write_header (stdout, "gimple-match-head.cc");
5236 else
5237 write_header (stdout, "generic-match-head.cc");
5239 /* Go over all predicates defined with patterns and perform
5240 lowering and code generation. */
5241 for (unsigned i = 0; i < p.user_predicates.length (); ++i)
5243 predicate_id *pred = p.user_predicates[i];
5244 lower (pred->matchers, gimple);
5246 if (verbose == 2)
5247 for (unsigned j = 0; j < pred->matchers.length (); ++j)
5248 print_matches (pred->matchers[j]);
5250 decision_tree dt;
5251 for (unsigned j = 0; j < pred->matchers.length (); ++j)
5252 dt.insert (pred->matchers[j], j);
5254 if (verbose == 2)
5255 dt.print (stderr);
5257 write_predicate (stdout, pred, dt, gimple);
5260 /* Lower the main simplifiers and generate code for them. */
5261 lower (p.simplifiers, gimple);
5263 if (verbose == 2)
5264 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
5265 print_matches (p.simplifiers[i]);
5267 decision_tree dt;
5268 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
5269 dt.insert (p.simplifiers[i], i);
5271 if (verbose == 2)
5272 dt.print (stderr);
5274 dt.gen (stdout, gimple);
5276 /* Finalize. */
5277 cpp_finish (r, NULL);
5278 cpp_destroy (r);
5280 delete operators;
5282 return 0;