PR modula2/116048 ICE when encountering wrong kind of qualident
[official-gcc.git] / gcc / genmatch.cc
bloba56bd90cb2c5b8d31aa6b193d9c53a0e4260b082
1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
4 Copyright (C) 2014-2024 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 "rich-location.h"
29 #include "errors.h"
30 #include "hash-table.h"
31 #include "hash-set.h"
32 #include "is-a.h"
33 #include "ordered-hash-map.h"
36 /* Stubs for GGC referenced through instantiations triggered by hash-map. */
37 void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
38 size_t, size_t MEM_STAT_DECL)
40 return NULL;
42 void ggc_free (void *)
47 /* Global state. */
49 /* Verboseness. 0 is quiet, 1 adds some warnings, 2 is for debugging. */
50 unsigned verbose;
53 /* libccp helpers. */
55 static class line_maps *line_table;
57 /* The rich_location class within libcpp requires a way to expand
58 location_t instances, and relies on the client code
59 providing a symbol named
60 linemap_client_expand_location_to_spelling_point
61 to do this.
63 This is the implementation for genmatch. */
65 expanded_location
66 linemap_client_expand_location_to_spelling_point (const line_maps *set,
67 location_t loc,
68 enum location_aspect)
70 const struct line_map_ordinary *map;
71 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
72 return linemap_expand_location (set, map, loc);
75 static bool
76 #if GCC_VERSION >= 4001
77 __attribute__((format (printf, 5, 0)))
78 #endif
79 diagnostic_cb (cpp_reader *, enum cpp_diagnostic_level errtype,
80 enum cpp_warning_reason, rich_location *richloc,
81 const char *msg, va_list *ap)
83 const line_map_ordinary *map;
84 location_t location = richloc->get_loc ();
85 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
86 expanded_location loc = linemap_expand_location (line_table, map, location);
87 fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column,
88 (errtype == CPP_DL_WARNING) ? "warning" : "error");
89 vfprintf (stderr, msg, *ap);
90 fprintf (stderr, "\n");
91 FILE *f = fopen (loc.file, "r");
92 if (f)
94 char buf[128];
95 while (loc.line > 0)
97 if (!fgets (buf, 128, f))
98 goto notfound;
99 if (buf[strlen (buf) - 1] != '\n')
101 if (loc.line > 1)
102 loc.line++;
104 loc.line--;
106 fprintf (stderr, "%s", buf);
107 for (int i = 0; i < loc.column - 1; ++i)
108 fputc (' ', stderr);
109 fputc ('^', stderr);
110 fputc ('\n', stderr);
111 notfound:
112 fclose (f);
115 if (errtype == CPP_DL_FATAL)
116 exit (1);
117 return false;
120 static void
121 #if GCC_VERSION >= 4001
122 __attribute__((format (printf, 2, 3)))
123 #endif
124 fatal_at (const cpp_token *tk, const char *msg, ...)
126 rich_location richloc (line_table, tk->src_loc);
127 va_list ap;
128 va_start (ap, msg);
129 diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
130 va_end (ap);
133 static void
134 #if GCC_VERSION >= 4001
135 __attribute__((format (printf, 2, 3)))
136 #endif
137 fatal_at (location_t loc, const char *msg, ...)
139 rich_location richloc (line_table, loc);
140 va_list ap;
141 va_start (ap, msg);
142 diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
143 va_end (ap);
146 static void
147 #if GCC_VERSION >= 4001
148 __attribute__((format (printf, 2, 3)))
149 #endif
150 warning_at (const cpp_token *tk, const char *msg, ...)
152 rich_location richloc (line_table, tk->src_loc);
153 va_list ap;
154 va_start (ap, msg);
155 diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
156 va_end (ap);
159 static void
160 #if GCC_VERSION >= 4001
161 __attribute__((format (printf, 2, 3)))
162 #endif
163 warning_at (location_t loc, const char *msg, ...)
165 rich_location richloc (line_table, loc);
166 va_list ap;
167 va_start (ap, msg);
168 diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
169 va_end (ap);
172 /* Like fprintf, but print INDENT spaces at the beginning. */
174 static void
175 #if GCC_VERSION >= 4001
176 __attribute__((format (printf, 3, 4)))
177 #endif
178 fprintf_indent (FILE *f, unsigned int indent, const char *format, ...)
180 va_list ap;
181 for (; indent >= 8; indent -= 8)
182 fputc ('\t', f);
183 fprintf (f, "%*s", indent, "");
184 va_start (ap, format);
185 vfprintf (f, format, ap);
186 va_end (ap);
189 /* Secondary stream for fp_decl. */
190 static FILE *header_file;
192 /* Start or continue emitting a declaration in fprintf-like manner,
193 printing both to F and global header_file, if non-null. */
194 static void
195 #if GCC_VERSION >= 4001
196 __attribute__((format (printf, 2, 3)))
197 #endif
198 fp_decl (FILE *f, const char *format, ...)
200 va_list ap;
201 va_start (ap, format);
202 vfprintf (f, format, ap);
203 va_end (ap);
205 if (!header_file)
206 return;
208 va_start (ap, format);
209 vfprintf (header_file, format, ap);
210 va_end (ap);
213 /* Finish a declaration being emitted by fp_decl. */
214 static void
215 fp_decl_done (FILE *f, const char *trailer)
217 fprintf (f, "%s\n", trailer);
218 if (header_file)
219 fprintf (header_file, "%s;", trailer);
222 /* Line numbers for use by indirect line directives. */
223 static vec<int> dbg_line_numbers;
225 static void
226 write_header_declarations (bool gimple, FILE *f)
228 fprintf (f, "\nextern void\n%s_dump_logs (const char *file1, int line1_id, "
229 "const char *file2, int line2, bool simplify);\n",
230 gimple ? "gimple" : "generic");
233 static void
234 define_dump_logs (bool gimple, FILE *f)
236 if (dbg_line_numbers.is_empty ())
237 return;
239 fprintf (f , "void\n%s_dump_logs (const char *file1, int line1_id, "
240 "const char *file2, int line2, bool simplify)\n{\n",
241 gimple ? "gimple" : "generic");
243 fprintf_indent (f, 2, "static int dbg_line_numbers[%d] = {",
244 dbg_line_numbers.length ());
246 for (unsigned i = 0; i < dbg_line_numbers.length () - 1; i++)
248 if (i % 20 == 0)
249 fprintf (f, "\n\t");
251 fprintf (f, "%d, ", dbg_line_numbers[i]);
253 fprintf (f, "%d\n };\n\n", dbg_line_numbers.last ());
256 fprintf_indent (f, 2, "fprintf (dump_file, \"%%s "
257 "%%s:%%d, %%s:%%d\\n\",\n");
258 fprintf_indent (f, 10, "simplify ? \"Applying pattern\" : "
259 "\"Matching expression\", file1, "
260 "dbg_line_numbers[line1_id], file2, line2);");
262 fprintf (f, "\n}\n\n");
265 static void
266 output_line_directive (FILE *f, location_t location,
267 bool dumpfile = false, bool fnargs = false,
268 bool indirect_line_numbers = false)
270 typedef pair_hash<nofree_string_hash, int_hash<int, -1>> location_hash;
271 static hash_map<location_hash, int> loc_id_map;
272 const line_map_ordinary *map;
273 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
274 expanded_location loc = linemap_expand_location (line_table, map, location);
275 if (dumpfile)
277 /* When writing to a dumpfile only dump the filename. */
278 const char *file = strrchr (loc.file, DIR_SEPARATOR);
279 #if defined(DIR_SEPARATOR_2)
280 const char *pos2 = strrchr (loc.file, DIR_SEPARATOR_2);
281 if (pos2 && (!file || (pos2 > file)))
282 file = pos2;
283 #endif
284 if (!file)
285 file = loc.file;
286 else
287 ++file;
289 if (fnargs)
291 if (indirect_line_numbers)
293 bool existed;
294 int &loc_id = loc_id_map.get_or_insert (
295 std::make_pair (file, loc.line), &existed);
296 if (!existed)
298 loc_id = dbg_line_numbers.length ();
299 dbg_line_numbers.safe_push (loc.line);
302 fprintf (f, "\"%s\", %d", file, loc_id);
304 else
305 fprintf (f, "\"%s\", %d", file, loc.line);
307 else
308 fprintf (f, "%s:%d", file, loc.line);
310 else if (verbose >= 2)
311 /* Other gen programs really output line directives here, at least for
312 development it's right now more convenient to have line information
313 from the generated file. Still keep the directives as comment for now
314 to easily back-point to the meta-description. */
315 fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file);
318 /* Find the file to write into next. We try to evenly distribute the contents
319 over the different files. */
321 #define SIZED_BASED_CHUNKS 1
323 static FILE *
324 choose_output (const vec<FILE *> &parts)
326 #ifdef SIZED_BASED_CHUNKS
327 FILE *shortest = NULL;
328 long min = 0;
329 for (FILE *part : parts)
331 long len = ftell (part);
332 if (!shortest || min > len)
333 shortest = part, min = len;
335 return shortest;
336 #else
337 static int current_file;
338 return parts[current_file++ % parts.length ()];
339 #endif
343 /* Pull in tree codes and builtin function codes from their
344 definition files. */
346 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
347 enum tree_code {
348 #include "tree.def"
349 MAX_TREE_CODES
351 #undef DEFTREECODE
353 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
354 enum built_in_function {
355 #include "builtins.def"
356 END_BUILTINS
359 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE,
360 enum internal_fn {
361 #include "internal-fn.def"
362 IFN_LAST
365 enum combined_fn {
366 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
367 CFN_##ENUM = int (ENUM),
368 #include "builtins.def"
370 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
371 CFN_##CODE = int (END_BUILTINS) + int (IFN_##CODE),
372 #include "internal-fn.def"
374 CFN_LAST
377 #include "case-cfn-macros.h"
379 /* Return true if CODE represents a commutative tree code. Otherwise
380 return false. */
381 bool
382 commutative_tree_code (enum tree_code code)
384 switch (code)
386 case PLUS_EXPR:
387 case MULT_EXPR:
388 case MULT_HIGHPART_EXPR:
389 case MIN_EXPR:
390 case MAX_EXPR:
391 case BIT_IOR_EXPR:
392 case BIT_XOR_EXPR:
393 case BIT_AND_EXPR:
394 case NE_EXPR:
395 case EQ_EXPR:
396 case UNORDERED_EXPR:
397 case ORDERED_EXPR:
398 case UNEQ_EXPR:
399 case LTGT_EXPR:
400 case TRUTH_AND_EXPR:
401 case TRUTH_XOR_EXPR:
402 case TRUTH_OR_EXPR:
403 case WIDEN_MULT_EXPR:
404 case VEC_WIDEN_MULT_HI_EXPR:
405 case VEC_WIDEN_MULT_LO_EXPR:
406 case VEC_WIDEN_MULT_EVEN_EXPR:
407 case VEC_WIDEN_MULT_ODD_EXPR:
408 return true;
410 default:
411 break;
413 return false;
416 /* Return true if CODE represents a ternary tree code for which the
417 first two operands are commutative. Otherwise return false. */
418 bool
419 commutative_ternary_tree_code (enum tree_code code)
421 switch (code)
423 case WIDEN_MULT_PLUS_EXPR:
424 case WIDEN_MULT_MINUS_EXPR:
425 case DOT_PROD_EXPR:
426 return true;
428 default:
429 break;
431 return false;
434 /* Return true if CODE is a comparison. */
436 bool
437 comparison_code_p (enum tree_code code)
439 switch (code)
441 case EQ_EXPR:
442 case NE_EXPR:
443 case ORDERED_EXPR:
444 case UNORDERED_EXPR:
445 case LTGT_EXPR:
446 case UNEQ_EXPR:
447 case GT_EXPR:
448 case GE_EXPR:
449 case LT_EXPR:
450 case LE_EXPR:
451 case UNGT_EXPR:
452 case UNGE_EXPR:
453 case UNLT_EXPR:
454 case UNLE_EXPR:
455 return true;
457 default:
458 break;
460 return false;
464 /* Base class for all identifiers the parser knows. */
466 class id_base : public nofree_ptr_hash<id_base>
468 public:
469 enum id_kind { CODE, FN, PREDICATE, USER, NULL_ID } kind;
471 id_base (id_kind, const char *, int = -1);
473 hashval_t hashval;
474 int nargs;
475 const char *id;
477 /* hash_table support. */
478 static inline hashval_t hash (const id_base *);
479 static inline int equal (const id_base *, const id_base *);
482 inline hashval_t
483 id_base::hash (const id_base *op)
485 return op->hashval;
488 inline int
489 id_base::equal (const id_base *op1,
490 const id_base *op2)
492 return (op1->hashval == op2->hashval
493 && strcmp (op1->id, op2->id) == 0);
496 /* The special id "null", which matches nothing. */
497 static id_base *null_id;
499 /* Hashtable of known pattern operators. This is pre-seeded from
500 all known tree codes and all known builtin function ids. */
501 static hash_table<id_base> *operators;
503 id_base::id_base (id_kind kind_, const char *id_, int nargs_)
505 kind = kind_;
506 id = id_;
507 nargs = nargs_;
508 hashval = htab_hash_string (id);
511 /* Identifier that maps to a tree code. */
513 class operator_id : public id_base
515 public:
516 operator_id (enum tree_code code_, const char *id_, unsigned nargs_,
517 const char *tcc_)
518 : id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {}
519 enum tree_code code;
520 const char *tcc;
523 /* Identifier that maps to a builtin or internal function code. */
525 class fn_id : public id_base
527 public:
528 fn_id (enum built_in_function fn_, const char *id_)
529 : id_base (id_base::FN, id_), fn (fn_) {}
530 fn_id (enum internal_fn fn_, const char *id_)
531 : id_base (id_base::FN, id_), fn (int (END_BUILTINS) + int (fn_)) {}
532 unsigned int fn;
535 class simplify;
537 /* Identifier that maps to a user-defined predicate. */
539 class predicate_id : public id_base
541 public:
542 predicate_id (const char *id_)
543 : id_base (id_base::PREDICATE, id_), matchers (vNULL) {}
544 vec<simplify *> matchers;
547 /* Identifier that maps to a operator defined by a 'for' directive. */
549 class user_id : public id_base
551 public:
552 user_id (const char *id_, bool is_oper_list_ = false)
553 : id_base (id_base::USER, id_), substitutes (vNULL),
554 used (false), is_oper_list (is_oper_list_) {}
555 vec<id_base *> substitutes;
556 bool used;
557 bool is_oper_list;
560 template<>
561 template<>
562 inline bool
563 is_a_helper <fn_id *>::test (id_base *id)
565 return id->kind == id_base::FN;
568 template<>
569 template<>
570 inline bool
571 is_a_helper <operator_id *>::test (id_base *id)
573 return id->kind == id_base::CODE;
576 template<>
577 template<>
578 inline bool
579 is_a_helper <predicate_id *>::test (id_base *id)
581 return id->kind == id_base::PREDICATE;
584 template<>
585 template<>
586 inline bool
587 is_a_helper <user_id *>::test (id_base *id)
589 return id->kind == id_base::USER;
592 /* If ID has a pair of consecutive, commutative operands, return the
593 index of the first, otherwise return -1. */
595 static int
596 commutative_op (id_base *id)
598 if (operator_id *code = dyn_cast <operator_id *> (id))
600 if (commutative_tree_code (code->code)
601 || commutative_ternary_tree_code (code->code))
602 return 0;
603 return -1;
605 if (fn_id *fn = dyn_cast <fn_id *> (id))
606 switch (fn->fn)
608 CASE_CFN_FMA:
609 case CFN_FMS:
610 case CFN_FNMA:
611 case CFN_FNMS:
612 return 0;
614 case CFN_COND_ADD:
615 case CFN_COND_MUL:
616 case CFN_COND_MIN:
617 case CFN_COND_MAX:
618 case CFN_COND_FMIN:
619 case CFN_COND_FMAX:
620 case CFN_COND_AND:
621 case CFN_COND_IOR:
622 case CFN_COND_XOR:
623 case CFN_COND_FMA:
624 case CFN_COND_FMS:
625 case CFN_COND_FNMA:
626 case CFN_COND_FNMS:
627 case CFN_COND_LEN_ADD:
628 case CFN_COND_LEN_MUL:
629 case CFN_COND_LEN_MIN:
630 case CFN_COND_LEN_MAX:
631 case CFN_COND_LEN_FMIN:
632 case CFN_COND_LEN_FMAX:
633 case CFN_COND_LEN_AND:
634 case CFN_COND_LEN_IOR:
635 case CFN_COND_LEN_XOR:
636 case CFN_COND_LEN_FMA:
637 case CFN_COND_LEN_FMS:
638 case CFN_COND_LEN_FNMA:
639 case CFN_COND_LEN_FNMS:
640 return 1;
642 default:
643 return -1;
645 if (user_id *uid = dyn_cast<user_id *> (id))
647 int res = commutative_op (uid->substitutes[0]);
648 if (res < 0)
649 return -1;
650 for (unsigned i = 1; i < uid->substitutes.length (); ++i)
651 if (res != commutative_op (uid->substitutes[i]))
652 return -1;
653 return res;
655 return -1;
658 /* Add a predicate identifier to the hash. */
660 static predicate_id *
661 add_predicate (const char *id)
663 predicate_id *p = new predicate_id (id);
664 id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT);
665 if (*slot)
666 fatal ("duplicate id definition");
667 *slot = p;
668 return p;
671 /* Add a tree code identifier to the hash. */
673 static void
674 add_operator (enum tree_code code, const char *id,
675 const char *tcc, unsigned nargs)
677 if (strcmp (tcc, "tcc_unary") != 0
678 && strcmp (tcc, "tcc_binary") != 0
679 && strcmp (tcc, "tcc_comparison") != 0
680 && strcmp (tcc, "tcc_expression") != 0
681 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
682 && strcmp (tcc, "tcc_reference") != 0
683 /* To have INTEGER_CST and friends as "predicate operators". */
684 && strcmp (tcc, "tcc_constant") != 0
685 /* And allow CONSTRUCTOR for vector initializers. */
686 && !(code == CONSTRUCTOR)
687 /* Allow SSA_NAME as predicate operator. */
688 && !(code == SSA_NAME))
689 return;
690 /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
691 if (code == ADDR_EXPR)
692 nargs = 0;
693 operator_id *op = new operator_id (code, id, nargs, tcc);
694 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
695 if (*slot)
696 fatal ("duplicate id definition");
697 *slot = op;
700 /* Add a built-in or internal function identifier to the hash. ID is
701 the name of its CFN_* enumeration value. */
703 template <typename T>
704 static void
705 add_function (T code, const char *id)
707 fn_id *fn = new fn_id (code, id);
708 id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT);
709 if (*slot)
710 fatal ("duplicate id definition");
711 *slot = fn;
714 /* Helper for easy comparing ID with tree code CODE. */
716 static bool
717 operator==(id_base &id, enum tree_code code)
719 if (operator_id *oid = dyn_cast <operator_id *> (&id))
720 return oid->code == code;
721 return false;
724 /* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */
726 id_base *
727 get_operator (const char *id, bool allow_null = false)
729 if (allow_null && strcmp (id, "null") == 0)
730 return null_id;
732 id_base tem (id_base::CODE, id);
734 id_base *op = operators->find_with_hash (&tem, tem.hashval);
735 if (op)
737 /* If this is a user-defined identifier track whether it was used. */
738 if (user_id *uid = dyn_cast<user_id *> (op))
739 uid->used = true;
740 return op;
743 char *id2;
744 bool all_upper = true;
745 bool all_lower = true;
746 for (unsigned int i = 0; id[i]; ++i)
747 if (ISUPPER (id[i]))
748 all_lower = false;
749 else if (ISLOWER (id[i]))
750 all_upper = false;
751 if (all_lower)
753 /* Try in caps with _EXPR appended. */
754 id2 = ACONCAT ((id, "_EXPR", NULL));
755 for (unsigned int i = 0; id2[i]; ++i)
756 id2[i] = TOUPPER (id2[i]);
758 else if (all_upper && startswith (id, "IFN_"))
759 /* Try CFN_ instead of IFN_. */
760 id2 = ACONCAT (("CFN_", id + 4, NULL));
761 else if (all_upper && startswith (id, "BUILT_IN_"))
762 /* Try prepending CFN_. */
763 id2 = ACONCAT (("CFN_", id, NULL));
764 else
765 return NULL;
767 new (&tem) id_base (id_base::CODE, id2);
768 return operators->find_with_hash (&tem, tem.hashval);
771 /* Return the comparison operators that results if the operands are
772 swapped. This is safe for floating-point. */
774 id_base *
775 swap_tree_comparison (operator_id *p)
777 switch (p->code)
779 case EQ_EXPR:
780 case NE_EXPR:
781 case ORDERED_EXPR:
782 case UNORDERED_EXPR:
783 case LTGT_EXPR:
784 case UNEQ_EXPR:
785 return p;
786 case GT_EXPR:
787 return get_operator ("LT_EXPR");
788 case GE_EXPR:
789 return get_operator ("LE_EXPR");
790 case LT_EXPR:
791 return get_operator ("GT_EXPR");
792 case LE_EXPR:
793 return get_operator ("GE_EXPR");
794 case UNGT_EXPR:
795 return get_operator ("UNLT_EXPR");
796 case UNGE_EXPR:
797 return get_operator ("UNLE_EXPR");
798 case UNLT_EXPR:
799 return get_operator ("UNGT_EXPR");
800 case UNLE_EXPR:
801 return get_operator ("UNGE_EXPR");
802 default:
803 gcc_unreachable ();
807 typedef hash_map<nofree_string_hash, unsigned> cid_map_t;
810 /* The AST produced by parsing of the pattern definitions. */
812 class dt_operand;
813 class capture_info;
815 /* The base class for operands. */
817 class operand {
818 public:
819 enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR, OP_IF, OP_WITH };
820 operand (enum op_type type_, location_t loc_)
821 : type (type_), location (loc_) {}
822 enum op_type type;
823 location_t location;
824 virtual void gen_transform (FILE *, int, const char *, bool, int,
825 const char *, capture_info *,
826 dt_operand ** = 0,
827 int = 0)
828 { gcc_unreachable (); }
831 /* A predicate operand. Predicates are leafs in the AST. */
833 class predicate : public operand
835 public:
836 predicate (predicate_id *p_, location_t loc)
837 : operand (OP_PREDICATE, loc), p (p_) {}
838 predicate_id *p;
841 /* An operand that constitutes an expression. Expressions include
842 function calls and user-defined predicate invocations. */
844 class expr : public operand
846 public:
847 expr (id_base *operation_, location_t loc, bool is_commutative_ = false)
848 : operand (OP_EXPR, loc), operation (operation_),
849 ops (vNULL), expr_type (NULL), is_commutative (is_commutative_),
850 is_generic (false), force_single_use (false), force_leaf (false),
851 match_phi (false), opt_grp (0) {}
852 expr (expr *e)
853 : operand (OP_EXPR, e->location), operation (e->operation),
854 ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commutative),
855 is_generic (e->is_generic), force_single_use (e->force_single_use),
856 force_leaf (e->force_leaf), match_phi (e->match_phi),
857 opt_grp (e->opt_grp) {}
858 void append_op (operand *op) { ops.safe_push (op); }
859 /* The operator and its operands. */
860 id_base *operation;
861 vec<operand *> ops;
862 /* An explicitely specified type - used exclusively for conversions. */
863 const char *expr_type;
864 /* Whether the operation is to be applied commutatively. This is
865 later lowered to two separate patterns. */
866 bool is_commutative;
867 /* Whether the expression is expected to be in GENERIC form. */
868 bool is_generic;
869 /* Whether pushing any stmt to the sequence should be conditional
870 on this expression having a single-use. */
871 bool force_single_use;
872 /* Whether in the result expression this should be a leaf node
873 with any children simplified down to simple operands. */
874 bool force_leaf;
875 /* Whether the COND_EXPR is matching PHI gimple, default false. */
876 bool match_phi;
877 /* If non-zero, the group for optional handling. */
878 unsigned char opt_grp;
879 void gen_transform (FILE *f, int, const char *, bool, int,
880 const char *, capture_info *,
881 dt_operand ** = 0, int = 0) override;
884 /* An operator that is represented by native C code. This is always
885 a leaf operand in the AST. This class is also used to represent
886 the code to be generated for 'if' and 'with' expressions. */
888 class c_expr : public operand
890 public:
891 /* A mapping of an identifier and its replacement. Used to apply
892 'for' lowering. */
893 class id_tab {
894 public:
895 const char *id;
896 const char *oper;
897 id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {}
900 c_expr (cpp_reader *r_, location_t loc,
901 vec<cpp_token> code_, unsigned nr_stmts_,
902 vec<id_tab> ids_, cid_map_t *capture_ids_)
903 : operand (OP_C_EXPR, loc), r (r_), code (code_),
904 capture_ids (capture_ids_), nr_stmts (nr_stmts_), ids (ids_) {}
905 /* cpplib tokens and state to transform this back to source. */
906 cpp_reader *r;
907 vec<cpp_token> code;
908 cid_map_t *capture_ids;
909 /* The number of statements parsed (well, the number of ';'s). */
910 unsigned nr_stmts;
911 /* The identifier replacement vector. */
912 vec<id_tab> ids;
913 void gen_transform (FILE *f, int, const char *, bool, int,
914 const char *, capture_info *,
915 dt_operand ** = 0, int = 0) final override;
918 /* A wrapper around another operand that captures its value. */
920 class capture : public operand
922 public:
923 capture (location_t loc, unsigned where_, operand *what_, bool value_)
924 : operand (OP_CAPTURE, loc), where (where_), value_match (value_),
925 what (what_) {}
926 /* Identifier index for the value. */
927 unsigned where;
928 /* Whether in a match of two operands the compare should be for
929 equal values rather than equal atoms (boils down to a type
930 check or not). */
931 bool value_match;
932 /* The captured value. */
933 operand *what;
934 void gen_transform (FILE *f, int, const char *, bool, int,
935 const char *, capture_info *,
936 dt_operand ** = 0, int = 0) final override;
939 /* if expression. */
941 class if_expr : public operand
943 public:
944 if_expr (location_t loc)
945 : operand (OP_IF, loc), cond (NULL), trueexpr (NULL), falseexpr (NULL) {}
946 c_expr *cond;
947 operand *trueexpr;
948 operand *falseexpr;
951 /* with expression. */
953 class with_expr : public operand
955 public:
956 with_expr (location_t loc)
957 : operand (OP_WITH, loc), with (NULL), subexpr (NULL) {}
958 c_expr *with;
959 operand *subexpr;
962 template<>
963 template<>
964 inline bool
965 is_a_helper <capture *>::test (operand *op)
967 return op->type == operand::OP_CAPTURE;
970 template<>
971 template<>
972 inline bool
973 is_a_helper <predicate *>::test (operand *op)
975 return op->type == operand::OP_PREDICATE;
978 template<>
979 template<>
980 inline bool
981 is_a_helper <c_expr *>::test (operand *op)
983 return op->type == operand::OP_C_EXPR;
986 template<>
987 template<>
988 inline bool
989 is_a_helper <expr *>::test (operand *op)
991 return op->type == operand::OP_EXPR;
994 template<>
995 template<>
996 inline bool
997 is_a_helper <if_expr *>::test (operand *op)
999 return op->type == operand::OP_IF;
1002 template<>
1003 template<>
1004 inline bool
1005 is_a_helper <with_expr *>::test (operand *op)
1007 return op->type == operand::OP_WITH;
1010 /* The main class of a pattern and its transform. This is used to
1011 represent both (simplify ...) and (match ...) kinds. The AST
1012 duplicates all outer 'if' and 'for' expressions here so each
1013 simplify can exist in isolation. */
1015 class simplify
1017 public:
1018 enum simplify_kind { SIMPLIFY, MATCH };
1020 simplify (simplify_kind kind_, unsigned id_, operand *match_,
1021 operand *result_, vec<vec<user_id *> > for_vec_,
1022 cid_map_t *capture_ids_)
1023 : kind (kind_), id (id_), match (match_), result (result_),
1024 for_vec (for_vec_), for_subst_vec (vNULL),
1025 capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
1027 simplify_kind kind;
1028 /* ID. This is kept to easily associate related simplifies expanded
1029 from the same original one. */
1030 unsigned id;
1031 /* The expression that is matched against the GENERIC or GIMPLE IL. */
1032 operand *match;
1033 /* For a (simplify ...) an expression with ifs and withs with the expression
1034 produced when the pattern applies in the leafs.
1035 For a (match ...) the leafs are either empty if it is a simple predicate
1036 or the single expression specifying the matched operands. */
1037 class operand *result;
1038 /* Collected 'for' expression operators that have to be replaced
1039 in the lowering phase. */
1040 vec<vec<user_id *> > for_vec;
1041 vec<std::pair<user_id *, id_base *> > for_subst_vec;
1042 /* A map of capture identifiers to indexes. */
1043 cid_map_t *capture_ids;
1044 int capture_max;
1047 /* Debugging routines for dumping the AST. */
1049 DEBUG_FUNCTION void
1050 print_operand (operand *o, FILE *f = stderr, bool flattened = false)
1052 if (capture *c = dyn_cast<capture *> (o))
1054 if (c->what && flattened == false)
1055 print_operand (c->what, f, flattened);
1056 fprintf (f, "@%u", c->where);
1059 else if (predicate *p = dyn_cast<predicate *> (o))
1060 fprintf (f, "%s", p->p->id);
1062 else if (is_a<c_expr *> (o))
1063 fprintf (f, "c_expr");
1065 else if (expr *e = dyn_cast<expr *> (o))
1067 if (e->ops.length () == 0)
1068 fprintf (f, "%s", e->operation->id);
1069 else
1071 fprintf (f, "(%s", e->operation->id);
1073 if (flattened == false)
1075 for (unsigned i = 0; i < e->ops.length (); ++i)
1077 putc (' ', f);
1078 print_operand (e->ops[i], f, flattened);
1081 putc (')', f);
1085 else
1086 gcc_unreachable ();
1089 DEBUG_FUNCTION void
1090 print_matches (class simplify *s, FILE *f = stderr)
1092 fprintf (f, "for expression: ");
1093 print_operand (s->match, f);
1094 putc ('\n', f);
1098 /* AST lowering. */
1100 /* Lowering of commutative operators. */
1102 static void
1103 cartesian_product (const vec< vec<operand *> >& ops_vector,
1104 vec< vec<operand *> >& result, vec<operand *>& v, unsigned n)
1106 if (n == ops_vector.length ())
1108 vec<operand *> xv = v.copy ();
1109 result.safe_push (xv);
1110 return;
1113 for (unsigned i = 0; i < ops_vector[n].length (); ++i)
1115 v[n] = ops_vector[n][i];
1116 cartesian_product (ops_vector, result, v, n + 1);
1120 /* Lower OP to two operands in case it is marked as commutative. */
1122 static vec<operand *>
1123 commutate (operand *op, vec<vec<user_id *> > &for_vec)
1125 vec<operand *> ret = vNULL;
1127 if (capture *c = dyn_cast <capture *> (op))
1129 if (!c->what)
1131 ret.safe_push (op);
1132 return ret;
1134 vec<operand *> v = commutate (c->what, for_vec);
1135 for (unsigned i = 0; i < v.length (); ++i)
1137 capture *nc = new capture (c->location, c->where, v[i],
1138 c->value_match);
1139 ret.safe_push (nc);
1141 return ret;
1144 expr *e = dyn_cast <expr *> (op);
1145 if (!e || e->ops.length () == 0)
1147 ret.safe_push (op);
1148 return ret;
1151 vec< vec<operand *> > ops_vector = vNULL;
1152 for (unsigned i = 0; i < e->ops.length (); ++i)
1153 ops_vector.safe_push (commutate (e->ops[i], for_vec));
1155 auto_vec< vec<operand *> > result;
1156 auto_vec<operand *> v (e->ops.length ());
1157 v.quick_grow_cleared (e->ops.length ());
1158 cartesian_product (ops_vector, result, v, 0);
1161 for (unsigned i = 0; i < result.length (); ++i)
1163 expr *ne = new expr (e);
1164 ne->is_commutative = false;
1165 for (unsigned j = 0; j < result[i].length (); ++j)
1166 ne->append_op (result[i][j]);
1167 ret.safe_push (ne);
1170 if (!e->is_commutative)
1171 return ret;
1173 /* The operation is always binary if it isn't inherently commutative. */
1174 int natural_opno = commutative_op (e->operation);
1175 unsigned int opno = natural_opno >= 0 ? natural_opno : 0;
1176 for (unsigned i = 0; i < result.length (); ++i)
1178 expr *ne = new expr (e);
1179 if (operator_id *r = dyn_cast <operator_id *> (ne->operation))
1181 if (comparison_code_p (r->code))
1182 ne->operation = swap_tree_comparison (r);
1184 else if (user_id *p = dyn_cast <user_id *> (ne->operation))
1186 bool found_compare = false;
1187 for (unsigned j = 0; j < p->substitutes.length (); ++j)
1188 if (operator_id *q = dyn_cast <operator_id *> (p->substitutes[j]))
1190 if (comparison_code_p (q->code)
1191 && swap_tree_comparison (q) != q)
1193 found_compare = true;
1194 break;
1197 if (found_compare)
1199 user_id *newop = new user_id ("<internal>");
1200 for (unsigned j = 0; j < p->substitutes.length (); ++j)
1202 id_base *subst = p->substitutes[j];
1203 if (operator_id *q = dyn_cast <operator_id *> (subst))
1205 if (comparison_code_p (q->code))
1206 subst = swap_tree_comparison (q);
1208 newop->substitutes.safe_push (subst);
1210 ne->operation = newop;
1211 /* Search for 'p' inside the for vector and push 'newop'
1212 to the same level. */
1213 for (unsigned j = 0; newop && j < for_vec.length (); ++j)
1214 for (unsigned k = 0; k < for_vec[j].length (); ++k)
1215 if (for_vec[j][k] == p)
1217 for_vec[j].safe_push (newop);
1218 newop = NULL;
1219 break;
1223 ne->is_commutative = false;
1224 for (unsigned j = 0; j < result[i].length (); ++j)
1226 int old_j = (j == opno ? opno + 1 : j == opno + 1 ? opno : j);
1227 ne->append_op (result[i][old_j]);
1229 ret.safe_push (ne);
1232 return ret;
1235 /* Lower operations marked as commutative in the AST of S and push
1236 the resulting patterns to SIMPLIFIERS. */
1238 static void
1239 lower_commutative (simplify *s, vec<simplify *>& simplifiers)
1241 vec<operand *> matchers = commutate (s->match, s->for_vec);
1242 for (unsigned i = 0; i < matchers.length (); ++i)
1244 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
1245 s->for_vec, s->capture_ids);
1246 simplifiers.safe_push (ns);
1250 /* Strip conditional operations using group GRP from O and its
1251 children if STRIP, else replace them with an unconditional operation. */
1253 operand *
1254 lower_opt (operand *o, unsigned char grp, bool strip)
1256 if (capture *c = dyn_cast<capture *> (o))
1258 if (c->what)
1259 return new capture (c->location, c->where,
1260 lower_opt (c->what, grp, strip),
1261 c->value_match);
1262 else
1263 return c;
1266 expr *e = dyn_cast<expr *> (o);
1267 if (!e)
1268 return o;
1270 if (e->opt_grp == grp)
1272 if (strip)
1273 return lower_opt (e->ops[0], grp, strip);
1275 expr *ne = new expr (e);
1276 ne->opt_grp = 0;
1277 ne->append_op (lower_opt (e->ops[0], grp, strip));
1278 return ne;
1281 expr *ne = new expr (e);
1282 for (unsigned i = 0; i < e->ops.length (); ++i)
1283 ne->append_op (lower_opt (e->ops[i], grp, strip));
1285 return ne;
1288 /* Determine whether O or its children uses the conditional operation
1289 group GRP. */
1291 static bool
1292 has_opt (operand *o, unsigned char grp)
1294 if (capture *c = dyn_cast<capture *> (o))
1296 if (c->what)
1297 return has_opt (c->what, grp);
1298 else
1299 return false;
1302 expr *e = dyn_cast<expr *> (o);
1303 if (!e)
1304 return false;
1306 if (e->opt_grp == grp)
1307 return true;
1309 for (unsigned i = 0; i < e->ops.length (); ++i)
1310 if (has_opt (e->ops[i], grp))
1311 return true;
1313 return false;
1316 /* Lower conditional convert operators in O, expanding it to a vector
1317 if required. */
1319 static vec<operand *>
1320 lower_opt (operand *o)
1322 vec<operand *> v1 = vNULL, v2;
1324 v1.safe_push (o);
1326 /* Conditional operations are lowered to a pattern with the
1327 operation and one without. All different conditional operation
1328 groups are lowered separately. */
1330 for (unsigned i = 1; i <= 10; ++i)
1332 v2 = vNULL;
1333 for (unsigned j = 0; j < v1.length (); ++j)
1334 if (has_opt (v1[j], i))
1336 v2.safe_push (lower_opt (v1[j], i, false));
1337 v2.safe_push (lower_opt (v1[j], i, true));
1340 if (v2 != vNULL)
1342 v1 = vNULL;
1343 for (unsigned j = 0; j < v2.length (); ++j)
1344 v1.safe_push (v2[j]);
1348 return v1;
1351 /* Lower conditional convert operators in the AST of S and push
1352 the resulting multiple patterns to SIMPLIFIERS. */
1354 static void
1355 lower_opt (simplify *s, vec<simplify *>& simplifiers)
1357 vec<operand *> matchers = lower_opt (s->match);
1358 for (unsigned i = 0; i < matchers.length (); ++i)
1360 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
1361 s->for_vec, s->capture_ids);
1362 simplifiers.safe_push (ns);
1366 /* Lower the compare operand of COND_EXPRs to a
1367 GENERIC and a GIMPLE variant. */
1369 static vec<operand *>
1370 lower_cond (operand *o)
1372 vec<operand *> ro = vNULL;
1374 if (capture *c = dyn_cast<capture *> (o))
1376 if (c->what)
1378 vec<operand *> lop = vNULL;
1379 lop = lower_cond (c->what);
1381 for (unsigned i = 0; i < lop.length (); ++i)
1382 ro.safe_push (new capture (c->location, c->where, lop[i],
1383 c->value_match));
1384 return ro;
1388 expr *e = dyn_cast<expr *> (o);
1389 if (!e || e->ops.length () == 0)
1391 ro.safe_push (o);
1392 return ro;
1395 vec< vec<operand *> > ops_vector = vNULL;
1396 for (unsigned i = 0; i < e->ops.length (); ++i)
1397 ops_vector.safe_push (lower_cond (e->ops[i]));
1399 auto_vec< vec<operand *> > result;
1400 auto_vec<operand *> v (e->ops.length ());
1401 v.quick_grow_cleared (e->ops.length ());
1402 cartesian_product (ops_vector, result, v, 0);
1404 for (unsigned i = 0; i < result.length (); ++i)
1406 expr *ne = new expr (e);
1407 for (unsigned j = 0; j < result[i].length (); ++j)
1408 ne->append_op (result[i][j]);
1409 ro.safe_push (ne);
1410 /* If this is a COND with a captured expression or an
1411 expression with two operands then also match a GENERIC
1412 form on the compare. */
1413 if (*e->operation == COND_EXPR
1414 && ((is_a <capture *> (e->ops[0])
1415 && as_a <capture *> (e->ops[0])->what
1416 && is_a <expr *> (as_a <capture *> (e->ops[0])->what)
1417 && as_a <expr *>
1418 (as_a <capture *> (e->ops[0])->what)->ops.length () == 2)
1419 || (is_a <expr *> (e->ops[0])
1420 && as_a <expr *> (e->ops[0])->ops.length () == 2)))
1422 ne = new expr (e);
1423 for (unsigned j = 0; j < result[i].length (); ++j)
1424 ne->append_op (result[i][j]);
1425 if (capture *c = dyn_cast <capture *> (ne->ops[0]))
1427 expr *ocmp = as_a <expr *> (c->what);
1428 expr *cmp = new expr (ocmp);
1429 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
1430 cmp->append_op (ocmp->ops[j]);
1431 cmp->is_generic = true;
1432 ne->ops[0] = new capture (c->location, c->where, cmp,
1433 c->value_match);
1435 else
1437 expr *ocmp = as_a <expr *> (ne->ops[0]);
1438 expr *cmp = new expr (ocmp);
1439 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
1440 cmp->append_op (ocmp->ops[j]);
1441 cmp->is_generic = true;
1442 ne->ops[0] = cmp;
1444 ro.safe_push (ne);
1448 return ro;
1451 /* Lower the compare operand of COND_EXPRs to a
1452 GENERIC and a GIMPLE variant. */
1454 static void
1455 lower_cond (simplify *s, vec<simplify *>& simplifiers)
1457 vec<operand *> matchers = lower_cond (s->match);
1458 for (unsigned i = 0; i < matchers.length (); ++i)
1460 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
1461 s->for_vec, s->capture_ids);
1462 ns->for_subst_vec.safe_splice (s->for_subst_vec);
1463 simplifiers.safe_push (ns);
1467 /* Return true if O refers to ID. */
1469 bool
1470 contains_id (operand *o, user_id *id)
1472 if (capture *c = dyn_cast<capture *> (o))
1473 return c->what && contains_id (c->what, id);
1475 if (expr *e = dyn_cast<expr *> (o))
1477 if (e->operation == id)
1478 return true;
1479 for (unsigned i = 0; i < e->ops.length (); ++i)
1480 if (contains_id (e->ops[i], id))
1481 return true;
1482 return false;
1485 if (with_expr *w = dyn_cast <with_expr *> (o))
1486 return (contains_id (w->with, id)
1487 || contains_id (w->subexpr, id));
1489 if (if_expr *ife = dyn_cast <if_expr *> (o))
1490 return (contains_id (ife->cond, id)
1491 || contains_id (ife->trueexpr, id)
1492 || (ife->falseexpr && contains_id (ife->falseexpr, id)));
1494 if (c_expr *ce = dyn_cast<c_expr *> (o))
1495 return ce->capture_ids && ce->capture_ids->get (id->id);
1497 return false;
1501 /* In AST operand O replace operator ID with operator WITH. */
1503 operand *
1504 replace_id (operand *o, user_id *id, id_base *with)
1506 /* Deep-copy captures and expressions, replacing operations as
1507 needed. */
1508 if (capture *c = dyn_cast<capture *> (o))
1510 if (!c->what)
1511 return c;
1512 return new capture (c->location, c->where,
1513 replace_id (c->what, id, with), c->value_match);
1515 else if (expr *e = dyn_cast<expr *> (o))
1517 expr *ne = new expr (e);
1518 if (e->operation == id)
1519 ne->operation = with;
1520 for (unsigned i = 0; i < e->ops.length (); ++i)
1521 ne->append_op (replace_id (e->ops[i], id, with));
1522 return ne;
1524 else if (with_expr *w = dyn_cast <with_expr *> (o))
1526 with_expr *nw = new with_expr (w->location);
1527 nw->with = as_a <c_expr *> (replace_id (w->with, id, with));
1528 nw->subexpr = replace_id (w->subexpr, id, with);
1529 return nw;
1531 else if (if_expr *ife = dyn_cast <if_expr *> (o))
1533 if_expr *nife = new if_expr (ife->location);
1534 nife->cond = as_a <c_expr *> (replace_id (ife->cond, id, with));
1535 nife->trueexpr = replace_id (ife->trueexpr, id, with);
1536 if (ife->falseexpr)
1537 nife->falseexpr = replace_id (ife->falseexpr, id, with);
1538 return nife;
1541 /* For c_expr we simply record a string replacement table which is
1542 applied at code-generation time. */
1543 if (c_expr *ce = dyn_cast<c_expr *> (o))
1545 vec<c_expr::id_tab> ids = ce->ids.copy ();
1546 ids.safe_push (c_expr::id_tab (id->id, with->id));
1547 return new c_expr (ce->r, ce->location,
1548 ce->code, ce->nr_stmts, ids, ce->capture_ids);
1551 return o;
1554 /* Return true if the binary operator OP is ok for delayed substitution
1555 during for lowering. */
1557 static bool
1558 binary_ok (operator_id *op)
1560 switch (op->code)
1562 case PLUS_EXPR:
1563 case MINUS_EXPR:
1564 case MULT_EXPR:
1565 case TRUNC_DIV_EXPR:
1566 case CEIL_DIV_EXPR:
1567 case FLOOR_DIV_EXPR:
1568 case ROUND_DIV_EXPR:
1569 case TRUNC_MOD_EXPR:
1570 case CEIL_MOD_EXPR:
1571 case FLOOR_MOD_EXPR:
1572 case ROUND_MOD_EXPR:
1573 case RDIV_EXPR:
1574 case EXACT_DIV_EXPR:
1575 case MIN_EXPR:
1576 case MAX_EXPR:
1577 case BIT_IOR_EXPR:
1578 case BIT_XOR_EXPR:
1579 case BIT_AND_EXPR:
1580 return true;
1581 default:
1582 return false;
1586 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1588 static void
1589 lower_for (simplify *sin, vec<simplify *>& simplifiers)
1591 vec<vec<user_id *> >& for_vec = sin->for_vec;
1592 unsigned worklist_start = 0;
1593 auto_vec<simplify *> worklist;
1594 worklist.safe_push (sin);
1596 /* Lower each recorded for separately, operating on the
1597 set of simplifiers created by the previous one.
1598 Lower inner-to-outer so inner for substitutes can refer
1599 to operators replaced by outer fors. */
1600 for (int fi = for_vec.length () - 1; fi >= 0; --fi)
1602 vec<user_id *>& ids = for_vec[fi];
1603 unsigned n_ids = ids.length ();
1604 unsigned max_n_opers = 0;
1605 bool can_delay_subst = true;
1606 for (unsigned i = 0; i < n_ids; ++i)
1608 if (ids[i]->substitutes.length () > max_n_opers)
1609 max_n_opers = ids[i]->substitutes.length ();
1610 /* Require that all substitutes are of the same kind so that
1611 if we delay substitution to the result op code generation
1612 can look at the first substitute for deciding things like
1613 types of operands. */
1614 enum id_base::id_kind kind = ids[i]->substitutes[0]->kind;
1615 for (unsigned j = 0; j < ids[i]->substitutes.length (); ++j)
1616 if (ids[i]->substitutes[j]->kind != kind)
1617 can_delay_subst = false;
1618 else if (operator_id *op
1619 = dyn_cast <operator_id *> (ids[i]->substitutes[j]))
1621 operator_id *op0
1622 = as_a <operator_id *> (ids[i]->substitutes[0]);
1623 if (strcmp (op->tcc, "tcc_comparison") == 0
1624 && strcmp (op0->tcc, "tcc_comparison") == 0)
1626 /* Unfortunately we can't just allow all tcc_binary. */
1627 else if (strcmp (op->tcc, "tcc_binary") == 0
1628 && strcmp (op0->tcc, "tcc_binary") == 0
1629 && binary_ok (op)
1630 && binary_ok (op0))
1632 else if ((strcmp (op->id + 1, "SHIFT_EXPR") == 0
1633 || strcmp (op->id + 1, "ROTATE_EXPR") == 0)
1634 && (strcmp (op0->id + 1, "SHIFT_EXPR") == 0
1635 || strcmp (op0->id + 1, "ROTATE_EXPR") == 0))
1637 else
1638 can_delay_subst = false;
1640 else if (is_a <fn_id *> (ids[i]->substitutes[j]))
1642 else
1643 can_delay_subst = false;
1645 if (sin->kind == simplify::MATCH
1646 && can_delay_subst)
1647 continue;
1649 unsigned worklist_end = worklist.length ();
1650 for (unsigned si = worklist_start; si < worklist_end; ++si)
1652 simplify *s = worklist[si];
1653 for (unsigned j = 0; j < max_n_opers; ++j)
1655 operand *match_op = s->match;
1656 operand *result_op = s->result;
1657 auto_vec<std::pair<user_id *, id_base *> > subst (n_ids);
1658 bool skip = false;
1659 for (unsigned i = 0; i < n_ids; ++i)
1661 user_id *id = ids[i];
1662 id_base *oper = id->substitutes[j % id->substitutes.length ()];
1663 if (oper == null_id
1664 && (contains_id (match_op, id)
1665 || contains_id (result_op, id)))
1667 skip = true;
1668 break;
1670 subst.quick_push (std::make_pair (id, oper));
1671 if (sin->kind == simplify::SIMPLIFY
1672 || !can_delay_subst)
1673 match_op = replace_id (match_op, id, oper);
1674 if (result_op
1675 && !can_delay_subst)
1676 result_op = replace_id (result_op, id, oper);
1678 if (skip)
1679 continue;
1681 simplify *ns = new simplify (s->kind, s->id, match_op, result_op,
1682 vNULL, s->capture_ids);
1683 ns->for_subst_vec.safe_splice (s->for_subst_vec);
1684 if (result_op
1685 && can_delay_subst)
1686 ns->for_subst_vec.safe_splice (subst);
1688 worklist.safe_push (ns);
1691 worklist_start = worklist_end;
1694 /* Copy out the result from the last for lowering. */
1695 for (unsigned i = worklist_start; i < worklist.length (); ++i)
1696 simplifiers.safe_push (worklist[i]);
1699 /* Lower the AST for everything in SIMPLIFIERS. */
1701 static void
1702 lower (vec<simplify *>& simplifiers, bool gimple)
1704 auto_vec<simplify *> out_simplifiers;
1705 for (auto s: simplifiers)
1706 lower_opt (s, out_simplifiers);
1708 simplifiers.truncate (0);
1709 for (auto s: out_simplifiers)
1710 lower_commutative (s, simplifiers);
1712 /* Lower for needs to happen before lowering cond
1713 to support (for cnd (cond vec_cond)). This is
1714 safe as substitution delay does not happen for
1715 cond or vec_cond. */
1716 out_simplifiers.truncate (0);
1717 for (auto s: simplifiers)
1718 lower_for (s, out_simplifiers);
1720 simplifiers.truncate (0);
1721 if (gimple)
1722 for (auto s: out_simplifiers)
1723 lower_cond (s, simplifiers);
1724 else
1725 simplifiers.safe_splice (out_simplifiers);
1731 /* The decision tree built for generating GIMPLE and GENERIC pattern
1732 matching code. It represents the 'match' expression of all
1733 simplifies and has those as its leafs. */
1735 class dt_simplify;
1737 /* A hash-map collecting semantically equivalent leafs in the decision
1738 tree for splitting out to separate functions. */
1739 struct sinfo
1741 dt_simplify *s;
1743 const char *fname;
1744 unsigned cnt;
1747 struct sinfo_hashmap_traits : simple_hashmap_traits<pointer_hash<dt_simplify>,
1748 sinfo *>
1750 static inline hashval_t hash (const key_type &);
1751 static inline bool equal_keys (const key_type &, const key_type &);
1752 template <typename T> static inline void remove (T &) {}
1755 typedef ordered_hash_map<void * /* unused */, sinfo *, sinfo_hashmap_traits>
1756 sinfo_map_t;
1758 /* Current simplifier ID we are processing during insertion into the
1759 decision tree. */
1760 static unsigned current_id;
1762 /* Decision tree base class, used for DT_NODE. */
1764 class dt_node
1766 public:
1767 enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY };
1769 enum dt_type type;
1770 unsigned level;
1771 dt_node *parent;
1772 vec<dt_node *> kids;
1774 /* Statistics. */
1775 unsigned num_leafs;
1776 unsigned total_size;
1777 unsigned max_level;
1779 dt_node (enum dt_type type_, dt_node *parent_)
1780 : type (type_), level (0), parent (parent_), kids (vNULL) {}
1782 dt_node *append_node (dt_node *);
1783 dt_node *append_op (operand *, dt_node *parent, unsigned pos);
1784 dt_node *append_true_op (operand *, dt_node *parent, unsigned pos);
1785 dt_node *append_match_op (operand *, dt_operand *, dt_node *parent,
1786 unsigned pos);
1787 dt_node *append_simplify (simplify *, unsigned, dt_operand **);
1789 virtual void gen (FILE *, int, bool, int) {}
1791 void gen_kids (FILE *, int, bool, int);
1792 void gen_kids_1 (FILE *, int, bool, int,
1793 const vec<dt_operand *> &, const vec<dt_operand *> &,
1794 const vec<dt_operand *> &, const vec<dt_operand *> &,
1795 const vec<dt_operand *> &, const vec<dt_node *> &);
1797 void analyze (sinfo_map_t &);
1800 /* Generic decision tree node used for DT_OPERAND, DT_MATCH and DT_TRUE. */
1802 class dt_operand : public dt_node
1804 public:
1805 operand *op;
1806 dt_operand *match_dop;
1807 unsigned pos;
1808 bool value_match;
1809 unsigned for_id;
1811 dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_,
1812 dt_operand *parent_, unsigned pos_)
1813 : dt_node (type, parent_), op (op_), match_dop (match_dop_),
1814 pos (pos_), value_match (false), for_id (current_id) {}
1816 void gen (FILE *, int, bool, int) final override;
1817 unsigned gen_predicate (FILE *, int, const char *, bool);
1818 unsigned gen_match_op (FILE *, int, const char *, bool);
1820 unsigned gen_gimple_expr (FILE *, int, int);
1821 unsigned gen_generic_expr (FILE *, int, const char *);
1823 char *get_name (char *);
1824 void gen_opname (char *, unsigned);
1825 void gen_phi_on_cond (FILE *, int, int);
1828 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1830 class dt_simplify : public dt_node
1832 public:
1833 simplify *s;
1834 unsigned pattern_no;
1835 dt_operand **indexes;
1836 sinfo *info;
1838 dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_)
1839 : dt_node (DT_SIMPLIFY, NULL), s (s_), pattern_no (pattern_no_),
1840 indexes (indexes_), info (NULL) {}
1842 void gen_1 (FILE *, int, bool, operand *);
1843 void gen (FILE *f, int, bool, int) final override;
1846 template<>
1847 template<>
1848 inline bool
1849 is_a_helper <dt_operand *>::test (dt_node *n)
1851 return (n->type == dt_node::DT_OPERAND
1852 || n->type == dt_node::DT_MATCH
1853 || n->type == dt_node::DT_TRUE);
1856 template<>
1857 template<>
1858 inline bool
1859 is_a_helper <dt_simplify *>::test (dt_node *n)
1861 return n->type == dt_node::DT_SIMPLIFY;
1866 /* A container for the actual decision tree. */
1868 class decision_tree
1870 public:
1871 dt_node *root;
1873 void insert (class simplify *, unsigned);
1874 void gen (vec <FILE *> &f, bool gimple);
1875 void print (FILE *f = stderr);
1877 decision_tree () { root = new dt_node (dt_node::DT_NODE, NULL); }
1879 static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes,
1880 unsigned pos = 0, dt_node *parent = 0);
1881 static dt_node *find_node (vec<dt_node *>&, dt_node *);
1882 static bool cmp_node (dt_node *, dt_node *);
1883 static void print_node (dt_node *, FILE *f = stderr, unsigned = 0);
1886 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1888 bool
1889 cmp_operand (operand *o1, operand *o2)
1891 if (!o1 || !o2 || o1->type != o2->type)
1892 return false;
1894 if (o1->type == operand::OP_PREDICATE)
1896 predicate *p1 = as_a<predicate *>(o1);
1897 predicate *p2 = as_a<predicate *>(o2);
1898 return p1->p == p2->p;
1900 else if (o1->type == operand::OP_EXPR)
1902 expr *e1 = static_cast<expr *>(o1);
1903 expr *e2 = static_cast<expr *>(o2);
1904 if (e1->operation != e2->operation
1905 || e1->is_generic != e2->is_generic
1906 || e1->match_phi != e2->match_phi)
1907 return false;
1908 if (e1->operation->kind == id_base::FN
1909 /* For function calls also compare number of arguments. */
1910 && e1->ops.length () != e2->ops.length ())
1911 return false;
1912 return true;
1914 else
1915 return false;
1918 /* Compare two decision tree nodes N1 and N2 and return true if they
1919 are equal. */
1921 bool
1922 decision_tree::cmp_node (dt_node *n1, dt_node *n2)
1924 if (!n1 || !n2 || n1->type != n2->type)
1925 return false;
1927 if (n1 == n2)
1928 return true;
1930 if (n1->type == dt_node::DT_TRUE)
1931 return false;
1933 if (n1->type == dt_node::DT_OPERAND)
1934 return cmp_operand ((as_a<dt_operand *> (n1))->op,
1935 (as_a<dt_operand *> (n2))->op);
1936 else if (n1->type == dt_node::DT_MATCH)
1937 return (((as_a<dt_operand *> (n1))->match_dop
1938 == (as_a<dt_operand *> (n2))->match_dop)
1939 && ((as_a<dt_operand *> (n1))->value_match
1940 == (as_a<dt_operand *> (n2))->value_match));
1941 return false;
1944 /* Search OPS for a decision tree node like P and return it if found. */
1946 dt_node *
1947 decision_tree::find_node (vec<dt_node *>& ops, dt_node *p)
1949 /* We can merge adjacent DT_TRUE. */
1950 if (p->type == dt_node::DT_TRUE
1951 && !ops.is_empty ()
1952 && ops.last ()->type == dt_node::DT_TRUE)
1953 return ops.last ();
1954 dt_operand *true_node = NULL;
1955 for (int i = ops.length () - 1; i >= 0; --i)
1957 /* But we can't merge across DT_TRUE nodes as they serve as
1958 pattern order barriers to make sure that patterns apply
1959 in order of appearance in case multiple matches are possible. */
1960 if (ops[i]->type == dt_node::DT_TRUE)
1962 if (! true_node
1963 || as_a <dt_operand *> (ops[i])->for_id > true_node->for_id)
1964 true_node = as_a <dt_operand *> (ops[i]);
1966 if (decision_tree::cmp_node (ops[i], p))
1968 /* Unless we are processing the same pattern or the blocking
1969 pattern is before the one we are going to merge with. */
1970 if (true_node
1971 && true_node->for_id != current_id
1972 && true_node->for_id > as_a <dt_operand *> (ops[i])->for_id)
1974 if (verbose >= 1)
1976 location_t p_loc = 0;
1977 if (p->type == dt_node::DT_OPERAND)
1978 p_loc = as_a <dt_operand *> (p)->op->location;
1979 location_t op_loc = 0;
1980 if (ops[i]->type == dt_node::DT_OPERAND)
1981 op_loc = as_a <dt_operand *> (ops[i])->op->location;
1982 location_t true_loc = 0;
1983 true_loc = true_node->op->location;
1984 warning_at (p_loc,
1985 "failed to merge decision tree node");
1986 warning_at (op_loc,
1987 "with the following");
1988 warning_at (true_loc,
1989 "because of the following which serves as ordering "
1990 "barrier");
1992 return NULL;
1994 return ops[i];
1997 return NULL;
2000 /* Append N to the decision tree if it there is not already an existing
2001 identical child. */
2003 dt_node *
2004 dt_node::append_node (dt_node *n)
2006 dt_node *kid;
2008 kid = decision_tree::find_node (kids, n);
2009 if (kid)
2010 return kid;
2012 kids.safe_push (n);
2013 n->level = this->level + 1;
2015 return n;
2018 /* Append OP to the decision tree. */
2020 dt_node *
2021 dt_node::append_op (operand *op, dt_node *parent, unsigned pos)
2023 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
2024 dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos);
2025 return append_node (n);
2028 /* Append a DT_TRUE decision tree node. */
2030 dt_node *
2031 dt_node::append_true_op (operand *op, dt_node *parent, unsigned pos)
2033 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
2034 dt_operand *n = new dt_operand (DT_TRUE, op, 0, parent_, pos);
2035 return append_node (n);
2038 /* Append a DT_MATCH decision tree node. */
2040 dt_node *
2041 dt_node::append_match_op (operand *op, dt_operand *match_dop,
2042 dt_node *parent, unsigned pos)
2044 dt_operand *parent_ = as_a<dt_operand *> (parent);
2045 dt_operand *n = new dt_operand (DT_MATCH, op, match_dop, parent_, pos);
2046 return append_node (n);
2049 /* Append S to the decision tree. */
2051 dt_node *
2052 dt_node::append_simplify (simplify *s, unsigned pattern_no,
2053 dt_operand **indexes)
2055 dt_simplify *s2;
2056 dt_simplify *n = new dt_simplify (s, pattern_no, indexes);
2057 for (unsigned i = 0; i < kids.length (); ++i)
2058 if ((s2 = dyn_cast <dt_simplify *> (kids[i]))
2059 && (verbose >= 1
2060 || s->match->location != s2->s->match->location))
2062 /* With a nested patters, it's hard to avoid these in order
2063 to keep match.pd rules relatively small. */
2064 warning_at (s->match->location, "duplicate pattern");
2065 warning_at (s2->s->match->location, "previous pattern defined here");
2066 print_operand (s->match, stderr);
2067 fprintf (stderr, "\n");
2069 return append_node (n);
2072 /* Analyze the node and its children. */
2074 void
2075 dt_node::analyze (sinfo_map_t &map)
2077 num_leafs = 0;
2078 total_size = 1;
2079 max_level = level;
2081 if (type == DT_SIMPLIFY)
2083 /* Populate the map of equivalent simplifies. */
2084 dt_simplify *s = as_a <dt_simplify *> (this);
2085 bool existed;
2086 sinfo *&si = map.get_or_insert (s, &existed);
2087 if (!existed)
2089 si = new sinfo;
2090 si->s = s;
2091 si->cnt = 1;
2092 si->fname = NULL;
2094 else
2095 si->cnt++;
2096 s->info = si;
2097 num_leafs = 1;
2098 return;
2101 for (unsigned i = 0; i < kids.length (); ++i)
2103 kids[i]->analyze (map);
2104 num_leafs += kids[i]->num_leafs;
2105 total_size += kids[i]->total_size;
2106 max_level = MAX (max_level, kids[i]->max_level);
2110 /* Insert O into the decision tree and return the decision tree node found
2111 or created. */
2113 dt_node *
2114 decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes,
2115 unsigned pos, dt_node *parent)
2117 dt_node *q, *elm = 0;
2119 if (capture *c = dyn_cast<capture *> (o))
2121 unsigned capt_index = c->where;
2123 if (indexes[capt_index] == 0)
2125 if (c->what)
2126 q = insert_operand (p, c->what, indexes, pos, parent);
2127 else
2129 q = elm = p->append_true_op (o, parent, pos);
2130 goto at_assert_elm;
2132 // get to the last capture
2133 for (operand *what = c->what;
2134 what && is_a<capture *> (what);
2135 c = as_a<capture *> (what), what = c->what)
2138 if (!c->what)
2140 unsigned cc_index = c->where;
2141 dt_operand *match_op = indexes[cc_index];
2143 dt_operand temp (dt_node::DT_TRUE, 0, 0, 0, 0);
2144 elm = decision_tree::find_node (p->kids, &temp);
2146 if (elm == 0)
2148 dt_operand match (dt_node::DT_MATCH, 0, match_op, 0, 0);
2149 match.value_match = c->value_match;
2150 elm = decision_tree::find_node (p->kids, &match);
2153 else
2155 dt_operand temp (dt_node::DT_OPERAND, c->what, 0, 0, 0);
2156 elm = decision_tree::find_node (p->kids, &temp);
2159 at_assert_elm:
2160 gcc_assert (elm->type == dt_node::DT_TRUE
2161 || elm->type == dt_node::DT_OPERAND
2162 || elm->type == dt_node::DT_MATCH);
2163 indexes[capt_index] = static_cast<dt_operand *> (elm);
2164 return q;
2166 else
2168 p = p->append_match_op (o, indexes[capt_index], parent, pos);
2169 as_a <dt_operand *>(p)->value_match = c->value_match;
2170 if (c->what)
2171 return insert_operand (p, c->what, indexes, 0, p);
2172 else
2173 return p;
2176 p = p->append_op (o, parent, pos);
2177 q = p;
2179 if (expr *e = dyn_cast <expr *>(o))
2181 for (unsigned i = 0; i < e->ops.length (); ++i)
2182 q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p);
2185 return q;
2188 /* Insert S into the decision tree. */
2190 void
2191 decision_tree::insert (class simplify *s, unsigned pattern_no)
2193 current_id = s->id;
2194 dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1);
2195 dt_node *p = decision_tree::insert_operand (root, s->match, indexes);
2196 p->append_simplify (s, pattern_no, indexes);
2199 /* Debug functions to dump the decision tree. */
2201 DEBUG_FUNCTION void
2202 decision_tree::print_node (dt_node *p, FILE *f, unsigned indent)
2204 if (p->type == dt_node::DT_NODE)
2205 fprintf (f, "root");
2206 else
2208 fprintf (f, "|");
2209 for (unsigned i = 0; i < indent; i++)
2210 fprintf (f, "-");
2212 if (p->type == dt_node::DT_OPERAND)
2214 dt_operand *dop = static_cast<dt_operand *>(p);
2215 print_operand (dop->op, f, true);
2217 else if (p->type == dt_node::DT_TRUE)
2218 fprintf (f, "true");
2219 else if (p->type == dt_node::DT_MATCH)
2220 fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop));
2221 else if (p->type == dt_node::DT_SIMPLIFY)
2223 dt_simplify *s = static_cast<dt_simplify *> (p);
2224 fprintf (f, "simplify_%u { ", s->pattern_no);
2225 for (int i = 0; i <= s->s->capture_max; ++i)
2226 fprintf (f, "%p, ", (void *) s->indexes[i]);
2227 fprintf (f, " } ");
2229 if (is_a <dt_operand *> (p))
2230 fprintf (f, " [%u]", as_a <dt_operand *> (p)->for_id);
2233 fprintf (stderr, " (%p, %p), %u, %u\n",
2234 (void *) p, (void *) p->parent, p->level, p->kids.length ());
2236 for (unsigned i = 0; i < p->kids.length (); ++i)
2237 decision_tree::print_node (p->kids[i], f, indent + 2);
2240 DEBUG_FUNCTION void
2241 decision_tree::print (FILE *f)
2243 return decision_tree::print_node (root, f);
2247 /* For GENERIC we have to take care of wrapping multiple-used
2248 expressions with side-effects in save_expr and preserve side-effects
2249 of expressions with omit_one_operand. Analyze captures in
2250 match, result and with expressions and perform early-outs
2251 on the outermost match expression operands for cases we cannot
2252 handle. */
2254 class capture_info
2256 public:
2257 capture_info (simplify *s, operand *, bool);
2258 void walk_match (operand *o, unsigned toplevel_arg, bool, bool);
2259 bool walk_result (operand *o, bool, operand *);
2260 void walk_c_expr (c_expr *);
2262 struct cinfo
2264 bool expr_p;
2265 bool cse_p;
2266 bool force_no_side_effects_p;
2267 bool force_single_use;
2268 bool cond_expr_cond_p;
2269 unsigned long toplevel_msk;
2270 unsigned match_use_count;
2271 unsigned result_use_count;
2272 unsigned same_as;
2273 capture *c;
2276 auto_vec<cinfo> info;
2277 unsigned long force_no_side_effects;
2278 bool gimple;
2281 /* Analyze captures in S. */
2283 capture_info::capture_info (simplify *s, operand *result, bool gimple_)
2285 gimple = gimple_;
2287 expr *e;
2288 if (s->kind == simplify::MATCH)
2290 force_no_side_effects = -1;
2291 return;
2294 force_no_side_effects = 0;
2295 info.safe_grow_cleared (s->capture_max + 1, true);
2296 for (int i = 0; i <= s->capture_max; ++i)
2297 info[i].same_as = i;
2299 e = as_a <expr *> (s->match);
2300 for (unsigned i = 0; i < e->ops.length (); ++i)
2301 walk_match (e->ops[i], i,
2302 (i != 0 && *e->operation == COND_EXPR)
2303 || *e->operation == TRUTH_ANDIF_EXPR
2304 || *e->operation == TRUTH_ORIF_EXPR,
2305 i == 0 && *e->operation == COND_EXPR);
2307 walk_result (s->result, false, result);
2310 /* Analyze captures in the match expression piece O. */
2312 void
2313 capture_info::walk_match (operand *o, unsigned toplevel_arg,
2314 bool conditional_p, bool cond_expr_cond_p)
2316 if (capture *c = dyn_cast <capture *> (o))
2318 unsigned where = c->where;
2319 info[where].match_use_count++;
2320 info[where].toplevel_msk |= 1 << toplevel_arg;
2321 info[where].force_no_side_effects_p |= conditional_p;
2322 info[where].cond_expr_cond_p |= cond_expr_cond_p;
2323 if (!info[where].c)
2324 info[where].c = c;
2325 if (!c->what)
2326 return;
2327 /* Recurse to exprs and captures. */
2328 if (is_a <capture *> (c->what)
2329 || is_a <expr *> (c->what))
2330 walk_match (c->what, toplevel_arg, conditional_p, false);
2331 /* We need to look past multiple captures to find a captured
2332 expression as with conditional converts two captures
2333 can be collapsed onto the same expression. Also collect
2334 what captures capture the same thing. */
2335 while (c->what && is_a <capture *> (c->what))
2337 c = as_a <capture *> (c->what);
2338 if (info[c->where].same_as != c->where
2339 && info[c->where].same_as != info[where].same_as)
2340 fatal_at (c->location, "cannot handle this collapsed capture");
2341 info[c->where].same_as = info[where].same_as;
2343 /* Mark expr (non-leaf) captures and forced single-use exprs. */
2344 expr *e;
2345 if (c->what
2346 && (e = dyn_cast <expr *> (c->what)))
2348 /* Zero-operand expression captures like ADDR_EXPR@0 are
2349 similar as predicates -- if they are not mentioned in
2350 the result we have to force them to have no side-effects. */
2351 if (e->ops.length () != 0)
2352 info[where].expr_p = true;
2353 info[where].force_single_use |= e->force_single_use;
2356 else if (expr *e = dyn_cast <expr *> (o))
2358 for (unsigned i = 0; i < e->ops.length (); ++i)
2360 bool cond_p = conditional_p;
2361 bool expr_cond_p = false;
2362 if (i != 0 && *e->operation == COND_EXPR)
2363 cond_p = true;
2364 else if (*e->operation == TRUTH_ANDIF_EXPR
2365 || *e->operation == TRUTH_ORIF_EXPR)
2366 cond_p = true;
2367 if (i == 0
2368 && *e->operation == COND_EXPR)
2369 expr_cond_p = true;
2370 walk_match (e->ops[i], toplevel_arg, cond_p, expr_cond_p);
2373 else if (is_a <predicate *> (o))
2375 /* Mark non-captured leafs toplevel arg for checking. */
2376 force_no_side_effects |= 1 << toplevel_arg;
2377 if (verbose >= 1
2378 && !gimple)
2379 warning_at (o->location,
2380 "forcing no side-effects on possibly lost leaf");
2382 else
2383 gcc_unreachable ();
2386 /* Analyze captures in the result expression piece O. Return true
2387 if RESULT was visited in one of the children. Only visit
2388 non-if/with children if they are rooted on RESULT. */
2390 bool
2391 capture_info::walk_result (operand *o, bool conditional_p, operand *result)
2393 if (capture *c = dyn_cast <capture *> (o))
2395 unsigned where = info[c->where].same_as;
2396 info[where].result_use_count++;
2397 /* If we substitute an expression capture we don't know
2398 which captures this will end up using (well, we don't
2399 compute that). Force the uses to be side-effect free
2400 which means forcing the toplevels that reach the
2401 expression side-effect free. */
2402 if (info[where].expr_p)
2403 force_no_side_effects |= info[where].toplevel_msk;
2404 /* Mark CSE capture uses as forced to have no side-effects. */
2405 if (c->what
2406 && is_a <expr *> (c->what))
2408 info[where].cse_p = true;
2409 walk_result (c->what, true, result);
2412 else if (expr *e = dyn_cast <expr *> (o))
2414 id_base *opr = e->operation;
2415 if (user_id *uid = dyn_cast <user_id *> (opr))
2416 opr = uid->substitutes[0];
2417 for (unsigned i = 0; i < e->ops.length (); ++i)
2419 bool cond_p = conditional_p;
2420 if (i != 0 && *e->operation == COND_EXPR)
2421 cond_p = true;
2422 else if (*e->operation == TRUTH_ANDIF_EXPR
2423 || *e->operation == TRUTH_ORIF_EXPR)
2424 cond_p = true;
2425 walk_result (e->ops[i], cond_p, result);
2428 else if (if_expr *ie = dyn_cast <if_expr *> (o))
2430 /* 'if' conditions should be all fine. */
2431 if (ie->trueexpr == result)
2433 walk_result (ie->trueexpr, false, result);
2434 return true;
2436 if (ie->falseexpr == result)
2438 walk_result (ie->falseexpr, false, result);
2439 return true;
2441 bool res = false;
2442 if (is_a <if_expr *> (ie->trueexpr)
2443 || is_a <with_expr *> (ie->trueexpr))
2444 res |= walk_result (ie->trueexpr, false, result);
2445 if (ie->falseexpr
2446 && (is_a <if_expr *> (ie->falseexpr)
2447 || is_a <with_expr *> (ie->falseexpr)))
2448 res |= walk_result (ie->falseexpr, false, result);
2449 return res;
2451 else if (with_expr *we = dyn_cast <with_expr *> (o))
2453 bool res = (we->subexpr == result);
2454 if (res
2455 || is_a <if_expr *> (we->subexpr)
2456 || is_a <with_expr *> (we->subexpr))
2457 res |= walk_result (we->subexpr, false, result);
2458 if (res)
2459 walk_c_expr (we->with);
2460 return res;
2462 else if (c_expr *ce = dyn_cast <c_expr *> (o))
2463 walk_c_expr (ce);
2464 else
2465 gcc_unreachable ();
2467 return false;
2470 /* Look for captures in the C expr E. */
2472 void
2473 capture_info::walk_c_expr (c_expr *e)
2475 /* Give up for C exprs mentioning captures not inside TREE_TYPE,
2476 TREE_REAL_CST, TREE_CODE or a predicate where they cannot
2477 really escape through. */
2478 unsigned p_depth = 0;
2479 for (unsigned i = 0; i < e->code.length (); ++i)
2481 const cpp_token *t = &e->code[i];
2482 const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL;
2483 id_base *id;
2484 if (t->type == CPP_NAME
2485 && (strcmp ((const char *)CPP_HASHNODE
2486 (t->val.node.node)->ident.str, "TREE_TYPE") == 0
2487 || strcmp ((const char *)CPP_HASHNODE
2488 (t->val.node.node)->ident.str, "TREE_CODE") == 0
2489 || strcmp ((const char *)CPP_HASHNODE
2490 (t->val.node.node)->ident.str, "TREE_REAL_CST") == 0
2491 || ((id = get_operator ((const char *)CPP_HASHNODE
2492 (t->val.node.node)->ident.str))
2493 && is_a <predicate_id *> (id)))
2494 && n->type == CPP_OPEN_PAREN)
2495 p_depth++;
2496 else if (t->type == CPP_CLOSE_PAREN
2497 && p_depth > 0)
2498 p_depth--;
2499 else if (p_depth == 0
2500 && t->type == CPP_ATSIGN
2501 && (n->type == CPP_NUMBER
2502 || n->type == CPP_NAME)
2503 && !(n->flags & PREV_WHITE))
2505 const char *id1;
2506 if (n->type == CPP_NUMBER)
2507 id1 = (const char *)n->val.str.text;
2508 else
2509 id1 = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
2510 unsigned *where = e->capture_ids->get(id1);
2511 if (! where)
2512 fatal_at (n, "unknown capture id '%s'", id1);
2513 info[info[*where].same_as].force_no_side_effects_p = true;
2514 if (verbose >= 1
2515 && !gimple)
2516 warning_at (t, "capture escapes");
2522 /* The current label failing the current matched pattern during
2523 code generation. */
2524 static char *fail_label;
2526 /* Code generation off the decision tree and the refered AST nodes. */
2528 bool
2529 is_conversion (id_base *op)
2531 return (*op == CONVERT_EXPR
2532 || *op == NOP_EXPR
2533 || *op == FLOAT_EXPR
2534 || *op == FIX_TRUNC_EXPR
2535 || *op == VIEW_CONVERT_EXPR);
2538 /* Get the type to be used for generating operand POS of OP from the
2539 various sources. */
2541 static const char *
2542 get_operand_type (id_base *op, unsigned pos,
2543 const char *in_type,
2544 const char *expr_type,
2545 const char *other_oprnd_type)
2547 /* Generally operands whose type does not match the type of the
2548 expression generated need to know their types but match and
2549 thus can fall back to 'other_oprnd_type'. */
2550 if (is_conversion (op))
2551 return other_oprnd_type;
2552 else if (*op == REALPART_EXPR
2553 || *op == IMAGPART_EXPR)
2554 return other_oprnd_type;
2555 else if (is_a <operator_id *> (op)
2556 && strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0)
2557 return other_oprnd_type;
2558 else if (*op == COND_EXPR
2559 && pos == 0)
2560 return "boolean_type_node";
2561 else if (startswith (op->id, "CFN_COND_"))
2563 /* IFN_COND_* operands 1 and later by default have the same type
2564 as the result. The type of operand 0 needs to be specified
2565 explicitly. */
2566 if (pos > 0 && expr_type)
2567 return expr_type;
2568 else if (pos > 0 && in_type)
2569 return in_type;
2570 else
2571 return NULL;
2573 else
2575 /* Otherwise all types should match - choose one in order of
2576 preference. */
2577 if (expr_type)
2578 return expr_type;
2579 else if (in_type)
2580 return in_type;
2581 else
2582 return other_oprnd_type;
2586 /* Generate transform code for an expression. */
2588 void
2589 expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
2590 int depth, const char *in_type, capture_info *cinfo,
2591 dt_operand **indexes, int)
2593 id_base *opr = operation;
2594 /* When we delay operator substituting during lowering of fors we
2595 make sure that for code-gen purposes the effects of each substitute
2596 are the same. Thus just look at that. */
2597 if (user_id *uid = dyn_cast <user_id *> (opr))
2598 opr = uid->substitutes[0];
2600 bool conversion_p = is_conversion (opr);
2601 const char *type = expr_type;
2602 char optype[64];
2603 if (type)
2604 /* If there was a type specification in the pattern use it. */
2606 else if (conversion_p)
2607 /* For conversions we need to build the expression using the
2608 outer type passed in. */
2609 type = in_type;
2610 else if (*opr == REALPART_EXPR
2611 || *opr == IMAGPART_EXPR)
2613 /* __real and __imag use the component type of its operand. */
2614 snprintf (optype, sizeof (optype), "TREE_TYPE (TREE_TYPE (_o%d[0]))",
2615 depth);
2616 type = optype;
2618 else if (is_a <operator_id *> (opr)
2619 && !strcmp (as_a <operator_id *> (opr)->tcc, "tcc_comparison"))
2621 /* comparisons use boolean_type_node (or what gets in), but
2622 their operands need to figure out the types themselves. */
2623 if (in_type)
2624 type = in_type;
2625 else
2627 snprintf (optype, sizeof (optype), "boolean_type_node");
2628 type = optype;
2630 in_type = NULL;
2632 else if (*opr == COND_EXPR
2633 || *opr == VEC_COND_EXPR
2634 || startswith (opr->id, "CFN_COND_"))
2636 /* Conditions are of the same type as their first alternative. */
2637 snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[1])", depth);
2638 type = optype;
2640 else
2642 /* Other operations are of the same type as their first operand. */
2643 snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[0])", depth);
2644 type = optype;
2646 if (!type)
2647 fatal_at (location, "cannot determine type of operand");
2649 fprintf_indent (f, indent, "{\n");
2650 indent += 2;
2651 fprintf_indent (f, indent,
2652 "tree _o%d[%u], _r%d;\n", depth, ops.length (), depth);
2653 char op0type[64];
2654 snprintf (op0type, sizeof (op0type), "TREE_TYPE (_o%d[0])", depth);
2655 for (unsigned i = 0; i < ops.length (); ++i)
2657 char dest1[32];
2658 snprintf (dest1, sizeof (dest1), "_o%d[%u]", depth, i);
2659 const char *optype1
2660 = get_operand_type (opr, i, in_type, expr_type,
2661 i == 0 ? NULL : op0type);
2662 ops[i]->gen_transform (f, indent, dest1, gimple, depth + 1, optype1,
2663 cinfo, indexes,
2664 *opr == COND_EXPR && i == 0 ? 1 : 2);
2667 const char *opr_name;
2668 if (*operation == CONVERT_EXPR)
2669 opr_name = "NOP_EXPR";
2670 else
2671 opr_name = operation->id;
2673 if (gimple)
2675 if (*opr == CONVERT_EXPR)
2677 fprintf_indent (f, indent,
2678 "if (%s != TREE_TYPE (_o%d[0])\n",
2679 type, depth);
2680 fprintf_indent (f, indent,
2681 " && !useless_type_conversion_p (%s, TREE_TYPE "
2682 "(_o%d[0])))\n",
2683 type, depth);
2684 fprintf_indent (f, indent + 2, "{\n");
2685 indent += 4;
2687 /* ??? Building a stmt can fail for various reasons here, seq being
2688 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
2689 So if we fail here we should continue matching other patterns. */
2690 fprintf_indent (f, indent, "gimple_match_op tem_op "
2691 "(res_op->cond.any_else (), %s, %s", opr_name, type);
2692 for (unsigned i = 0; i < ops.length (); ++i)
2693 fprintf (f, ", _o%d[%u]", depth, i);
2694 fprintf (f, ");\n");
2695 fprintf_indent (f, indent, "tem_op.resimplify (%s, valueize);\n",
2696 !force_leaf ? "lseq" : "NULL");
2697 fprintf_indent (f, indent,
2698 "_r%d = maybe_push_res_to_seq (&tem_op, %s);\n", depth,
2699 !force_leaf ? "lseq" : "NULL");
2700 fprintf_indent (f, indent,
2701 "if (!_r%d) goto %s;\n",
2702 depth, fail_label);
2703 if (*opr == CONVERT_EXPR)
2705 indent -= 4;
2706 fprintf_indent (f, indent, " }\n");
2707 fprintf_indent (f, indent, "else\n");
2708 fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
2711 else
2713 if (*opr == CONVERT_EXPR)
2715 fprintf_indent (f, indent, "if (TREE_TYPE (_o%d[0]) != %s)\n",
2716 depth, type);
2717 fprintf_indent (f, indent + 2, "{\n");
2718 indent += 4;
2720 if (opr->kind == id_base::CODE)
2721 fprintf_indent (f, indent, "_r%d = fold_build%d_loc (loc, %s, %s",
2722 depth, ops.length(), opr_name, type);
2723 else
2724 fprintf_indent (f, indent, "_r%d = maybe_build_call_expr_loc (loc, "
2725 "%s, %s, %d", depth, opr_name, type, ops.length());
2726 for (unsigned i = 0; i < ops.length (); ++i)
2727 fprintf (f, ", _o%d[%u]", depth, i);
2728 fprintf (f, ");\n");
2729 if (opr->kind != id_base::CODE)
2731 fprintf_indent (f, indent, "if (!_r%d)\n", depth);
2732 fprintf_indent (f, indent, " goto %s;\n", fail_label);
2734 if (force_leaf)
2736 fprintf_indent (f, indent, "if (EXPR_P (_r%d))\n", depth);
2737 fprintf_indent (f, indent, " goto %s;\n", fail_label);
2739 if (*opr == CONVERT_EXPR)
2741 fprintf_indent (f, indent - 2, "}\n");
2742 indent -= 4;
2743 fprintf_indent (f, indent, "else\n");
2744 fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
2747 fprintf_indent (f, indent, "%s = _r%d;\n", dest, depth);
2748 indent -= 2;
2749 fprintf_indent (f, indent, "}\n");
2752 /* Generate code for a c_expr which is either the expression inside
2753 an if statement or a sequence of statements which computes a
2754 result to be stored to DEST. */
2756 void
2757 c_expr::gen_transform (FILE *f, int indent, const char *dest,
2758 bool, int, const char *, capture_info *,
2759 dt_operand **, int)
2761 if (dest && nr_stmts == 1)
2762 fprintf_indent (f, indent, "%s = ", dest);
2764 unsigned stmt_nr = 1;
2765 int prev_line = -1;
2766 for (unsigned i = 0; i < code.length (); ++i)
2768 const cpp_token *token = &code[i];
2770 /* We can't recover from all lexing losses but we can roughly restore line
2771 breaks from location info. */
2772 const line_map_ordinary *map;
2773 linemap_resolve_location (line_table, token->src_loc,
2774 LRK_SPELLING_LOCATION, &map);
2775 expanded_location loc = linemap_expand_location (line_table, map,
2776 token->src_loc);
2777 if (prev_line != -1 && loc.line != prev_line)
2778 fputc ('\n', f);
2779 prev_line = loc.line;
2781 /* Replace captures for code-gen. */
2782 if (token->type == CPP_ATSIGN)
2784 const cpp_token *n = &code[i+1];
2785 if ((n->type == CPP_NUMBER
2786 || n->type == CPP_NAME)
2787 && !(n->flags & PREV_WHITE))
2789 if (token->flags & PREV_WHITE)
2790 fputc (' ', f);
2791 const char *id;
2792 if (n->type == CPP_NUMBER)
2793 id = (const char *)n->val.str.text;
2794 else
2795 id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
2796 unsigned *cid = capture_ids->get (id);
2797 if (!cid)
2798 fatal_at (token, "unknown capture id");
2799 fprintf (f, "captures[%u]", *cid);
2800 ++i;
2801 continue;
2805 if (token->flags & PREV_WHITE)
2806 fputc (' ', f);
2808 if (token->type == CPP_NAME)
2810 const char *id = (const char *) NODE_NAME (token->val.node.node);
2811 unsigned j;
2812 for (j = 0; j < ids.length (); ++j)
2814 if (strcmp (id, ids[j].id) == 0)
2816 fprintf (f, "%s", ids[j].oper);
2817 break;
2820 if (j < ids.length ())
2821 continue;
2824 /* Output the token as string. */
2825 char *tk = (char *)cpp_token_as_text (r, token);
2826 fputs (tk, f);
2828 if (token->type == CPP_SEMICOLON)
2830 stmt_nr++;
2831 if (dest && stmt_nr == nr_stmts)
2832 fprintf_indent (f, indent, "%s = ", dest);
2835 fputc ('\n', f);
2838 /* Generate transform code for a capture. */
2840 void
2841 capture::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
2842 int depth, const char *in_type, capture_info *cinfo,
2843 dt_operand **indexes, int cond_handling)
2845 if (what && is_a<expr *> (what))
2847 if (indexes[where] == 0)
2849 char buf[20];
2850 snprintf (buf, sizeof (buf), "captures[%u]", where);
2851 what->gen_transform (f, indent, buf, gimple, depth, in_type,
2852 cinfo, NULL);
2856 /* If in GENERIC some capture is used multiple times, unshare it except
2857 when emitting the last use. */
2858 if (!gimple
2859 && cinfo->info.exists ()
2860 && cinfo->info[cinfo->info[where].same_as].result_use_count > 1)
2862 fprintf_indent (f, indent, "%s = unshare_expr (captures[%u]);\n",
2863 dest, where);
2864 cinfo->info[cinfo->info[where].same_as].result_use_count--;
2866 else
2867 fprintf_indent (f, indent, "%s = captures[%u];\n", dest, where);
2869 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2870 with substituting a capture of that. */
2871 if (gimple
2872 && cond_handling != 0
2873 && cinfo->info[where].cond_expr_cond_p)
2875 /* If substituting into a cond_expr condition, unshare. */
2876 if (cond_handling == 1)
2877 fprintf_indent (f, indent, "%s = unshare_expr (%s);\n", dest, dest);
2878 /* If substituting elsewhere we might need to decompose it. */
2879 else if (cond_handling == 2)
2881 /* ??? Returning false here will also not allow any other patterns
2882 to match unless this generator was split out. */
2883 fprintf_indent (f, indent, "if (COMPARISON_CLASS_P (%s))\n", dest);
2884 fprintf_indent (f, indent, " {\n");
2885 fprintf_indent (f, indent, " if (!seq) return false;\n");
2886 fprintf_indent (f, indent, " %s = gimple_build (seq,"
2887 " TREE_CODE (%s),"
2888 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
2889 " TREE_OPERAND (%s, 1));\n",
2890 dest, dest, dest, dest, dest);
2891 fprintf_indent (f, indent, " }\n");
2896 /* Return the name of the operand representing the decision tree node.
2897 Use NAME as space to generate it. */
2899 char *
2900 dt_operand::get_name (char *name)
2902 if (! parent)
2903 sprintf (name, "t");
2904 else if (parent->level == 1)
2905 sprintf (name, "_p%u", pos);
2906 else if (parent->type == dt_node::DT_MATCH)
2907 return as_a <dt_operand *> (parent)->get_name (name);
2908 else
2909 sprintf (name, "_q%u%u", parent->level, pos);
2910 return name;
2913 /* Fill NAME with the operand name at position POS. */
2915 void
2916 dt_operand::gen_opname (char *name, unsigned pos)
2918 if (! parent)
2919 sprintf (name, "_p%u", pos);
2920 else
2921 sprintf (name, "_q%u%u", level, pos);
2924 /* Generate matching code for the decision tree operand which is
2925 a predicate. */
2927 unsigned
2928 dt_operand::gen_predicate (FILE *f, int indent, const char *opname, bool gimple)
2930 predicate *p = as_a <predicate *> (op);
2932 if (p->p->matchers.exists ())
2934 /* If this is a predicate generated from a pattern mangle its
2935 name and pass on the valueize hook. */
2936 if (gimple)
2937 fprintf_indent (f, indent, "if (gimple_%s (%s, valueize))\n",
2938 p->p->id, opname);
2939 else
2940 fprintf_indent (f, indent, "if (tree_%s (%s))\n", p->p->id, opname);
2942 else
2943 fprintf_indent (f, indent, "if (%s (%s))\n", p->p->id, opname);
2944 fprintf_indent (f, indent + 2, "{\n");
2945 return 1;
2948 /* Generate matching code for the decision tree operand which is
2949 a capture-match. */
2951 unsigned
2952 dt_operand::gen_match_op (FILE *f, int indent, const char *opname, bool)
2954 char match_opname[20];
2955 match_dop->get_name (match_opname);
2956 if (value_match)
2957 fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
2958 "|| operand_equal_p (%s, %s, 0))\n",
2959 opname, match_opname, opname, opname, match_opname);
2960 else
2961 fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
2962 "|| (operand_equal_p (%s, %s, 0) "
2963 "&& types_match (%s, %s)))\n",
2964 opname, match_opname, opname, opname, match_opname,
2965 opname, match_opname);
2966 fprintf_indent (f, indent + 2, "{\n");
2967 return 1;
2970 /* Generate GIMPLE matching code for the decision tree operand. */
2972 unsigned
2973 dt_operand::gen_gimple_expr (FILE *f, int indent, int depth)
2975 expr *e = static_cast<expr *> (op);
2976 id_base *id = e->operation;
2977 unsigned n_ops = e->ops.length ();
2978 unsigned n_braces = 0;
2980 if (user_id *u = dyn_cast <user_id *> (id))
2981 id = u->substitutes[0];
2983 for (unsigned i = 0; i < n_ops; ++i)
2985 char child_opname[20];
2986 gen_opname (child_opname, i);
2988 if (id->kind == id_base::CODE)
2990 if (e->is_generic
2991 || *id == REALPART_EXPR || *id == IMAGPART_EXPR
2992 || *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR)
2994 /* ??? If this is a memory operation we can't (and should not)
2995 match this. The only sensible operand types are
2996 SSA names and invariants. */
2997 if (e->is_generic)
2999 char opname[20];
3000 get_name (opname);
3001 fprintf_indent (f, indent,
3002 "tree %s = TREE_OPERAND (%s, %i);\n",
3003 child_opname, opname, i);
3005 else
3006 fprintf_indent (f, indent,
3007 "tree %s = TREE_OPERAND "
3008 "(gimple_assign_rhs1 (_a%d), %i);\n",
3009 child_opname, depth, i);
3010 fprintf_indent (f, indent,
3011 "if ((TREE_CODE (%s) == SSA_NAME\n",
3012 child_opname);
3013 fprintf_indent (f, indent,
3014 " || is_gimple_min_invariant (%s)))\n",
3015 child_opname);
3016 fprintf_indent (f, indent,
3017 " {\n");
3018 indent += 4;
3019 n_braces++;
3020 fprintf_indent (f, indent,
3021 "%s = do_valueize (valueize, %s);\n",
3022 child_opname, child_opname);
3023 continue;
3025 else
3026 fprintf_indent (f, indent,
3027 "tree %s = gimple_assign_rhs%u (_a%d);\n",
3028 child_opname, i + 1, depth);
3030 else
3031 fprintf_indent (f, indent,
3032 "tree %s = gimple_call_arg (_c%d, %u);\n",
3033 child_opname, depth, i);
3034 fprintf_indent (f, indent,
3035 "%s = do_valueize (valueize, %s);\n",
3036 child_opname, child_opname);
3038 /* While the toplevel operands are canonicalized by the caller
3039 after valueizing operands of sub-expressions we have to
3040 re-canonicalize operand order. */
3041 int opno = commutative_op (id);
3042 if (opno >= 0)
3044 char child_opname0[20], child_opname1[20];
3045 gen_opname (child_opname0, opno);
3046 gen_opname (child_opname1, opno + 1);
3047 fprintf_indent (f, indent,
3048 "if (tree_swap_operands_p (%s, %s))\n",
3049 child_opname0, child_opname1);
3050 fprintf_indent (f, indent,
3051 " std::swap (%s, %s);\n",
3052 child_opname0, child_opname1);
3055 return n_braces;
3058 /* Generate GENERIC matching code for the decision tree operand. */
3060 unsigned
3061 dt_operand::gen_generic_expr (FILE *f, int indent, const char *opname)
3063 expr *e = static_cast<expr *> (op);
3064 id_base *id = e->operation;
3065 unsigned n_ops = e->ops.length ();
3067 if (user_id *u = dyn_cast <user_id *> (id))
3068 id = u->substitutes[0];
3070 for (unsigned i = 0; i < n_ops; ++i)
3072 char child_opname[20];
3073 gen_opname (child_opname, i);
3075 if (id->kind == id_base::CODE)
3076 fprintf_indent (f, indent, "tree %s = TREE_OPERAND (%s, %u);\n",
3077 child_opname, opname, i);
3078 else
3079 fprintf_indent (f, indent, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
3080 child_opname, opname, i);
3083 return 0;
3086 /* Compare 2 fns or generic_fns vector entries for vector sorting.
3087 Same operation entries with different number of arguments should
3088 be adjacent. */
3090 static int
3091 fns_cmp (const void *p1, const void *p2)
3093 dt_operand *op1 = *(dt_operand *const *) p1;
3094 dt_operand *op2 = *(dt_operand *const *) p2;
3095 expr *e1 = as_a <expr *> (op1->op);
3096 expr *e2 = as_a <expr *> (op2->op);
3097 id_base *b1 = e1->operation;
3098 id_base *b2 = e2->operation;
3099 if (b1->hashval < b2->hashval)
3100 return -1;
3101 if (b1->hashval > b2->hashval)
3102 return 1;
3103 return strcmp (b1->id, b2->id);
3106 /* Generate matching code for the children of the decision tree node. */
3108 void
3109 dt_node::gen_kids (FILE *f, int indent, bool gimple, int depth)
3111 auto_vec<dt_operand *> gimple_exprs;
3112 auto_vec<dt_operand *> generic_exprs;
3113 auto_vec<dt_operand *> fns;
3114 auto_vec<dt_operand *> generic_fns;
3115 auto_vec<dt_operand *> preds;
3116 auto_vec<dt_node *> others;
3118 for (unsigned i = 0; i < kids.length (); ++i)
3120 if (kids[i]->type == dt_node::DT_OPERAND)
3122 dt_operand *op = as_a<dt_operand *> (kids[i]);
3123 if (expr *e = dyn_cast <expr *> (op->op))
3125 if (e->ops.length () == 0
3126 /* In GIMPLE a CONSTRUCTOR always appears in a
3127 separate definition. */
3128 && (!gimple || !(*e->operation == CONSTRUCTOR)))
3130 generic_exprs.safe_push (op);
3131 /* But ADDR_EXPRs can appear directly when invariant
3132 and in a separate definition when not. */
3133 if (gimple && *e->operation == ADDR_EXPR)
3134 gimple_exprs.safe_push (op);
3136 else if (e->operation->kind == id_base::FN)
3138 if (gimple)
3139 fns.safe_push (op);
3140 else
3141 generic_fns.safe_push (op);
3143 else if (e->operation->kind == id_base::PREDICATE)
3144 preds.safe_push (op);
3145 else
3147 user_id *u = dyn_cast <user_id *> (e->operation);
3148 if (u && u->substitutes[0]->kind == id_base::FN)
3150 if (gimple)
3151 fns.safe_push (op);
3152 else
3153 generic_fns.safe_push (op);
3155 else
3157 if (gimple && !e->is_generic)
3158 gimple_exprs.safe_push (op);
3159 else
3160 generic_exprs.safe_push (op);
3164 else if (op->op->type == operand::OP_PREDICATE)
3165 others.safe_push (kids[i]);
3166 else
3167 gcc_unreachable ();
3169 else if (kids[i]->type == dt_node::DT_SIMPLIFY)
3170 others.safe_push (kids[i]);
3171 else if (kids[i]->type == dt_node::DT_MATCH
3172 || kids[i]->type == dt_node::DT_TRUE)
3174 /* A DT_TRUE operand serves as a barrier - generate code now
3175 for what we have collected sofar.
3176 Like DT_TRUE, DT_MATCH serves as a barrier as it can cause
3177 dependent matches to get out-of-order. Generate code now
3178 for what we have collected sofar. */
3179 fns.qsort (fns_cmp);
3180 generic_fns.qsort (fns_cmp);
3181 gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
3182 fns, generic_fns, preds, others);
3183 /* And output the true operand itself. */
3184 kids[i]->gen (f, indent, gimple, depth);
3185 gimple_exprs.truncate (0);
3186 generic_exprs.truncate (0);
3187 fns.truncate (0);
3188 generic_fns.truncate (0);
3189 preds.truncate (0);
3190 others.truncate (0);
3192 else
3193 gcc_unreachable ();
3196 /* Generate code for the remains. */
3197 fns.qsort (fns_cmp);
3198 generic_fns.qsort (fns_cmp);
3199 gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
3200 fns, generic_fns, preds, others);
3203 /* Generate matching code for the children of the decision tree node. */
3205 void
3206 dt_node::gen_kids_1 (FILE *f, int indent, bool gimple, int depth,
3207 const vec<dt_operand *> &gimple_exprs,
3208 const vec<dt_operand *> &generic_exprs,
3209 const vec<dt_operand *> &fns,
3210 const vec<dt_operand *> &generic_fns,
3211 const vec<dt_operand *> &preds,
3212 const vec<dt_node *> &others)
3214 char buf[128];
3215 char *kid_opname = buf;
3217 unsigned exprs_len = gimple_exprs.length ();
3218 unsigned gexprs_len = generic_exprs.length ();
3219 unsigned fns_len = fns.length ();
3220 unsigned gfns_len = generic_fns.length ();
3222 if (exprs_len || fns_len || gexprs_len || gfns_len)
3224 if (exprs_len)
3225 gimple_exprs[0]->get_name (kid_opname);
3226 else if (fns_len)
3227 fns[0]->get_name (kid_opname);
3228 else if (gfns_len)
3229 generic_fns[0]->get_name (kid_opname);
3230 else
3231 generic_exprs[0]->get_name (kid_opname);
3233 fprintf_indent (f, indent, "switch (TREE_CODE (%s))\n", kid_opname);
3234 fprintf_indent (f, indent, " {\n");
3235 indent += 2;
3238 if (exprs_len || fns_len)
3240 depth++;
3241 fprintf_indent (f, indent,
3242 "case SSA_NAME:\n");
3243 fprintf_indent (f, indent,
3244 " if (gimple *_d%d = get_def (valueize, %s))\n",
3245 depth, kid_opname);
3246 fprintf_indent (f, indent,
3247 " {\n");
3248 indent += 6;
3249 if (exprs_len)
3251 fprintf_indent (f, indent,
3252 "if (gassign *_a%d = dyn_cast <gassign *> (_d%d))\n",
3253 depth, depth);
3254 fprintf_indent (f, indent,
3255 " switch (gimple_assign_rhs_code (_a%d))\n",
3256 depth);
3257 indent += 4;
3258 fprintf_indent (f, indent, "{\n");
3259 bool cond_expr_p = false;
3260 for (unsigned i = 0; i < exprs_len; ++i)
3262 expr *e = as_a <expr *> (gimple_exprs[i]->op);
3263 if (user_id *u = dyn_cast <user_id *> (e->operation))
3265 for (auto id : u->substitutes)
3266 fprintf_indent (f, indent, "case %s:\n", id->id);
3268 else
3270 id_base *op = e->operation;
3271 cond_expr_p |= (*op == COND_EXPR && e->match_phi);
3272 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
3273 fprintf_indent (f, indent, "CASE_CONVERT:\n");
3274 else
3275 fprintf_indent (f, indent, "case %s:\n", op->id);
3277 fprintf_indent (f, indent, " {\n");
3278 gimple_exprs[i]->gen (f, indent + 4, true, depth);
3279 fprintf_indent (f, indent, " break;\n");
3280 fprintf_indent (f, indent, " }\n");
3282 fprintf_indent (f, indent, "default:;\n");
3283 fprintf_indent (f, indent, "}\n");
3284 indent -= 4;
3286 if (cond_expr_p)
3288 fprintf_indent (f, indent,
3289 "else if (gphi *_a%d = dyn_cast <gphi *> (_d%d))\n",
3290 depth, depth);
3291 indent += 2;
3292 fprintf_indent (f, indent, "{\n");
3293 indent += 2;
3295 for (unsigned i = 0; i < exprs_len; i++)
3297 expr *e = as_a <expr *> (gimple_exprs[i]->op);
3298 if (*e->operation == COND_EXPR && e->match_phi)
3299 gimple_exprs[i]->gen_phi_on_cond (f, indent, depth);
3302 indent -= 2;
3303 fprintf_indent (f, indent, "}\n");
3304 indent -= 2;
3308 if (fns_len)
3310 fprintf_indent (f, indent,
3311 "%sif (gcall *_c%d = dyn_cast <gcall *> (_d%d))\n",
3312 exprs_len ? "else " : "", depth, depth);
3313 fprintf_indent (f, indent,
3314 " switch (gimple_call_combined_fn (_c%d))\n",
3315 depth);
3317 indent += 4;
3318 fprintf_indent (f, indent, "{\n");
3319 id_base *last_op = NULL;
3320 for (unsigned i = 0; i < fns_len; ++i)
3322 expr *e = as_a <expr *>(fns[i]->op);
3323 if (e->operation != last_op)
3325 if (i)
3326 fprintf_indent (f, indent, " break;\n");
3327 if (user_id *u = dyn_cast <user_id *> (e->operation))
3328 for (auto id : u->substitutes)
3329 fprintf_indent (f, indent, "case %s:\n", id->id);
3330 else
3331 fprintf_indent (f, indent, "case %s:\n", e->operation->id);
3333 last_op = e->operation;
3334 /* We need to be defensive against bogus prototypes allowing
3335 calls with not enough arguments. */
3336 fprintf_indent (f, indent,
3337 " if (gimple_call_num_args (_c%d) == %d)\n",
3338 depth, e->ops.length ());
3339 fprintf_indent (f, indent, " {\n");
3340 fns[i]->gen (f, indent + 6, true, depth);
3341 fprintf_indent (f, indent, " }\n");
3344 fprintf_indent (f, indent, " break;\n");
3345 fprintf_indent (f, indent, "default:;\n");
3346 fprintf_indent (f, indent, "}\n");
3347 indent -= 4;
3350 indent -= 6;
3351 depth--;
3352 fprintf_indent (f, indent, " }\n");
3353 /* See if there is SSA_NAME among generic_exprs and if yes, emit it
3354 here rather than in the next loop. */
3355 for (unsigned i = 0; i < generic_exprs.length (); ++i)
3357 expr *e = as_a <expr *>(generic_exprs[i]->op);
3358 id_base *op = e->operation;
3359 if (*op == SSA_NAME && (exprs_len || fns_len))
3361 fprintf_indent (f, indent + 4, "{\n");
3362 generic_exprs[i]->gen (f, indent + 6, gimple, depth);
3363 fprintf_indent (f, indent + 4, "}\n");
3367 fprintf_indent (f, indent, " break;\n");
3370 for (unsigned i = 0; i < generic_exprs.length (); ++i)
3372 expr *e = as_a <expr *>(generic_exprs[i]->op);
3373 id_base *op = e->operation;
3374 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
3375 fprintf_indent (f, indent, "CASE_CONVERT:\n");
3376 else if (*op == SSA_NAME && (exprs_len || fns_len))
3377 /* Already handled above. */
3378 continue;
3379 else
3381 if (user_id *u = dyn_cast <user_id *> (op))
3382 for (auto id : u->substitutes)
3383 fprintf_indent (f, indent, "case %s:\n", id->id);
3384 else
3385 fprintf_indent (f, indent, "case %s:\n", op->id);
3387 fprintf_indent (f, indent, " {\n");
3388 generic_exprs[i]->gen (f, indent + 4, gimple, depth);
3389 fprintf_indent (f, indent, " break;\n");
3390 fprintf_indent (f, indent, " }\n");
3393 if (gfns_len)
3395 fprintf_indent (f, indent,
3396 "case CALL_EXPR:\n");
3397 fprintf_indent (f, indent,
3398 " switch (get_call_combined_fn (%s))\n",
3399 kid_opname);
3400 fprintf_indent (f, indent,
3401 " {\n");
3402 indent += 4;
3404 id_base *last_op = NULL;
3405 for (unsigned j = 0; j < generic_fns.length (); ++j)
3407 expr *e = as_a <expr *>(generic_fns[j]->op);
3408 gcc_assert (e->operation->kind == id_base::FN);
3410 if (e->operation != last_op)
3412 if (j)
3413 fprintf_indent (f, indent, " break;\n");
3414 fprintf_indent (f, indent, "case %s:\n", e->operation->id);
3416 last_op = e->operation;
3417 fprintf_indent (f, indent, " if (call_expr_nargs (%s) == %d)\n"
3418 " {\n", kid_opname, e->ops.length ());
3419 generic_fns[j]->gen (f, indent + 6, false, depth);
3420 fprintf_indent (f, indent, " }\n");
3422 fprintf_indent (f, indent, " break;\n");
3423 fprintf_indent (f, indent, "default:;\n");
3425 indent -= 4;
3426 fprintf_indent (f, indent, " }\n");
3427 fprintf_indent (f, indent, " break;\n");
3430 /* Close switch (TREE_CODE ()). */
3431 if (exprs_len || fns_len || gexprs_len || gfns_len)
3433 indent -= 4;
3434 fprintf_indent (f, indent, " default:;\n");
3435 fprintf_indent (f, indent, " }\n");
3438 for (unsigned i = 0; i < preds.length (); ++i)
3440 expr *e = as_a <expr *> (preds[i]->op);
3441 predicate_id *p = as_a <predicate_id *> (e->operation);
3442 preds[i]->get_name (kid_opname);
3443 fprintf_indent (f, indent, "{\n");
3444 indent += 2;
3445 fprintf_indent (f, indent, "tree %s_pops[%d];\n", kid_opname, p->nargs);
3446 fprintf_indent (f, indent, "if (%s_%s (%s, %s_pops%s))\n",
3447 gimple ? "gimple" : "tree",
3448 p->id, kid_opname, kid_opname,
3449 gimple ? ", valueize" : "");
3450 fprintf_indent (f, indent, " {\n");
3451 for (int j = 0; j < p->nargs; ++j)
3453 char child_opname[20];
3454 preds[i]->gen_opname (child_opname, j);
3455 fprintf_indent (f, indent + 4, "tree %s = %s_pops[%d];\n",
3456 child_opname, kid_opname, j);
3458 preds[i]->gen_kids (f, indent + 4, gimple, depth);
3459 fprintf_indent (f, indent, " }\n");
3460 indent -= 2;
3461 fprintf_indent (f, indent, "}\n");
3464 for (unsigned i = 0; i < others.length (); ++i)
3465 others[i]->gen (f, indent, gimple, depth);
3468 /* Generate matching code for the decision tree operand. */
3470 void
3471 dt_operand::gen (FILE *f, int indent, bool gimple, int depth)
3473 char opname[20];
3474 get_name (opname);
3476 unsigned n_braces = 0;
3478 if (type == DT_OPERAND)
3479 switch (op->type)
3481 case operand::OP_PREDICATE:
3482 n_braces = gen_predicate (f, indent, opname, gimple);
3483 break;
3485 case operand::OP_EXPR:
3486 if (gimple)
3487 n_braces = gen_gimple_expr (f, indent, depth);
3488 else
3489 n_braces = gen_generic_expr (f, indent, opname);
3490 break;
3492 default:
3493 gcc_unreachable ();
3495 else if (type == DT_TRUE)
3497 else if (type == DT_MATCH)
3498 n_braces = gen_match_op (f, indent, opname, gimple);
3499 else
3500 gcc_unreachable ();
3502 indent += 4 * n_braces;
3503 gen_kids (f, indent, gimple, depth);
3505 for (unsigned i = 0; i < n_braces; ++i)
3507 indent -= 4;
3508 if (indent < 0)
3509 indent = 0;
3510 fprintf_indent (f, indent, " }\n");
3514 /* Generate matching code for the phi when meet COND_EXPR. */
3516 void
3517 dt_operand::gen_phi_on_cond (FILE *f, int indent, int depth)
3519 fprintf_indent (f, indent,
3520 "basic_block _b%d = gimple_bb (_a%d);\n", depth, depth);
3522 fprintf_indent (f, indent, "if (gimple_phi_num_args (_a%d) == 2)\n", depth);
3524 indent += 2;
3525 fprintf_indent (f, indent, "{\n");
3526 indent += 2;
3528 fprintf_indent (f, indent,
3529 "basic_block _pb_0_%d = EDGE_PRED (_b%d, 0)->src;\n", depth, depth);
3530 fprintf_indent (f, indent,
3531 "basic_block _pb_1_%d = EDGE_PRED (_b%d, 1)->src;\n", depth, depth);
3532 fprintf_indent (f, indent,
3533 "basic_block _db_%d = safe_dyn_cast <gcond *> (*gsi_last_bb (_pb_0_%d)) ? "
3534 "_pb_0_%d : _pb_1_%d;\n", depth, depth, depth, depth);
3535 fprintf_indent (f, indent,
3536 "basic_block _other_db_%d = safe_dyn_cast <gcond *> "
3537 "(*gsi_last_bb (_pb_0_%d)) ? _pb_1_%d : _pb_0_%d;\n",
3538 depth, depth, depth, depth);
3540 fprintf_indent (f, indent,
3541 "gcond *_ct_%d = safe_dyn_cast <gcond *> (*gsi_last_bb (_db_%d));\n",
3542 depth, depth);
3543 fprintf_indent (f, indent, "if (_ct_%d"
3544 " && EDGE_COUNT (_other_db_%d->preds) == 1\n", depth, depth);
3545 fprintf_indent (f, indent,
3546 " && EDGE_COUNT (_other_db_%d->succs) == 1\n", depth);
3547 fprintf_indent (f, indent,
3548 " && EDGE_PRED (_other_db_%d, 0)->src == _db_%d)\n", depth, depth);
3550 indent += 2;
3551 fprintf_indent (f, indent, "{\n");
3552 indent += 2;
3554 fprintf_indent (f, indent,
3555 "tree _cond_lhs_%d = gimple_cond_lhs (_ct_%d);\n", depth, depth);
3556 fprintf_indent (f, indent,
3557 "tree _cond_rhs_%d = gimple_cond_rhs (_ct_%d);\n", depth, depth);
3559 char opname_0[20];
3560 char opname_1[20];
3561 char opname_2[20];
3562 gen_opname (opname_0, 0);
3564 fprintf_indent (f, indent,
3565 "tree %s = build2 (gimple_cond_code (_ct_%d), "
3566 "boolean_type_node, _cond_lhs_%d, _cond_rhs_%d);\n",
3567 opname_0, depth, depth, depth);
3569 fprintf_indent (f, indent,
3570 "bool _arg_0_is_true_%d = gimple_phi_arg_edge (_a%d, 0)->flags"
3571 " & EDGE_TRUE_VALUE;\n", depth, depth);
3573 gen_opname (opname_1, 1);
3574 fprintf_indent (f, indent,
3575 "tree %s = gimple_phi_arg_def (_a%d, _arg_0_is_true_%d ? 0 : 1);\n",
3576 opname_1, depth, depth);
3578 gen_opname (opname_2, 2);
3579 fprintf_indent (f, indent,
3580 "tree %s = gimple_phi_arg_def (_a%d, _arg_0_is_true_%d ? 1 : 0);\n",
3581 opname_2, depth, depth);
3583 gen_kids (f, indent, true, depth);
3585 indent -= 2;
3586 fprintf_indent (f, indent, "}\n");
3587 indent -= 2;
3589 indent -= 2;
3590 fprintf_indent (f, indent, "}\n");
3591 indent -= 2;
3594 /* Emit a logging call to the debug file to the file F, with the INDENT from
3595 either the RESULT location or the S's match location if RESULT is null. */
3596 static void
3597 emit_logging_call (FILE *f, int indent, class simplify *s, operand *result,
3598 bool gimple)
3600 fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
3601 "%s_dump_logs (", gimple ? "gimple" : "generic");
3602 output_line_directive (f,
3603 result ? result->location : s->match->location,
3604 true, true, true);
3605 fprintf (f, ", __FILE__, __LINE__, %s);\n",
3606 s->kind == simplify::SIMPLIFY ? "true" : "false");
3609 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3610 step of a '(simplify ...)' or '(match ...)'. This handles everything
3611 that is not part of the decision tree (simplify->match).
3612 Main recursive worker. */
3614 void
3615 dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
3617 if (result)
3619 if (with_expr *w = dyn_cast <with_expr *> (result))
3621 fprintf_indent (f, indent, "{\n");
3622 indent += 4;
3623 output_line_directive (f, w->location);
3624 w->with->gen_transform (f, indent, NULL, true, 1, "type", NULL);
3625 gen_1 (f, indent, gimple, w->subexpr);
3626 indent -= 4;
3627 fprintf_indent (f, indent, "}\n");
3628 return;
3630 else if (if_expr *ife = dyn_cast <if_expr *> (result))
3632 output_line_directive (f, ife->location);
3633 fprintf_indent (f, indent, "if (");
3634 ife->cond->gen_transform (f, indent, NULL, true, 1, "type", NULL);
3635 fprintf (f, ")\n");
3636 fprintf_indent (f, indent + 2, "{\n");
3637 indent += 4;
3638 gen_1 (f, indent, gimple, ife->trueexpr);
3639 indent -= 4;
3640 fprintf_indent (f, indent + 2, "}\n");
3641 if (ife->falseexpr)
3643 fprintf_indent (f, indent, "else\n");
3644 fprintf_indent (f, indent + 2, "{\n");
3645 indent += 4;
3646 gen_1 (f, indent, gimple, ife->falseexpr);
3647 indent -= 4;
3648 fprintf_indent (f, indent + 2, "}\n");
3650 return;
3654 static unsigned fail_label_cnt;
3655 char local_fail_label[256];
3656 snprintf (local_fail_label, 256, "next_after_fail%u", ++fail_label_cnt);
3657 fail_label = local_fail_label;
3658 bool needs_label = false;
3660 /* Analyze captures and perform early-outs on the incoming arguments
3661 that cover cases we cannot handle. */
3662 capture_info cinfo (s, result, gimple);
3663 if (s->kind == simplify::SIMPLIFY)
3665 if (!gimple)
3667 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
3668 if (cinfo.force_no_side_effects & (1 << i))
3670 fprintf_indent (f, indent,
3671 "if (TREE_SIDE_EFFECTS (_p%d)) goto %s;\n",
3672 i, fail_label);
3673 needs_label = true;
3674 if (verbose >= 1)
3675 warning_at (as_a <expr *> (s->match)->ops[i]->location,
3676 "forcing toplevel operand to have no "
3677 "side-effects");
3679 for (int i = 0; i <= s->capture_max; ++i)
3680 if (cinfo.info[i].cse_p)
3682 else if (cinfo.info[i].force_no_side_effects_p
3683 && (cinfo.info[i].toplevel_msk
3684 & cinfo.force_no_side_effects) == 0)
3686 fprintf_indent (f, indent,
3687 "if (TREE_SIDE_EFFECTS (captures[%d])) "
3688 "goto %s;\n", i, fail_label);
3689 needs_label = true;
3690 if (verbose >= 1)
3691 warning_at (cinfo.info[i].c->location,
3692 "forcing captured operand to have no "
3693 "side-effects");
3695 else if ((cinfo.info[i].toplevel_msk
3696 & cinfo.force_no_side_effects) != 0)
3697 /* Mark capture as having no side-effects if we had to verify
3698 that via forced toplevel operand checks. */
3699 cinfo.info[i].force_no_side_effects_p = true;
3701 if (gimple)
3703 /* Force single-use restriction by only allowing simple
3704 results via setting seq to NULL. */
3705 fprintf_indent (f, indent, "gimple_seq *lseq = seq;\n");
3706 bool first_p = true;
3707 for (int i = 0; i <= s->capture_max; ++i)
3708 if (cinfo.info[i].force_single_use)
3710 if (first_p)
3712 fprintf_indent (f, indent, "if (lseq\n");
3713 fprintf_indent (f, indent, " && (");
3714 first_p = false;
3716 else
3718 fprintf (f, "\n");
3719 fprintf_indent (f, indent, " || ");
3721 fprintf (f, "!single_use (captures[%d])", i);
3723 if (!first_p)
3725 fprintf (f, "))\n");
3726 fprintf_indent (f, indent, " lseq = NULL;\n");
3731 if (s->kind == simplify::SIMPLIFY)
3733 fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s;\n", fail_label);
3734 needs_label = true;
3737 fprintf_indent (f, indent, "{\n");
3738 indent += 2;
3739 if (!result)
3741 /* If there is no result then this is a predicate implementation. */
3742 emit_logging_call (f, indent, s, result, gimple);
3743 fprintf_indent (f, indent, "return true;\n");
3745 else if (gimple)
3747 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
3748 in outermost position). */
3749 if (result->type == operand::OP_EXPR
3750 && *as_a <expr *> (result)->operation == NON_LVALUE_EXPR)
3751 result = as_a <expr *> (result)->ops[0];
3752 if (result->type == operand::OP_EXPR)
3754 expr *e = as_a <expr *> (result);
3755 id_base *opr = e->operation;
3756 bool is_predicate = false;
3757 /* When we delay operator substituting during lowering of fors we
3758 make sure that for code-gen purposes the effects of each substitute
3759 are the same. Thus just look at that. */
3760 if (user_id *uid = dyn_cast <user_id *> (opr))
3761 opr = uid->substitutes[0];
3762 else if (is_a <predicate_id *> (opr))
3763 is_predicate = true;
3764 if (!is_predicate)
3765 fprintf_indent (f, indent, "res_op->set_op (%s, type, %d);\n",
3766 *e->operation == CONVERT_EXPR
3767 ? "NOP_EXPR" : e->operation->id,
3768 e->ops.length ());
3769 for (unsigned j = 0; j < e->ops.length (); ++j)
3771 char dest[32];
3772 if (is_predicate)
3773 snprintf (dest, sizeof (dest), "res_ops[%d]", j);
3774 else
3775 snprintf (dest, sizeof (dest), "res_op->ops[%d]", j);
3776 const char *optype
3777 = get_operand_type (opr, j,
3778 "type", e->expr_type,
3779 j == 0 ? NULL
3780 : "TREE_TYPE (res_op->ops[0])");
3781 /* We need to expand GENERIC conditions we captured from
3782 COND_EXPRs and we need to unshare them when substituting
3783 into COND_EXPRs. */
3784 int cond_handling = 0;
3785 if (!is_predicate)
3786 cond_handling = (*opr == COND_EXPR && j == 0) ? 1 : 2;
3787 e->ops[j]->gen_transform (f, indent, dest, true, 1, optype,
3788 &cinfo, indexes, cond_handling);
3791 /* Re-fold the toplevel result. It's basically an embedded
3792 gimple_build w/o actually building the stmt. */
3793 if (!is_predicate)
3795 fprintf_indent (f, indent,
3796 "res_op->resimplify (%s, valueize);\n",
3797 !e->force_leaf ? "lseq" : "NULL");
3798 if (e->force_leaf)
3800 fprintf_indent (f, indent,
3801 "if (!maybe_push_res_to_seq (res_op, NULL)) "
3802 "goto %s;\n", fail_label);
3803 needs_label = true;
3807 else if (result->type == operand::OP_CAPTURE
3808 || result->type == operand::OP_C_EXPR)
3810 fprintf_indent (f, indent, "tree tem;\n");
3811 result->gen_transform (f, indent, "tem", true, 1, "type",
3812 &cinfo, indexes);
3813 fprintf_indent (f, indent, "res_op->set_value (tem);\n");
3814 if (is_a <capture *> (result)
3815 && cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p)
3817 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
3818 with substituting a capture of that. */
3819 fprintf_indent (f, indent,
3820 "if (COMPARISON_CLASS_P (tem))\n");
3821 fprintf_indent (f, indent,
3822 " {\n");
3823 fprintf_indent (f, indent,
3824 " res_op->ops[0] = TREE_OPERAND (tem, 0);\n");
3825 fprintf_indent (f, indent,
3826 " res_op->ops[1] = TREE_OPERAND (tem, 1);\n");
3827 fprintf_indent (f, indent,
3828 " }\n");
3831 else
3832 gcc_unreachable ();
3833 emit_logging_call (f, indent, s, result, gimple);
3834 fprintf_indent (f, indent, "return true;\n");
3836 else /* GENERIC */
3838 bool is_predicate = false;
3839 if (result->type == operand::OP_EXPR)
3841 expr *e = as_a <expr *> (result);
3842 id_base *opr = e->operation;
3843 /* When we delay operator substituting during lowering of fors we
3844 make sure that for code-gen purposes the effects of each substitute
3845 are the same. Thus just look at that. */
3846 if (user_id *uid = dyn_cast <user_id *> (opr))
3847 opr = uid->substitutes[0];
3848 else if (is_a <predicate_id *> (opr))
3849 is_predicate = true;
3850 /* Search for captures used multiple times in the result expression
3851 and wrap them in a SAVE_EXPR. Allow as many uses as in the
3852 original expression. */
3853 if (!is_predicate)
3854 for (int i = 0; i < s->capture_max + 1; ++i)
3856 if (cinfo.info[i].same_as != (unsigned)i
3857 || cinfo.info[i].cse_p)
3858 continue;
3859 if (cinfo.info[i].result_use_count
3860 > cinfo.info[i].match_use_count)
3862 fprintf_indent (f, indent,
3863 "if (! tree_invariant_p (captures[%d])) "
3864 "goto %s;\n", i, fail_label);
3865 needs_label = true;
3868 for (unsigned j = 0; j < e->ops.length (); ++j)
3870 char dest[32];
3871 if (is_predicate)
3872 snprintf (dest, sizeof (dest), "res_ops[%d]", j);
3873 else
3875 fprintf_indent (f, indent, "tree res_op%d;\n", j);
3876 snprintf (dest, sizeof (dest), "res_op%d", j);
3878 const char *optype
3879 = get_operand_type (opr, j,
3880 "type", e->expr_type,
3881 j == 0
3882 ? NULL : "TREE_TYPE (res_op0)");
3883 e->ops[j]->gen_transform (f, indent, dest, false, 1, optype,
3884 &cinfo, indexes);
3886 if (is_predicate)
3888 emit_logging_call (f, indent, s, result, gimple);
3889 fprintf_indent (f, indent, "return true;\n");
3891 else
3893 fprintf_indent (f, indent, "tree _r;\n");
3894 /* Re-fold the toplevel result. Use non_lvalue to
3895 build NON_LVALUE_EXPRs so they get properly
3896 ignored when in GIMPLE form. */
3897 if (*opr == NON_LVALUE_EXPR)
3898 fprintf_indent (f, indent,
3899 "_r = non_lvalue_loc (loc, res_op0);\n");
3900 else
3902 if (is_a <operator_id *> (opr))
3903 fprintf_indent (f, indent,
3904 "_r = fold_build%d_loc (loc, %s, type",
3905 e->ops.length (),
3906 *e->operation == CONVERT_EXPR
3907 ? "NOP_EXPR" : e->operation->id);
3908 else
3909 fprintf_indent (f, indent,
3910 "_r = maybe_build_call_expr_loc (loc, "
3911 "%s, type, %d", e->operation->id,
3912 e->ops.length());
3913 for (unsigned j = 0; j < e->ops.length (); ++j)
3914 fprintf (f, ", res_op%d", j);
3915 fprintf (f, ");\n");
3916 if (!is_a <operator_id *> (opr))
3918 fprintf_indent (f, indent, "if (!_r)\n");
3919 fprintf_indent (f, indent, " goto %s;\n", fail_label);
3920 needs_label = true;
3925 else if (result->type == operand::OP_CAPTURE
3926 || result->type == operand::OP_C_EXPR)
3929 fprintf_indent (f, indent, "tree _r;\n");
3930 result->gen_transform (f, indent, "_r", false, 1, "type",
3931 &cinfo, indexes);
3933 else
3934 gcc_unreachable ();
3935 if (!is_predicate)
3937 /* Search for captures not used in the result expression and dependent
3938 on TREE_SIDE_EFFECTS emit omit_one_operand. */
3939 for (int i = 0; i < s->capture_max + 1; ++i)
3941 if (cinfo.info[i].same_as != (unsigned)i)
3942 continue;
3943 if (!cinfo.info[i].force_no_side_effects_p
3944 && !cinfo.info[i].expr_p
3945 && cinfo.info[i].result_use_count == 0)
3947 fprintf_indent (f, indent,
3948 "if (TREE_SIDE_EFFECTS (captures[%d]))\n",
3950 fprintf_indent (f, indent + 2,
3951 "_r = build2_loc (loc, COMPOUND_EXPR, type, "
3952 "fold_ignored_result (captures[%d]), _r);\n",
3956 emit_logging_call (f, indent, s, result, gimple);
3957 fprintf_indent (f, indent, "return _r;\n");
3960 indent -= 2;
3961 fprintf_indent (f, indent, "}\n");
3962 if (needs_label)
3963 fprintf (f, "%s:;\n", fail_label);
3964 fail_label = NULL;
3967 /* Generate code for the '(if ...)', '(with ..)' and actual transform
3968 step of a '(simplify ...)' or '(match ...)'. This handles everything
3969 that is not part of the decision tree (simplify->match). */
3971 void
3972 dt_simplify::gen (FILE *f, int indent, bool gimple, int depth ATTRIBUTE_UNUSED)
3974 fprintf_indent (f, indent, "{\n");
3975 indent += 2;
3976 output_line_directive (f,
3977 s->result ? s->result->location : s->match->location);
3978 if (s->capture_max >= 0)
3980 char opname[20];
3981 fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = { %s",
3982 s->capture_max + 1, indexes[0]->get_name (opname));
3984 for (int i = 1; i <= s->capture_max; ++i)
3986 if (!indexes[i])
3987 break;
3988 fprintf (f, ", %s", indexes[i]->get_name (opname));
3990 fprintf (f, " };\n");
3993 /* If we have a split-out function for the actual transform, call it. */
3994 if (info && info->fname)
3996 if (gimple)
3998 fprintf_indent (f, indent, "if (%s (res_op, seq, "
3999 "valueize, type, captures", info->fname);
4000 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
4001 if (s->for_subst_vec[i].first->used)
4002 fprintf (f, ", %s", s->for_subst_vec[i].second->id);
4003 fprintf (f, "))\n");
4004 fprintf_indent (f, indent, " return true;\n");
4006 else
4008 fprintf_indent (f, indent, "tree res = %s (loc, type",
4009 info->fname);
4010 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
4011 fprintf (f, ", _p%d", i);
4012 fprintf (f, ", captures");
4013 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
4015 if (s->for_subst_vec[i].first->used)
4016 fprintf (f, ", %s", s->for_subst_vec[i].second->id);
4018 fprintf (f, ");\n");
4019 fprintf_indent (f, indent, "if (res) return res;\n");
4022 else
4024 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
4026 if (! s->for_subst_vec[i].first->used)
4027 continue;
4028 if (is_a <operator_id *> (s->for_subst_vec[i].second))
4029 fprintf_indent (f, indent, "const enum tree_code %s = %s;\n",
4030 s->for_subst_vec[i].first->id,
4031 s->for_subst_vec[i].second->id);
4032 else if (is_a <fn_id *> (s->for_subst_vec[i].second))
4033 fprintf_indent (f, indent, "const combined_fn %s = %s;\n",
4034 s->for_subst_vec[i].first->id,
4035 s->for_subst_vec[i].second->id);
4036 else
4037 gcc_unreachable ();
4039 gen_1 (f, indent, gimple, s->result);
4042 indent -= 2;
4043 fprintf_indent (f, indent, "}\n");
4047 /* Hash function for finding equivalent transforms. */
4049 hashval_t
4050 sinfo_hashmap_traits::hash (const key_type &v)
4052 /* Only bother to compare those originating from the same source pattern. */
4053 return v->s->result->location;
4056 /* Compare function for finding equivalent transforms. */
4058 static bool
4059 compare_op (operand *o1, simplify *s1, operand *o2, simplify *s2)
4061 if (o1->type != o2->type)
4062 return false;
4064 switch (o1->type)
4066 case operand::OP_IF:
4068 if_expr *if1 = as_a <if_expr *> (o1);
4069 if_expr *if2 = as_a <if_expr *> (o2);
4070 /* ??? Properly compare c-exprs. */
4071 if (if1->cond != if2->cond)
4072 return false;
4073 if (!compare_op (if1->trueexpr, s1, if2->trueexpr, s2))
4074 return false;
4075 if (if1->falseexpr != if2->falseexpr
4076 || (if1->falseexpr
4077 && !compare_op (if1->falseexpr, s1, if2->falseexpr, s2)))
4078 return false;
4079 return true;
4081 case operand::OP_WITH:
4083 with_expr *with1 = as_a <with_expr *> (o1);
4084 with_expr *with2 = as_a <with_expr *> (o2);
4085 if (with1->with != with2->with)
4086 return false;
4087 return compare_op (with1->subexpr, s1, with2->subexpr, s2);
4089 default:;
4092 /* We've hit a result. Time to compare capture-infos - this is required
4093 in addition to the conservative pointer-equivalency of the result IL. */
4094 capture_info cinfo1 (s1, o1, true);
4095 capture_info cinfo2 (s2, o2, true);
4097 if (cinfo1.force_no_side_effects != cinfo2.force_no_side_effects
4098 || cinfo1.info.length () != cinfo2.info.length ())
4099 return false;
4101 for (unsigned i = 0; i < cinfo1.info.length (); ++i)
4103 if (cinfo1.info[i].expr_p != cinfo2.info[i].expr_p
4104 || cinfo1.info[i].cse_p != cinfo2.info[i].cse_p
4105 || (cinfo1.info[i].force_no_side_effects_p
4106 != cinfo2.info[i].force_no_side_effects_p)
4107 || cinfo1.info[i].force_single_use != cinfo2.info[i].force_single_use
4108 || cinfo1.info[i].cond_expr_cond_p != cinfo2.info[i].cond_expr_cond_p
4109 /* toplevel_msk is an optimization */
4110 || cinfo1.info[i].result_use_count != cinfo2.info[i].result_use_count
4111 || cinfo1.info[i].same_as != cinfo2.info[i].same_as
4112 /* the pointer back to the capture is for diagnostics only */)
4113 return false;
4116 /* ??? Deep-compare the actual result. */
4117 return o1 == o2;
4120 bool
4121 sinfo_hashmap_traits::equal_keys (const key_type &v,
4122 const key_type &candidate)
4124 return compare_op (v->s->result, v->s, candidate->s->result, candidate->s);
4128 /* Main entry to generate code for matching GIMPLE IL off the decision
4129 tree. */
4131 void
4132 decision_tree::gen (vec <FILE *> &files, bool gimple)
4134 sinfo_map_t si;
4136 root->analyze (si);
4138 fprintf (stderr, "%s decision tree has %u leafs, maximum depth %u and "
4139 "a total number of %u nodes\n",
4140 gimple ? "GIMPLE" : "GENERIC",
4141 root->num_leafs, root->max_level, root->total_size);
4143 /* First split out the transform part of equal leafs. */
4144 unsigned rcnt = 0;
4145 unsigned fcnt = 1;
4146 for (sinfo_map_t::iterator iter = si.begin ();
4147 iter != si.end (); ++iter)
4149 sinfo *s = (*iter).second;
4150 /* Do not split out single uses. */
4151 if (s->cnt <= 1)
4152 continue;
4154 rcnt += s->cnt - 1;
4155 if (verbose >= 1)
4157 fprintf (stderr, "found %u uses of", s->cnt);
4158 output_line_directive (stderr, s->s->s->result->location);
4161 /* Cycle the file buffers. */
4162 FILE *f = choose_output (files);
4164 /* Generate a split out function with the leaf transform code. */
4165 s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic",
4166 fcnt++);
4167 if (gimple)
4168 fp_decl (f, "\nbool\n"
4169 "%s (gimple_match_op *res_op, gimple_seq *seq,\n"
4170 " tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
4171 " const tree ARG_UNUSED (type), tree *ARG_UNUSED "
4172 "(captures)",
4173 s->fname);
4174 else
4176 fp_decl (f, "\ntree\n"
4177 "%s (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type),\n",
4178 (*iter).second->fname);
4179 for (unsigned i = 0;
4180 i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
4181 fp_decl (f, " tree ARG_UNUSED (_p%d),", i);
4182 fp_decl (f, " tree *ARG_UNUSED (captures)");
4184 for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
4186 if (! s->s->s->for_subst_vec[i].first->used)
4187 continue;
4188 if (is_a <operator_id *> (s->s->s->for_subst_vec[i].second))
4189 fp_decl (f, ",\n const enum tree_code ARG_UNUSED (%s)",
4190 s->s->s->for_subst_vec[i].first->id);
4191 else if (is_a <fn_id *> (s->s->s->for_subst_vec[i].second))
4192 fp_decl (f, ",\n const combined_fn ARG_UNUSED (%s)",
4193 s->s->s->for_subst_vec[i].first->id);
4196 fp_decl_done (f, ")");
4197 fprintf (f, "{\n");
4198 fprintf_indent (f, 2, "const bool debug_dump = "
4199 "dump_file && (dump_flags & TDF_FOLDING);\n");
4200 s->s->gen_1 (f, 2, gimple, s->s->s->result);
4201 if (gimple)
4202 fprintf (f, " return false;\n");
4203 else
4204 fprintf (f, " return NULL_TREE;\n");
4205 fprintf (f, "}\n");
4207 fprintf (stderr, "removed %u duplicate tails\n", rcnt);
4209 for (unsigned n = 1; n <= 7; ++n)
4211 bool has_kids_p = false;
4213 /* First generate split-out functions. */
4214 for (unsigned j = 0; j < root->kids.length (); j++)
4216 dt_operand *dop = static_cast<dt_operand *>(root->kids[j]);
4217 expr *e = static_cast<expr *>(dop->op);
4218 if (e->ops.length () != n
4219 /* Builtin simplifications are somewhat premature on
4220 GENERIC. The following drops patterns with outermost
4221 calls. It's easy to emit overloads for function code
4222 though if necessary. */
4223 || (!gimple
4224 && e->operation->kind != id_base::CODE))
4225 continue;
4228 /* Cycle the file buffers. */
4229 FILE *f = choose_output (files);
4231 if (gimple)
4232 fp_decl (f, "\nbool\n"
4233 "gimple_simplify_%s (gimple_match_op *res_op,"
4234 " gimple_seq *seq,\n"
4235 " tree (*valueize)(tree) "
4236 "ATTRIBUTE_UNUSED,\n"
4237 " code_helper ARG_UNUSED (code), tree "
4238 "ARG_UNUSED (type)",
4239 e->operation->id);
4240 else
4241 fp_decl (f, "\ntree\n"
4242 "generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
4243 "tree_code ARG_UNUSED (code), const tree ARG_UNUSED (type)",
4244 e->operation->id);
4245 for (unsigned i = 0; i < n; ++i)
4246 fp_decl (f, ", tree _p%d", i);
4247 fp_decl_done (f, ")");
4248 fprintf (f, "{\n");
4249 fprintf_indent (f, 2, "const bool debug_dump = "
4250 "dump_file && (dump_flags & TDF_FOLDING);\n");
4251 dop->gen_kids (f, 2, gimple, 0);
4252 if (gimple)
4253 fprintf (f, " return false;\n");
4254 else
4255 fprintf (f, " return NULL_TREE;\n");
4256 fprintf (f, "}\n");
4257 has_kids_p = true;
4260 /* If this main entry has no children, avoid generating code
4261 with compiler warnings, by generating a simple stub. */
4262 if (! has_kids_p)
4265 /* Cycle the file buffers. */
4266 FILE *f = choose_output (files);
4268 if (gimple)
4269 fp_decl (f, "\nbool\n"
4270 "gimple_simplify (gimple_match_op*, gimple_seq*,\n"
4271 " tree (*)(tree), code_helper,\n"
4272 " const tree");
4273 else
4274 fp_decl (f, "\ntree\n"
4275 "generic_simplify (location_t, enum tree_code,\n"
4276 " const tree");
4277 for (unsigned i = 0; i < n; ++i)
4278 fp_decl (f, ", tree");
4279 fp_decl_done (f, ")");
4280 fprintf (f, "{\n");
4281 if (gimple)
4282 fprintf (f, " return false;\n");
4283 else
4284 fprintf (f, " return NULL_TREE;\n");
4285 fprintf (f, "}\n");
4286 continue;
4290 /* Cycle the file buffers. */
4291 FILE *f = choose_output (files);
4293 /* Then generate the main entry with the outermost switch and
4294 tail-calls to the split-out functions. */
4295 if (gimple)
4296 fp_decl (f, "\nbool\n"
4297 "gimple_simplify (gimple_match_op *res_op, gimple_seq *seq,\n"
4298 " tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
4299 " code_helper code, const tree type");
4300 else
4301 fp_decl (f, "\ntree\n"
4302 "generic_simplify (location_t loc, enum tree_code code, "
4303 "const tree type ATTRIBUTE_UNUSED");
4304 for (unsigned i = 0; i < n; ++i)
4305 fp_decl (f, ", tree _p%d", i);
4306 fp_decl_done (f, ")");
4307 fprintf (f, "{\n");
4309 if (gimple)
4310 fprintf (f, " switch (code.get_rep())\n"
4311 " {\n");
4312 else
4313 fprintf (f, " switch (code)\n"
4314 " {\n");
4315 for (unsigned i = 0; i < root->kids.length (); i++)
4317 dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
4318 expr *e = static_cast<expr *>(dop->op);
4319 if (e->ops.length () != n
4320 /* Builtin simplifications are somewhat premature on
4321 GENERIC. The following drops patterns with outermost
4322 calls. It's easy to emit overloads for function code
4323 though if necessary. */
4324 || (!gimple
4325 && e->operation->kind != id_base::CODE))
4326 continue;
4328 if (*e->operation == CONVERT_EXPR
4329 || *e->operation == NOP_EXPR)
4330 fprintf (f, " CASE_CONVERT:\n");
4331 else
4332 fprintf (f, " case %s%s:\n",
4333 is_a <fn_id *> (e->operation) ? "-" : "",
4334 e->operation->id);
4335 if (gimple)
4336 fprintf (f, " return gimple_simplify_%s (res_op, "
4337 "seq, valueize, code, type", e->operation->id);
4338 else
4339 fprintf (f, " return generic_simplify_%s (loc, code, type",
4340 e->operation->id);
4341 for (unsigned j = 0; j < n; ++j)
4342 fprintf (f, ", _p%d", j);
4343 fprintf (f, ");\n");
4345 fprintf (f, " default:;\n"
4346 " }\n");
4348 if (gimple)
4349 fprintf (f, " return false;\n");
4350 else
4351 fprintf (f, " return NULL_TREE;\n");
4352 fprintf (f, "}\n");
4356 /* Output code to implement the predicate P from the decision tree DT. */
4358 void
4359 write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
4361 fp_decl (f, "\nbool\n%s%s (tree t%s%s)",
4362 gimple ? "gimple_" : "tree_", p->id,
4363 p->nargs > 0 ? ", tree *res_ops" : "",
4364 gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
4365 fp_decl_done (f, "");
4366 fprintf (f, "{\n");
4367 /* Conveniently make 'type' available. */
4368 fprintf_indent (f, 2, "const tree type = TREE_TYPE (t);\n");
4369 fprintf_indent (f, 2, "const bool debug_dump = "
4370 "dump_file && (dump_flags & TDF_FOLDING);\n");
4372 if (!gimple)
4373 fprintf_indent (f, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
4374 dt.root->gen_kids (f, 2, gimple, 0);
4376 fprintf_indent (f, 2, "return false;\n"
4377 "}\n");
4380 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
4382 static void
4383 write_header (FILE *f, const char *head)
4385 fprintf (f, "/* Generated automatically by the program `genmatch' from\n");
4386 fprintf (f, " a IL pattern matching and simplification description. */\n");
4387 fprintf (f, "#pragma GCC diagnostic push\n");
4388 fprintf (f, "#pragma GCC diagnostic ignored \"-Wunused-variable\"\n");
4389 fprintf (f, "#pragma GCC diagnostic ignored \"-Wunused-function\"\n");
4391 /* Include the header instead of writing it awkwardly quoted here. */
4392 if (head)
4393 fprintf (f, "\n#include \"%s\"\n", head);
4398 /* AST parsing. */
4400 class parser
4402 public:
4403 parser (cpp_reader *, bool gimple);
4405 private:
4406 const cpp_token *next ();
4407 const cpp_token *peek (unsigned = 1);
4408 const cpp_token *peek_ident (const char * = NULL, unsigned = 1);
4409 const cpp_token *expect (enum cpp_ttype);
4410 const cpp_token *eat_token (enum cpp_ttype);
4411 const char *get_string ();
4412 const char *get_ident ();
4413 const cpp_token *eat_ident (const char *);
4414 const char *get_number ();
4416 unsigned get_internal_capture_id ();
4418 id_base *parse_operation (unsigned char &);
4419 operand *parse_capture (operand *, bool);
4420 operand *parse_expr ();
4421 c_expr *parse_c_expr (cpp_ttype);
4422 operand *parse_op ();
4424 void record_operlist (location_t, user_id *);
4426 void parse_pattern ();
4427 operand *parse_result (operand *, predicate_id *);
4428 void push_simplify (simplify::simplify_kind,
4429 vec<simplify *>&, operand *, operand *);
4430 void parse_simplify (simplify::simplify_kind,
4431 vec<simplify *>&, predicate_id *, operand *);
4432 void parse_for (location_t);
4433 void parse_if (location_t);
4434 void parse_predicates (location_t);
4435 void parse_operator_list (location_t);
4437 void finish_match_operand (operand *);
4439 cpp_reader *r;
4440 bool gimple;
4441 vec<c_expr *> active_ifs;
4442 vec<vec<user_id *> > active_fors;
4443 hash_set<user_id *> *oper_lists_set;
4444 vec<user_id *> oper_lists;
4446 cid_map_t *capture_ids;
4447 unsigned last_id;
4449 public:
4450 vec<simplify *> simplifiers;
4451 vec<predicate_id *> user_predicates;
4452 bool parsing_match_operand;
4455 /* Lexing helpers. */
4457 /* Read the next non-whitespace token from R. */
4459 const cpp_token *
4460 parser::next ()
4462 const cpp_token *token;
4465 token = cpp_get_token (r);
4467 while (token->type == CPP_PADDING);
4468 return token;
4471 /* Peek at the next non-whitespace token from R. */
4473 const cpp_token *
4474 parser::peek (unsigned num)
4476 const cpp_token *token;
4477 unsigned i = 0;
4480 token = cpp_peek_token (r, i++);
4482 while (token->type == CPP_PADDING
4483 || (--num > 0));
4484 /* If we peek at EOF this is a fatal error as it leaves the
4485 cpp_reader in unusable state. Assume we really wanted a
4486 token and thus this EOF is unexpected. */
4487 if (token->type == CPP_EOF)
4488 fatal_at (token, "unexpected end of file");
4489 return token;
4492 /* Peek at the next identifier token (or return NULL if the next
4493 token is not an identifier or equal to ID if supplied). */
4495 const cpp_token *
4496 parser::peek_ident (const char *id, unsigned num)
4498 const cpp_token *token = peek (num);
4499 if (token->type != CPP_NAME)
4500 return 0;
4502 if (id == 0)
4503 return token;
4505 const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
4506 if (strcmp (id, t) == 0)
4507 return token;
4509 return 0;
4512 /* Read the next token from R and assert it is of type TK. */
4514 const cpp_token *
4515 parser::expect (enum cpp_ttype tk)
4517 const cpp_token *token = next ();
4518 if (token->type != tk)
4519 fatal_at (token, "expected %s, got %s",
4520 cpp_type2name (tk, 0), cpp_type2name (token->type, 0));
4522 return token;
4525 /* Consume the next token from R and assert it is of type TK. */
4527 const cpp_token *
4528 parser::eat_token (enum cpp_ttype tk)
4530 return expect (tk);
4533 /* Read the next token from R and assert it is of type CPP_STRING and
4534 return its value. */
4536 const char *
4537 parser::get_string ()
4539 const cpp_token *token = expect (CPP_STRING);
4540 return (const char *)token->val.str.text;
4543 /* Read the next token from R and assert it is of type CPP_NAME and
4544 return its value. */
4546 const char *
4547 parser::get_ident ()
4549 const cpp_token *token = expect (CPP_NAME);
4550 return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
4553 /* Eat an identifier token with value S from R. */
4555 const cpp_token *
4556 parser::eat_ident (const char *s)
4558 const cpp_token *token = peek ();
4559 const char *t = get_ident ();
4560 if (strcmp (s, t) != 0)
4561 fatal_at (token, "expected '%s' got '%s'\n", s, t);
4562 return token;
4565 /* Read the next token from R and assert it is of type CPP_NUMBER and
4566 return its value. */
4568 const char *
4569 parser::get_number ()
4571 const cpp_token *token = expect (CPP_NUMBER);
4572 return (const char *)token->val.str.text;
4575 /* Return a capture ID that can be used internally. */
4577 unsigned
4578 parser::get_internal_capture_id ()
4580 unsigned newid = capture_ids->elements ();
4581 /* Big enough for a 32-bit UINT_MAX plus prefix. */
4582 char id[13];
4583 bool existed;
4584 snprintf (id, sizeof (id), "__%u", newid);
4585 capture_ids->get_or_insert (xstrdup (id), &existed);
4586 if (existed)
4587 fatal ("reserved capture id '%s' already used", id);
4588 return newid;
4591 /* Record an operator-list use for transparent for handling. */
4593 void
4594 parser::record_operlist (location_t loc, user_id *p)
4596 if (!oper_lists_set->add (p))
4598 if (!oper_lists.is_empty ()
4599 && oper_lists[0]->substitutes.length () != p->substitutes.length ())
4600 fatal_at (loc, "User-defined operator list does not have the "
4601 "same number of entries as others used in the pattern");
4602 oper_lists.safe_push (p);
4606 /* Parse the operator ID, special-casing convert?, convert1? and
4607 convert2? */
4609 id_base *
4610 parser::parse_operation (unsigned char &opt_grp)
4612 const cpp_token *id_tok = peek ();
4613 char *alt_id = NULL;
4614 const char *id = get_ident ();
4615 const cpp_token *token = peek ();
4616 opt_grp = 0;
4617 if (token->type == CPP_QUERY
4618 && !(token->flags & PREV_WHITE))
4620 if (!parsing_match_operand)
4621 fatal_at (id_tok, "conditional convert can only be used in "
4622 "match expression");
4623 if (ISDIGIT (id[strlen (id) - 1]))
4625 opt_grp = id[strlen (id) - 1] - '0' + 1;
4626 alt_id = xstrdup (id);
4627 alt_id[strlen (id) - 1] = '\0';
4628 if (opt_grp == 1)
4629 fatal_at (id_tok, "use '%s?' here", alt_id);
4631 else
4632 opt_grp = 1;
4633 eat_token (CPP_QUERY);
4635 id_base *op = get_operator (alt_id ? alt_id : id);
4636 if (!op)
4637 fatal_at (id_tok, "unknown operator %s", alt_id ? alt_id : id);
4638 if (alt_id)
4639 free (alt_id);
4640 user_id *p = dyn_cast<user_id *> (op);
4641 if (p && p->is_oper_list)
4643 if (active_fors.length() == 0)
4644 record_operlist (id_tok->src_loc, p);
4645 else
4646 fatal_at (id_tok, "operator-list %s cannot be expanded inside 'for'", id);
4648 return op;
4651 /* Parse a capture.
4652 capture = '@'<number> */
4654 class operand *
4655 parser::parse_capture (operand *op, bool require_existing)
4657 location_t src_loc = eat_token (CPP_ATSIGN)->src_loc;
4658 const cpp_token *token = peek ();
4659 const char *id = NULL;
4660 bool value_match = false;
4661 /* For matches parse @@ as a value-match denoting the prevailing operand. */
4662 if (token->type == CPP_ATSIGN
4663 && ! (token->flags & PREV_WHITE)
4664 && parsing_match_operand)
4666 eat_token (CPP_ATSIGN);
4667 token = peek ();
4668 value_match = true;
4670 if (token->type == CPP_NUMBER)
4671 id = get_number ();
4672 else if (token->type == CPP_NAME)
4673 id = get_ident ();
4674 else
4675 fatal_at (token, "expected number or identifier");
4676 unsigned next_id = capture_ids->elements ();
4677 bool existed;
4678 unsigned &num = capture_ids->get_or_insert (id, &existed);
4679 if (!existed)
4681 if (require_existing)
4682 fatal_at (src_loc, "unknown capture id");
4683 num = next_id;
4685 return new capture (src_loc, num, op, value_match);
4688 /* Parse an expression
4689 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
4691 class operand *
4692 parser::parse_expr ()
4694 const cpp_token *token = peek ();
4695 unsigned char opt_grp;
4696 expr *e = new expr (parse_operation (opt_grp), token->src_loc);
4697 token = peek ();
4698 operand *op;
4699 bool is_commutative = false;
4700 bool force_capture = false;
4701 const char *expr_type = NULL;
4703 if (!parsing_match_operand
4704 && token->type == CPP_NOT
4705 && !(token->flags & PREV_WHITE))
4707 eat_token (CPP_NOT);
4708 e->force_leaf = true;
4711 if (token->type == CPP_XOR && !(token->flags & PREV_WHITE))
4713 if (!parsing_match_operand)
4714 fatal_at (token, "modifier '^' is only valid in a match expression");
4716 if (!(*e->operation == COND_EXPR))
4717 fatal_at (token, "modifier '^' can only act on operation COND_EXPR");
4719 eat_token (CPP_XOR);
4720 e->match_phi = true;
4723 if (token->type == CPP_COLON
4724 && !(token->flags & PREV_WHITE))
4726 eat_token (CPP_COLON);
4727 token = peek ();
4728 if (token->type == CPP_NAME
4729 && !(token->flags & PREV_WHITE))
4731 const char *s = get_ident ();
4732 if (!parsing_match_operand)
4733 expr_type = s;
4734 else
4736 const char *sp = s;
4737 while (*sp)
4739 if (*sp == 'c')
4741 if (operator_id *o
4742 = dyn_cast<operator_id *> (e->operation))
4744 if (!commutative_tree_code (o->code)
4745 && !comparison_code_p (o->code))
4746 fatal_at (token, "operation is not commutative");
4748 else if (user_id *p = dyn_cast<user_id *> (e->operation))
4749 for (unsigned i = 0;
4750 i < p->substitutes.length (); ++i)
4752 if (operator_id *q
4753 = dyn_cast<operator_id *> (p->substitutes[i]))
4755 if (!commutative_tree_code (q->code)
4756 && !comparison_code_p (q->code))
4757 fatal_at (token, "operation %s is not "
4758 "commutative", q->id);
4761 is_commutative = true;
4763 else if (*sp == 'C')
4764 is_commutative = true;
4765 else if (*sp == 's')
4767 e->force_single_use = true;
4768 force_capture = true;
4770 else
4771 fatal_at (token, "flag %c not recognized", *sp);
4772 sp++;
4775 token = peek ();
4777 else
4778 fatal_at (token, "expected flag or type specifying identifier");
4781 if (token->type == CPP_ATSIGN
4782 && !(token->flags & PREV_WHITE))
4783 op = parse_capture (e, false);
4784 else if (force_capture)
4786 unsigned num = get_internal_capture_id ();
4787 op = new capture (token->src_loc, num, e, false);
4789 else
4790 op = e;
4793 token = peek ();
4794 if (token->type == CPP_CLOSE_PAREN)
4796 if (e->operation->nargs != -1
4797 && e->operation->nargs != (int) e->ops.length ())
4798 fatal_at (token, "'%s' expects %u operands, not %u",
4799 e->operation->id, e->operation->nargs, e->ops.length ());
4800 if (is_commutative)
4802 if (e->ops.length () == 2
4803 || commutative_op (e->operation) >= 0)
4804 e->is_commutative = true;
4805 else
4806 fatal_at (token, "only binary operators or functions with "
4807 "two arguments can be marked commutative, "
4808 "unless the operation is known to be inherently "
4809 "commutative");
4811 e->expr_type = expr_type;
4812 if (opt_grp != 0)
4814 if (e->ops.length () != 1)
4815 fatal_at (token, "only unary operations can be conditional");
4816 e->opt_grp = opt_grp;
4818 return op;
4820 else if (!(token->flags & PREV_WHITE))
4821 fatal_at (token, "expected expression operand");
4823 e->append_op (parse_op ());
4825 while (1);
4828 /* Lex native C code delimited by START recording the preprocessing tokens
4829 for later processing.
4830 c_expr = ('{'|'(') <pp token>... ('}'|')') */
4832 c_expr *
4833 parser::parse_c_expr (cpp_ttype start)
4835 const cpp_token *token;
4836 cpp_ttype end;
4837 unsigned opencnt;
4838 vec<cpp_token> code = vNULL;
4839 unsigned nr_stmts = 0;
4840 location_t loc = eat_token (start)->src_loc;
4841 if (start == CPP_OPEN_PAREN)
4842 end = CPP_CLOSE_PAREN;
4843 else if (start == CPP_OPEN_BRACE)
4844 end = CPP_CLOSE_BRACE;
4845 else
4846 gcc_unreachable ();
4847 opencnt = 1;
4850 token = next ();
4852 /* Count brace pairs to find the end of the expr to match. */
4853 if (token->type == start)
4854 opencnt++;
4855 else if (token->type == end
4856 && --opencnt == 0)
4857 break;
4858 else if (token->type == CPP_EOF)
4859 fatal_at (token, "unexpected end of file");
4861 /* This is a lame way of counting the number of statements. */
4862 if (token->type == CPP_SEMICOLON)
4863 nr_stmts++;
4865 /* If this is possibly a user-defined identifier mark it used. */
4866 if (token->type == CPP_NAME)
4868 const char *str
4869 = (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
4870 if (strcmp (str, "return") == 0)
4871 fatal_at (token, "return statement not allowed in C expression");
4872 /* Mark user operators corresponding to 'str' as used. */
4873 get_operator (str);
4876 /* Record the token. */
4877 code.safe_push (*token);
4879 while (1);
4880 return new c_expr (r, loc, code, nr_stmts, vNULL, capture_ids);
4883 /* Parse an operand which is either an expression, a predicate or
4884 a standalone capture.
4885 op = predicate | expr | c_expr | capture */
4887 class operand *
4888 parser::parse_op ()
4890 const cpp_token *token = peek ();
4891 class operand *op = NULL;
4892 if (token->type == CPP_OPEN_PAREN)
4894 eat_token (CPP_OPEN_PAREN);
4895 op = parse_expr ();
4896 eat_token (CPP_CLOSE_PAREN);
4898 else if (token->type == CPP_OPEN_BRACE)
4900 op = parse_c_expr (CPP_OPEN_BRACE);
4902 else
4904 /* Remaining ops are either empty or predicates */
4905 if (token->type == CPP_NAME)
4907 const char *id = get_ident ();
4908 id_base *opr = get_operator (id);
4909 if (!opr)
4910 fatal_at (token, "expected predicate name");
4911 if (operator_id *code1 = dyn_cast <operator_id *> (opr))
4913 if (code1->nargs != 0)
4914 fatal_at (token, "using an operator with operands as predicate");
4915 /* Parse the zero-operand operator "predicates" as
4916 expression. */
4917 op = new expr (opr, token->src_loc);
4919 else if (user_id *code2 = dyn_cast <user_id *> (opr))
4921 if (code2->nargs != 0)
4922 fatal_at (token, "using an operator with operands as predicate");
4923 /* Parse the zero-operand operator "predicates" as
4924 expression. */
4925 op = new expr (opr, token->src_loc);
4927 else if (predicate_id *p = dyn_cast <predicate_id *> (opr))
4928 op = new predicate (p, token->src_loc);
4929 else
4930 fatal_at (token, "using an unsupported operator as predicate");
4931 if (!parsing_match_operand)
4932 fatal_at (token, "predicates are only allowed in match expression");
4933 token = peek ();
4934 if (token->flags & PREV_WHITE)
4935 return op;
4937 else if (token->type != CPP_COLON
4938 && token->type != CPP_ATSIGN)
4939 fatal_at (token, "expected expression or predicate");
4940 /* optionally followed by a capture and a predicate. */
4941 if (token->type == CPP_COLON)
4942 fatal_at (token, "not implemented: predicate on leaf operand");
4943 if (token->type == CPP_ATSIGN)
4944 op = parse_capture (op, !parsing_match_operand);
4947 return op;
4950 /* Create a new simplify from the current parsing state and MATCH,
4951 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
4953 void
4954 parser::push_simplify (simplify::simplify_kind kind,
4955 vec<simplify *>& simplifiers,
4956 operand *match, operand *result)
4958 /* Build and push a temporary for operator list uses in expressions. */
4959 if (!oper_lists.is_empty ())
4960 active_fors.safe_push (oper_lists);
4962 simplifiers.safe_push
4963 (new simplify (kind, last_id++, match, result,
4964 active_fors.copy (), capture_ids));
4966 if (!oper_lists.is_empty ())
4967 active_fors.pop ();
4970 /* Parse
4971 <result-op> = <op> | <if> | <with>
4972 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
4973 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
4974 and return it. */
4976 operand *
4977 parser::parse_result (operand *result, predicate_id *matcher)
4979 const cpp_token *token = peek ();
4980 if (token->type != CPP_OPEN_PAREN)
4981 return parse_op ();
4983 eat_token (CPP_OPEN_PAREN);
4984 if (peek_ident ("if"))
4986 eat_ident ("if");
4987 if_expr *ife = new if_expr (token->src_loc);
4988 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4989 if (peek ()->type == CPP_OPEN_PAREN)
4991 ife->trueexpr = parse_result (result, matcher);
4992 if (peek ()->type == CPP_OPEN_PAREN)
4993 ife->falseexpr = parse_result (result, matcher);
4994 else if (peek ()->type != CPP_CLOSE_PAREN)
4995 ife->falseexpr = parse_op ();
4997 else if (peek ()->type != CPP_CLOSE_PAREN)
4999 ife->trueexpr = parse_op ();
5000 if (peek ()->type == CPP_OPEN_PAREN)
5001 ife->falseexpr = parse_result (result, matcher);
5002 else if (peek ()->type != CPP_CLOSE_PAREN)
5003 ife->falseexpr = parse_op ();
5005 /* If this if is immediately closed then it contains a
5006 manual matcher or is part of a predicate definition. */
5007 else /* if (peek ()->type == CPP_CLOSE_PAREN) */
5009 if (!matcher)
5010 fatal_at (peek (), "manual transform not implemented");
5011 ife->trueexpr = result;
5013 eat_token (CPP_CLOSE_PAREN);
5014 return ife;
5016 else if (peek_ident ("with"))
5018 eat_ident ("with");
5019 with_expr *withe = new with_expr (token->src_loc);
5020 /* Parse (with c-expr expr) as (if-with (true) expr). */
5021 withe->with = parse_c_expr (CPP_OPEN_BRACE);
5022 withe->with->nr_stmts = 0;
5023 withe->subexpr = parse_result (result, matcher);
5024 eat_token (CPP_CLOSE_PAREN);
5025 return withe;
5027 else if (peek_ident ("switch"))
5029 token = eat_ident ("switch");
5030 location_t ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
5031 eat_ident ("if");
5032 if_expr *ife = new if_expr (ifloc);
5033 operand *res = ife;
5034 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
5035 if (peek ()->type == CPP_OPEN_PAREN)
5036 ife->trueexpr = parse_result (result, matcher);
5037 else
5038 ife->trueexpr = parse_op ();
5039 eat_token (CPP_CLOSE_PAREN);
5040 if (peek ()->type != CPP_OPEN_PAREN
5041 || !peek_ident ("if", 2))
5042 fatal_at (token, "switch can be implemented with a single if");
5043 while (peek ()->type != CPP_CLOSE_PAREN)
5045 if (peek ()->type == CPP_OPEN_PAREN)
5047 if (peek_ident ("if", 2))
5049 ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
5050 eat_ident ("if");
5051 ife->falseexpr = new if_expr (ifloc);
5052 ife = as_a <if_expr *> (ife->falseexpr);
5053 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
5054 if (peek ()->type == CPP_OPEN_PAREN)
5055 ife->trueexpr = parse_result (result, matcher);
5056 else
5057 ife->trueexpr = parse_op ();
5058 if (peek ()->type == CPP_OPEN_PAREN)
5059 fatal_at (peek(), "if inside switch cannot have an else");
5060 eat_token (CPP_CLOSE_PAREN);
5062 else
5064 /* switch default clause */
5065 ife->falseexpr = parse_result (result, matcher);
5066 eat_token (CPP_CLOSE_PAREN);
5067 return res;
5070 else
5072 /* switch default clause */
5073 ife->falseexpr = parse_op ();
5074 eat_token (CPP_CLOSE_PAREN);
5075 return res;
5078 eat_token (CPP_CLOSE_PAREN);
5079 return res;
5081 else
5083 operand *op = result;
5084 if (!matcher)
5085 op = parse_expr ();
5086 eat_token (CPP_CLOSE_PAREN);
5087 return op;
5091 /* Parse
5092 simplify = 'simplify' <expr> <result-op>
5094 match = 'match' <ident> <expr> [<result-op>]
5095 and fill SIMPLIFIERS with the results. */
5097 void
5098 parser::parse_simplify (simplify::simplify_kind kind,
5099 vec<simplify *>& simplifiers, predicate_id *matcher,
5100 operand *result)
5102 /* Reset the capture map. */
5103 if (!capture_ids)
5104 capture_ids = new cid_map_t;
5105 /* Reset oper_lists and set. */
5106 hash_set <user_id *> olist;
5107 oper_lists_set = &olist;
5108 oper_lists = vNULL;
5110 const cpp_token *loc = peek ();
5111 parsing_match_operand = true;
5112 class operand *match = parse_op ();
5113 finish_match_operand (match);
5114 parsing_match_operand = false;
5115 if (match->type == operand::OP_CAPTURE && !matcher)
5116 fatal_at (loc, "outermost expression cannot be captured");
5117 if (match->type == operand::OP_EXPR
5118 && is_a <predicate_id *> (as_a <expr *> (match)->operation))
5119 fatal_at (loc, "outermost expression cannot be a predicate");
5121 /* Splice active_ifs onto result and continue parsing the
5122 "then" expr. */
5123 if_expr *active_if = NULL;
5124 for (int i = active_ifs.length (); i > 0; --i)
5126 if_expr *ifc = new if_expr (active_ifs[i-1]->location);
5127 ifc->cond = active_ifs[i-1];
5128 ifc->trueexpr = active_if;
5129 active_if = ifc;
5131 if_expr *outermost_if = active_if;
5132 while (active_if && active_if->trueexpr)
5133 active_if = as_a <if_expr *> (active_if->trueexpr);
5135 const cpp_token *token = peek ();
5137 /* If this if is immediately closed then it is part of a predicate
5138 definition. Push it. */
5139 if (token->type == CPP_CLOSE_PAREN)
5141 if (!matcher)
5142 fatal_at (token, "expected transform expression");
5143 if (active_if)
5145 active_if->trueexpr = result;
5146 result = outermost_if;
5148 push_simplify (kind, simplifiers, match, result);
5149 return;
5152 operand *tem = parse_result (result, matcher);
5153 if (active_if)
5155 active_if->trueexpr = tem;
5156 result = outermost_if;
5158 else
5159 result = tem;
5161 push_simplify (kind, simplifiers, match, result);
5164 /* Parsing of the outer control structures. */
5166 /* Parse a for expression
5167 for = '(' 'for' <subst>... <pattern> ')'
5168 subst = <ident> '(' <ident>... ')' */
5170 void
5171 parser::parse_for (location_t)
5173 auto_vec<const cpp_token *> user_id_tokens;
5174 vec<user_id *> user_ids = vNULL;
5175 const cpp_token *token;
5176 unsigned min_n_opers = 0, max_n_opers = 0;
5178 while (1)
5180 token = peek ();
5181 if (token->type != CPP_NAME)
5182 break;
5184 /* Insert the user defined operators into the operator hash. */
5185 const char *id = get_ident ();
5186 if (get_operator (id, true) != NULL)
5187 fatal_at (token, "operator already defined");
5188 user_id *op = new user_id (id);
5189 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
5190 *slot = op;
5191 user_ids.safe_push (op);
5192 user_id_tokens.safe_push (token);
5194 eat_token (CPP_OPEN_PAREN);
5196 int arity = -1;
5197 while ((token = peek_ident ()) != 0)
5199 const char *oper = get_ident ();
5200 id_base *idb = get_operator (oper, true);
5201 if (idb == NULL)
5202 fatal_at (token, "no such operator '%s'", oper);
5204 if (arity == -1)
5205 arity = idb->nargs;
5206 else if (idb->nargs == -1)
5208 else if (idb->nargs != arity)
5209 fatal_at (token, "operator '%s' with arity %d does not match "
5210 "others with arity %d", oper, idb->nargs, arity);
5212 user_id *p = dyn_cast<user_id *> (idb);
5213 if (p)
5215 if (p->is_oper_list)
5216 op->substitutes.safe_splice (p->substitutes);
5217 else
5218 fatal_at (token, "iterator cannot be used as operator-list");
5220 else
5221 op->substitutes.safe_push (idb);
5223 op->nargs = arity;
5224 token = expect (CPP_CLOSE_PAREN);
5226 unsigned nsubstitutes = op->substitutes.length ();
5227 if (nsubstitutes == 0)
5228 fatal_at (token, "A user-defined operator must have at least "
5229 "one substitution");
5230 if (max_n_opers == 0)
5232 min_n_opers = nsubstitutes;
5233 max_n_opers = nsubstitutes;
5235 else
5237 if (nsubstitutes % min_n_opers != 0
5238 && min_n_opers % nsubstitutes != 0)
5239 fatal_at (token, "All user-defined identifiers must have a "
5240 "multiple number of operator substitutions of the "
5241 "smallest number of substitutions");
5242 if (nsubstitutes < min_n_opers)
5243 min_n_opers = nsubstitutes;
5244 else if (nsubstitutes > max_n_opers)
5245 max_n_opers = nsubstitutes;
5249 unsigned n_ids = user_ids.length ();
5250 if (n_ids == 0)
5251 fatal_at (token, "for requires at least one user-defined identifier");
5253 token = peek ();
5254 if (token->type == CPP_CLOSE_PAREN)
5255 fatal_at (token, "no pattern defined in for");
5257 active_fors.safe_push (user_ids);
5258 while (1)
5260 token = peek ();
5261 if (token->type == CPP_CLOSE_PAREN)
5262 break;
5263 parse_pattern ();
5265 active_fors.pop ();
5267 /* Remove user-defined operators from the hash again. */
5268 for (unsigned i = 0; i < user_ids.length (); ++i)
5270 if (!user_ids[i]->used)
5271 warning_at (user_id_tokens[i],
5272 "operator %s defined but not used", user_ids[i]->id);
5273 operators->remove_elt (user_ids[i]);
5277 /* Parse an identifier associated with a list of operators.
5278 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
5280 void
5281 parser::parse_operator_list (location_t)
5283 const cpp_token *token = peek ();
5284 const char *id = get_ident ();
5286 if (get_operator (id, true) != 0)
5287 fatal_at (token, "operator %s already defined", id);
5289 user_id *op = new user_id (id, true);
5290 int arity = -1;
5292 while ((token = peek_ident ()) != 0)
5294 token = peek ();
5295 const char *oper = get_ident ();
5296 id_base *idb = get_operator (oper, true);
5298 if (idb == 0)
5299 fatal_at (token, "no such operator '%s'", oper);
5301 if (arity == -1)
5302 arity = idb->nargs;
5303 else if (idb->nargs == -1)
5305 else if (arity != idb->nargs)
5306 fatal_at (token, "operator '%s' with arity %d does not match "
5307 "others with arity %d", oper, idb->nargs, arity);
5309 /* We allow composition of multiple operator lists. */
5310 if (user_id *p = dyn_cast<user_id *> (idb))
5311 op->substitutes.safe_splice (p->substitutes);
5312 else
5313 op->substitutes.safe_push (idb);
5316 // Check that there is no junk after id-list
5317 token = peek();
5318 if (token->type != CPP_CLOSE_PAREN)
5319 fatal_at (token, "expected identifier got %s", cpp_type2name (token->type, 0));
5321 if (op->substitutes.length () == 0)
5322 fatal_at (token, "operator-list cannot be empty");
5324 op->nargs = arity;
5325 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
5326 *slot = op;
5329 /* Parse an outer if expression.
5330 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
5332 void
5333 parser::parse_if (location_t)
5335 c_expr *ifexpr = parse_c_expr (CPP_OPEN_PAREN);
5337 const cpp_token *token = peek ();
5338 if (token->type == CPP_CLOSE_PAREN)
5339 fatal_at (token, "no pattern defined in if");
5341 active_ifs.safe_push (ifexpr);
5342 while (1)
5344 token = peek ();
5345 if (token->type == CPP_CLOSE_PAREN)
5346 break;
5348 parse_pattern ();
5350 active_ifs.pop ();
5353 /* Parse a list of predefined predicate identifiers.
5354 preds = '(' 'define_predicates' <ident>... ')' */
5356 void
5357 parser::parse_predicates (location_t)
5361 const cpp_token *token = peek ();
5362 if (token->type != CPP_NAME)
5363 break;
5365 add_predicate (get_ident ());
5367 while (1);
5370 /* Parse outer control structures.
5371 pattern = <preds>|<for>|<if>|<simplify>|<match> */
5373 void
5374 parser::parse_pattern ()
5376 /* All clauses start with '('. */
5377 eat_token (CPP_OPEN_PAREN);
5378 const cpp_token *token = peek ();
5379 const char *id = get_ident ();
5380 if (strcmp (id, "simplify") == 0)
5382 parse_simplify (simplify::SIMPLIFY, simplifiers, NULL, NULL);
5383 capture_ids = NULL;
5385 else if (strcmp (id, "match") == 0)
5387 bool with_args = false;
5388 location_t e_loc = peek ()->src_loc;
5389 if (peek ()->type == CPP_OPEN_PAREN)
5391 eat_token (CPP_OPEN_PAREN);
5392 with_args = true;
5394 const char *name = get_ident ();
5395 id_base *id1 = get_operator (name);
5396 predicate_id *p;
5397 if (!id1)
5399 p = add_predicate (name);
5400 user_predicates.safe_push (p);
5402 else if ((p = dyn_cast <predicate_id *> (id1)))
5404 else
5405 fatal_at (token, "cannot add a match to a non-predicate ID");
5406 /* Parse (match <id> <arg>... (match-expr)) here. */
5407 expr *e = NULL;
5408 if (with_args)
5410 capture_ids = new cid_map_t;
5411 e = new expr (p, e_loc);
5412 while (peek ()->type == CPP_ATSIGN)
5413 e->append_op (parse_capture (NULL, false));
5414 eat_token (CPP_CLOSE_PAREN);
5416 if (p->nargs != -1
5417 && ((e && e->ops.length () != (unsigned)p->nargs)
5418 || (!e && p->nargs != 0)))
5419 fatal_at (token, "non-matching number of match operands");
5420 p->nargs = e ? e->ops.length () : 0;
5421 parse_simplify (simplify::MATCH, p->matchers, p, e);
5422 capture_ids = NULL;
5424 else if (strcmp (id, "for") == 0)
5425 parse_for (token->src_loc);
5426 else if (strcmp (id, "if") == 0)
5427 parse_if (token->src_loc);
5428 else if (strcmp (id, "define_predicates") == 0)
5430 if (active_ifs.length () > 0
5431 || active_fors.length () > 0)
5432 fatal_at (token, "define_predicates inside if or for is not supported");
5433 parse_predicates (token->src_loc);
5435 else if (strcmp (id, "define_operator_list") == 0)
5437 if (active_ifs.length () > 0
5438 || active_fors.length () > 0)
5439 fatal_at (token, "operator-list inside if or for is not supported");
5440 parse_operator_list (token->src_loc);
5442 else
5443 fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'",
5444 active_ifs.length () == 0 && active_fors.length () == 0
5445 ? "'define_predicates', " : "");
5447 eat_token (CPP_CLOSE_PAREN);
5450 /* Helper for finish_match_operand, collecting captures of OP in CPTS
5451 recursively. */
5453 static void
5454 walk_captures (operand *op, vec<vec<capture *> > &cpts)
5456 if (! op)
5457 return;
5459 if (capture *c = dyn_cast <capture *> (op))
5461 cpts[c->where].safe_push (c);
5462 walk_captures (c->what, cpts);
5464 else if (expr *e = dyn_cast <expr *> (op))
5465 for (unsigned i = 0; i < e->ops.length (); ++i)
5466 walk_captures (e->ops[i], cpts);
5469 /* Finish up OP which is a match operand. */
5471 void
5472 parser::finish_match_operand (operand *op)
5474 /* Look for matching captures, diagnose mis-uses of @@ and apply
5475 early lowering and distribution of value_match. */
5476 auto_vec<vec<capture *> > cpts;
5477 cpts.safe_grow_cleared (capture_ids->elements (), true);
5478 walk_captures (op, cpts);
5479 for (unsigned i = 0; i < cpts.length (); ++i)
5481 capture *value_match = NULL;
5482 for (unsigned j = 0; j < cpts[i].length (); ++j)
5484 if (cpts[i][j]->value_match)
5486 if (value_match)
5487 fatal_at (cpts[i][j]->location, "duplicate @@");
5488 value_match = cpts[i][j];
5491 if (cpts[i].length () == 1 && value_match)
5492 fatal_at (value_match->location, "@@ without a matching capture");
5493 if (value_match)
5495 /* Duplicate prevailing capture with the existing ID, create
5496 a fake ID and rewrite all captures to use it. This turns
5497 @@1 into @__<newid>@1 and @1 into @__<newid>. */
5498 value_match->what = new capture (value_match->location,
5499 value_match->where,
5500 value_match->what, false);
5501 /* Create a fake ID and rewrite all captures to use it. */
5502 unsigned newid = get_internal_capture_id ();
5503 for (unsigned j = 0; j < cpts[i].length (); ++j)
5505 cpts[i][j]->where = newid;
5506 cpts[i][j]->value_match = true;
5509 cpts[i].release ();
5513 /* Main entry of the parser. Repeatedly parse outer control structures. */
5515 parser::parser (cpp_reader *r_, bool gimple_)
5517 r = r_;
5518 gimple = gimple_;
5519 active_ifs = vNULL;
5520 active_fors = vNULL;
5521 simplifiers = vNULL;
5522 oper_lists_set = NULL;
5523 oper_lists = vNULL;
5524 capture_ids = NULL;
5525 user_predicates = vNULL;
5526 parsing_match_operand = false;
5527 last_id = 0;
5529 const cpp_token *token = next ();
5530 while (token->type != CPP_EOF)
5532 _cpp_backup_tokens (r, 1);
5533 parse_pattern ();
5534 token = next ();
5539 /* Helper for the linemap code. */
5541 static size_t
5542 round_alloc_size (size_t s)
5544 return s;
5548 /* Construct and display the help menu. */
5550 static void
5551 usage ()
5553 const char *usage = "Usage:\n"
5554 " %s [--gimple|--generic] [-v[v]] <input>\n"
5555 " %s [options] [--include=FILE] --header=FILE <input> <output>...\n";
5556 fprintf (stderr, usage, progname, progname);
5559 /* Write out the correct include to the match-head fle containing the helper
5560 files. */
5562 static void
5563 write_header_includes (bool gimple, FILE *header_file)
5565 if (gimple)
5566 fprintf (header_file, "#include \"gimple-match-head.cc\"\n");
5567 else
5568 fprintf (header_file, "#include \"generic-match-head.cc\"\n");
5571 /* The genmatch generator program. It reads from a pattern description
5572 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
5575 main (int argc, char **argv)
5577 cpp_reader *r;
5579 progname = "genmatch";
5581 bool gimple = true;
5582 char *s_header_file = NULL;
5583 char *s_include_file = NULL;
5584 auto_vec <char *> files;
5585 char *input = NULL;
5586 int last_file = argc - 1;
5587 for (int i = argc - 1; i >= 1; --i)
5589 if (strcmp (argv[i], "--gimple") == 0)
5590 gimple = true;
5591 else if (strcmp (argv[i], "--generic") == 0)
5592 gimple = false;
5593 else if (strncmp (argv[i], "--header=", 9) == 0)
5594 s_header_file = &argv[i][9];
5595 else if (strncmp (argv[i], "--include=", 10) == 0)
5596 s_include_file = &argv[i][10];
5597 else if (strcmp (argv[i], "-v") == 0)
5598 verbose = 1;
5599 else if (strcmp (argv[i], "-vv") == 0)
5600 verbose = 2;
5601 else if (strncmp (argv[i], "--", 2) != 0 && last_file-- == i)
5602 files.safe_push (argv[i]);
5603 else
5605 usage ();
5606 return 1;
5610 /* Validate if the combinations are valid. */
5611 if ((files.length () > 1 && !s_header_file) || files.is_empty ())
5613 usage ();
5614 return 1;
5617 if (!s_include_file)
5618 s_include_file = s_header_file;
5620 /* Input file is the last in the reverse list. */
5621 input = files.pop ();
5623 line_table = XCNEW (class line_maps);
5624 linemap_init (line_table, 0);
5625 line_table->m_reallocator = xrealloc;
5626 line_table->m_round_alloc_size = round_alloc_size;
5628 r = cpp_create_reader (CLK_GNUC99, NULL, line_table);
5629 cpp_callbacks *cb = cpp_get_callbacks (r);
5630 cb->diagnostic = diagnostic_cb;
5632 /* Add the build directory to the #include "" search path. */
5633 cpp_dir *dir = XCNEW (cpp_dir);
5634 dir->name = getpwd ();
5635 if (!dir->name)
5636 dir->name = ASTRDUP (".");
5637 cpp_set_include_chains (r, dir, NULL, false);
5639 if (!cpp_read_main_file (r, input))
5640 return 1;
5641 cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1");
5642 cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0");
5644 null_id = new id_base (id_base::NULL_ID, "null");
5646 /* Pre-seed operators. */
5647 operators = new hash_table<id_base> (1024);
5648 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
5649 add_operator (SYM, # SYM, # TYPE, NARGS);
5650 #define END_OF_BASE_TREE_CODES
5651 #include "tree.def"
5652 #undef END_OF_BASE_TREE_CODES
5653 #undef DEFTREECODE
5655 /* Pre-seed builtin functions.
5656 ??? Cannot use N (name) as that is targetm.emultls.get_address
5657 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
5658 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
5659 add_function (ENUM, "CFN_" # ENUM);
5660 #include "builtins.def"
5662 #define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \
5663 add_function (IFN_##CODE, "CFN_" #CODE);
5664 #include "internal-fn.def"
5666 /* Parse ahead! */
5667 parser p (r, gimple);
5669 /* Create file buffers. */
5670 int n_parts = files.is_empty () ? 1 : files.length ();
5671 auto_vec <FILE *> parts (n_parts);
5672 if (files.is_empty ())
5674 parts.quick_push (stdout);
5675 write_header (stdout, s_include_file);
5676 write_header_includes (gimple, stdout);
5677 write_header_declarations (gimple, stdout);
5679 else
5681 for (char *s_file : files)
5683 parts.quick_push (fopen (s_file, "w"));
5684 write_header (parts.last (), s_include_file);
5687 header_file = fopen (s_header_file, "w");
5688 fprintf (header_file, "#ifndef GCC_GIMPLE_MATCH_AUTO_H\n"
5689 "#define GCC_GIMPLE_MATCH_AUTO_H\n");
5690 write_header_includes (gimple, header_file);
5691 write_header_declarations (gimple, header_file);
5694 /* Go over all predicates defined with patterns and perform
5695 lowering and code generation. */
5696 for (unsigned i = 0; i < p.user_predicates.length (); ++i)
5698 predicate_id *pred = p.user_predicates[i];
5699 lower (pred->matchers, gimple);
5701 if (verbose == 2)
5702 for (unsigned j = 0; j < pred->matchers.length (); ++j)
5703 print_matches (pred->matchers[j]);
5705 decision_tree dt;
5706 for (unsigned j = 0; j < pred->matchers.length (); ++j)
5707 dt.insert (pred->matchers[j], j);
5709 if (verbose == 2)
5710 dt.print (stderr);
5712 /* Cycle the file buffers. */
5713 FILE *f = choose_output (parts);
5715 write_predicate (f, pred, dt, gimple);
5718 /* Lower the main simplifiers and generate code for them. */
5719 lower (p.simplifiers, gimple);
5721 if (verbose == 2)
5722 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
5723 print_matches (p.simplifiers[i]);
5725 decision_tree dt;
5726 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
5727 dt.insert (p.simplifiers[i], i);
5729 if (verbose == 2)
5730 dt.print (stderr);
5732 dt.gen (parts, gimple);
5734 define_dump_logs (gimple, choose_output (parts));
5736 for (FILE *f : parts)
5738 fprintf (f, "#pragma GCC diagnostic pop\n");
5739 fclose (f);
5742 if (header_file)
5744 fprintf (header_file, "\n#endif /* GCC_GIMPLE_MATCH_AUTO_H. */\n");
5745 fclose (header_file);
5748 /* Finalize. */
5749 cpp_finish (r, NULL);
5750 cpp_destroy (r);
5752 delete operators;
5754 return 0;