]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genmatch.c
Rename identifiers in a test-case.
[thirdparty/gcc.git] / gcc / genmatch.c
CommitLineData
3d2cf79f
RB
1/* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
3
a5544970 4 Copyright (C) 2014-2019 Free Software Foundation, Inc.
3d2cf79f
RB
5 Contributed by Richard Biener <rguenther@suse.de>
6 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
7
8This file is part of GCC.
9
10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
12Software Foundation; either version 3, or (at your option) any later
13version.
14
15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
19
20You should have received a copy of the GNU General Public License
21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
23
24#include "bconfig.h"
3d2cf79f
RB
25#include "system.h"
26#include "coretypes.h"
27#include <cpplib.h>
28#include "errors.h"
3d2cf79f 29#include "hash-table.h"
1c9b0448 30#include "hash-set.h"
3d2cf79f
RB
31#include "is-a.h"
32
33
f1308e4b
RB
34/* Stubs for GGC referenced through instantiations triggered by hash-map. */
35void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
e7d1000c 36 size_t, size_t MEM_STAT_DECL)
f1308e4b
RB
37{
38 return NULL;
39}
40void ggc_free (void *)
41{
42}
43
44
53a19317
RB
45/* Global state. */
46
47/* Verboseness. 0 is quiet, 1 adds some warnings, 2 is for debugging. */
48unsigned verbose;
49
50
3d2cf79f
RB
51/* libccp helpers. */
52
99b1c316 53static class line_maps *line_table;
3d2cf79f 54
8a645150 55/* The rich_location class within libcpp requires a way to expand
620e594b 56 location_t instances, and relies on the client code
8a645150
DM
57 providing a symbol named
58 linemap_client_expand_location_to_spelling_point
59 to do this.
60
61 This is the implementation for genmatch. */
62
63expanded_location
620e594b 64linemap_client_expand_location_to_spelling_point (location_t loc,
c471c6ed 65 enum location_aspect)
8a645150
DM
66{
67 const struct line_map_ordinary *map;
68 loc = linemap_resolve_location (line_table, loc, LRK_SPELLING_LOCATION, &map);
69 return linemap_expand_location (line_table, map, loc);
70}
71
3d2cf79f
RB
72static bool
73#if GCC_VERSION >= 4001
8a645150 74__attribute__((format (printf, 5, 0)))
3d2cf79f 75#endif
c24300ba
DM
76diagnostic_cb (cpp_reader *, enum cpp_diagnostic_level errtype,
77 enum cpp_warning_reason, rich_location *richloc,
78 const char *msg, va_list *ap)
3d2cf79f 79{
0e50b624 80 const line_map_ordinary *map;
620e594b 81 location_t location = richloc->get_loc ();
3d2cf79f
RB
82 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
83 expanded_location loc = linemap_expand_location (line_table, map, location);
72eb311d
RB
84 fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column,
85 (errtype == CPP_DL_WARNING) ? "warning" : "error");
3d2cf79f
RB
86 vfprintf (stderr, msg, *ap);
87 fprintf (stderr, "\n");
88 FILE *f = fopen (loc.file, "r");
89 if (f)
90 {
91 char buf[128];
92 while (loc.line > 0)
93 {
94 if (!fgets (buf, 128, f))
95 goto notfound;
96 if (buf[strlen (buf) - 1] != '\n')
97 {
98 if (loc.line > 1)
99 loc.line++;
100 }
101 loc.line--;
102 }
103 fprintf (stderr, "%s", buf);
104 for (int i = 0; i < loc.column - 1; ++i)
105 fputc (' ', stderr);
106 fputc ('^', stderr);
107 fputc ('\n', stderr);
108notfound:
109 fclose (f);
110 }
72eb311d
RB
111
112 if (errtype == CPP_DL_FATAL)
113 exit (1);
114 return false;
3d2cf79f
RB
115}
116
117static void
118#if GCC_VERSION >= 4001
119__attribute__((format (printf, 2, 3)))
120#endif
121fatal_at (const cpp_token *tk, const char *msg, ...)
122{
ebedc9a3 123 rich_location richloc (line_table, tk->src_loc);
3d2cf79f
RB
124 va_list ap;
125 va_start (ap, msg);
c24300ba 126 diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
3d2cf79f
RB
127 va_end (ap);
128}
129
1c9b0448
RB
130static void
131#if GCC_VERSION >= 4001
132__attribute__((format (printf, 2, 3)))
133#endif
620e594b 134fatal_at (location_t loc, const char *msg, ...)
1c9b0448 135{
ebedc9a3 136 rich_location richloc (line_table, loc);
1c9b0448
RB
137 va_list ap;
138 va_start (ap, msg);
c24300ba 139 diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
1c9b0448
RB
140 va_end (ap);
141}
142
72eb311d
RB
143static void
144#if GCC_VERSION >= 4001
145__attribute__((format (printf, 2, 3)))
146#endif
147warning_at (const cpp_token *tk, const char *msg, ...)
148{
ebedc9a3 149 rich_location richloc (line_table, tk->src_loc);
72eb311d
RB
150 va_list ap;
151 va_start (ap, msg);
c24300ba 152 diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
72eb311d
RB
153 va_end (ap);
154}
155
53a19317
RB
156static void
157#if GCC_VERSION >= 4001
158__attribute__((format (printf, 2, 3)))
159#endif
620e594b 160warning_at (location_t loc, const char *msg, ...)
53a19317 161{
ebedc9a3 162 rich_location richloc (line_table, loc);
53a19317
RB
163 va_list ap;
164 va_start (ap, msg);
c24300ba 165 diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
53a19317
RB
166 va_end (ap);
167}
168
c551c21d
MM
169/* Like fprintf, but print INDENT spaces at the beginning. */
170
171static void
172#if GCC_VERSION >= 4001
173__attribute__((format (printf, 3, 4)))
174#endif
175fprintf_indent (FILE *f, unsigned int indent, const char *format, ...)
176{
177 va_list ap;
178 for (; indent >= 8; indent -= 8)
179 fputc ('\t', f);
180 fprintf (f, "%*s", indent, "");
181 va_start (ap, format);
182 vfprintf (f, format, ap);
183 va_end (ap);
184}
185
3d2cf79f 186static void
620e594b 187output_line_directive (FILE *f, location_t location,
8fcfe047 188 bool dumpfile = false, bool fnargs = false)
3d2cf79f 189{
0e50b624 190 const line_map_ordinary *map;
3d2cf79f
RB
191 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
192 expanded_location loc = linemap_expand_location (line_table, map, location);
193 if (dumpfile)
194 {
195 /* When writing to a dumpfile only dump the filename. */
196 const char *file = strrchr (loc.file, DIR_SEPARATOR);
c265c2dd
RB
197#if defined(DIR_SEPARATOR_2)
198 const char *pos2 = strrchr (loc.file, DIR_SEPARATOR_2);
199 if (pos2 && (!file || (pos2 > file)))
200 file = pos2;
201#endif
3d2cf79f
RB
202 if (!file)
203 file = loc.file;
204 else
205 ++file;
8fcfe047
ML
206
207 if (fnargs)
208 fprintf (f, "\"%s\", %d", file, loc.line);
209 else
210 fprintf (f, "%s:%d", file, loc.line);
3d2cf79f
RB
211 }
212 else
213 /* Other gen programs really output line directives here, at least for
214 development it's right now more convenient to have line information
215 from the generated file. Still keep the directives as comment for now
216 to easily back-point to the meta-description. */
217 fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file);
218}
219
220
221/* Pull in tree codes and builtin function codes from their
222 definition files. */
223
224#define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
225enum tree_code {
226#include "tree.def"
227CONVERT0,
228CONVERT1,
229CONVERT2,
39791822
RB
230VIEW_CONVERT0,
231VIEW_CONVERT1,
232VIEW_CONVERT2,
3d2cf79f
RB
233MAX_TREE_CODES
234};
235#undef DEFTREECODE
236
237#define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
238enum built_in_function {
239#include "builtins.def"
240END_BUILTINS
241};
3d2cf79f 242
c9e926ce
RS
243#define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE,
244enum internal_fn {
245#include "internal-fn.def"
246 IFN_LAST
247};
248
c566cc9f
RS
249enum combined_fn {
250#define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
251 CFN_##ENUM = int (ENUM),
252#include "builtins.def"
253
254#define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
255 CFN_##CODE = int (END_BUILTINS) + int (IFN_##CODE),
256#include "internal-fn.def"
257
258 CFN_LAST
259};
260
261#include "case-cfn-macros.h"
262
805a5406
RB
263/* Return true if CODE represents a commutative tree code. Otherwise
264 return false. */
265bool
266commutative_tree_code (enum tree_code code)
267{
268 switch (code)
269 {
270 case PLUS_EXPR:
271 case MULT_EXPR:
272 case MULT_HIGHPART_EXPR:
273 case MIN_EXPR:
274 case MAX_EXPR:
275 case BIT_IOR_EXPR:
276 case BIT_XOR_EXPR:
277 case BIT_AND_EXPR:
278 case NE_EXPR:
279 case EQ_EXPR:
280 case UNORDERED_EXPR:
281 case ORDERED_EXPR:
282 case UNEQ_EXPR:
283 case LTGT_EXPR:
284 case TRUTH_AND_EXPR:
285 case TRUTH_XOR_EXPR:
286 case TRUTH_OR_EXPR:
287 case WIDEN_MULT_EXPR:
288 case VEC_WIDEN_MULT_HI_EXPR:
289 case VEC_WIDEN_MULT_LO_EXPR:
290 case VEC_WIDEN_MULT_EVEN_EXPR:
291 case VEC_WIDEN_MULT_ODD_EXPR:
292 return true;
293
294 default:
295 break;
296 }
297 return false;
298}
299
300/* Return true if CODE represents a ternary tree code for which the
301 first two operands are commutative. Otherwise return false. */
302bool
303commutative_ternary_tree_code (enum tree_code code)
304{
305 switch (code)
306 {
307 case WIDEN_MULT_PLUS_EXPR:
308 case WIDEN_MULT_MINUS_EXPR:
309 case DOT_PROD_EXPR:
805a5406
RB
310 return true;
311
312 default:
313 break;
314 }
315 return false;
316}
317
e04d2a35
RB
318/* Return true if CODE is a comparison. */
319
320bool
321comparison_code_p (enum tree_code code)
322{
323 switch (code)
324 {
325 case EQ_EXPR:
326 case NE_EXPR:
327 case ORDERED_EXPR:
328 case UNORDERED_EXPR:
329 case LTGT_EXPR:
330 case UNEQ_EXPR:
331 case GT_EXPR:
332 case GE_EXPR:
333 case LT_EXPR:
334 case LE_EXPR:
335 case UNGT_EXPR:
336 case UNGE_EXPR:
337 case UNLT_EXPR:
338 case UNLE_EXPR:
339 return true;
340
341 default:
342 break;
343 }
344 return false;
345}
346
3d2cf79f
RB
347
348/* Base class for all identifiers the parser knows. */
349
6c1dae73 350class id_base : public nofree_ptr_hash<id_base>
3d2cf79f 351{
6c1dae73 352public:
fa74b47a 353 enum id_kind { CODE, FN, PREDICATE, USER, NULL_ID } kind;
3d2cf79f
RB
354
355 id_base (id_kind, const char *, int = -1);
356
357 hashval_t hashval;
358 int nargs;
359 const char *id;
360
361 /* hash_table support. */
67f58944
TS
362 static inline hashval_t hash (const id_base *);
363 static inline int equal (const id_base *, const id_base *);
3d2cf79f
RB
364};
365
366inline hashval_t
67f58944 367id_base::hash (const id_base *op)
3d2cf79f
RB
368{
369 return op->hashval;
370}
371
372inline int
67f58944
TS
373id_base::equal (const id_base *op1,
374 const id_base *op2)
3d2cf79f
RB
375{
376 return (op1->hashval == op2->hashval
377 && strcmp (op1->id, op2->id) == 0);
378}
379
fa74b47a
RS
380/* The special id "null", which matches nothing. */
381static id_base *null_id;
382
3d2cf79f
RB
383/* Hashtable of known pattern operators. This is pre-seeded from
384 all known tree codes and all known builtin function ids. */
385static hash_table<id_base> *operators;
386
387id_base::id_base (id_kind kind_, const char *id_, int nargs_)
388{
389 kind = kind_;
390 id = id_;
391 nargs = nargs_;
392 hashval = htab_hash_string (id);
393}
394
395/* Identifier that maps to a tree code. */
396
6c1dae73 397class operator_id : public id_base
3d2cf79f 398{
6c1dae73 399public:
3d2cf79f
RB
400 operator_id (enum tree_code code_, const char *id_, unsigned nargs_,
401 const char *tcc_)
402 : id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {}
403 enum tree_code code;
404 const char *tcc;
405};
406
c9e926ce 407/* Identifier that maps to a builtin or internal function code. */
3d2cf79f 408
6c1dae73 409class fn_id : public id_base
3d2cf79f 410{
6c1dae73 411public:
3d2cf79f
RB
412 fn_id (enum built_in_function fn_, const char *id_)
413 : id_base (id_base::FN, id_), fn (fn_) {}
c9e926ce
RS
414 fn_id (enum internal_fn fn_, const char *id_)
415 : id_base (id_base::FN, id_), fn (int (END_BUILTINS) + int (fn_)) {}
416 unsigned int fn;
3d2cf79f
RB
417};
418
99b1c316 419class simplify;
3d2cf79f
RB
420
421/* Identifier that maps to a user-defined predicate. */
422
6c1dae73 423class predicate_id : public id_base
3d2cf79f 424{
6c1dae73 425public:
3d2cf79f
RB
426 predicate_id (const char *id_)
427 : id_base (id_base::PREDICATE, id_), matchers (vNULL) {}
428 vec<simplify *> matchers;
429};
430
431/* Identifier that maps to a operator defined by a 'for' directive. */
432
6c1dae73 433class user_id : public id_base
3d2cf79f 434{
6c1dae73 435public:
72eb311d
RB
436 user_id (const char *id_, bool is_oper_list_ = false)
437 : id_base (id_base::USER, id_), substitutes (vNULL),
438 used (false), is_oper_list (is_oper_list_) {}
3d2cf79f 439 vec<id_base *> substitutes;
72eb311d
RB
440 bool used;
441 bool is_oper_list;
3d2cf79f
RB
442};
443
444template<>
445template<>
446inline bool
447is_a_helper <fn_id *>::test (id_base *id)
448{
449 return id->kind == id_base::FN;
450}
451
452template<>
453template<>
454inline bool
455is_a_helper <operator_id *>::test (id_base *id)
456{
457 return id->kind == id_base::CODE;
458}
459
460template<>
461template<>
462inline bool
463is_a_helper <predicate_id *>::test (id_base *id)
464{
465 return id->kind == id_base::PREDICATE;
466}
467
468template<>
469template<>
470inline bool
471is_a_helper <user_id *>::test (id_base *id)
472{
473 return id->kind == id_base::USER;
474}
475
c566cc9f
RS
476/* If ID has a pair of consecutive, commutative operands, return the
477 index of the first, otherwise return -1. */
478
479static int
480commutative_op (id_base *id)
481{
482 if (operator_id *code = dyn_cast <operator_id *> (id))
483 {
484 if (commutative_tree_code (code->code)
485 || commutative_ternary_tree_code (code->code))
486 return 0;
487 return -1;
488 }
489 if (fn_id *fn = dyn_cast <fn_id *> (id))
490 switch (fn->fn)
491 {
492 CASE_CFN_FMA:
493 case CFN_FMS:
494 case CFN_FNMA:
495 case CFN_FNMS:
496 return 0;
497
498 default:
499 return -1;
500 }
501 if (user_id *uid = dyn_cast<user_id *> (id))
502 {
503 int res = commutative_op (uid->substitutes[0]);
504 if (res < 0)
505 return 0;
506 for (unsigned i = 1; i < uid->substitutes.length (); ++i)
507 if (res != commutative_op (uid->substitutes[i]))
508 return -1;
509 return res;
510 }
511 return -1;
512}
513
3d2cf79f
RB
514/* Add a predicate identifier to the hash. */
515
516static predicate_id *
517add_predicate (const char *id)
518{
519 predicate_id *p = new predicate_id (id);
520 id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT);
521 if (*slot)
522 fatal ("duplicate id definition");
523 *slot = p;
524 return p;
525}
526
527/* Add a tree code identifier to the hash. */
528
529static void
530add_operator (enum tree_code code, const char *id,
531 const char *tcc, unsigned nargs)
532{
533 if (strcmp (tcc, "tcc_unary") != 0
534 && strcmp (tcc, "tcc_binary") != 0
535 && strcmp (tcc, "tcc_comparison") != 0
536 && strcmp (tcc, "tcc_expression") != 0
537 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
538 && strcmp (tcc, "tcc_reference") != 0
539 /* To have INTEGER_CST and friends as "predicate operators". */
10230017
RB
540 && strcmp (tcc, "tcc_constant") != 0
541 /* And allow CONSTRUCTOR for vector initializers. */
96a111a3
RB
542 && !(code == CONSTRUCTOR)
543 /* Allow SSA_NAME as predicate operator. */
544 && !(code == SSA_NAME))
3d2cf79f 545 return;
99e943a2
RB
546 /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
547 if (code == ADDR_EXPR)
548 nargs = 0;
3d2cf79f
RB
549 operator_id *op = new operator_id (code, id, nargs, tcc);
550 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
551 if (*slot)
552 fatal ("duplicate id definition");
553 *slot = op;
554}
555
c9e926ce
RS
556/* Add a built-in or internal function identifier to the hash. ID is
557 the name of its CFN_* enumeration value. */
3d2cf79f 558
c9e926ce 559template <typename T>
3d2cf79f 560static void
c9e926ce 561add_function (T code, const char *id)
3d2cf79f
RB
562{
563 fn_id *fn = new fn_id (code, id);
564 id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT);
565 if (*slot)
566 fatal ("duplicate id definition");
567 *slot = fn;
568}
569
570/* Helper for easy comparing ID with tree code CODE. */
571
572static bool
573operator==(id_base &id, enum tree_code code)
574{
575 if (operator_id *oid = dyn_cast <operator_id *> (&id))
576 return oid->code == code;
577 return false;
578}
579
fa74b47a 580/* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */
3d2cf79f
RB
581
582id_base *
fa74b47a 583get_operator (const char *id, bool allow_null = false)
3d2cf79f 584{
fa74b47a
RS
585 if (allow_null && strcmp (id, "null") == 0)
586 return null_id;
587
3d2cf79f
RB
588 id_base tem (id_base::CODE, id);
589
590 id_base *op = operators->find_with_hash (&tem, tem.hashval);
591 if (op)
72eb311d
RB
592 {
593 /* If this is a user-defined identifier track whether it was used. */
594 if (user_id *uid = dyn_cast<user_id *> (op))
595 uid->used = true;
596 return op;
597 }
3d2cf79f 598
c9e926ce
RS
599 char *id2;
600 bool all_upper = true;
601 bool all_lower = true;
602 for (unsigned int i = 0; id[i]; ++i)
603 if (ISUPPER (id[i]))
604 all_lower = false;
605 else if (ISLOWER (id[i]))
606 all_upper = false;
607 if (all_lower)
3d2cf79f 608 {
c9e926ce
RS
609 /* Try in caps with _EXPR appended. */
610 id2 = ACONCAT ((id, "_EXPR", NULL));
611 for (unsigned int i = 0; id2[i]; ++i)
612 id2[i] = TOUPPER (id2[i]);
3d2cf79f 613 }
c9e926ce
RS
614 else if (all_upper && strncmp (id, "IFN_", 4) == 0)
615 /* Try CFN_ instead of IFN_. */
616 id2 = ACONCAT (("CFN_", id + 4, NULL));
617 else if (all_upper && strncmp (id, "BUILT_IN_", 9) == 0)
618 /* Try prepending CFN_. */
619 id2 = ACONCAT (("CFN_", id, NULL));
620 else
621 return NULL;
3d2cf79f 622
3d2cf79f 623 new (&tem) id_base (id_base::CODE, id2);
c9e926ce 624 return operators->find_with_hash (&tem, tem.hashval);
3d2cf79f
RB
625}
626
e04d2a35
RB
627/* Return the comparison operators that results if the operands are
628 swapped. This is safe for floating-point. */
629
630id_base *
631swap_tree_comparison (operator_id *p)
632{
633 switch (p->code)
634 {
635 case EQ_EXPR:
636 case NE_EXPR:
637 case ORDERED_EXPR:
638 case UNORDERED_EXPR:
639 case LTGT_EXPR:
640 case UNEQ_EXPR:
641 return p;
642 case GT_EXPR:
643 return get_operator ("LT_EXPR");
644 case GE_EXPR:
645 return get_operator ("LE_EXPR");
646 case LT_EXPR:
647 return get_operator ("GT_EXPR");
648 case LE_EXPR:
649 return get_operator ("GE_EXPR");
650 case UNGT_EXPR:
651 return get_operator ("UNLT_EXPR");
652 case UNGE_EXPR:
653 return get_operator ("UNLE_EXPR");
654 case UNLT_EXPR:
655 return get_operator ("UNGT_EXPR");
656 case UNLE_EXPR:
657 return get_operator ("UNGE_EXPR");
658 default:
659 gcc_unreachable ();
660 }
661}
662
fb5c464a 663typedef hash_map<nofree_string_hash, unsigned> cid_map_t;
f1308e4b 664
3d2cf79f
RB
665
666/* The AST produced by parsing of the pattern definitions. */
667
99b1c316
MS
668class dt_operand;
669class capture_info;
3d2cf79f
RB
670
671/* The base class for operands. */
672
6c1dae73
MS
673class operand {
674public:
8fdc6c67 675 enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR, OP_IF, OP_WITH };
620e594b 676 operand (enum op_type type_, location_t loc_)
0889c52f 677 : type (type_), location (loc_) {}
3d2cf79f 678 enum op_type type;
620e594b 679 location_t location;
c551c21d 680 virtual void gen_transform (FILE *, int, const char *, bool, int,
47b25362
RB
681 const char *, capture_info *,
682 dt_operand ** = 0,
a3ca1bc5 683 int = 0)
3d2cf79f
RB
684 { gcc_unreachable (); }
685};
686
687/* A predicate operand. Predicates are leafs in the AST. */
688
6c1dae73 689class predicate : public operand
3d2cf79f 690{
6c1dae73 691public:
620e594b 692 predicate (predicate_id *p_, location_t loc)
0889c52f 693 : operand (OP_PREDICATE, loc), p (p_) {}
3d2cf79f
RB
694 predicate_id *p;
695};
696
697/* An operand that constitutes an expression. Expressions include
698 function calls and user-defined predicate invocations. */
699
6c1dae73 700class expr : public operand
3d2cf79f 701{
6c1dae73 702public:
620e594b 703 expr (id_base *operation_, location_t loc, bool is_commutative_ = false)
0889c52f 704 : operand (OP_EXPR, loc), operation (operation_),
47b25362 705 ops (vNULL), expr_type (NULL), is_commutative (is_commutative_),
44fc0a51
RB
706 is_generic (false), force_single_use (false) {}
707 expr (expr *e)
0889c52f 708 : operand (OP_EXPR, e->location), operation (e->operation),
44fc0a51
RB
709 ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commutative),
710 is_generic (e->is_generic), force_single_use (e->force_single_use) {}
3d2cf79f
RB
711 void append_op (operand *op) { ops.safe_push (op); }
712 /* The operator and its operands. */
713 id_base *operation;
714 vec<operand *> ops;
715 /* An explicitely specified type - used exclusively for conversions. */
716 const char *expr_type;
717 /* Whether the operation is to be applied commutatively. This is
718 later lowered to two separate patterns. */
719 bool is_commutative;
47b25362
RB
720 /* Whether the expression is expected to be in GENERIC form. */
721 bool is_generic;
44fc0a51
RB
722 /* Whether pushing any stmt to the sequence should be conditional
723 on this expression having a single-use. */
724 bool force_single_use;
c551c21d 725 virtual void gen_transform (FILE *f, int, const char *, bool, int,
47b25362 726 const char *, capture_info *,
a3ca1bc5 727 dt_operand ** = 0, int = 0);
3d2cf79f
RB
728};
729
730/* An operator that is represented by native C code. This is always
731 a leaf operand in the AST. This class is also used to represent
732 the code to be generated for 'if' and 'with' expressions. */
733
6c1dae73 734class c_expr : public operand
3d2cf79f 735{
6c1dae73 736public:
3d2cf79f
RB
737 /* A mapping of an identifier and its replacement. Used to apply
738 'for' lowering. */
6c1dae73
MS
739 class id_tab {
740 public:
3d2cf79f
RB
741 const char *id;
742 const char *oper;
743 id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {}
744 };
745
620e594b 746 c_expr (cpp_reader *r_, location_t loc,
0889c52f 747 vec<cpp_token> code_, unsigned nr_stmts_,
f1308e4b 748 vec<id_tab> ids_, cid_map_t *capture_ids_)
0889c52f
RB
749 : operand (OP_C_EXPR, loc), r (r_), code (code_),
750 capture_ids (capture_ids_), nr_stmts (nr_stmts_), ids (ids_) {}
3d2cf79f
RB
751 /* cpplib tokens and state to transform this back to source. */
752 cpp_reader *r;
753 vec<cpp_token> code;
f1308e4b 754 cid_map_t *capture_ids;
3d2cf79f
RB
755 /* The number of statements parsed (well, the number of ';'s). */
756 unsigned nr_stmts;
757 /* The identifier replacement vector. */
758 vec<id_tab> ids;
c551c21d 759 virtual void gen_transform (FILE *f, int, const char *, bool, int,
47b25362 760 const char *, capture_info *,
a3ca1bc5 761 dt_operand ** = 0, int = 0);
3d2cf79f
RB
762};
763
764/* A wrapper around another operand that captures its value. */
765
6c1dae73 766class capture : public operand
3d2cf79f 767{
6c1dae73 768public:
620e594b 769 capture (location_t loc, unsigned where_, operand *what_, bool value_)
2eef1fc1
RB
770 : operand (OP_CAPTURE, loc), where (where_), value_match (value_),
771 what (what_) {}
3d2cf79f
RB
772 /* Identifier index for the value. */
773 unsigned where;
2eef1fc1
RB
774 /* Whether in a match of two operands the compare should be for
775 equal values rather than equal atoms (boils down to a type
776 check or not). */
777 bool value_match;
3d2cf79f
RB
778 /* The captured value. */
779 operand *what;
c551c21d 780 virtual void gen_transform (FILE *f, int, const char *, bool, int,
47b25362 781 const char *, capture_info *,
a3ca1bc5 782 dt_operand ** = 0, int = 0);
3d2cf79f
RB
783};
784
8fdc6c67
RB
785/* if expression. */
786
6c1dae73 787class if_expr : public operand
8fdc6c67 788{
6c1dae73 789public:
620e594b 790 if_expr (location_t loc)
0889c52f 791 : operand (OP_IF, loc), cond (NULL), trueexpr (NULL), falseexpr (NULL) {}
8fdc6c67
RB
792 c_expr *cond;
793 operand *trueexpr;
794 operand *falseexpr;
795};
796
797/* with expression. */
798
6c1dae73 799class with_expr : public operand
8fdc6c67 800{
6c1dae73 801public:
620e594b 802 with_expr (location_t loc)
0889c52f 803 : operand (OP_WITH, loc), with (NULL), subexpr (NULL) {}
8fdc6c67
RB
804 c_expr *with;
805 operand *subexpr;
806};
807
3d2cf79f
RB
808template<>
809template<>
810inline bool
811is_a_helper <capture *>::test (operand *op)
812{
813 return op->type == operand::OP_CAPTURE;
814}
815
816template<>
817template<>
818inline bool
819is_a_helper <predicate *>::test (operand *op)
820{
821 return op->type == operand::OP_PREDICATE;
822}
823
824template<>
825template<>
826inline bool
827is_a_helper <c_expr *>::test (operand *op)
828{
829 return op->type == operand::OP_C_EXPR;
830}
831
832template<>
833template<>
834inline bool
835is_a_helper <expr *>::test (operand *op)
836{
837 return op->type == operand::OP_EXPR;
838}
839
8fdc6c67
RB
840template<>
841template<>
842inline bool
843is_a_helper <if_expr *>::test (operand *op)
844{
845 return op->type == operand::OP_IF;
846}
3d2cf79f 847
8fdc6c67
RB
848template<>
849template<>
850inline bool
851is_a_helper <with_expr *>::test (operand *op)
3d2cf79f 852{
8fdc6c67
RB
853 return op->type == operand::OP_WITH;
854}
3d2cf79f
RB
855
856/* The main class of a pattern and its transform. This is used to
857 represent both (simplify ...) and (match ...) kinds. The AST
858 duplicates all outer 'if' and 'for' expressions here so each
859 simplify can exist in isolation. */
860
6c1dae73 861class simplify
3d2cf79f 862{
6c1dae73 863public:
8fdc6c67
RB
864 enum simplify_kind { SIMPLIFY, MATCH };
865
6c35e5b0
RB
866 simplify (simplify_kind kind_, unsigned id_, operand *match_,
867 operand *result_, vec<vec<user_id *> > for_vec_,
868 cid_map_t *capture_ids_)
869 : kind (kind_), id (id_), match (match_), result (result_),
d4b71b95 870 for_vec (for_vec_), for_subst_vec (vNULL),
f1308e4b 871 capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
3d2cf79f 872
8fdc6c67 873 simplify_kind kind;
6c35e5b0
RB
874 /* ID. This is kept to easily associate related simplifies expanded
875 from the same original one. */
876 unsigned id;
3d2cf79f
RB
877 /* The expression that is matched against the GENERIC or GIMPLE IL. */
878 operand *match;
8fdc6c67
RB
879 /* For a (simplify ...) an expression with ifs and withs with the expression
880 produced when the pattern applies in the leafs.
881 For a (match ...) the leafs are either empty if it is a simple predicate
882 or the single expression specifying the matched operands. */
99b1c316 883 class operand *result;
3d2cf79f
RB
884 /* Collected 'for' expression operators that have to be replaced
885 in the lowering phase. */
886 vec<vec<user_id *> > for_vec;
d4b71b95 887 vec<std::pair<user_id *, id_base *> > for_subst_vec;
3d2cf79f 888 /* A map of capture identifiers to indexes. */
f1308e4b 889 cid_map_t *capture_ids;
3d2cf79f
RB
890 int capture_max;
891};
892
893/* Debugging routines for dumping the AST. */
894
895DEBUG_FUNCTION void
896print_operand (operand *o, FILE *f = stderr, bool flattened = false)
897{
898 if (capture *c = dyn_cast<capture *> (o))
899 {
3d2cf79f 900 if (c->what && flattened == false)
eee7b6c4
RB
901 print_operand (c->what, f, flattened);
902 fprintf (f, "@%u", c->where);
3d2cf79f
RB
903 }
904
905 else if (predicate *p = dyn_cast<predicate *> (o))
906 fprintf (f, "%s", p->p->id);
907
908 else if (is_a<c_expr *> (o))
909 fprintf (f, "c_expr");
910
911 else if (expr *e = dyn_cast<expr *> (o))
912 {
eee7b6c4
RB
913 if (e->ops.length () == 0)
914 fprintf (f, "%s", e->operation->id);
915 else
3d2cf79f 916 {
eee7b6c4
RB
917 fprintf (f, "(%s", e->operation->id);
918
919 if (flattened == false)
3d2cf79f 920 {
eee7b6c4
RB
921 for (unsigned i = 0; i < e->ops.length (); ++i)
922 {
923 putc (' ', f);
924 print_operand (e->ops[i], f, flattened);
925 }
3d2cf79f 926 }
eee7b6c4 927 putc (')', f);
3d2cf79f 928 }
3d2cf79f
RB
929 }
930
931 else
932 gcc_unreachable ();
933}
934
935DEBUG_FUNCTION void
99b1c316 936print_matches (class simplify *s, FILE *f = stderr)
3d2cf79f
RB
937{
938 fprintf (f, "for expression: ");
939 print_operand (s->match, f);
940 putc ('\n', f);
941}
942
943
944/* AST lowering. */
945
946/* Lowering of commutative operators. */
947
948static void
949cartesian_product (const vec< vec<operand *> >& ops_vector,
950 vec< vec<operand *> >& result, vec<operand *>& v, unsigned n)
951{
952 if (n == ops_vector.length ())
953 {
954 vec<operand *> xv = v.copy ();
955 result.safe_push (xv);
956 return;
957 }
958
959 for (unsigned i = 0; i < ops_vector[n].length (); ++i)
960 {
961 v[n] = ops_vector[n][i];
962 cartesian_product (ops_vector, result, v, n + 1);
963 }
964}
965
966/* Lower OP to two operands in case it is marked as commutative. */
967
968static vec<operand *>
e04d2a35 969commutate (operand *op, vec<vec<user_id *> > &for_vec)
3d2cf79f
RB
970{
971 vec<operand *> ret = vNULL;
972
973 if (capture *c = dyn_cast <capture *> (op))
974 {
975 if (!c->what)
976 {
977 ret.safe_push (op);
978 return ret;
979 }
e04d2a35 980 vec<operand *> v = commutate (c->what, for_vec);
3d2cf79f
RB
981 for (unsigned i = 0; i < v.length (); ++i)
982 {
2eef1fc1
RB
983 capture *nc = new capture (c->location, c->where, v[i],
984 c->value_match);
3d2cf79f
RB
985 ret.safe_push (nc);
986 }
987 return ret;
988 }
989
990 expr *e = dyn_cast <expr *> (op);
991 if (!e || e->ops.length () == 0)
992 {
993 ret.safe_push (op);
994 return ret;
995 }
996
997 vec< vec<operand *> > ops_vector = vNULL;
998 for (unsigned i = 0; i < e->ops.length (); ++i)
e04d2a35 999 ops_vector.safe_push (commutate (e->ops[i], for_vec));
3d2cf79f
RB
1000
1001 auto_vec< vec<operand *> > result;
1002 auto_vec<operand *> v (e->ops.length ());
1003 v.quick_grow_cleared (e->ops.length ());
1004 cartesian_product (ops_vector, result, v, 0);
1005
1006
1007 for (unsigned i = 0; i < result.length (); ++i)
1008 {
44fc0a51
RB
1009 expr *ne = new expr (e);
1010 ne->is_commutative = false;
3d2cf79f
RB
1011 for (unsigned j = 0; j < result[i].length (); ++j)
1012 ne->append_op (result[i][j]);
1013 ret.safe_push (ne);
1014 }
1015
1016 if (!e->is_commutative)
1017 return ret;
1018
c566cc9f
RS
1019 /* The operation is always binary if it isn't inherently commutative. */
1020 int natural_opno = commutative_op (e->operation);
1021 unsigned int opno = natural_opno >= 0 ? natural_opno : 0;
3d2cf79f
RB
1022 for (unsigned i = 0; i < result.length (); ++i)
1023 {
44fc0a51 1024 expr *ne = new expr (e);
4e4791ff 1025 if (operator_id *r = dyn_cast <operator_id *> (ne->operation))
e04d2a35 1026 {
4e4791ff
BE
1027 if (comparison_code_p (r->code))
1028 ne->operation = swap_tree_comparison (r);
e04d2a35
RB
1029 }
1030 else if (user_id *p = dyn_cast <user_id *> (ne->operation))
1031 {
1032 bool found_compare = false;
1033 for (unsigned j = 0; j < p->substitutes.length (); ++j)
1034 if (operator_id *q = dyn_cast <operator_id *> (p->substitutes[j]))
1035 {
1036 if (comparison_code_p (q->code)
1037 && swap_tree_comparison (q) != q)
1038 {
1039 found_compare = true;
1040 break;
1041 }
1042 }
1043 if (found_compare)
1044 {
1045 user_id *newop = new user_id ("<internal>");
1046 for (unsigned j = 0; j < p->substitutes.length (); ++j)
1047 {
1048 id_base *subst = p->substitutes[j];
1049 if (operator_id *q = dyn_cast <operator_id *> (subst))
1050 {
1051 if (comparison_code_p (q->code))
1052 subst = swap_tree_comparison (q);
1053 }
1054 newop->substitutes.safe_push (subst);
1055 }
1056 ne->operation = newop;
1057 /* Search for 'p' inside the for vector and push 'newop'
1058 to the same level. */
1059 for (unsigned j = 0; newop && j < for_vec.length (); ++j)
1060 for (unsigned k = 0; k < for_vec[j].length (); ++k)
1061 if (for_vec[j][k] == p)
1062 {
1063 for_vec[j].safe_push (newop);
1064 newop = NULL;
1065 break;
1066 }
1067 }
1068 }
44fc0a51 1069 ne->is_commutative = false;
c566cc9f
RS
1070 for (unsigned j = 0; j < result[i].length (); ++j)
1071 {
1072 int old_j = (j == opno ? opno + 1 : j == opno + 1 ? opno : j);
1073 ne->append_op (result[i][old_j]);
1074 }
3d2cf79f
RB
1075 ret.safe_push (ne);
1076 }
1077
1078 return ret;
1079}
1080
1081/* Lower operations marked as commutative in the AST of S and push
1082 the resulting patterns to SIMPLIFIERS. */
1083
1084static void
1085lower_commutative (simplify *s, vec<simplify *>& simplifiers)
1086{
e04d2a35 1087 vec<operand *> matchers = commutate (s->match, s->for_vec);
3d2cf79f
RB
1088 for (unsigned i = 0; i < matchers.length (); ++i)
1089 {
6c35e5b0 1090 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
3d2cf79f
RB
1091 s->for_vec, s->capture_ids);
1092 simplifiers.safe_push (ns);
1093 }
1094}
1095
1096/* Strip conditional conversios using operator OPER from O and its
1097 children if STRIP, else replace them with an unconditional convert. */
1098
1099operand *
39791822
RB
1100lower_opt_convert (operand *o, enum tree_code oper,
1101 enum tree_code to_oper, bool strip)
3d2cf79f
RB
1102{
1103 if (capture *c = dyn_cast<capture *> (o))
1104 {
1105 if (c->what)
0889c52f 1106 return new capture (c->location, c->where,
2eef1fc1
RB
1107 lower_opt_convert (c->what, oper, to_oper, strip),
1108 c->value_match);
3d2cf79f
RB
1109 else
1110 return c;
1111 }
1112
1113 expr *e = dyn_cast<expr *> (o);
1114 if (!e)
1115 return o;
1116
1117 if (*e->operation == oper)
1118 {
1119 if (strip)
39791822 1120 return lower_opt_convert (e->ops[0], oper, to_oper, strip);
3d2cf79f 1121
44fc0a51
RB
1122 expr *ne = new expr (e);
1123 ne->operation = (to_oper == CONVERT_EXPR
1124 ? get_operator ("CONVERT_EXPR")
1125 : get_operator ("VIEW_CONVERT_EXPR"));
39791822 1126 ne->append_op (lower_opt_convert (e->ops[0], oper, to_oper, strip));
3d2cf79f
RB
1127 return ne;
1128 }
1129
44fc0a51 1130 expr *ne = new expr (e);
3d2cf79f 1131 for (unsigned i = 0; i < e->ops.length (); ++i)
39791822 1132 ne->append_op (lower_opt_convert (e->ops[i], oper, to_oper, strip));
3d2cf79f
RB
1133
1134 return ne;
1135}
1136
1137/* Determine whether O or its children uses the conditional conversion
1138 operator OPER. */
1139
1140static bool
1141has_opt_convert (operand *o, enum tree_code oper)
1142{
1143 if (capture *c = dyn_cast<capture *> (o))
1144 {
1145 if (c->what)
1146 return has_opt_convert (c->what, oper);
1147 else
1148 return false;
1149 }
1150
1151 expr *e = dyn_cast<expr *> (o);
1152 if (!e)
1153 return false;
1154
1155 if (*e->operation == oper)
1156 return true;
1157
1158 for (unsigned i = 0; i < e->ops.length (); ++i)
1159 if (has_opt_convert (e->ops[i], oper))
1160 return true;
1161
1162 return false;
1163}
1164
1165/* Lower conditional convert operators in O, expanding it to a vector
1166 if required. */
1167
1168static vec<operand *>
1169lower_opt_convert (operand *o)
1170{
1171 vec<operand *> v1 = vNULL, v2;
1172
1173 v1.safe_push (o);
1174
39791822
RB
1175 enum tree_code opers[]
1176 = { CONVERT0, CONVERT_EXPR,
1177 CONVERT1, CONVERT_EXPR,
1178 CONVERT2, CONVERT_EXPR,
1179 VIEW_CONVERT0, VIEW_CONVERT_EXPR,
1180 VIEW_CONVERT1, VIEW_CONVERT_EXPR,
1181 VIEW_CONVERT2, VIEW_CONVERT_EXPR };
3d2cf79f
RB
1182
1183 /* Conditional converts are lowered to a pattern with the
1184 conversion and one without. The three different conditional
1185 convert codes are lowered separately. */
1186
39791822 1187 for (unsigned i = 0; i < sizeof (opers) / sizeof (enum tree_code); i += 2)
3d2cf79f
RB
1188 {
1189 v2 = vNULL;
1190 for (unsigned j = 0; j < v1.length (); ++j)
1191 if (has_opt_convert (v1[j], opers[i]))
1192 {
39791822
RB
1193 v2.safe_push (lower_opt_convert (v1[j],
1194 opers[i], opers[i+1], false));
1195 v2.safe_push (lower_opt_convert (v1[j],
1196 opers[i], opers[i+1], true));
3d2cf79f
RB
1197 }
1198
1199 if (v2 != vNULL)
1200 {
1201 v1 = vNULL;
1202 for (unsigned j = 0; j < v2.length (); ++j)
1203 v1.safe_push (v2[j]);
1204 }
1205 }
1206
1207 return v1;
1208}
1209
1210/* Lower conditional convert operators in the AST of S and push
1211 the resulting multiple patterns to SIMPLIFIERS. */
1212
1213static void
1214lower_opt_convert (simplify *s, vec<simplify *>& simplifiers)
1215{
1216 vec<operand *> matchers = lower_opt_convert (s->match);
1217 for (unsigned i = 0; i < matchers.length (); ++i)
1218 {
6c35e5b0 1219 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
3d2cf79f
RB
1220 s->for_vec, s->capture_ids);
1221 simplifiers.safe_push (ns);
1222 }
1223}
1224
47b25362
RB
1225/* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
1226 GENERIC and a GIMPLE variant. */
1227
1228static vec<operand *>
1229lower_cond (operand *o)
1230{
1231 vec<operand *> ro = vNULL;
1232
1233 if (capture *c = dyn_cast<capture *> (o))
1234 {
1235 if (c->what)
1236 {
1237 vec<operand *> lop = vNULL;
1238 lop = lower_cond (c->what);
1239
1240 for (unsigned i = 0; i < lop.length (); ++i)
2eef1fc1
RB
1241 ro.safe_push (new capture (c->location, c->where, lop[i],
1242 c->value_match));
47b25362
RB
1243 return ro;
1244 }
1245 }
1246
1247 expr *e = dyn_cast<expr *> (o);
1248 if (!e || e->ops.length () == 0)
1249 {
1250 ro.safe_push (o);
1251 return ro;
1252 }
1253
1254 vec< vec<operand *> > ops_vector = vNULL;
1255 for (unsigned i = 0; i < e->ops.length (); ++i)
1256 ops_vector.safe_push (lower_cond (e->ops[i]));
1257
1258 auto_vec< vec<operand *> > result;
1259 auto_vec<operand *> v (e->ops.length ());
1260 v.quick_grow_cleared (e->ops.length ());
1261 cartesian_product (ops_vector, result, v, 0);
1262
1263 for (unsigned i = 0; i < result.length (); ++i)
1264 {
44fc0a51 1265 expr *ne = new expr (e);
47b25362
RB
1266 for (unsigned j = 0; j < result[i].length (); ++j)
1267 ne->append_op (result[i][j]);
1268 ro.safe_push (ne);
1269 /* If this is a COND with a captured expression or an
1270 expression with two operands then also match a GENERIC
1271 form on the compare. */
1272 if ((*e->operation == COND_EXPR
1273 || *e->operation == VEC_COND_EXPR)
1274 && ((is_a <capture *> (e->ops[0])
1275 && as_a <capture *> (e->ops[0])->what
1276 && is_a <expr *> (as_a <capture *> (e->ops[0])->what)
1277 && as_a <expr *>
1278 (as_a <capture *> (e->ops[0])->what)->ops.length () == 2)
1279 || (is_a <expr *> (e->ops[0])
1280 && as_a <expr *> (e->ops[0])->ops.length () == 2)))
1281 {
4e4791ff 1282 ne = new expr (e);
47b25362
RB
1283 for (unsigned j = 0; j < result[i].length (); ++j)
1284 ne->append_op (result[i][j]);
1285 if (capture *c = dyn_cast <capture *> (ne->ops[0]))
1286 {
1287 expr *ocmp = as_a <expr *> (c->what);
44fc0a51 1288 expr *cmp = new expr (ocmp);
47b25362
RB
1289 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
1290 cmp->append_op (ocmp->ops[j]);
1291 cmp->is_generic = true;
2eef1fc1
RB
1292 ne->ops[0] = new capture (c->location, c->where, cmp,
1293 c->value_match);
47b25362
RB
1294 }
1295 else
1296 {
1297 expr *ocmp = as_a <expr *> (ne->ops[0]);
44fc0a51 1298 expr *cmp = new expr (ocmp);
47b25362
RB
1299 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
1300 cmp->append_op (ocmp->ops[j]);
1301 cmp->is_generic = true;
1302 ne->ops[0] = cmp;
1303 }
1304 ro.safe_push (ne);
1305 }
1306 }
1307
1308 return ro;
1309}
1310
1311/* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
1312 GENERIC and a GIMPLE variant. */
1313
1314static void
1315lower_cond (simplify *s, vec<simplify *>& simplifiers)
1316{
1317 vec<operand *> matchers = lower_cond (s->match);
1318 for (unsigned i = 0; i < matchers.length (); ++i)
1319 {
6c35e5b0 1320 simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
47b25362
RB
1321 s->for_vec, s->capture_ids);
1322 simplifiers.safe_push (ns);
1323 }
1324}
1325
fa74b47a
RS
1326/* Return true if O refers to ID. */
1327
1328bool
1329contains_id (operand *o, user_id *id)
1330{
1331 if (capture *c = dyn_cast<capture *> (o))
1332 return c->what && contains_id (c->what, id);
1333
1334 if (expr *e = dyn_cast<expr *> (o))
1335 {
1336 if (e->operation == id)
1337 return true;
1338 for (unsigned i = 0; i < e->ops.length (); ++i)
1339 if (contains_id (e->ops[i], id))
1340 return true;
1341 return false;
1342 }
1343
1344 if (with_expr *w = dyn_cast <with_expr *> (o))
1345 return (contains_id (w->with, id)
1346 || contains_id (w->subexpr, id));
1347
1348 if (if_expr *ife = dyn_cast <if_expr *> (o))
1349 return (contains_id (ife->cond, id)
1350 || contains_id (ife->trueexpr, id)
1351 || (ife->falseexpr && contains_id (ife->falseexpr, id)));
1352
1353 if (c_expr *ce = dyn_cast<c_expr *> (o))
1354 return ce->capture_ids && ce->capture_ids->get (id->id);
1355
1356 return false;
1357}
1358
1359
3d2cf79f
RB
1360/* In AST operand O replace operator ID with operator WITH. */
1361
1362operand *
1363replace_id (operand *o, user_id *id, id_base *with)
1364{
1365 /* Deep-copy captures and expressions, replacing operations as
1366 needed. */
1367 if (capture *c = dyn_cast<capture *> (o))
1368 {
1369 if (!c->what)
1370 return c;
0889c52f 1371 return new capture (c->location, c->where,
2eef1fc1 1372 replace_id (c->what, id, with), c->value_match);
3d2cf79f
RB
1373 }
1374 else if (expr *e = dyn_cast<expr *> (o))
1375 {
44fc0a51
RB
1376 expr *ne = new expr (e);
1377 if (e->operation == id)
1378 ne->operation = with;
3d2cf79f
RB
1379 for (unsigned i = 0; i < e->ops.length (); ++i)
1380 ne->append_op (replace_id (e->ops[i], id, with));
1381 return ne;
1382 }
8fdc6c67
RB
1383 else if (with_expr *w = dyn_cast <with_expr *> (o))
1384 {
0889c52f 1385 with_expr *nw = new with_expr (w->location);
8fdc6c67
RB
1386 nw->with = as_a <c_expr *> (replace_id (w->with, id, with));
1387 nw->subexpr = replace_id (w->subexpr, id, with);
1388 return nw;
1389 }
1390 else if (if_expr *ife = dyn_cast <if_expr *> (o))
1391 {
0889c52f 1392 if_expr *nife = new if_expr (ife->location);
8fdc6c67
RB
1393 nife->cond = as_a <c_expr *> (replace_id (ife->cond, id, with));
1394 nife->trueexpr = replace_id (ife->trueexpr, id, with);
1395 if (ife->falseexpr)
1396 nife->falseexpr = replace_id (ife->falseexpr, id, with);
1397 return nife;
1398 }
3d2cf79f
RB
1399
1400 /* For c_expr we simply record a string replacement table which is
1401 applied at code-generation time. */
1402 if (c_expr *ce = dyn_cast<c_expr *> (o))
1403 {
1404 vec<c_expr::id_tab> ids = ce->ids.copy ();
1405 ids.safe_push (c_expr::id_tab (id->id, with->id));
0889c52f
RB
1406 return new c_expr (ce->r, ce->location,
1407 ce->code, ce->nr_stmts, ids, ce->capture_ids);
3d2cf79f
RB
1408 }
1409
1410 return o;
1411}
1412
d4b71b95
RB
1413/* Return true if the binary operator OP is ok for delayed substitution
1414 during for lowering. */
1415
1416static bool
1417binary_ok (operator_id *op)
1418{
1419 switch (op->code)
1420 {
1421 case PLUS_EXPR:
1422 case MINUS_EXPR:
1423 case MULT_EXPR:
1424 case TRUNC_DIV_EXPR:
1425 case CEIL_DIV_EXPR:
1426 case FLOOR_DIV_EXPR:
1427 case ROUND_DIV_EXPR:
1428 case TRUNC_MOD_EXPR:
1429 case CEIL_MOD_EXPR:
1430 case FLOOR_MOD_EXPR:
1431 case ROUND_MOD_EXPR:
1432 case RDIV_EXPR:
1433 case EXACT_DIV_EXPR:
1434 case MIN_EXPR:
1435 case MAX_EXPR:
1436 case BIT_IOR_EXPR:
1437 case BIT_XOR_EXPR:
1438 case BIT_AND_EXPR:
1439 return true;
1440 default:
1441 return false;
1442 }
1443}
1444
3d2cf79f
RB
1445/* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1446
1447static void
1448lower_for (simplify *sin, vec<simplify *>& simplifiers)
1449{
1450 vec<vec<user_id *> >& for_vec = sin->for_vec;
1451 unsigned worklist_start = 0;
1452 auto_vec<simplify *> worklist;
1453 worklist.safe_push (sin);
1454
1455 /* Lower each recorded for separately, operating on the
1456 set of simplifiers created by the previous one.
1457 Lower inner-to-outer so inner for substitutes can refer
1458 to operators replaced by outer fors. */
1459 for (int fi = for_vec.length () - 1; fi >= 0; --fi)
1460 {
1461 vec<user_id *>& ids = for_vec[fi];
1462 unsigned n_ids = ids.length ();
1463 unsigned max_n_opers = 0;
d4b71b95 1464 bool can_delay_subst = (sin->kind == simplify::SIMPLIFY);
3d2cf79f 1465 for (unsigned i = 0; i < n_ids; ++i)
d4b71b95
RB
1466 {
1467 if (ids[i]->substitutes.length () > max_n_opers)
1468 max_n_opers = ids[i]->substitutes.length ();
1469 /* Require that all substitutes are of the same kind so that
1470 if we delay substitution to the result op code generation
1471 can look at the first substitute for deciding things like
1472 types of operands. */
1473 enum id_base::id_kind kind = ids[i]->substitutes[0]->kind;
1474 for (unsigned j = 0; j < ids[i]->substitutes.length (); ++j)
1475 if (ids[i]->substitutes[j]->kind != kind)
1476 can_delay_subst = false;
1477 else if (operator_id *op
1478 = dyn_cast <operator_id *> (ids[i]->substitutes[j]))
1479 {
1480 operator_id *op0
1481 = as_a <operator_id *> (ids[i]->substitutes[0]);
1482 if (strcmp (op->tcc, "tcc_comparison") == 0
1483 && strcmp (op0->tcc, "tcc_comparison") == 0)
1484 ;
1485 /* Unfortunately we can't just allow all tcc_binary. */
1486 else if (strcmp (op->tcc, "tcc_binary") == 0
1487 && strcmp (op0->tcc, "tcc_binary") == 0
1488 && binary_ok (op)
1489 && binary_ok (op0))
1490 ;
1491 else if ((strcmp (op->id + 1, "SHIFT_EXPR") == 0
1492 || strcmp (op->id + 1, "ROTATE_EXPR") == 0)
1493 && (strcmp (op0->id + 1, "SHIFT_EXPR") == 0
1494 || strcmp (op0->id + 1, "ROTATE_EXPR") == 0))
1495 ;
1496 else
1497 can_delay_subst = false;
1498 }
1499 else if (is_a <fn_id *> (ids[i]->substitutes[j]))
1500 ;
1501 else
1502 can_delay_subst = false;
1503 }
3d2cf79f
RB
1504
1505 unsigned worklist_end = worklist.length ();
1506 for (unsigned si = worklist_start; si < worklist_end; ++si)
1507 {
1508 simplify *s = worklist[si];
1509 for (unsigned j = 0; j < max_n_opers; ++j)
1510 {
1511 operand *match_op = s->match;
1512 operand *result_op = s->result;
8c681247 1513 auto_vec<std::pair<user_id *, id_base *> > subst (n_ids);
fa74b47a 1514 bool skip = false;
3d2cf79f
RB
1515 for (unsigned i = 0; i < n_ids; ++i)
1516 {
1517 user_id *id = ids[i];
1518 id_base *oper = id->substitutes[j % id->substitutes.length ()];
fa74b47a
RS
1519 if (oper == null_id
1520 && (contains_id (match_op, id)
1521 || contains_id (result_op, id)))
1522 {
1523 skip = true;
1524 break;
1525 }
d4b71b95 1526 subst.quick_push (std::make_pair (id, oper));
3d2cf79f 1527 match_op = replace_id (match_op, id, oper);
d4b71b95
RB
1528 if (result_op
1529 && !can_delay_subst)
3d2cf79f 1530 result_op = replace_id (result_op, id, oper);
3d2cf79f 1531 }
fa74b47a 1532 if (skip)
8c681247
TS
1533 continue;
1534
6c35e5b0 1535 simplify *ns = new simplify (s->kind, s->id, match_op, result_op,
8fdc6c67 1536 vNULL, s->capture_ids);
d4b71b95
RB
1537 ns->for_subst_vec.safe_splice (s->for_subst_vec);
1538 if (result_op
1539 && can_delay_subst)
1540 ns->for_subst_vec.safe_splice (subst);
8c681247 1541
3d2cf79f
RB
1542 worklist.safe_push (ns);
1543 }
1544 }
1545 worklist_start = worklist_end;
1546 }
1547
1548 /* Copy out the result from the last for lowering. */
1549 for (unsigned i = worklist_start; i < worklist.length (); ++i)
1550 simplifiers.safe_push (worklist[i]);
1551}
1552
1553/* Lower the AST for everything in SIMPLIFIERS. */
1554
1555static void
47b25362 1556lower (vec<simplify *>& simplifiers, bool gimple)
3d2cf79f 1557{
47b25362 1558 auto_vec<simplify *> out_simplifiers;
3d2cf79f 1559 for (unsigned i = 0; i < simplifiers.length (); ++i)
47b25362
RB
1560 lower_opt_convert (simplifiers[i], out_simplifiers);
1561
1562 simplifiers.truncate (0);
1563 for (unsigned i = 0; i < out_simplifiers.length (); ++i)
1564 lower_commutative (out_simplifiers[i], simplifiers);
1565
1566 out_simplifiers.truncate (0);
1567 if (gimple)
1568 for (unsigned i = 0; i < simplifiers.length (); ++i)
1569 lower_cond (simplifiers[i], out_simplifiers);
1570 else
1571 out_simplifiers.safe_splice (simplifiers);
3d2cf79f 1572
3d2cf79f
RB
1573
1574 simplifiers.truncate (0);
47b25362
RB
1575 for (unsigned i = 0; i < out_simplifiers.length (); ++i)
1576 lower_for (out_simplifiers[i], simplifiers);
3d2cf79f
RB
1577}
1578
1579
1580
1581
1582/* The decision tree built for generating GIMPLE and GENERIC pattern
1583 matching code. It represents the 'match' expression of all
1584 simplifies and has those as its leafs. */
1585
99b1c316 1586class dt_simplify;
03038b8b
RB
1587
1588/* A hash-map collecting semantically equivalent leafs in the decision
1589 tree for splitting out to separate functions. */
1590struct sinfo
1591{
1592 dt_simplify *s;
1593
1594 const char *fname;
1595 unsigned cnt;
1596};
1597
76b6ddbf
TS
1598struct sinfo_hashmap_traits : simple_hashmap_traits<pointer_hash<dt_simplify>,
1599 sinfo *>
03038b8b
RB
1600{
1601 static inline hashval_t hash (const key_type &);
1602 static inline bool equal_keys (const key_type &, const key_type &);
1603 template <typename T> static inline void remove (T &) {}
1604};
1605
1606typedef hash_map<void * /* unused */, sinfo *, sinfo_hashmap_traits>
1607 sinfo_map_t;
1608
6c35e5b0
RB
1609/* Current simplifier ID we are processing during insertion into the
1610 decision tree. */
1611static unsigned current_id;
03038b8b 1612
6c35e5b0 1613/* Decision tree base class, used for DT_NODE. */
3d2cf79f 1614
6c1dae73 1615class dt_node
3d2cf79f 1616{
6c1dae73 1617public:
3d2cf79f
RB
1618 enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY };
1619
1620 enum dt_type type;
1621 unsigned level;
6c35e5b0 1622 dt_node *parent;
3d2cf79f
RB
1623 vec<dt_node *> kids;
1624
803835de
RB
1625 /* Statistics. */
1626 unsigned num_leafs;
1627 unsigned total_size;
1628 unsigned max_level;
1629
6c35e5b0
RB
1630 dt_node (enum dt_type type_, dt_node *parent_)
1631 : type (type_), level (0), parent (parent_), kids (vNULL) {}
3d2cf79f
RB
1632
1633 dt_node *append_node (dt_node *);
6c35e5b0
RB
1634 dt_node *append_op (operand *, dt_node *parent, unsigned pos);
1635 dt_node *append_true_op (operand *, dt_node *parent, unsigned pos);
1636 dt_node *append_match_op (operand *, dt_operand *, dt_node *parent,
1637 unsigned pos);
3d2cf79f
RB
1638 dt_node *append_simplify (simplify *, unsigned, dt_operand **);
1639
4e4791ff 1640 virtual void gen (FILE *, int, bool, int) {}
3d2cf79f 1641
4e4791ff
BE
1642 void gen_kids (FILE *, int, bool, int);
1643 void gen_kids_1 (FILE *, int, bool, int,
7e015fce
RB
1644 vec<dt_operand *>, vec<dt_operand *>, vec<dt_operand *>,
1645 vec<dt_operand *>, vec<dt_operand *>, vec<dt_node *>);
803835de 1646
03038b8b 1647 void analyze (sinfo_map_t &);
3d2cf79f
RB
1648};
1649
6c35e5b0 1650/* Generic decision tree node used for DT_OPERAND, DT_MATCH and DT_TRUE. */
3d2cf79f 1651
6c1dae73 1652class dt_operand : public dt_node
3d2cf79f 1653{
6c1dae73 1654public:
3d2cf79f
RB
1655 operand *op;
1656 dt_operand *match_dop;
3d2cf79f 1657 unsigned pos;
2eef1fc1 1658 bool value_match;
6c35e5b0 1659 unsigned for_id;
3d2cf79f
RB
1660
1661 dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_,
6c35e5b0
RB
1662 dt_operand *parent_, unsigned pos_)
1663 : dt_node (type, parent_), op (op_), match_dop (match_dop_),
1664 pos (pos_), value_match (false), for_id (current_id) {}
3d2cf79f 1665
4e4791ff 1666 void gen (FILE *, int, bool, int);
c551c21d 1667 unsigned gen_predicate (FILE *, int, const char *, bool);
39bb7d01 1668 unsigned gen_match_op (FILE *, int, const char *, bool);
3d2cf79f 1669
4e4791ff 1670 unsigned gen_gimple_expr (FILE *, int, int);
c551c21d 1671 unsigned gen_generic_expr (FILE *, int, const char *);
3d2cf79f
RB
1672
1673 char *get_name (char *);
1674 void gen_opname (char *, unsigned);
1675};
1676
1677/* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1678
6c1dae73 1679class dt_simplify : public dt_node
3d2cf79f 1680{
6c1dae73 1681public:
3d2cf79f
RB
1682 simplify *s;
1683 unsigned pattern_no;
1684 dt_operand **indexes;
03038b8b 1685 sinfo *info;
3d2cf79f
RB
1686
1687 dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_)
6c35e5b0 1688 : dt_node (DT_SIMPLIFY, NULL), s (s_), pattern_no (pattern_no_),
03038b8b 1689 indexes (indexes_), info (NULL) {}
3d2cf79f 1690
8fdc6c67 1691 void gen_1 (FILE *, int, bool, operand *);
4e4791ff 1692 void gen (FILE *f, int, bool, int);
3d2cf79f
RB
1693};
1694
1695template<>
1696template<>
1697inline bool
1698is_a_helper <dt_operand *>::test (dt_node *n)
1699{
1700 return (n->type == dt_node::DT_OPERAND
6c35e5b0
RB
1701 || n->type == dt_node::DT_MATCH
1702 || n->type == dt_node::DT_TRUE);
3d2cf79f
RB
1703}
1704
03038b8b
RB
1705template<>
1706template<>
1707inline bool
1708is_a_helper <dt_simplify *>::test (dt_node *n)
1709{
1710 return n->type == dt_node::DT_SIMPLIFY;
1711}
1712
1713
1714
3d2cf79f
RB
1715/* A container for the actual decision tree. */
1716
6c1dae73 1717class decision_tree
3d2cf79f 1718{
6c1dae73 1719public:
3d2cf79f
RB
1720 dt_node *root;
1721
99b1c316 1722 void insert (class simplify *, unsigned);
b21ce9ce 1723 void gen (FILE *f, bool gimple);
3d2cf79f
RB
1724 void print (FILE *f = stderr);
1725
6c35e5b0 1726 decision_tree () { root = new dt_node (dt_node::DT_NODE, NULL); }
3d2cf79f
RB
1727
1728 static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes,
1729 unsigned pos = 0, dt_node *parent = 0);
1730 static dt_node *find_node (vec<dt_node *>&, dt_node *);
1731 static bool cmp_node (dt_node *, dt_node *);
1732 static void print_node (dt_node *, FILE *f = stderr, unsigned = 0);
1733};
1734
1735/* Compare two AST operands O1 and O2 and return true if they are equal. */
1736
1737bool
1738cmp_operand (operand *o1, operand *o2)
1739{
1740 if (!o1 || !o2 || o1->type != o2->type)
1741 return false;
1742
1743 if (o1->type == operand::OP_PREDICATE)
1744 {
1745 predicate *p1 = as_a<predicate *>(o1);
1746 predicate *p2 = as_a<predicate *>(o2);
1747 return p1->p == p2->p;
1748 }
1749 else if (o1->type == operand::OP_EXPR)
1750 {
1751 expr *e1 = static_cast<expr *>(o1);
1752 expr *e2 = static_cast<expr *>(o2);
47b25362
RB
1753 return (e1->operation == e2->operation
1754 && e1->is_generic == e2->is_generic);
3d2cf79f
RB
1755 }
1756 else
1757 return false;
1758}
1759
1760/* Compare two decision tree nodes N1 and N2 and return true if they
1761 are equal. */
1762
1763bool
1764decision_tree::cmp_node (dt_node *n1, dt_node *n2)
1765{
1766 if (!n1 || !n2 || n1->type != n2->type)
1767 return false;
1768
7e015fce 1769 if (n1 == n2)
3d2cf79f
RB
1770 return true;
1771
7e015fce
RB
1772 if (n1->type == dt_node::DT_TRUE)
1773 return false;
1774
3d2cf79f
RB
1775 if (n1->type == dt_node::DT_OPERAND)
1776 return cmp_operand ((as_a<dt_operand *> (n1))->op,
1777 (as_a<dt_operand *> (n2))->op);
1778 else if (n1->type == dt_node::DT_MATCH)
2eef1fc1
RB
1779 return (((as_a<dt_operand *> (n1))->match_dop
1780 == (as_a<dt_operand *> (n2))->match_dop)
1781 && ((as_a<dt_operand *> (n1))->value_match
1782 == (as_a<dt_operand *> (n2))->value_match));
3d2cf79f
RB
1783 return false;
1784}
1785
1786/* Search OPS for a decision tree node like P and return it if found. */
1787
1788dt_node *
1789decision_tree::find_node (vec<dt_node *>& ops, dt_node *p)
1790{
7e015fce
RB
1791 /* We can merge adjacent DT_TRUE. */
1792 if (p->type == dt_node::DT_TRUE
1793 && !ops.is_empty ()
1794 && ops.last ()->type == dt_node::DT_TRUE)
1795 return ops.last ();
6c35e5b0 1796 dt_operand *true_node = NULL;
7e015fce
RB
1797 for (int i = ops.length () - 1; i >= 0; --i)
1798 {
1799 /* But we can't merge across DT_TRUE nodes as they serve as
1800 pattern order barriers to make sure that patterns apply
1801 in order of appearance in case multiple matches are possible. */
1802 if (ops[i]->type == dt_node::DT_TRUE)
6c35e5b0
RB
1803 {
1804 if (! true_node
1805 || as_a <dt_operand *> (ops[i])->for_id > true_node->for_id)
1806 true_node = as_a <dt_operand *> (ops[i]);
1807 }
7e015fce 1808 if (decision_tree::cmp_node (ops[i], p))
6c35e5b0
RB
1809 {
1810 /* Unless we are processing the same pattern or the blocking
1811 pattern is before the one we are going to merge with. */
1812 if (true_node
1813 && true_node->for_id != current_id
1814 && true_node->for_id > as_a <dt_operand *> (ops[i])->for_id)
1815 {
1816 if (verbose >= 1)
1817 {
620e594b 1818 location_t p_loc = 0;
6c35e5b0
RB
1819 if (p->type == dt_node::DT_OPERAND)
1820 p_loc = as_a <dt_operand *> (p)->op->location;
620e594b 1821 location_t op_loc = 0;
6c35e5b0
RB
1822 if (ops[i]->type == dt_node::DT_OPERAND)
1823 op_loc = as_a <dt_operand *> (ops[i])->op->location;
620e594b 1824 location_t true_loc = 0;
6c35e5b0
RB
1825 true_loc = true_node->op->location;
1826 warning_at (p_loc,
1827 "failed to merge decision tree node");
1828 warning_at (op_loc,
1829 "with the following");
1830 warning_at (true_loc,
1831 "because of the following which serves as ordering "
1832 "barrier");
1833 }
1834 return NULL;
1835 }
1836 return ops[i];
1837 }
7e015fce 1838 }
3d2cf79f
RB
1839 return NULL;
1840}
1841
1842/* Append N to the decision tree if it there is not already an existing
1843 identical child. */
1844
1845dt_node *
1846dt_node::append_node (dt_node *n)
1847{
1848 dt_node *kid;
1849
1850 kid = decision_tree::find_node (kids, n);
1851 if (kid)
1852 return kid;
1853
1854 kids.safe_push (n);
1855 n->level = this->level + 1;
1856
3d2cf79f
RB
1857 return n;
1858}
1859
1860/* Append OP to the decision tree. */
1861
1862dt_node *
1863dt_node::append_op (operand *op, dt_node *parent, unsigned pos)
1864{
1865 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
1866 dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos);
1867 return append_node (n);
1868}
1869
1870/* Append a DT_TRUE decision tree node. */
1871
1872dt_node *
6c35e5b0 1873dt_node::append_true_op (operand *op, dt_node *parent, unsigned pos)
3d2cf79f 1874{
5609420f 1875 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
6c35e5b0 1876 dt_operand *n = new dt_operand (DT_TRUE, op, 0, parent_, pos);
3d2cf79f
RB
1877 return append_node (n);
1878}
1879
1880/* Append a DT_MATCH decision tree node. */
1881
1882dt_node *
6c35e5b0
RB
1883dt_node::append_match_op (operand *op, dt_operand *match_dop,
1884 dt_node *parent, unsigned pos)
3d2cf79f
RB
1885{
1886 dt_operand *parent_ = as_a<dt_operand *> (parent);
6c35e5b0 1887 dt_operand *n = new dt_operand (DT_MATCH, op, match_dop, parent_, pos);
3d2cf79f
RB
1888 return append_node (n);
1889}
1890
1891/* Append S to the decision tree. */
1892
1893dt_node *
1894dt_node::append_simplify (simplify *s, unsigned pattern_no,
1895 dt_operand **indexes)
1896{
ae9c3507 1897 dt_simplify *s2;
3d2cf79f 1898 dt_simplify *n = new dt_simplify (s, pattern_no, indexes);
eee7b6c4 1899 for (unsigned i = 0; i < kids.length (); ++i)
ae9c3507
ML
1900 if ((s2 = dyn_cast <dt_simplify *> (kids[i]))
1901 && (verbose >= 1
1902 || s->match->location != s2->s->match->location))
eee7b6c4 1903 {
ae9c3507
ML
1904 /* With a nested patters, it's hard to avoid these in order
1905 to keep match.pd rules relatively small. */
eee7b6c4
RB
1906 warning_at (s->match->location, "duplicate pattern");
1907 warning_at (s2->s->match->location, "previous pattern defined here");
1908 print_operand (s->match, stderr);
1909 fprintf (stderr, "\n");
1910 }
3d2cf79f
RB
1911 return append_node (n);
1912}
1913
803835de
RB
1914/* Analyze the node and its children. */
1915
1916void
03038b8b 1917dt_node::analyze (sinfo_map_t &map)
803835de
RB
1918{
1919 num_leafs = 0;
1920 total_size = 1;
1921 max_level = level;
1922
1923 if (type == DT_SIMPLIFY)
1924 {
03038b8b
RB
1925 /* Populate the map of equivalent simplifies. */
1926 dt_simplify *s = as_a <dt_simplify *> (this);
1927 bool existed;
1928 sinfo *&si = map.get_or_insert (s, &existed);
1929 if (!existed)
1930 {
1931 si = new sinfo;
1932 si->s = s;
1933 si->cnt = 1;
1934 si->fname = NULL;
1935 }
1936 else
1937 si->cnt++;
1938 s->info = si;
803835de
RB
1939 num_leafs = 1;
1940 return;
1941 }
1942
1943 for (unsigned i = 0; i < kids.length (); ++i)
1944 {
03038b8b 1945 kids[i]->analyze (map);
803835de
RB
1946 num_leafs += kids[i]->num_leafs;
1947 total_size += kids[i]->total_size;
1948 max_level = MAX (max_level, kids[i]->max_level);
1949 }
1950}
1951
3d2cf79f
RB
1952/* Insert O into the decision tree and return the decision tree node found
1953 or created. */
1954
1955dt_node *
1956decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes,
1957 unsigned pos, dt_node *parent)
1958{
1959 dt_node *q, *elm = 0;
1960
1961 if (capture *c = dyn_cast<capture *> (o))
1962 {
1963 unsigned capt_index = c->where;
1964
1965 if (indexes[capt_index] == 0)
1966 {
1967 if (c->what)
1968 q = insert_operand (p, c->what, indexes, pos, parent);
1969 else
1970 {
6c35e5b0 1971 q = elm = p->append_true_op (o, parent, pos);
3d2cf79f
RB
1972 goto at_assert_elm;
1973 }
1974 // get to the last capture
1975 for (operand *what = c->what;
1976 what && is_a<capture *> (what);
1977 c = as_a<capture *> (what), what = c->what)
1978 ;
1979
1980 if (!c->what)
1981 {
1982 unsigned cc_index = c->where;
1983 dt_operand *match_op = indexes[cc_index];
1984
6c35e5b0 1985 dt_operand temp (dt_node::DT_TRUE, 0, 0, 0, 0);
3d2cf79f
RB
1986 elm = decision_tree::find_node (p->kids, &temp);
1987
1988 if (elm == 0)
1989 {
4e4791ff
BE
1990 dt_operand match (dt_node::DT_MATCH, 0, match_op, 0, 0);
1991 match.value_match = c->value_match;
1992 elm = decision_tree::find_node (p->kids, &match);
3d2cf79f
RB
1993 }
1994 }
1995 else
1996 {
6c35e5b0 1997 dt_operand temp (dt_node::DT_OPERAND, c->what, 0, 0, 0);
3d2cf79f
RB
1998 elm = decision_tree::find_node (p->kids, &temp);
1999 }
2000
2001at_assert_elm:
2002 gcc_assert (elm->type == dt_node::DT_TRUE
2003 || elm->type == dt_node::DT_OPERAND
2004 || elm->type == dt_node::DT_MATCH);
2005 indexes[capt_index] = static_cast<dt_operand *> (elm);
2006 return q;
2007 }
2008 else
2009 {
6c35e5b0 2010 p = p->append_match_op (o, indexes[capt_index], parent, pos);
2eef1fc1 2011 as_a <dt_operand *>(p)->value_match = c->value_match;
3d2cf79f
RB
2012 if (c->what)
2013 return insert_operand (p, c->what, indexes, 0, p);
2014 else
2015 return p;
2016 }
2017 }
2018 p = p->append_op (o, parent, pos);
2019 q = p;
2020
2021 if (expr *e = dyn_cast <expr *>(o))
2022 {
2023 for (unsigned i = 0; i < e->ops.length (); ++i)
2024 q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p);
2025 }
2026
2027 return q;
2028}
2029
2030/* Insert S into the decision tree. */
2031
2032void
99b1c316 2033decision_tree::insert (class simplify *s, unsigned pattern_no)
3d2cf79f 2034{
6c35e5b0 2035 current_id = s->id;
3d2cf79f
RB
2036 dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1);
2037 dt_node *p = decision_tree::insert_operand (root, s->match, indexes);
2038 p->append_simplify (s, pattern_no, indexes);
2039}
2040
2041/* Debug functions to dump the decision tree. */
2042
2043DEBUG_FUNCTION void
2044decision_tree::print_node (dt_node *p, FILE *f, unsigned indent)
2045{
2046 if (p->type == dt_node::DT_NODE)
2047 fprintf (f, "root");
2048 else
2049 {
2050 fprintf (f, "|");
2051 for (unsigned i = 0; i < indent; i++)
2052 fprintf (f, "-");
2053
2054 if (p->type == dt_node::DT_OPERAND)
2055 {
2056 dt_operand *dop = static_cast<dt_operand *>(p);
2057 print_operand (dop->op, f, true);
2058 }
2059 else if (p->type == dt_node::DT_TRUE)
2060 fprintf (f, "true");
2061 else if (p->type == dt_node::DT_MATCH)
2062 fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop));
2063 else if (p->type == dt_node::DT_SIMPLIFY)
2064 {
2065 dt_simplify *s = static_cast<dt_simplify *> (p);
2066 fprintf (f, "simplify_%u { ", s->pattern_no);
2067 for (int i = 0; i <= s->s->capture_max; ++i)
2068 fprintf (f, "%p, ", (void *) s->indexes[i]);
2069 fprintf (f, " } ");
2070 }
6c35e5b0
RB
2071 if (is_a <dt_operand *> (p))
2072 fprintf (f, " [%u]", as_a <dt_operand *> (p)->for_id);
3d2cf79f
RB
2073 }
2074
6c35e5b0
RB
2075 fprintf (stderr, " (%p, %p), %u, %u\n",
2076 (void *) p, (void *) p->parent, p->level, p->kids.length ());
3d2cf79f
RB
2077
2078 for (unsigned i = 0; i < p->kids.length (); ++i)
2079 decision_tree::print_node (p->kids[i], f, indent + 2);
2080}
2081
2082DEBUG_FUNCTION void
2083decision_tree::print (FILE *f)
2084{
2085 return decision_tree::print_node (root, f);
2086}
2087
2088
47b25362
RB
2089/* For GENERIC we have to take care of wrapping multiple-used
2090 expressions with side-effects in save_expr and preserve side-effects
2091 of expressions with omit_one_operand. Analyze captures in
2092 match, result and with expressions and perform early-outs
2093 on the outermost match expression operands for cases we cannot
2094 handle. */
2095
6c1dae73 2096class capture_info
47b25362 2097{
6c1dae73 2098public:
53a19317 2099 capture_info (simplify *s, operand *, bool);
47b25362 2100 void walk_match (operand *o, unsigned toplevel_arg, bool, bool);
8fdc6c67 2101 bool walk_result (operand *o, bool, operand *);
47b25362
RB
2102 void walk_c_expr (c_expr *);
2103
2104 struct cinfo
2105 {
2106 bool expr_p;
2107 bool cse_p;
2108 bool force_no_side_effects_p;
44fc0a51 2109 bool force_single_use;
47b25362
RB
2110 bool cond_expr_cond_p;
2111 unsigned long toplevel_msk;
2d3f4bf7
RB
2112 unsigned match_use_count;
2113 unsigned result_use_count;
fa138f6e 2114 unsigned same_as;
53a19317 2115 capture *c;
47b25362
RB
2116 };
2117
2118 auto_vec<cinfo> info;
2119 unsigned long force_no_side_effects;
53a19317 2120 bool gimple;
47b25362
RB
2121};
2122
2123/* Analyze captures in S. */
2124
53a19317 2125capture_info::capture_info (simplify *s, operand *result, bool gimple_)
47b25362 2126{
53a19317
RB
2127 gimple = gimple_;
2128
47b25362 2129 expr *e;
8fdc6c67 2130 if (s->kind == simplify::MATCH)
47b25362
RB
2131 {
2132 force_no_side_effects = -1;
2133 return;
2134 }
2135
2136 force_no_side_effects = 0;
2137 info.safe_grow_cleared (s->capture_max + 1);
fa138f6e
RB
2138 for (int i = 0; i <= s->capture_max; ++i)
2139 info[i].same_as = i;
2140
47b25362
RB
2141 e = as_a <expr *> (s->match);
2142 for (unsigned i = 0; i < e->ops.length (); ++i)
2143 walk_match (e->ops[i], i,
2144 (i != 0 && *e->operation == COND_EXPR)
2145 || *e->operation == TRUTH_ANDIF_EXPR
2146 || *e->operation == TRUTH_ORIF_EXPR,
2147 i == 0
2148 && (*e->operation == COND_EXPR
2149 || *e->operation == VEC_COND_EXPR));
2150
8fdc6c67 2151 walk_result (s->result, false, result);
47b25362
RB
2152}
2153
2154/* Analyze captures in the match expression piece O. */
2155
2156void
2157capture_info::walk_match (operand *o, unsigned toplevel_arg,
2158 bool conditional_p, bool cond_expr_cond_p)
2159{
2160 if (capture *c = dyn_cast <capture *> (o))
2161 {
2d799646 2162 unsigned where = c->where;
2d3f4bf7 2163 info[where].match_use_count++;
2d799646
RB
2164 info[where].toplevel_msk |= 1 << toplevel_arg;
2165 info[where].force_no_side_effects_p |= conditional_p;
2166 info[where].cond_expr_cond_p |= cond_expr_cond_p;
53a19317
RB
2167 if (!info[where].c)
2168 info[where].c = c;
2d799646
RB
2169 if (!c->what)
2170 return;
2171 /* Recurse to exprs and captures. */
2172 if (is_a <capture *> (c->what)
2173 || is_a <expr *> (c->what))
2174 walk_match (c->what, toplevel_arg, conditional_p, false);
2175 /* We need to look past multiple captures to find a captured
2176 expression as with conditional converts two captures
fa138f6e
RB
2177 can be collapsed onto the same expression. Also collect
2178 what captures capture the same thing. */
2d799646 2179 while (c->what && is_a <capture *> (c->what))
fa138f6e
RB
2180 {
2181 c = as_a <capture *> (c->what);
2182 if (info[c->where].same_as != c->where
2183 && info[c->where].same_as != info[where].same_as)
2184 fatal_at (c->location, "cannot handle this collapsed capture");
2185 info[c->where].same_as = info[where].same_as;
2186 }
2d799646 2187 /* Mark expr (non-leaf) captures and forced single-use exprs. */
44fc0a51 2188 expr *e;
47b25362 2189 if (c->what
44fc0a51 2190 && (e = dyn_cast <expr *> (c->what)))
47b25362 2191 {
9348eb67
RB
2192 /* Zero-operand expression captures like ADDR_EXPR@0 are
2193 similar as predicates -- if they are not mentioned in
2194 the result we have to force them to have no side-effects. */
2195 if (e->ops.length () != 0)
2196 info[where].expr_p = true;
2d799646 2197 info[where].force_single_use |= e->force_single_use;
47b25362
RB
2198 }
2199 }
2200 else if (expr *e = dyn_cast <expr *> (o))
2201 {
2202 for (unsigned i = 0; i < e->ops.length (); ++i)
2203 {
2204 bool cond_p = conditional_p;
4e4791ff 2205 bool expr_cond_p = false;
47b25362
RB
2206 if (i != 0 && *e->operation == COND_EXPR)
2207 cond_p = true;
2208 else if (*e->operation == TRUTH_ANDIF_EXPR
2209 || *e->operation == TRUTH_ORIF_EXPR)
2210 cond_p = true;
2211 if (i == 0
2212 && (*e->operation == COND_EXPR
2213 || *e->operation == VEC_COND_EXPR))
4e4791ff
BE
2214 expr_cond_p = true;
2215 walk_match (e->ops[i], toplevel_arg, cond_p, expr_cond_p);
47b25362
RB
2216 }
2217 }
2218 else if (is_a <predicate *> (o))
2219 {
2220 /* Mark non-captured leafs toplevel arg for checking. */
2221 force_no_side_effects |= 1 << toplevel_arg;
53a19317
RB
2222 if (verbose >= 1
2223 && !gimple)
2224 warning_at (o->location,
2225 "forcing no side-effects on possibly lost leaf");
47b25362
RB
2226 }
2227 else
2228 gcc_unreachable ();
2229}
2230
8fdc6c67
RB
2231/* Analyze captures in the result expression piece O. Return true
2232 if RESULT was visited in one of the children. Only visit
2233 non-if/with children if they are rooted on RESULT. */
47b25362 2234
8fdc6c67
RB
2235bool
2236capture_info::walk_result (operand *o, bool conditional_p, operand *result)
47b25362
RB
2237{
2238 if (capture *c = dyn_cast <capture *> (o))
2239 {
fa138f6e
RB
2240 unsigned where = info[c->where].same_as;
2241 info[where].result_use_count++;
47b25362
RB
2242 /* If we substitute an expression capture we don't know
2243 which captures this will end up using (well, we don't
2244 compute that). Force the uses to be side-effect free
2245 which means forcing the toplevels that reach the
2246 expression side-effect free. */
fa138f6e
RB
2247 if (info[where].expr_p)
2248 force_no_side_effects |= info[where].toplevel_msk;
026c3cfd 2249 /* Mark CSE capture uses as forced to have no side-effects. */
47b25362
RB
2250 if (c->what
2251 && is_a <expr *> (c->what))
2252 {
fa138f6e 2253 info[where].cse_p = true;
8fdc6c67 2254 walk_result (c->what, true, result);
47b25362
RB
2255 }
2256 }
2257 else if (expr *e = dyn_cast <expr *> (o))
2258 {
d4b71b95
RB
2259 id_base *opr = e->operation;
2260 if (user_id *uid = dyn_cast <user_id *> (opr))
2261 opr = uid->substitutes[0];
47b25362
RB
2262 for (unsigned i = 0; i < e->ops.length (); ++i)
2263 {
2264 bool cond_p = conditional_p;
2265 if (i != 0 && *e->operation == COND_EXPR)
2266 cond_p = true;
2267 else if (*e->operation == TRUTH_ANDIF_EXPR
2268 || *e->operation == TRUTH_ORIF_EXPR)
2269 cond_p = true;
8fdc6c67
RB
2270 walk_result (e->ops[i], cond_p, result);
2271 }
2272 }
4e4791ff 2273 else if (if_expr *ie = dyn_cast <if_expr *> (o))
8fdc6c67
RB
2274 {
2275 /* 'if' conditions should be all fine. */
4e4791ff 2276 if (ie->trueexpr == result)
8fdc6c67 2277 {
4e4791ff 2278 walk_result (ie->trueexpr, false, result);
8fdc6c67
RB
2279 return true;
2280 }
4e4791ff 2281 if (ie->falseexpr == result)
8fdc6c67 2282 {
4e4791ff 2283 walk_result (ie->falseexpr, false, result);
8fdc6c67 2284 return true;
47b25362 2285 }
8fdc6c67 2286 bool res = false;
4e4791ff
BE
2287 if (is_a <if_expr *> (ie->trueexpr)
2288 || is_a <with_expr *> (ie->trueexpr))
2289 res |= walk_result (ie->trueexpr, false, result);
2290 if (ie->falseexpr
2291 && (is_a <if_expr *> (ie->falseexpr)
2292 || is_a <with_expr *> (ie->falseexpr)))
2293 res |= walk_result (ie->falseexpr, false, result);
8fdc6c67
RB
2294 return res;
2295 }
4e4791ff 2296 else if (with_expr *we = dyn_cast <with_expr *> (o))
8fdc6c67 2297 {
4e4791ff 2298 bool res = (we->subexpr == result);
8fdc6c67 2299 if (res
4e4791ff
BE
2300 || is_a <if_expr *> (we->subexpr)
2301 || is_a <with_expr *> (we->subexpr))
2302 res |= walk_result (we->subexpr, false, result);
8fdc6c67 2303 if (res)
4e4791ff 2304 walk_c_expr (we->with);
8fdc6c67 2305 return res;
47b25362 2306 }
4e4791ff
BE
2307 else if (c_expr *ce = dyn_cast <c_expr *> (o))
2308 walk_c_expr (ce);
47b25362
RB
2309 else
2310 gcc_unreachable ();
8fdc6c67
RB
2311
2312 return false;
47b25362
RB
2313}
2314
2315/* Look for captures in the C expr E. */
2316
2317void
2318capture_info::walk_c_expr (c_expr *e)
2319{
53a19317
RB
2320 /* Give up for C exprs mentioning captures not inside TREE_TYPE,
2321 TREE_REAL_CST, TREE_CODE or a predicate where they cannot
2322 really escape through. */
47b25362
RB
2323 unsigned p_depth = 0;
2324 for (unsigned i = 0; i < e->code.length (); ++i)
2325 {
2326 const cpp_token *t = &e->code[i];
2327 const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL;
53a19317 2328 id_base *id;
47b25362 2329 if (t->type == CPP_NAME
53a19317
RB
2330 && (strcmp ((const char *)CPP_HASHNODE
2331 (t->val.node.node)->ident.str, "TREE_TYPE") == 0
2332 || strcmp ((const char *)CPP_HASHNODE
2333 (t->val.node.node)->ident.str, "TREE_CODE") == 0
2334 || strcmp ((const char *)CPP_HASHNODE
2335 (t->val.node.node)->ident.str, "TREE_REAL_CST") == 0
2336 || ((id = get_operator ((const char *)CPP_HASHNODE
2337 (t->val.node.node)->ident.str))
2338 && is_a <predicate_id *> (id)))
47b25362
RB
2339 && n->type == CPP_OPEN_PAREN)
2340 p_depth++;
2341 else if (t->type == CPP_CLOSE_PAREN
2342 && p_depth > 0)
2343 p_depth--;
2344 else if (p_depth == 0
2345 && t->type == CPP_ATSIGN
2346 && (n->type == CPP_NUMBER
2347 || n->type == CPP_NAME)
2348 && !(n->flags & PREV_WHITE))
2349 {
4e4791ff 2350 const char *id1;
47b25362 2351 if (n->type == CPP_NUMBER)
4e4791ff 2352 id1 = (const char *)n->val.str.text;
47b25362 2353 else
4e4791ff
BE
2354 id1 = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
2355 unsigned *where = e->capture_ids->get(id1);
496326bc 2356 if (! where)
4e4791ff 2357 fatal_at (n, "unknown capture id '%s'", id1);
496326bc 2358 info[info[*where].same_as].force_no_side_effects_p = true;
53a19317
RB
2359 if (verbose >= 1
2360 && !gimple)
2361 warning_at (t, "capture escapes");
47b25362
RB
2362 }
2363 }
2364}
2365
3d2cf79f
RB
2366
2367/* Code generation off the decision tree and the refered AST nodes. */
2368
2369bool
2370is_conversion (id_base *op)
2371{
2372 return (*op == CONVERT_EXPR
2373 || *op == NOP_EXPR
2374 || *op == FLOAT_EXPR
2375 || *op == FIX_TRUNC_EXPR
2376 || *op == VIEW_CONVERT_EXPR);
2377}
2378
f00b6283 2379/* Get the type to be used for generating operand POS of OP from the
3d2cf79f
RB
2380 various sources. */
2381
2382static const char *
f00b6283
RB
2383get_operand_type (id_base *op, unsigned pos,
2384 const char *in_type,
3d2cf79f
RB
2385 const char *expr_type,
2386 const char *other_oprnd_type)
2387{
2388 /* Generally operands whose type does not match the type of the
2389 expression generated need to know their types but match and
2390 thus can fall back to 'other_oprnd_type'. */
2391 if (is_conversion (op))
2392 return other_oprnd_type;
2393 else if (*op == REALPART_EXPR
2394 || *op == IMAGPART_EXPR)
2395 return other_oprnd_type;
2396 else if (is_a <operator_id *> (op)
2397 && strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0)
2398 return other_oprnd_type;
f00b6283
RB
2399 else if (*op == COND_EXPR
2400 && pos == 0)
2401 return "boolean_type_node";
0d2b3bca
RS
2402 else if (strncmp (op->id, "CFN_COND_", 9) == 0)
2403 {
2404 /* IFN_COND_* operands 1 and later by default have the same type
2405 as the result. The type of operand 0 needs to be specified
2406 explicitly. */
2407 if (pos > 0 && expr_type)
2408 return expr_type;
2409 else if (pos > 0 && in_type)
2410 return in_type;
2411 else
2412 return NULL;
2413 }
3d2cf79f
RB
2414 else
2415 {
2416 /* Otherwise all types should match - choose one in order of
2417 preference. */
2418 if (expr_type)
2419 return expr_type;
2420 else if (in_type)
2421 return in_type;
2422 else
2423 return other_oprnd_type;
2424 }
2425}
2426
2427/* Generate transform code for an expression. */
2428
2429void
c551c21d
MM
2430expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
2431 int depth, const char *in_type, capture_info *cinfo,
a3ca1bc5 2432 dt_operand **indexes, int)
3d2cf79f 2433{
d4b71b95
RB
2434 id_base *opr = operation;
2435 /* When we delay operator substituting during lowering of fors we
2436 make sure that for code-gen purposes the effects of each substitute
2437 are the same. Thus just look at that. */
2438 if (user_id *uid = dyn_cast <user_id *> (opr))
2439 opr = uid->substitutes[0];
2440
2441 bool conversion_p = is_conversion (opr);
3d2cf79f
RB
2442 const char *type = expr_type;
2443 char optype[64];
2444 if (type)
2445 /* If there was a type specification in the pattern use it. */
2446 ;
2447 else if (conversion_p)
2448 /* For conversions we need to build the expression using the
2449 outer type passed in. */
2450 type = in_type;
d4b71b95
RB
2451 else if (*opr == REALPART_EXPR
2452 || *opr == IMAGPART_EXPR)
3d2cf79f
RB
2453 {
2454 /* __real and __imag use the component type of its operand. */
4e4791ff
BE
2455 snprintf (optype, sizeof (optype), "TREE_TYPE (TREE_TYPE (_o%d[0]))",
2456 depth);
3d2cf79f
RB
2457 type = optype;
2458 }
d4b71b95
RB
2459 else if (is_a <operator_id *> (opr)
2460 && !strcmp (as_a <operator_id *> (opr)->tcc, "tcc_comparison"))
3d2cf79f
RB
2461 {
2462 /* comparisons use boolean_type_node (or what gets in), but
2463 their operands need to figure out the types themselves. */
30934c5d
RB
2464 if (in_type)
2465 type = in_type;
2466 else
2467 {
4e4791ff 2468 snprintf (optype, sizeof (optype), "boolean_type_node");
30934c5d
RB
2469 type = optype;
2470 }
2471 in_type = NULL;
3d2cf79f 2472 }
d4b71b95 2473 else if (*opr == COND_EXPR
0d2b3bca
RS
2474 || *opr == VEC_COND_EXPR
2475 || strncmp (opr->id, "CFN_COND_", 9) == 0)
7026b8df
MG
2476 {
2477 /* Conditions are of the same type as their first alternative. */
4e4791ff 2478 snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[1])", depth);
7026b8df
MG
2479 type = optype;
2480 }
3d2cf79f
RB
2481 else
2482 {
2483 /* Other operations are of the same type as their first operand. */
4e4791ff 2484 snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[0])", depth);
3d2cf79f
RB
2485 type = optype;
2486 }
2487 if (!type)
38b52b2f 2488 fatal_at (location, "cannot determine type of operand");
3d2cf79f 2489
c551c21d
MM
2490 fprintf_indent (f, indent, "{\n");
2491 indent += 2;
4e4791ff
BE
2492 fprintf_indent (f, indent,
2493 "tree _o%d[%u], _r%d;\n", depth, ops.length (), depth);
3d2cf79f 2494 char op0type[64];
4e4791ff 2495 snprintf (op0type, sizeof (op0type), "TREE_TYPE (_o%d[0])", depth);
3d2cf79f
RB
2496 for (unsigned i = 0; i < ops.length (); ++i)
2497 {
4e4791ff
BE
2498 char dest1[32];
2499 snprintf (dest1, sizeof (dest1), "_o%d[%u]", depth, i);
2500 const char *optype1
f00b6283 2501 = get_operand_type (opr, i, in_type, expr_type,
3d2cf79f 2502 i == 0 ? NULL : op0type);
4e4791ff 2503 ops[i]->gen_transform (f, indent, dest1, gimple, depth + 1, optype1,
c551c21d 2504 cinfo, indexes,
a3ca1bc5
RB
2505 (*opr == COND_EXPR
2506 || *opr == VEC_COND_EXPR) && i == 0 ? 1 : 2);
3d2cf79f
RB
2507 }
2508
d4b71b95 2509 const char *opr_name;
32dfd2e0 2510 if (*operation == CONVERT_EXPR)
d4b71b95 2511 opr_name = "NOP_EXPR";
32dfd2e0 2512 else
d4b71b95 2513 opr_name = operation->id;
32dfd2e0 2514
3d2cf79f
RB
2515 if (gimple)
2516 {
d4b71b95 2517 if (*opr == CONVERT_EXPR)
c551c21d
MM
2518 {
2519 fprintf_indent (f, indent,
4e4791ff 2520 "if (%s != TREE_TYPE (_o%d[0])\n",
c551c21d
MM
2521 type, depth);
2522 fprintf_indent (f, indent,
4e4791ff
BE
2523 " && !useless_type_conversion_p (%s, TREE_TYPE "
2524 "(_o%d[0])))\n",
c551c21d
MM
2525 type, depth);
2526 fprintf_indent (f, indent + 2, "{\n");
2527 indent += 4;
2528 }
c09a3914
RB
2529 /* ??? Building a stmt can fail for various reasons here, seq being
2530 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
2531 So if we fail here we should continue matching other patterns. */
6a86928d
RS
2532 fprintf_indent (f, indent, "gimple_match_op tem_op "
2533 "(res_op->cond.any_else (), %s, %s", opr_name, type);
3d2cf79f 2534 for (unsigned i = 0; i < ops.length (); ++i)
4e4791ff 2535 fprintf (f, ", _o%d[%u]", depth, i);
5d75ad95 2536 fprintf (f, ");\n");
c551c21d 2537 fprintf_indent (f, indent,
5d75ad95
RS
2538 "gimple_resimplify%d (lseq, &tem_op, valueize);\n",
2539 ops.length ());
c551c21d 2540 fprintf_indent (f, indent,
4e4791ff
BE
2541 "_r%d = maybe_push_res_to_seq (&tem_op, lseq);\n",
2542 depth);
c551c21d 2543 fprintf_indent (f, indent,
4e4791ff
BE
2544 "if (!_r%d) return false;\n",
2545 depth);
d4b71b95 2546 if (*opr == CONVERT_EXPR)
c551c21d
MM
2547 {
2548 indent -= 4;
2549 fprintf_indent (f, indent, " }\n");
2550 fprintf_indent (f, indent, "else\n");
4e4791ff 2551 fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
c551c21d 2552 }
3d2cf79f
RB
2553 }
2554 else
2555 {
d4b71b95 2556 if (*opr == CONVERT_EXPR)
c551c21d 2557 {
4e4791ff 2558 fprintf_indent (f, indent, "if (TREE_TYPE (_o%d[0]) != %s)\n",
c551c21d
MM
2559 depth, type);
2560 indent += 2;
2561 }
d4b71b95 2562 if (opr->kind == id_base::CODE)
4e4791ff
BE
2563 fprintf_indent (f, indent, "_r%d = fold_build%d_loc (loc, %s, %s",
2564 depth, ops.length(), opr_name, type);
3d2cf79f 2565 else
e4cdf6a8
RB
2566 {
2567 fprintf_indent (f, indent, "{\n");
4e4791ff
BE
2568 fprintf_indent (f, indent, " _r%d = maybe_build_call_expr_loc (loc, "
2569 "%s, %s, %d", depth, opr_name, type, ops.length());
e4cdf6a8 2570 }
3d2cf79f 2571 for (unsigned i = 0; i < ops.length (); ++i)
4e4791ff 2572 fprintf (f, ", _o%d[%u]", depth, i);
3d2cf79f 2573 fprintf (f, ");\n");
e4cdf6a8 2574 if (opr->kind != id_base::CODE)
c9e926ce 2575 {
4e4791ff 2576 fprintf_indent (f, indent, " if (!_r%d)\n", depth);
c9e926ce
RS
2577 fprintf_indent (f, indent, " return NULL_TREE;\n");
2578 fprintf_indent (f, indent, "}\n");
2579 }
d4b71b95 2580 if (*opr == CONVERT_EXPR)
c551c21d
MM
2581 {
2582 indent -= 2;
2583 fprintf_indent (f, indent, "else\n");
4e4791ff 2584 fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
c551c21d 2585 }
3d2cf79f 2586 }
4e4791ff 2587 fprintf_indent (f, indent, "%s = _r%d;\n", dest, depth);
c551c21d
MM
2588 indent -= 2;
2589 fprintf_indent (f, indent, "}\n");
3d2cf79f
RB
2590}
2591
2592/* Generate code for a c_expr which is either the expression inside
2593 an if statement or a sequence of statements which computes a
2594 result to be stored to DEST. */
2595
2596void
c551c21d 2597c_expr::gen_transform (FILE *f, int indent, const char *dest,
47b25362 2598 bool, int, const char *, capture_info *,
a3ca1bc5 2599 dt_operand **, int)
3d2cf79f
RB
2600{
2601 if (dest && nr_stmts == 1)
c551c21d 2602 fprintf_indent (f, indent, "%s = ", dest);
3d2cf79f
RB
2603
2604 unsigned stmt_nr = 1;
2605 for (unsigned i = 0; i < code.length (); ++i)
2606 {
2607 const cpp_token *token = &code[i];
2608
2609 /* Replace captures for code-gen. */
2610 if (token->type == CPP_ATSIGN)
2611 {
2612 const cpp_token *n = &code[i+1];
2613 if ((n->type == CPP_NUMBER
2614 || n->type == CPP_NAME)
2615 && !(n->flags & PREV_WHITE))
2616 {
2617 if (token->flags & PREV_WHITE)
2618 fputc (' ', f);
2619 const char *id;
2620 if (n->type == CPP_NUMBER)
2621 id = (const char *)n->val.str.text;
2622 else
2623 id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
c56f494f
RB
2624 unsigned *cid = capture_ids->get (id);
2625 if (!cid)
2626 fatal_at (token, "unknown capture id");
2627 fprintf (f, "captures[%u]", *cid);
3d2cf79f
RB
2628 ++i;
2629 continue;
2630 }
2631 }
2632
2633 if (token->flags & PREV_WHITE)
2634 fputc (' ', f);
2635
2636 if (token->type == CPP_NAME)
2637 {
2638 const char *id = (const char *) NODE_NAME (token->val.node.node);
2639 unsigned j;
2640 for (j = 0; j < ids.length (); ++j)
2641 {
2642 if (strcmp (id, ids[j].id) == 0)
2643 {
2644 fprintf (f, "%s", ids[j].oper);
2645 break;
2646 }
2647 }
2648 if (j < ids.length ())
2649 continue;
2650 }
2651
2652 /* Output the token as string. */
2653 char *tk = (char *)cpp_token_as_text (r, token);
2654 fputs (tk, f);
2655
2656 if (token->type == CPP_SEMICOLON)
2657 {
2658 stmt_nr++;
c551c21d 2659 fputc ('\n', f);
3d2cf79f 2660 if (dest && stmt_nr == nr_stmts)
c551c21d 2661 fprintf_indent (f, indent, "%s = ", dest);
3d2cf79f
RB
2662 }
2663 }
2664}
2665
2666/* Generate transform code for a capture. */
2667
2668void
c551c21d
MM
2669capture::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
2670 int depth, const char *in_type, capture_info *cinfo,
a3ca1bc5 2671 dt_operand **indexes, int cond_handling)
3d2cf79f
RB
2672{
2673 if (what && is_a<expr *> (what))
2674 {
2675 if (indexes[where] == 0)
2676 {
2677 char buf[20];
4e4791ff 2678 snprintf (buf, sizeof (buf), "captures[%u]", where);
c551c21d
MM
2679 what->gen_transform (f, indent, buf, gimple, depth, in_type,
2680 cinfo, NULL);
3d2cf79f
RB
2681 }
2682 }
2683
e1201dff
JJ
2684 /* If in GENERIC some capture is used multiple times, unshare it except
2685 when emitting the last use. */
2686 if (!gimple
2687 && cinfo->info.exists ()
2688 && cinfo->info[cinfo->info[where].same_as].result_use_count > 1)
2689 {
2690 fprintf_indent (f, indent, "%s = unshare_expr (captures[%u]);\n",
2691 dest, where);
2692 cinfo->info[cinfo->info[where].same_as].result_use_count--;
2693 }
2694 else
2695 fprintf_indent (f, indent, "%s = captures[%u];\n", dest, where);
47b25362
RB
2696
2697 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
a3ca1bc5
RB
2698 with substituting a capture of that. */
2699 if (gimple
2700 && cond_handling != 0
47b25362 2701 && cinfo->info[where].cond_expr_cond_p)
c551c21d 2702 {
a3ca1bc5
RB
2703 /* If substituting into a cond_expr condition, unshare. */
2704 if (cond_handling == 1)
2705 fprintf_indent (f, indent, "%s = unshare_expr (%s);\n", dest, dest);
2706 /* If substituting elsewhere we might need to decompose it. */
2707 else if (cond_handling == 2)
2708 {
2709 /* ??? Returning false here will also not allow any other patterns
2710 to match unless this generator was split out. */
2711 fprintf_indent (f, indent, "if (COMPARISON_CLASS_P (%s))\n", dest);
2712 fprintf_indent (f, indent, " {\n");
2713 fprintf_indent (f, indent, " if (!seq) return false;\n");
2714 fprintf_indent (f, indent, " %s = gimple_build (seq,"
2715 " TREE_CODE (%s),"
2716 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
2717 " TREE_OPERAND (%s, 1));\n",
2718 dest, dest, dest, dest, dest);
2719 fprintf_indent (f, indent, " }\n");
2720 }
c551c21d 2721 }
3d2cf79f
RB
2722}
2723
2724/* Return the name of the operand representing the decision tree node.
2725 Use NAME as space to generate it. */
2726
2727char *
2728dt_operand::get_name (char *name)
2729{
6c35e5b0 2730 if (! parent)
3d2cf79f
RB
2731 sprintf (name, "t");
2732 else if (parent->level == 1)
4e4791ff 2733 sprintf (name, "_p%u", pos);
3d2cf79f 2734 else if (parent->type == dt_node::DT_MATCH)
6c35e5b0 2735 return as_a <dt_operand *> (parent)->get_name (name);
3d2cf79f 2736 else
4e4791ff 2737 sprintf (name, "_q%u%u", parent->level, pos);
3d2cf79f
RB
2738 return name;
2739}
2740
2741/* Fill NAME with the operand name at position POS. */
2742
2743void
2744dt_operand::gen_opname (char *name, unsigned pos)
2745{
6c35e5b0 2746 if (! parent)
4e4791ff 2747 sprintf (name, "_p%u", pos);
3d2cf79f 2748 else
4e4791ff 2749 sprintf (name, "_q%u%u", level, pos);
3d2cf79f
RB
2750}
2751
2752/* Generate matching code for the decision tree operand which is
2753 a predicate. */
2754
2755unsigned
c551c21d 2756dt_operand::gen_predicate (FILE *f, int indent, const char *opname, bool gimple)
3d2cf79f
RB
2757{
2758 predicate *p = as_a <predicate *> (op);
2759
2760 if (p->p->matchers.exists ())
2761 {
2762 /* If this is a predicate generated from a pattern mangle its
2763 name and pass on the valueize hook. */
2764 if (gimple)
c551c21d
MM
2765 fprintf_indent (f, indent, "if (gimple_%s (%s, valueize))\n",
2766 p->p->id, opname);
3d2cf79f 2767 else
c551c21d 2768 fprintf_indent (f, indent, "if (tree_%s (%s))\n", p->p->id, opname);
3d2cf79f
RB
2769 }
2770 else
c551c21d
MM
2771 fprintf_indent (f, indent, "if (%s (%s))\n", p->p->id, opname);
2772 fprintf_indent (f, indent + 2, "{\n");
3d2cf79f
RB
2773 return 1;
2774}
2775
2776/* Generate matching code for the decision tree operand which is
2777 a capture-match. */
2778
2779unsigned
2eef1fc1 2780dt_operand::gen_match_op (FILE *f, int indent, const char *opname, bool)
3d2cf79f
RB
2781{
2782 char match_opname[20];
2783 match_dop->get_name (match_opname);
2eef1fc1 2784 if (value_match)
1544db9a
BE
2785 fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
2786 "|| operand_equal_p (%s, %s, 0))\n",
2787 opname, match_opname, opname, opname, match_opname);
2eef1fc1 2788 else
1544db9a
BE
2789 fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
2790 "|| (operand_equal_p (%s, %s, 0) "
39bb7d01 2791 "&& types_match (%s, %s)))\n",
1544db9a 2792 opname, match_opname, opname, opname, match_opname,
39bb7d01 2793 opname, match_opname);
c551c21d 2794 fprintf_indent (f, indent + 2, "{\n");
3d2cf79f
RB
2795 return 1;
2796}
2797
2798/* Generate GIMPLE matching code for the decision tree operand. */
2799
2800unsigned
4e4791ff 2801dt_operand::gen_gimple_expr (FILE *f, int indent, int depth)
3d2cf79f
RB
2802{
2803 expr *e = static_cast<expr *> (op);
2804 id_base *id = e->operation;
2805 unsigned n_ops = e->ops.length ();
4f450a2b 2806 unsigned n_braces = 0;
3d2cf79f
RB
2807
2808 for (unsigned i = 0; i < n_ops; ++i)
2809 {
2810 char child_opname[20];
2811 gen_opname (child_opname, i);
2812
2813 if (id->kind == id_base::CODE)
2814 {
47b25362
RB
2815 if (e->is_generic
2816 || *id == REALPART_EXPR || *id == IMAGPART_EXPR
3d2cf79f
RB
2817 || *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR)
2818 {
2819 /* ??? If this is a memory operation we can't (and should not)
2820 match this. The only sensible operand types are
2821 SSA names and invariants. */
699acd5b
RB
2822 if (e->is_generic)
2823 {
2824 char opname[20];
2825 get_name (opname);
2826 fprintf_indent (f, indent,
2827 "tree %s = TREE_OPERAND (%s, %i);\n",
2828 child_opname, opname, i);
2829 }
2830 else
2831 fprintf_indent (f, indent,
2832 "tree %s = TREE_OPERAND "
4e4791ff
BE
2833 "(gimple_assign_rhs1 (_a%d), %i);\n",
2834 child_opname, depth, i);
c551c21d
MM
2835 fprintf_indent (f, indent,
2836 "if ((TREE_CODE (%s) == SSA_NAME\n",
2837 child_opname);
2838 fprintf_indent (f, indent,
4f450a2b 2839 " || is_gimple_min_invariant (%s)))\n",
c551c21d 2840 child_opname);
c551c21d
MM
2841 fprintf_indent (f, indent,
2842 " {\n");
2843 indent += 4;
4f450a2b
RB
2844 n_braces++;
2845 fprintf_indent (f, indent,
2846 "%s = do_valueize (valueize, %s);\n",
2847 child_opname, child_opname);
3d2cf79f
RB
2848 continue;
2849 }
2850 else
c551c21d 2851 fprintf_indent (f, indent,
4e4791ff
BE
2852 "tree %s = gimple_assign_rhs%u (_a%d);\n",
2853 child_opname, i + 1, depth);
3d2cf79f
RB
2854 }
2855 else
c551c21d 2856 fprintf_indent (f, indent,
4e4791ff
BE
2857 "tree %s = gimple_call_arg (_c%d, %u);\n",
2858 child_opname, depth, i);
c551c21d 2859 fprintf_indent (f, indent,
4f450a2b 2860 "%s = do_valueize (valueize, %s);\n",
c551c21d 2861 child_opname, child_opname);
3d2cf79f 2862 }
805a5406
RB
2863 /* While the toplevel operands are canonicalized by the caller
2864 after valueizing operands of sub-expressions we have to
2865 re-canonicalize operand order. */
c566cc9f
RS
2866 int opno = commutative_op (id);
2867 if (opno >= 0)
805a5406 2868 {
c566cc9f
RS
2869 char child_opname0[20], child_opname1[20];
2870 gen_opname (child_opname0, opno);
2871 gen_opname (child_opname1, opno + 1);
2872 fprintf_indent (f, indent,
2873 "if (tree_swap_operands_p (%s, %s))\n",
2874 child_opname0, child_opname1);
2875 fprintf_indent (f, indent,
2876 " std::swap (%s, %s);\n",
2877 child_opname0, child_opname1);
805a5406 2878 }
3d2cf79f 2879
4f450a2b 2880 return n_braces;
3d2cf79f
RB
2881}
2882
2883/* Generate GENERIC matching code for the decision tree operand. */
2884
2885unsigned
c551c21d 2886dt_operand::gen_generic_expr (FILE *f, int indent, const char *opname)
3d2cf79f
RB
2887{
2888 expr *e = static_cast<expr *> (op);
2889 unsigned n_ops = e->ops.length ();
2890
2891 for (unsigned i = 0; i < n_ops; ++i)
2892 {
2893 char child_opname[20];
2894 gen_opname (child_opname, i);
2895
2896 if (e->operation->kind == id_base::CODE)
c551c21d
MM
2897 fprintf_indent (f, indent, "tree %s = TREE_OPERAND (%s, %u);\n",
2898 child_opname, opname, i);
3d2cf79f 2899 else
c551c21d
MM
2900 fprintf_indent (f, indent, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2901 child_opname, opname, i);
3d2cf79f
RB
2902 }
2903
2904 return 0;
2905}
2906
2907/* Generate matching code for the children of the decision tree node. */
2908
2909void
4e4791ff 2910dt_node::gen_kids (FILE *f, int indent, bool gimple, int depth)
3d2cf79f
RB
2911{
2912 auto_vec<dt_operand *> gimple_exprs;
2913 auto_vec<dt_operand *> generic_exprs;
2914 auto_vec<dt_operand *> fns;
2915 auto_vec<dt_operand *> generic_fns;
2916 auto_vec<dt_operand *> preds;
2917 auto_vec<dt_node *> others;
3d2cf79f
RB
2918
2919 for (unsigned i = 0; i < kids.length (); ++i)
2920 {
2921 if (kids[i]->type == dt_node::DT_OPERAND)
2922 {
2923 dt_operand *op = as_a<dt_operand *> (kids[i]);
2924 if (expr *e = dyn_cast <expr *> (op->op))
2925 {
10230017
RB
2926 if (e->ops.length () == 0
2927 && (!gimple || !(*e->operation == CONSTRUCTOR)))
3d2cf79f
RB
2928 generic_exprs.safe_push (op);
2929 else if (e->operation->kind == id_base::FN)
2930 {
2931 if (gimple)
2932 fns.safe_push (op);
2933 else
2934 generic_fns.safe_push (op);
2935 }
2936 else if (e->operation->kind == id_base::PREDICATE)
2937 preds.safe_push (op);
2938 else
2939 {
c954de7f 2940 if (gimple && !e->is_generic)
3d2cf79f
RB
2941 gimple_exprs.safe_push (op);
2942 else
2943 generic_exprs.safe_push (op);
2944 }
2945 }
2946 else if (op->op->type == operand::OP_PREDICATE)
2947 others.safe_push (kids[i]);
2948 else
2949 gcc_unreachable ();
2950 }
69746629 2951 else if (kids[i]->type == dt_node::DT_SIMPLIFY)
3d2cf79f 2952 others.safe_push (kids[i]);
69746629
RB
2953 else if (kids[i]->type == dt_node::DT_MATCH
2954 || kids[i]->type == dt_node::DT_TRUE)
7e015fce
RB
2955 {
2956 /* A DT_TRUE operand serves as a barrier - generate code now
69746629
RB
2957 for what we have collected sofar.
2958 Like DT_TRUE, DT_MATCH serves as a barrier as it can cause
2959 dependent matches to get out-of-order. Generate code now
7e015fce 2960 for what we have collected sofar. */
4e4791ff 2961 gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
7e015fce
RB
2962 fns, generic_fns, preds, others);
2963 /* And output the true operand itself. */
4e4791ff 2964 kids[i]->gen (f, indent, gimple, depth);
7e015fce
RB
2965 gimple_exprs.truncate (0);
2966 generic_exprs.truncate (0);
2967 fns.truncate (0);
2968 generic_fns.truncate (0);
2969 preds.truncate (0);
2970 others.truncate (0);
2971 }
3d2cf79f
RB
2972 else
2973 gcc_unreachable ();
2974 }
2975
7e015fce 2976 /* Generate code for the remains. */
4e4791ff 2977 gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
7e015fce
RB
2978 fns, generic_fns, preds, others);
2979}
2980
2981/* Generate matching code for the children of the decision tree node. */
2982
2983void
4e4791ff 2984dt_node::gen_kids_1 (FILE *f, int indent, bool gimple, int depth,
7e015fce
RB
2985 vec<dt_operand *> gimple_exprs,
2986 vec<dt_operand *> generic_exprs,
2987 vec<dt_operand *> fns,
2988 vec<dt_operand *> generic_fns,
2989 vec<dt_operand *> preds,
2990 vec<dt_node *> others)
2991{
3d2cf79f
RB
2992 char buf[128];
2993 char *kid_opname = buf;
2994
2995 unsigned exprs_len = gimple_exprs.length ();
2996 unsigned gexprs_len = generic_exprs.length ();
2997 unsigned fns_len = fns.length ();
2998 unsigned gfns_len = generic_fns.length ();
2999
3000 if (exprs_len || fns_len || gexprs_len || gfns_len)
3001 {
3002 if (exprs_len)
3003 gimple_exprs[0]->get_name (kid_opname);
3004 else if (fns_len)
3005 fns[0]->get_name (kid_opname);
3006 else if (gfns_len)
3007 generic_fns[0]->get_name (kid_opname);
3008 else
3009 generic_exprs[0]->get_name (kid_opname);
3010
c551c21d
MM
3011 fprintf_indent (f, indent, "switch (TREE_CODE (%s))\n", kid_opname);
3012 fprintf_indent (f, indent, " {\n");
1651e9b1 3013 indent += 2;
3d2cf79f
RB
3014 }
3015
3016 if (exprs_len || fns_len)
3017 {
4e4791ff 3018 depth++;
c551c21d
MM
3019 fprintf_indent (f, indent,
3020 "case SSA_NAME:\n");
3021 fprintf_indent (f, indent,
4e4791ff
BE
3022 " if (gimple *_d%d = get_def (valueize, %s))\n",
3023 depth, kid_opname);
c551c21d
MM
3024 fprintf_indent (f, indent,
3025 " {\n");
c551c21d 3026 indent += 6;
3d2cf79f
RB
3027 if (exprs_len)
3028 {
c551c21d 3029 fprintf_indent (f, indent,
4e4791ff
BE
3030 "if (gassign *_a%d = dyn_cast <gassign *> (_d%d))\n",
3031 depth, depth);
c551c21d 3032 fprintf_indent (f, indent,
4e4791ff
BE
3033 " switch (gimple_assign_rhs_code (_a%d))\n",
3034 depth);
c551c21d
MM
3035 indent += 4;
3036 fprintf_indent (f, indent, "{\n");
3d2cf79f
RB
3037 for (unsigned i = 0; i < exprs_len; ++i)
3038 {
3039 expr *e = as_a <expr *> (gimple_exprs[i]->op);
3040 id_base *op = e->operation;
3041 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
1651e9b1 3042 fprintf_indent (f, indent, "CASE_CONVERT:\n");
3d2cf79f 3043 else
1651e9b1
RB
3044 fprintf_indent (f, indent, "case %s:\n", op->id);
3045 fprintf_indent (f, indent, " {\n");
4e4791ff 3046 gimple_exprs[i]->gen (f, indent + 4, true, depth);
1651e9b1
RB
3047 fprintf_indent (f, indent, " break;\n");
3048 fprintf_indent (f, indent, " }\n");
3d2cf79f 3049 }
1651e9b1 3050 fprintf_indent (f, indent, "default:;\n");
c551c21d 3051 fprintf_indent (f, indent, "}\n");
1ec1fa94 3052 indent -= 4;
3d2cf79f
RB
3053 }
3054
3055 if (fns_len)
3056 {
c8136c36 3057 fprintf_indent (f, indent,
4e4791ff
BE
3058 "%sif (gcall *_c%d = dyn_cast <gcall *> (_d%d))\n",
3059 exprs_len ? "else " : "", depth, depth);
c551c21d 3060 fprintf_indent (f, indent,
4e4791ff
BE
3061 " switch (gimple_call_combined_fn (_c%d))\n",
3062 depth);
3d2cf79f 3063
c9e926ce
RS
3064 indent += 4;
3065 fprintf_indent (f, indent, "{\n");
3d2cf79f
RB
3066 for (unsigned i = 0; i < fns_len; ++i)
3067 {
3068 expr *e = as_a <expr *>(fns[i]->op);
c551c21d
MM
3069 fprintf_indent (f, indent, "case %s:\n", e->operation->id);
3070 fprintf_indent (f, indent, " {\n");
4e4791ff 3071 fns[i]->gen (f, indent + 4, true, depth);
c551c21d
MM
3072 fprintf_indent (f, indent, " break;\n");
3073 fprintf_indent (f, indent, " }\n");
3d2cf79f
RB
3074 }
3075
c551c21d 3076 fprintf_indent (f, indent, "default:;\n");
1ec1fa94 3077 fprintf_indent (f, indent, "}\n");
c9e926ce 3078 indent -= 4;
3d2cf79f
RB
3079 }
3080
c551c21d 3081 indent -= 6;
4e4791ff 3082 depth--;
c551c21d 3083 fprintf_indent (f, indent, " }\n");
d003cf5e
JJ
3084 /* See if there is SSA_NAME among generic_exprs and if yes, emit it
3085 here rather than in the next loop. */
3086 for (unsigned i = 0; i < generic_exprs.length (); ++i)
3087 {
3088 expr *e = as_a <expr *>(generic_exprs[i]->op);
3089 id_base *op = e->operation;
3090 if (*op == SSA_NAME && (exprs_len || fns_len))
3091 {
3092 fprintf_indent (f, indent + 4, "{\n");
4e4791ff 3093 generic_exprs[i]->gen (f, indent + 6, gimple, depth);
d003cf5e
JJ
3094 fprintf_indent (f, indent + 4, "}\n");
3095 }
3096 }
3097
c551c21d 3098 fprintf_indent (f, indent, " break;\n");
3d2cf79f
RB
3099 }
3100
3101 for (unsigned i = 0; i < generic_exprs.length (); ++i)
3102 {
3103 expr *e = as_a <expr *>(generic_exprs[i]->op);
3104 id_base *op = e->operation;
3105 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
c551c21d 3106 fprintf_indent (f, indent, "CASE_CONVERT:\n");
d003cf5e
JJ
3107 else if (*op == SSA_NAME && (exprs_len || fns_len))
3108 /* Already handled above. */
3109 continue;
3d2cf79f 3110 else
c551c21d
MM
3111 fprintf_indent (f, indent, "case %s:\n", op->id);
3112 fprintf_indent (f, indent, " {\n");
4e4791ff 3113 generic_exprs[i]->gen (f, indent + 4, gimple, depth);
c551c21d
MM
3114 fprintf_indent (f, indent, " break;\n");
3115 fprintf_indent (f, indent, " }\n");
3d2cf79f
RB
3116 }
3117
3118 if (gfns_len)
3119 {
c551c21d
MM
3120 fprintf_indent (f, indent,
3121 "case CALL_EXPR:\n");
3122 fprintf_indent (f, indent,
c9e926ce 3123 " switch (get_call_combined_fn (%s))\n",
c551c21d
MM
3124 kid_opname);
3125 fprintf_indent (f, indent,
c9e926ce
RS
3126 " {\n");
3127 indent += 4;
3d2cf79f
RB
3128
3129 for (unsigned j = 0; j < generic_fns.length (); ++j)
3130 {
3131 expr *e = as_a <expr *>(generic_fns[j]->op);
3132 gcc_assert (e->operation->kind == id_base::FN);
3133
c551c21d
MM
3134 fprintf_indent (f, indent, "case %s:\n", e->operation->id);
3135 fprintf_indent (f, indent, " {\n");
4e4791ff 3136 generic_fns[j]->gen (f, indent + 4, false, depth);
c551c21d
MM
3137 fprintf_indent (f, indent, " break;\n");
3138 fprintf_indent (f, indent, " }\n");
3d2cf79f 3139 }
c9e926ce 3140 fprintf_indent (f, indent, "default:;\n");
3d2cf79f 3141
c9e926ce
RS
3142 indent -= 4;
3143 fprintf_indent (f, indent, " }\n");
3144 fprintf_indent (f, indent, " break;\n");
3d2cf79f
RB
3145 }
3146
3147 /* Close switch (TREE_CODE ()). */
3148 if (exprs_len || fns_len || gexprs_len || gfns_len)
c551c21d
MM
3149 {
3150 indent -= 4;
3151 fprintf_indent (f, indent, " default:;\n");
1ec1fa94 3152 fprintf_indent (f, indent, " }\n");
c551c21d 3153 }
3d2cf79f
RB
3154
3155 for (unsigned i = 0; i < preds.length (); ++i)
3156 {
3157 expr *e = as_a <expr *> (preds[i]->op);
3158 predicate_id *p = as_a <predicate_id *> (e->operation);
3159 preds[i]->get_name (kid_opname);
3a6461f3
RB
3160 fprintf_indent (f, indent, "{\n");
3161 indent += 2;
c551c21d
MM
3162 fprintf_indent (f, indent, "tree %s_pops[%d];\n", kid_opname, p->nargs);
3163 fprintf_indent (f, indent, "if (%s_%s (%s, %s_pops%s))\n",
3d2cf79f
RB
3164 gimple ? "gimple" : "tree",
3165 p->id, kid_opname, kid_opname,
3166 gimple ? ", valueize" : "");
c551c21d 3167 fprintf_indent (f, indent, " {\n");
3d2cf79f
RB
3168 for (int j = 0; j < p->nargs; ++j)
3169 {
3170 char child_opname[20];
3171 preds[i]->gen_opname (child_opname, j);
c551c21d
MM
3172 fprintf_indent (f, indent + 4, "tree %s = %s_pops[%d];\n",
3173 child_opname, kid_opname, j);
3d2cf79f 3174 }
4e4791ff 3175 preds[i]->gen_kids (f, indent + 4, gimple, depth);
3d2cf79f 3176 fprintf (f, "}\n");
3a6461f3
RB
3177 indent -= 2;
3178 fprintf_indent (f, indent, "}\n");
3d2cf79f
RB
3179 }
3180
3181 for (unsigned i = 0; i < others.length (); ++i)
4e4791ff 3182 others[i]->gen (f, indent, gimple, depth);
3d2cf79f
RB
3183}
3184
3185/* Generate matching code for the decision tree operand. */
3186
3187void
4e4791ff 3188dt_operand::gen (FILE *f, int indent, bool gimple, int depth)
3d2cf79f
RB
3189{
3190 char opname[20];
3191 get_name (opname);
3192
3193 unsigned n_braces = 0;
3194
3195 if (type == DT_OPERAND)
3196 switch (op->type)
3197 {
3198 case operand::OP_PREDICATE:
c551c21d 3199 n_braces = gen_predicate (f, indent, opname, gimple);
3d2cf79f
RB
3200 break;
3201
3202 case operand::OP_EXPR:
3203 if (gimple)
4e4791ff 3204 n_braces = gen_gimple_expr (f, indent, depth);
3d2cf79f 3205 else
c551c21d 3206 n_braces = gen_generic_expr (f, indent, opname);
3d2cf79f
RB
3207 break;
3208
3209 default:
3210 gcc_unreachable ();
3211 }
3212 else if (type == DT_TRUE)
3213 ;
3214 else if (type == DT_MATCH)
39bb7d01 3215 n_braces = gen_match_op (f, indent, opname, gimple);
3d2cf79f
RB
3216 else
3217 gcc_unreachable ();
3218
c551c21d 3219 indent += 4 * n_braces;
4e4791ff 3220 gen_kids (f, indent, gimple, depth);
3d2cf79f
RB
3221
3222 for (unsigned i = 0; i < n_braces; ++i)
c551c21d
MM
3223 {
3224 indent -= 4;
3225 if (indent < 0)
3226 indent = 0;
3227 fprintf_indent (f, indent, " }\n");
3228 }
3d2cf79f
RB
3229}
3230
e0ee10ed 3231
3d2cf79f
RB
3232/* Generate code for the '(if ...)', '(with ..)' and actual transform
3233 step of a '(simplify ...)' or '(match ...)'. This handles everything
8fdc6c67
RB
3234 that is not part of the decision tree (simplify->match).
3235 Main recursive worker. */
3d2cf79f
RB
3236
3237void
8fdc6c67 3238dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
3d2cf79f 3239{
8fdc6c67 3240 if (result)
3d2cf79f 3241 {
8fdc6c67 3242 if (with_expr *w = dyn_cast <with_expr *> (result))
3d2cf79f 3243 {
8fdc6c67
RB
3244 fprintf_indent (f, indent, "{\n");
3245 indent += 4;
0889c52f 3246 output_line_directive (f, w->location);
8fdc6c67
RB
3247 w->with->gen_transform (f, indent, NULL, true, 1, "type", NULL);
3248 gen_1 (f, indent, gimple, w->subexpr);
3249 indent -= 4;
3250 fprintf_indent (f, indent, "}\n");
3251 return;
3252 }
3253 else if (if_expr *ife = dyn_cast <if_expr *> (result))
3254 {
0889c52f 3255 output_line_directive (f, ife->location);
8fdc6c67
RB
3256 fprintf_indent (f, indent, "if (");
3257 ife->cond->gen_transform (f, indent, NULL, true, 1, "type", NULL);
3258 fprintf (f, ")\n");
3259 fprintf_indent (f, indent + 2, "{\n");
3260 indent += 4;
3261 gen_1 (f, indent, gimple, ife->trueexpr);
3262 indent -= 4;
3263 fprintf_indent (f, indent + 2, "}\n");
3264 if (ife->falseexpr)
3d2cf79f 3265 {
8fdc6c67
RB
3266 fprintf_indent (f, indent, "else\n");
3267 fprintf_indent (f, indent + 2, "{\n");
c551c21d 3268 indent += 4;
8fdc6c67
RB
3269 gen_1 (f, indent, gimple, ife->falseexpr);
3270 indent -= 4;
3271 fprintf_indent (f, indent + 2, "}\n");
3d2cf79f 3272 }
8fdc6c67 3273 return;
3d2cf79f 3274 }
3d2cf79f
RB
3275 }
3276
e0ee10ed
RB
3277 /* Analyze captures and perform early-outs on the incoming arguments
3278 that cover cases we cannot handle. */
53a19317 3279 capture_info cinfo (s, result, gimple);
8fdc6c67 3280 if (s->kind == simplify::SIMPLIFY)
e0ee10ed 3281 {
44fc0a51
RB
3282 if (!gimple)
3283 {
3284 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
3285 if (cinfo.force_no_side_effects & (1 << i))
53a19317
RB
3286 {
3287 fprintf_indent (f, indent,
4e4791ff 3288 "if (TREE_SIDE_EFFECTS (_p%d)) return NULL_TREE;\n",
53a19317
RB
3289 i);
3290 if (verbose >= 1)
3291 warning_at (as_a <expr *> (s->match)->ops[i]->location,
3292 "forcing toplevel operand to have no "
3293 "side-effects");
3294 }
44fc0a51
RB
3295 for (int i = 0; i <= s->capture_max; ++i)
3296 if (cinfo.info[i].cse_p)
3297 ;
3298 else if (cinfo.info[i].force_no_side_effects_p
3299 && (cinfo.info[i].toplevel_msk
3300 & cinfo.force_no_side_effects) == 0)
53a19317
RB
3301 {
3302 fprintf_indent (f, indent,
3303 "if (TREE_SIDE_EFFECTS (captures[%d])) "
3304 "return NULL_TREE;\n", i);
3305 if (verbose >= 1)
3306 warning_at (cinfo.info[i].c->location,
3307 "forcing captured operand to have no "
3308 "side-effects");
3309 }
44fc0a51
RB
3310 else if ((cinfo.info[i].toplevel_msk
3311 & cinfo.force_no_side_effects) != 0)
3312 /* Mark capture as having no side-effects if we had to verify
3313 that via forced toplevel operand checks. */
3314 cinfo.info[i].force_no_side_effects_p = true;
3315 }
3316 if (gimple)
3317 {
3318 /* Force single-use restriction by only allowing simple
3319 results via setting seq to NULL. */
c551c21d 3320 fprintf_indent (f, indent, "gimple_seq *lseq = seq;\n");
44fc0a51
RB
3321 bool first_p = true;
3322 for (int i = 0; i <= s->capture_max; ++i)
3323 if (cinfo.info[i].force_single_use)
3324 {
3325 if (first_p)
3326 {
c551c21d
MM
3327 fprintf_indent (f, indent, "if (lseq\n");
3328 fprintf_indent (f, indent, " && (");
44fc0a51
RB
3329 first_p = false;
3330 }
3331 else
c551c21d
MM
3332 {
3333 fprintf (f, "\n");
3334 fprintf_indent (f, indent, " || ");
3335 }
44fc0a51
RB
3336 fprintf (f, "!single_use (captures[%d])", i);
3337 }
3338 if (!first_p)
c551c21d
MM
3339 {
3340 fprintf (f, "))\n");
3341 fprintf_indent (f, indent, " lseq = NULL;\n");
3342 }
44fc0a51 3343 }
e0ee10ed
RB
3344 }
3345
d398999d
ML
3346 if (s->kind == simplify::SIMPLIFY)
3347 fprintf_indent (f, indent, "if (__builtin_expect (!dbg_cnt (match), 0)) return %s;\n",
3348 gimple ? "false" : "NULL_TREE");
3349
8fcfe047 3350 fprintf_indent (f, indent, "if (__builtin_expect (dump_file && (dump_flags & TDF_FOLDING), 0)) "
7cc0fa5d
RB
3351 "fprintf (dump_file, \"%s ",
3352 s->kind == simplify::SIMPLIFY
3353 ? "Applying pattern" : "Matching expression");
8fcfe047 3354 fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
0889c52f 3355 output_line_directive (f,
8fcfe047
ML
3356 result ? result->location : s->match->location, true,
3357 true);
3358 fprintf (f, ", __FILE__, __LINE__);\n");
3d2cf79f 3359
e0ee10ed 3360 if (!result)
3d2cf79f
RB
3361 {
3362 /* If there is no result then this is a predicate implementation. */
c551c21d 3363 fprintf_indent (f, indent, "return true;\n");
3d2cf79f
RB
3364 }
3365 else if (gimple)
3366 {
e0ee10ed
RB
3367 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
3368 in outermost position). */
3369 if (result->type == operand::OP_EXPR
3370 && *as_a <expr *> (result)->operation == NON_LVALUE_EXPR)
3371 result = as_a <expr *> (result)->ops[0];
3372 if (result->type == operand::OP_EXPR)
3d2cf79f 3373 {
e0ee10ed 3374 expr *e = as_a <expr *> (result);
d4b71b95
RB
3375 id_base *opr = e->operation;
3376 bool is_predicate = false;
3377 /* When we delay operator substituting during lowering of fors we
3378 make sure that for code-gen purposes the effects of each substitute
3379 are the same. Thus just look at that. */
3380 if (user_id *uid = dyn_cast <user_id *> (opr))
3381 opr = uid->substitutes[0];
3382 else if (is_a <predicate_id *> (opr))
3383 is_predicate = true;
3d2cf79f 3384 if (!is_predicate)
5d75ad95 3385 fprintf_indent (f, indent, "res_op->set_op (%s, type, %d);\n",
c551c21d 3386 *e->operation == CONVERT_EXPR
5d75ad95
RS
3387 ? "NOP_EXPR" : e->operation->id,
3388 e->ops.length ());
3d2cf79f
RB
3389 for (unsigned j = 0; j < e->ops.length (); ++j)
3390 {
3391 char dest[32];
5d75ad95 3392 if (is_predicate)
4e4791ff 3393 snprintf (dest, sizeof (dest), "res_ops[%d]", j);
5d75ad95 3394 else
4e4791ff 3395 snprintf (dest, sizeof (dest), "res_op->ops[%d]", j);
3d2cf79f 3396 const char *optype
f00b6283 3397 = get_operand_type (opr, j,
3d2cf79f 3398 "type", e->expr_type,
5d75ad95
RS
3399 j == 0 ? NULL
3400 : "TREE_TYPE (res_op->ops[0])");
47b25362 3401 /* We need to expand GENERIC conditions we captured from
a3ca1bc5
RB
3402 COND_EXPRs and we need to unshare them when substituting
3403 into COND_EXPRs. */
3404 int cond_handling = 0;
3405 if (!is_predicate)
3406 cond_handling = ((*opr == COND_EXPR
3407 || *opr == VEC_COND_EXPR) && j == 0) ? 1 : 2;
c551c21d 3408 e->ops[j]->gen_transform (f, indent, dest, true, 1, optype,
a3ca1bc5 3409 &cinfo, indexes, cond_handling);
3d2cf79f
RB
3410 }
3411
3412 /* Re-fold the toplevel result. It's basically an embedded
3413 gimple_build w/o actually building the stmt. */
3414 if (!is_predicate)
c551c21d 3415 fprintf_indent (f, indent,
5d75ad95
RS
3416 "gimple_resimplify%d (lseq, res_op,"
3417 " valueize);\n", e->ops.length ());
3d2cf79f 3418 }
e0ee10ed
RB
3419 else if (result->type == operand::OP_CAPTURE
3420 || result->type == operand::OP_C_EXPR)
3d2cf79f 3421 {
5d75ad95
RS
3422 fprintf_indent (f, indent, "tree tem;\n");
3423 result->gen_transform (f, indent, "tem", true, 1, "type",
a3ca1bc5 3424 &cinfo, indexes);
5d75ad95 3425 fprintf_indent (f, indent, "res_op->set_value (tem);\n");
47b25362
RB
3426 if (is_a <capture *> (result)
3427 && cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p)
3428 {
3429 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
3430 with substituting a capture of that. */
c551c21d 3431 fprintf_indent (f, indent,
5d75ad95 3432 "if (COMPARISON_CLASS_P (tem))\n");
c551c21d
MM
3433 fprintf_indent (f, indent,
3434 " {\n");
3435 fprintf_indent (f, indent,
5d75ad95 3436 " res_op->ops[0] = TREE_OPERAND (tem, 0);\n");
c551c21d 3437 fprintf_indent (f, indent,
5d75ad95 3438 " res_op->ops[1] = TREE_OPERAND (tem, 1);\n");
c551c21d
MM
3439 fprintf_indent (f, indent,
3440 " }\n");
47b25362 3441 }
3d2cf79f
RB
3442 }
3443 else
3444 gcc_unreachable ();
c551c21d 3445 fprintf_indent (f, indent, "return true;\n");
3d2cf79f
RB
3446 }
3447 else /* GENERIC */
3448 {
e0ee10ed
RB
3449 bool is_predicate = false;
3450 if (result->type == operand::OP_EXPR)
3d2cf79f 3451 {
e0ee10ed 3452 expr *e = as_a <expr *> (result);
d4b71b95
RB
3453 id_base *opr = e->operation;
3454 /* When we delay operator substituting during lowering of fors we
3455 make sure that for code-gen purposes the effects of each substitute
3456 are the same. Thus just look at that. */
3457 if (user_id *uid = dyn_cast <user_id *> (opr))
3458 opr = uid->substitutes[0];
3459 else if (is_a <predicate_id *> (opr))
3460 is_predicate = true;
e0ee10ed 3461 /* Search for captures used multiple times in the result expression
2d3f4bf7
RB
3462 and wrap them in a SAVE_EXPR. Allow as many uses as in the
3463 original expression. */
e0ee10ed
RB
3464 if (!is_predicate)
3465 for (int i = 0; i < s->capture_max + 1; ++i)
3466 {
2d3f4bf7
RB
3467 if (cinfo.info[i].same_as != (unsigned)i
3468 || cinfo.info[i].cse_p)
fa138f6e 3469 continue;
2d3f4bf7
RB
3470 if (cinfo.info[i].result_use_count
3471 > cinfo.info[i].match_use_count)
6be52f62 3472 fprintf_indent (f, indent,
7b2eca00
RB
3473 "if (! tree_invariant_p (captures[%d])) "
3474 "return NULL_TREE;\n", i);
e0ee10ed 3475 }
3d2cf79f
RB
3476 for (unsigned j = 0; j < e->ops.length (); ++j)
3477 {
3478 char dest[32];
3479 if (is_predicate)
4e4791ff 3480 snprintf (dest, sizeof (dest), "res_ops[%d]", j);
3d2cf79f
RB
3481 else
3482 {
c551c21d 3483 fprintf_indent (f, indent, "tree res_op%d;\n", j);
4e4791ff 3484 snprintf (dest, sizeof (dest), "res_op%d", j);
3d2cf79f
RB
3485 }
3486 const char *optype
f00b6283 3487 = get_operand_type (opr, j,
3d2cf79f
RB
3488 "type", e->expr_type,
3489 j == 0
3490 ? NULL : "TREE_TYPE (res_op0)");
c551c21d 3491 e->ops[j]->gen_transform (f, indent, dest, false, 1, optype,
47b25362 3492 &cinfo, indexes);
3d2cf79f 3493 }
e0ee10ed 3494 if (is_predicate)
c551c21d 3495 fprintf_indent (f, indent, "return true;\n");
3d2cf79f
RB
3496 else
3497 {
4e4791ff 3498 fprintf_indent (f, indent, "tree _r;\n");
e0ee10ed 3499 /* Re-fold the toplevel result. Use non_lvalue to
4e4791ff 3500 build NON_LVALUE_EXPRs so they get properly
e0ee10ed 3501 ignored when in GIMPLE form. */
d4b71b95 3502 if (*opr == NON_LVALUE_EXPR)
c551c21d 3503 fprintf_indent (f, indent,
4e4791ff 3504 "_r = non_lvalue_loc (loc, res_op0);\n");
3d2cf79f 3505 else
e0ee10ed 3506 {
d4b71b95 3507 if (is_a <operator_id *> (opr))
c551c21d 3508 fprintf_indent (f, indent,
4e4791ff 3509 "_r = fold_build%d_loc (loc, %s, type",
c551c21d
MM
3510 e->ops.length (),
3511 *e->operation == CONVERT_EXPR
3512 ? "NOP_EXPR" : e->operation->id);
e0ee10ed 3513 else
c9e926ce 3514 fprintf_indent (f, indent,
4e4791ff 3515 "_r = maybe_build_call_expr_loc (loc, "
c9e926ce
RS
3516 "%s, type, %d", e->operation->id,
3517 e->ops.length());
e0ee10ed
RB
3518 for (unsigned j = 0; j < e->ops.length (); ++j)
3519 fprintf (f, ", res_op%d", j);
3520 fprintf (f, ");\n");
e4cdf6a8 3521 if (!is_a <operator_id *> (opr))
c9e926ce 3522 {
4e4791ff 3523 fprintf_indent (f, indent, "if (!_r)\n");
c9e926ce
RS
3524 fprintf_indent (f, indent, " return NULL_TREE;\n");
3525 }
e0ee10ed 3526 }
3d2cf79f
RB
3527 }
3528 }
e0ee10ed
RB
3529 else if (result->type == operand::OP_CAPTURE
3530 || result->type == operand::OP_C_EXPR)
3531
3d2cf79f 3532 {
4e4791ff
BE
3533 fprintf_indent (f, indent, "tree _r;\n");
3534 result->gen_transform (f, indent, "_r", false, 1, "type",
47b25362 3535 &cinfo, indexes);
3d2cf79f
RB
3536 }
3537 else
3538 gcc_unreachable ();
e0ee10ed
RB
3539 if (!is_predicate)
3540 {
3541 /* Search for captures not used in the result expression and dependent
3542 on TREE_SIDE_EFFECTS emit omit_one_operand. */
3543 for (int i = 0; i < s->capture_max + 1; ++i)
3544 {
fa138f6e
RB
3545 if (cinfo.info[i].same_as != (unsigned)i)
3546 continue;
e0ee10ed
RB
3547 if (!cinfo.info[i].force_no_side_effects_p
3548 && !cinfo.info[i].expr_p
3549 && cinfo.info[i].result_use_count == 0)
c551c21d
MM
3550 {
3551 fprintf_indent (f, indent,
3552 "if (TREE_SIDE_EFFECTS (captures[%d]))\n",
3553 i);
3554 fprintf_indent (f, indent + 2,
4e4791ff
BE
3555 "_r = build2_loc (loc, COMPOUND_EXPR, type, "
3556 "fold_ignored_result (captures[%d]), _r);\n",
c551c21d
MM
3557 i);
3558 }
e0ee10ed 3559 }
4e4791ff 3560 fprintf_indent (f, indent, "return _r;\n");
e0ee10ed 3561 }
3d2cf79f 3562 }
8fdc6c67 3563}
3d2cf79f 3564
8fdc6c67
RB
3565/* Generate code for the '(if ...)', '(with ..)' and actual transform
3566 step of a '(simplify ...)' or '(match ...)'. This handles everything
3567 that is not part of the decision tree (simplify->match). */
3568
3569void
4e4791ff 3570dt_simplify::gen (FILE *f, int indent, bool gimple, int depth ATTRIBUTE_UNUSED)
8fdc6c67
RB
3571{
3572 fprintf_indent (f, indent, "{\n");
3573 indent += 2;
0889c52f
RB
3574 output_line_directive (f,
3575 s->result ? s->result->location : s->match->location);
8fdc6c67 3576 if (s->capture_max >= 0)
1d2fdec6
RB
3577 {
3578 char opname[20];
3579 fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = { %s",
3580 s->capture_max + 1, indexes[0]->get_name (opname));
8fdc6c67 3581
1d2fdec6 3582 for (int i = 1; i <= s->capture_max; ++i)
96285749
RS
3583 {
3584 if (!indexes[i])
3585 break;
3586 fprintf (f, ", %s", indexes[i]->get_name (opname));
3587 }
1d2fdec6
RB
3588 fprintf (f, " };\n");
3589 }
8fdc6c67 3590
03038b8b
RB
3591 /* If we have a split-out function for the actual transform, call it. */
3592 if (info && info->fname)
3593 {
3594 if (gimple)
3595 {
5d75ad95 3596 fprintf_indent (f, indent, "if (%s (res_op, seq, "
d4b71b95
RB
3597 "valueize, type, captures", info->fname);
3598 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
e04d2a35
RB
3599 if (s->for_subst_vec[i].first->used)
3600 fprintf (f, ", %s", s->for_subst_vec[i].second->id);
d4b71b95 3601 fprintf (f, "))\n");
03038b8b
RB
3602 fprintf_indent (f, indent, " return true;\n");
3603 }
3604 else
3605 {
3606 fprintf_indent (f, indent, "tree res = %s (loc, type",
3607 info->fname);
3608 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
4e4791ff 3609 fprintf (f, ", _p%d", i);
d4b71b95
RB
3610 fprintf (f, ", captures");
3611 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
e04d2a35
RB
3612 {
3613 if (s->for_subst_vec[i].first->used)
3614 fprintf (f, ", %s", s->for_subst_vec[i].second->id);
3615 }
d4b71b95 3616 fprintf (f, ");\n");
03038b8b
RB
3617 fprintf_indent (f, indent, "if (res) return res;\n");
3618 }
3619 }
3620 else
d4b71b95
RB
3621 {
3622 for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
3623 {
e04d2a35
RB
3624 if (! s->for_subst_vec[i].first->used)
3625 continue;
d4b71b95 3626 if (is_a <operator_id *> (s->for_subst_vec[i].second))
9e7af053 3627 fprintf_indent (f, indent, "const enum tree_code %s = %s;\n",
d4b71b95
RB
3628 s->for_subst_vec[i].first->id,
3629 s->for_subst_vec[i].second->id);
3630 else if (is_a <fn_id *> (s->for_subst_vec[i].second))
9e7af053 3631 fprintf_indent (f, indent, "const combined_fn %s = %s;\n",
d4b71b95
RB
3632 s->for_subst_vec[i].first->id,
3633 s->for_subst_vec[i].second->id);
3634 else
3635 gcc_unreachable ();
3636 }
3637 gen_1 (f, indent, gimple, s->result);
3638 }
3d2cf79f 3639
c551c21d
MM
3640 indent -= 2;
3641 fprintf_indent (f, indent, "}\n");
3d2cf79f
RB
3642}
3643
03038b8b
RB
3644
3645/* Hash function for finding equivalent transforms. */
3646
3647hashval_t
3648sinfo_hashmap_traits::hash (const key_type &v)
3649{
3650 /* Only bother to compare those originating from the same source pattern. */
3651 return v->s->result->location;
3652}
3653
3654/* Compare function for finding equivalent transforms. */
3655
3656static bool
3657compare_op (operand *o1, simplify *s1, operand *o2, simplify *s2)
3658{
3659 if (o1->type != o2->type)
3660 return false;
3661
3662 switch (o1->type)
3663 {
3664 case operand::OP_IF:
3665 {
3666 if_expr *if1 = as_a <if_expr *> (o1);
3667 if_expr *if2 = as_a <if_expr *> (o2);
3668 /* ??? Properly compare c-exprs. */
3669 if (if1->cond != if2->cond)
3670 return false;
3671 if (!compare_op (if1->trueexpr, s1, if2->trueexpr, s2))
3672 return false;
3673 if (if1->falseexpr != if2->falseexpr
3674 || (if1->falseexpr
3675 && !compare_op (if1->falseexpr, s1, if2->falseexpr, s2)))
3676 return false;
3677 return true;
3678 }
3679 case operand::OP_WITH:
3680 {
3681 with_expr *with1 = as_a <with_expr *> (o1);
3682 with_expr *with2 = as_a <with_expr *> (o2);
3683 if (with1->with != with2->with)
3684 return false;
3685 return compare_op (with1->subexpr, s1, with2->subexpr, s2);
3686 }
3687 default:;
3688 }
3689
3690 /* We've hit a result. Time to compare capture-infos - this is required
3691 in addition to the conservative pointer-equivalency of the result IL. */
3692 capture_info cinfo1 (s1, o1, true);
3693 capture_info cinfo2 (s2, o2, true);
3694
3695 if (cinfo1.force_no_side_effects != cinfo2.force_no_side_effects
3696 || cinfo1.info.length () != cinfo2.info.length ())
3697 return false;
3698
3699 for (unsigned i = 0; i < cinfo1.info.length (); ++i)
3700 {
3701 if (cinfo1.info[i].expr_p != cinfo2.info[i].expr_p
3702 || cinfo1.info[i].cse_p != cinfo2.info[i].cse_p
3703 || (cinfo1.info[i].force_no_side_effects_p
3704 != cinfo2.info[i].force_no_side_effects_p)
3705 || cinfo1.info[i].force_single_use != cinfo2.info[i].force_single_use
3706 || cinfo1.info[i].cond_expr_cond_p != cinfo2.info[i].cond_expr_cond_p
3707 /* toplevel_msk is an optimization */
3708 || cinfo1.info[i].result_use_count != cinfo2.info[i].result_use_count
3709 || cinfo1.info[i].same_as != cinfo2.info[i].same_as
3710 /* the pointer back to the capture is for diagnostics only */)
3711 return false;
3712 }
3713
3714 /* ??? Deep-compare the actual result. */
3715 return o1 == o2;
3716}
3717
3718bool
3719sinfo_hashmap_traits::equal_keys (const key_type &v,
3720 const key_type &candidate)
3721{
3722 return compare_op (v->s->result, v->s, candidate->s->result, candidate->s);
3723}
3724
3725
3d2cf79f
RB
3726/* Main entry to generate code for matching GIMPLE IL off the decision
3727 tree. */
3728
3729void
b21ce9ce 3730decision_tree::gen (FILE *f, bool gimple)
3d2cf79f 3731{
03038b8b
RB
3732 sinfo_map_t si;
3733
3734 root->analyze (si);
803835de 3735
b21ce9ce
RB
3736 fprintf (stderr, "%s decision tree has %u leafs, maximum depth %u and "
3737 "a total number of %u nodes\n",
3738 gimple ? "GIMPLE" : "GENERIC",
3739 root->num_leafs, root->max_level, root->total_size);
803835de 3740
03038b8b
RB
3741 /* First split out the transform part of equal leafs. */
3742 unsigned rcnt = 0;
3743 unsigned fcnt = 1;
3744 for (sinfo_map_t::iterator iter = si.begin ();
3745 iter != si.end (); ++iter)
3746 {
3747 sinfo *s = (*iter).second;
3748 /* Do not split out single uses. */
3749 if (s->cnt <= 1)
3750 continue;
3751
3752 rcnt += s->cnt - 1;
3753 if (verbose >= 1)
3754 {
3755 fprintf (stderr, "found %u uses of", s->cnt);
3756 output_line_directive (stderr, s->s->s->result->location);
3757 }
3758
3759 /* Generate a split out function with the leaf transform code. */
3760 s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic",
3761 fcnt++);
3762 if (gimple)
3763 fprintf (f, "\nstatic bool\n"
5d75ad95
RS
3764 "%s (gimple_match_op *res_op, gimple_seq *seq,\n"
3765 " tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
9e7af053 3766 " const tree ARG_UNUSED (type), tree *ARG_UNUSED "
d4b71b95 3767 "(captures)\n",
03038b8b
RB
3768 s->fname);
3769 else
3770 {
3771 fprintf (f, "\nstatic tree\n"
9e7af053 3772 "%s (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type),\n",
03038b8b
RB
3773 (*iter).second->fname);
3774 for (unsigned i = 0;
3775 i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
4e4791ff 3776 fprintf (f, " tree ARG_UNUSED (_p%d),", i);
d4b71b95
RB
3777 fprintf (f, " tree *captures\n");
3778 }
3779 for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
3780 {
e04d2a35
RB
3781 if (! s->s->s->for_subst_vec[i].first->used)
3782 continue;
d4b71b95 3783 if (is_a <operator_id *> (s->s->s->for_subst_vec[i].second))
9e7af053 3784 fprintf (f, ", const enum tree_code ARG_UNUSED (%s)",
d4b71b95
RB
3785 s->s->s->for_subst_vec[i].first->id);
3786 else if (is_a <fn_id *> (s->s->s->for_subst_vec[i].second))
9e7af053 3787 fprintf (f, ", const combined_fn ARG_UNUSED (%s)",
d4b71b95 3788 s->s->s->for_subst_vec[i].first->id);
03038b8b
RB
3789 }
3790
d4b71b95 3791 fprintf (f, ")\n{\n");
03038b8b
RB
3792 s->s->gen_1 (f, 2, gimple, s->s->s->result);
3793 if (gimple)
3794 fprintf (f, " return false;\n");
3795 else
3796 fprintf (f, " return NULL_TREE;\n");
3797 fprintf (f, "}\n");
3798 }
3799 fprintf (stderr, "removed %u duplicate tails\n", rcnt);
3800
b41d1f6e 3801 for (unsigned n = 1; n <= 5; ++n)
3d2cf79f 3802 {
0fd357f2 3803 /* First generate split-out functions. */
4e4791ff 3804 for (unsigned j = 0; j < root->kids.length (); j++)
0fd357f2 3805 {
4e4791ff 3806 dt_operand *dop = static_cast<dt_operand *>(root->kids[j]);
0fd357f2
RB
3807 expr *e = static_cast<expr *>(dop->op);
3808 if (e->ops.length () != n
3809 /* Builtin simplifications are somewhat premature on
b21ce9ce 3810 GENERIC. The following drops patterns with outermost
0fd357f2
RB
3811 calls. It's easy to emit overloads for function code
3812 though if necessary. */
b21ce9ce
RB
3813 || (!gimple
3814 && e->operation->kind != id_base::CODE))
0fd357f2
RB
3815 continue;
3816
b21ce9ce
RB
3817 if (gimple)
3818 fprintf (f, "\nstatic bool\n"
5d75ad95
RS
3819 "gimple_simplify_%s (gimple_match_op *res_op,"
3820 " gimple_seq *seq,\n"
3821 " tree (*valueize)(tree) "
b21ce9ce
RB
3822 "ATTRIBUTE_UNUSED,\n"
3823 " code_helper ARG_UNUSED (code), tree "
3824 "ARG_UNUSED (type)\n",
3825 e->operation->id);
3826 else
3827 fprintf (f, "\nstatic tree\n"
3828 "generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
9e7af053 3829 "tree_code ARG_UNUSED (code), const tree ARG_UNUSED (type)",
b21ce9ce 3830 e->operation->id);
0fd357f2 3831 for (unsigned i = 0; i < n; ++i)
4e4791ff 3832 fprintf (f, ", tree _p%d", i);
0fd357f2
RB
3833 fprintf (f, ")\n");
3834 fprintf (f, "{\n");
4e4791ff 3835 dop->gen_kids (f, 2, gimple, 0);
b21ce9ce
RB
3836 if (gimple)
3837 fprintf (f, " return false;\n");
3838 else
3839 fprintf (f, " return NULL_TREE;\n");
0fd357f2
RB
3840 fprintf (f, "}\n");
3841 }
3842
3843 /* Then generate the main entry with the outermost switch and
3844 tail-calls to the split-out functions. */
b21ce9ce
RB
3845 if (gimple)
3846 fprintf (f, "\nstatic bool\n"
5d75ad95
RS
3847 "gimple_simplify (gimple_match_op *res_op, gimple_seq *seq,\n"
3848 " tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
9e7af053 3849 " code_helper code, const tree type");
b21ce9ce
RB
3850 else
3851 fprintf (f, "\ntree\n"
3852 "generic_simplify (location_t loc, enum tree_code code, "
9e7af053 3853 "const tree type ATTRIBUTE_UNUSED");
3d2cf79f 3854 for (unsigned i = 0; i < n; ++i)
4e4791ff 3855 fprintf (f, ", tree _p%d", i);
3d2cf79f
RB
3856 fprintf (f, ")\n");
3857 fprintf (f, "{\n");
3858
b21ce9ce
RB
3859 if (gimple)
3860 fprintf (f, " switch (code.get_rep())\n"
3861 " {\n");
3862 else
3863 fprintf (f, " switch (code)\n"
3864 " {\n");
3d2cf79f
RB
3865 for (unsigned i = 0; i < root->kids.length (); i++)
3866 {
3867 dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
3868 expr *e = static_cast<expr *>(dop->op);
3869 if (e->ops.length () != n
3870 /* Builtin simplifications are somewhat premature on
b21ce9ce 3871 GENERIC. The following drops patterns with outermost
3d2cf79f
RB
3872 calls. It's easy to emit overloads for function code
3873 though if necessary. */
b21ce9ce
RB
3874 || (!gimple
3875 && e->operation->kind != id_base::CODE))
3d2cf79f
RB
3876 continue;
3877
b21ce9ce
RB
3878 if (*e->operation == CONVERT_EXPR
3879 || *e->operation == NOP_EXPR)
1651e9b1 3880 fprintf (f, " CASE_CONVERT:\n");
3d2cf79f 3881 else
b21ce9ce
RB
3882 fprintf (f, " case %s%s:\n",
3883 is_a <fn_id *> (e->operation) ? "-" : "",
3884 e->operation->id);
3885 if (gimple)
5d75ad95 3886 fprintf (f, " return gimple_simplify_%s (res_op, "
b21ce9ce
RB
3887 "seq, valueize, code, type", e->operation->id);
3888 else
3889 fprintf (f, " return generic_simplify_%s (loc, code, type",
3890 e->operation->id);
4e4791ff
BE
3891 for (unsigned j = 0; j < n; ++j)
3892 fprintf (f, ", _p%d", j);
0fd357f2 3893 fprintf (f, ");\n");
3d2cf79f 3894 }
b21ce9ce
RB
3895 fprintf (f, " default:;\n"
3896 " }\n");
3d2cf79f 3897
b21ce9ce
RB
3898 if (gimple)
3899 fprintf (f, " return false;\n");
3900 else
3901 fprintf (f, " return NULL_TREE;\n");
3d2cf79f
RB
3902 fprintf (f, "}\n");
3903 }
3904}
3905
3906/* Output code to implement the predicate P from the decision tree DT. */
3907
3908void
3909write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
3910{
3911 fprintf (f, "\nbool\n"
3912 "%s%s (tree t%s%s)\n"
3913 "{\n", gimple ? "gimple_" : "tree_", p->id,
3914 p->nargs > 0 ? ", tree *res_ops" : "",
6b6aa8d3 3915 gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
3d2cf79f 3916 /* Conveniently make 'type' available. */
9e7af053 3917 fprintf_indent (f, 2, "const tree type = TREE_TYPE (t);\n");
3d2cf79f
RB
3918
3919 if (!gimple)
c551c21d 3920 fprintf_indent (f, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
4e4791ff 3921 dt.root->gen_kids (f, 2, gimple, 0);
3d2cf79f 3922
c551c21d 3923 fprintf_indent (f, 2, "return false;\n"
3d2cf79f
RB
3924 "}\n");
3925}
3926
3927/* Write the common header for the GIMPLE/GENERIC IL matching routines. */
3928
3929static void
3930write_header (FILE *f, const char *head)
3931{
3932 fprintf (f, "/* Generated automatically by the program `genmatch' from\n");
3933 fprintf (f, " a IL pattern matching and simplification description. */\n");
3934
3935 /* Include the header instead of writing it awkwardly quoted here. */
3936 fprintf (f, "\n#include \"%s\"\n", head);
3937}
3938
3939
3940
3941/* AST parsing. */
3942
3943class parser
3944{
3945public:
3946 parser (cpp_reader *);
3947
3948private:
3949 const cpp_token *next ();
64d3a1f0
RB
3950 const cpp_token *peek (unsigned = 1);
3951 const cpp_token *peek_ident (const char * = NULL, unsigned = 1);
3d2cf79f 3952 const cpp_token *expect (enum cpp_ttype);
64d3a1f0 3953 const cpp_token *eat_token (enum cpp_ttype);
3d2cf79f
RB
3954 const char *get_string ();
3955 const char *get_ident ();
64d3a1f0 3956 const cpp_token *eat_ident (const char *);
3d2cf79f
RB
3957 const char *get_number ();
3958
2eef1fc1
RB
3959 unsigned get_internal_capture_id ();
3960
3d2cf79f 3961 id_base *parse_operation ();
c56f494f 3962 operand *parse_capture (operand *, bool);
3d2cf79f
RB
3963 operand *parse_expr ();
3964 c_expr *parse_c_expr (cpp_ttype);
3965 operand *parse_op ();
3966
620e594b 3967 void record_operlist (location_t, user_id *);
1c9b0448 3968
3d2cf79f 3969 void parse_pattern ();
8fdc6c67
RB
3970 operand *parse_result (operand *, predicate_id *);
3971 void push_simplify (simplify::simplify_kind,
0889c52f 3972 vec<simplify *>&, operand *, operand *);
8fdc6c67 3973 void parse_simplify (simplify::simplify_kind,
0889c52f 3974 vec<simplify *>&, predicate_id *, operand *);
620e594b
DM
3975 void parse_for (location_t);
3976 void parse_if (location_t);
3977 void parse_predicates (location_t);
3978 void parse_operator_list (location_t);
3d2cf79f 3979
2eef1fc1
RB
3980 void finish_match_operand (operand *);
3981
3d2cf79f 3982 cpp_reader *r;
8fdc6c67 3983 vec<c_expr *> active_ifs;
3d2cf79f 3984 vec<vec<user_id *> > active_fors;
1c9b0448
RB
3985 hash_set<user_id *> *oper_lists_set;
3986 vec<user_id *> oper_lists;
3d2cf79f 3987
f1308e4b 3988 cid_map_t *capture_ids;
6c35e5b0 3989 unsigned last_id;
3d2cf79f
RB
3990
3991public:
3992 vec<simplify *> simplifiers;
3993 vec<predicate_id *> user_predicates;
72eb311d 3994 bool parsing_match_operand;
3d2cf79f
RB
3995};
3996
3997/* Lexing helpers. */
3998
3999/* Read the next non-whitespace token from R. */
4000
4001const cpp_token *
4002parser::next ()
4003{
4004 const cpp_token *token;
4005 do
4006 {
4007 token = cpp_get_token (r);
4008 }
d59b7253 4009 while (token->type == CPP_PADDING);
3d2cf79f
RB
4010 return token;
4011}
4012
4013/* Peek at the next non-whitespace token from R. */
4014
4015const cpp_token *
64d3a1f0 4016parser::peek (unsigned num)
3d2cf79f
RB
4017{
4018 const cpp_token *token;
4019 unsigned i = 0;
4020 do
4021 {
4022 token = cpp_peek_token (r, i++);
4023 }
d59b7253 4024 while (token->type == CPP_PADDING
64d3a1f0 4025 || (--num > 0));
3d2cf79f
RB
4026 /* If we peek at EOF this is a fatal error as it leaves the
4027 cpp_reader in unusable state. Assume we really wanted a
4028 token and thus this EOF is unexpected. */
4029 if (token->type == CPP_EOF)
4030 fatal_at (token, "unexpected end of file");
4031 return token;
4032}
4033
4034/* Peek at the next identifier token (or return NULL if the next
4035 token is not an identifier or equal to ID if supplied). */
4036
4037const cpp_token *
64d3a1f0 4038parser::peek_ident (const char *id, unsigned num)
3d2cf79f 4039{
64d3a1f0 4040 const cpp_token *token = peek (num);
3d2cf79f
RB
4041 if (token->type != CPP_NAME)
4042 return 0;
4043
4044 if (id == 0)
4045 return token;
4046
4047 const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
4048 if (strcmp (id, t) == 0)
4049 return token;
4050
4051 return 0;
4052}
4053
4054/* Read the next token from R and assert it is of type TK. */
4055
4056const cpp_token *
4057parser::expect (enum cpp_ttype tk)
4058{
4059 const cpp_token *token = next ();
4060 if (token->type != tk)
4061 fatal_at (token, "expected %s, got %s",
4062 cpp_type2name (tk, 0), cpp_type2name (token->type, 0));
4063
4064 return token;
4065}
4066
4067/* Consume the next token from R and assert it is of type TK. */
4068
64d3a1f0 4069const cpp_token *
3d2cf79f
RB
4070parser::eat_token (enum cpp_ttype tk)
4071{
64d3a1f0 4072 return expect (tk);
3d2cf79f
RB
4073}
4074
4075/* Read the next token from R and assert it is of type CPP_STRING and
4076 return its value. */
4077
4078const char *
4079parser::get_string ()
4080{
4081 const cpp_token *token = expect (CPP_STRING);
4082 return (const char *)token->val.str.text;
4083}
4084
4085/* Read the next token from R and assert it is of type CPP_NAME and
4086 return its value. */
4087
4088const char *
4089parser::get_ident ()
4090{
4091 const cpp_token *token = expect (CPP_NAME);
4092 return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
4093}
4094
4095/* Eat an identifier token with value S from R. */
4096
64d3a1f0 4097const cpp_token *
3d2cf79f
RB
4098parser::eat_ident (const char *s)
4099{
4100 const cpp_token *token = peek ();
4101 const char *t = get_ident ();
4102 if (strcmp (s, t) != 0)
4103 fatal_at (token, "expected '%s' got '%s'\n", s, t);
64d3a1f0 4104 return token;
3d2cf79f
RB
4105}
4106
4107/* Read the next token from R and assert it is of type CPP_NUMBER and
4108 return its value. */
4109
4110const char *
4111parser::get_number ()
4112{
4113 const cpp_token *token = expect (CPP_NUMBER);
4114 return (const char *)token->val.str.text;
4115}
4116
2eef1fc1
RB
4117/* Return a capture ID that can be used internally. */
4118
4119unsigned
4120parser::get_internal_capture_id ()
4121{
4122 unsigned newid = capture_ids->elements ();
4123 /* Big enough for a 32-bit UINT_MAX plus prefix. */
4124 char id[13];
4125 bool existed;
4e4791ff 4126 snprintf (id, sizeof (id), "__%u", newid);
2eef1fc1
RB
4127 capture_ids->get_or_insert (xstrdup (id), &existed);
4128 if (existed)
4129 fatal ("reserved capture id '%s' already used", id);
4130 return newid;
4131}
3d2cf79f 4132
1c9b0448
RB
4133/* Record an operator-list use for transparent for handling. */
4134
4135void
620e594b 4136parser::record_operlist (location_t loc, user_id *p)
1c9b0448
RB
4137{
4138 if (!oper_lists_set->add (p))
4139 {
4140 if (!oper_lists.is_empty ()
4141 && oper_lists[0]->substitutes.length () != p->substitutes.length ())
4142 fatal_at (loc, "User-defined operator list does not have the "
4143 "same number of entries as others used in the pattern");
4144 oper_lists.safe_push (p);
4145 }
4146}
4147
3d2cf79f
RB
4148/* Parse the operator ID, special-casing convert?, convert1? and
4149 convert2? */
4150
4151id_base *
4152parser::parse_operation ()
4153{
4154 const cpp_token *id_tok = peek ();
4155 const char *id = get_ident ();
4156 const cpp_token *token = peek ();
4157 if (strcmp (id, "convert0") == 0)
4158 fatal_at (id_tok, "use 'convert?' here");
39791822
RB
4159 else if (strcmp (id, "view_convert0") == 0)
4160 fatal_at (id_tok, "use 'view_convert?' here");
3d2cf79f
RB
4161 if (token->type == CPP_QUERY
4162 && !(token->flags & PREV_WHITE))
4163 {
4164 if (strcmp (id, "convert") == 0)
4165 id = "convert0";
39791822 4166 else if (strcmp (id, "convert1") == 0)
3d2cf79f 4167 ;
39791822
RB
4168 else if (strcmp (id, "convert2") == 0)
4169 ;
4170 else if (strcmp (id, "view_convert") == 0)
4171 id = "view_convert0";
4172 else if (strcmp (id, "view_convert1") == 0)
4173 ;
4174 else if (strcmp (id, "view_convert2") == 0)
3d2cf79f
RB
4175 ;
4176 else
4177 fatal_at (id_tok, "non-convert operator conditionalized");
72eb311d
RB
4178
4179 if (!parsing_match_operand)
4180 fatal_at (id_tok, "conditional convert can only be used in "
4181 "match expression");
3d2cf79f
RB
4182 eat_token (CPP_QUERY);
4183 }
39791822
RB
4184 else if (strcmp (id, "convert1") == 0
4185 || strcmp (id, "convert2") == 0
4186 || strcmp (id, "view_convert1") == 0
4187 || strcmp (id, "view_convert2") == 0)
3d2cf79f
RB
4188 fatal_at (id_tok, "expected '?' after conditional operator");
4189 id_base *op = get_operator (id);
4190 if (!op)
4191 fatal_at (id_tok, "unknown operator %s", id);
72eb311d
RB
4192
4193 user_id *p = dyn_cast<user_id *> (op);
4194 if (p && p->is_oper_list)
94cbafd1
PK
4195 {
4196 if (active_fors.length() == 0)
4197 record_operlist (id_tok->src_loc, p);
4198 else
35574a7b 4199 fatal_at (id_tok, "operator-list %s cannot be expanded inside 'for'", id);
94cbafd1 4200 }
3d2cf79f
RB
4201 return op;
4202}
4203
4204/* Parse a capture.
4205 capture = '@'<number> */
4206
99b1c316 4207class operand *
c56f494f 4208parser::parse_capture (operand *op, bool require_existing)
3d2cf79f 4209{
620e594b 4210 location_t src_loc = eat_token (CPP_ATSIGN)->src_loc;
3d2cf79f 4211 const cpp_token *token = peek ();
72eb311d 4212 const char *id = NULL;
2eef1fc1
RB
4213 bool value_match = false;
4214 /* For matches parse @@ as a value-match denoting the prevailing operand. */
4215 if (token->type == CPP_ATSIGN
4216 && ! (token->flags & PREV_WHITE)
4217 && parsing_match_operand)
4218 {
4219 eat_token (CPP_ATSIGN);
4220 token = peek ();
4221 value_match = true;
4222 }
3d2cf79f
RB
4223 if (token->type == CPP_NUMBER)
4224 id = get_number ();
4225 else if (token->type == CPP_NAME)
4226 id = get_ident ();
4227 else
4228 fatal_at (token, "expected number or identifier");
f1308e4b
RB
4229 unsigned next_id = capture_ids->elements ();
4230 bool existed;
4231 unsigned &num = capture_ids->get_or_insert (id, &existed);
4232 if (!existed)
c56f494f
RB
4233 {
4234 if (require_existing)
4235 fatal_at (src_loc, "unknown capture id");
4236 num = next_id;
4237 }
2eef1fc1 4238 return new capture (src_loc, num, op, value_match);
3d2cf79f
RB
4239}
4240
4241/* Parse an expression
4242 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
4243
99b1c316 4244class operand *
3d2cf79f
RB
4245parser::parse_expr ()
4246{
3d2cf79f 4247 const cpp_token *token = peek ();
0889c52f
RB
4248 expr *e = new expr (parse_operation (), token->src_loc);
4249 token = peek ();
3d2cf79f
RB
4250 operand *op;
4251 bool is_commutative = false;
44fc0a51 4252 bool force_capture = false;
3d2cf79f
RB
4253 const char *expr_type = NULL;
4254
4255 if (token->type == CPP_COLON
4256 && !(token->flags & PREV_WHITE))
4257 {
4258 eat_token (CPP_COLON);
4259 token = peek ();
4260 if (token->type == CPP_NAME
4261 && !(token->flags & PREV_WHITE))
4262 {
4263 const char *s = get_ident ();
44fc0a51
RB
4264 if (!parsing_match_operand)
4265 expr_type = s;
4266 else
72eb311d 4267 {
44fc0a51
RB
4268 const char *sp = s;
4269 while (*sp)
4270 {
4271 if (*sp == 'c')
e04d2a35 4272 {
4e4791ff 4273 if (operator_id *o
e04d2a35
RB
4274 = dyn_cast<operator_id *> (e->operation))
4275 {
4e4791ff
BE
4276 if (!commutative_tree_code (o->code)
4277 && !comparison_code_p (o->code))
e04d2a35
RB
4278 fatal_at (token, "operation is not commutative");
4279 }
4280 else if (user_id *p = dyn_cast<user_id *> (e->operation))
4281 for (unsigned i = 0;
4282 i < p->substitutes.length (); ++i)
4283 {
4284 if (operator_id *q
4285 = dyn_cast<operator_id *> (p->substitutes[i]))
4286 {
4287 if (!commutative_tree_code (q->code)
4288 && !comparison_code_p (q->code))
4289 fatal_at (token, "operation %s is not "
4290 "commutative", q->id);
4291 }
4292 }
4293 is_commutative = true;
4294 }
4295 else if (*sp == 'C')
44fc0a51
RB
4296 is_commutative = true;
4297 else if (*sp == 's')
4298 {
4299 e->force_single_use = true;
4300 force_capture = true;
4301 }
4302 else
4303 fatal_at (token, "flag %c not recognized", *sp);
4304 sp++;
4305 }
72eb311d 4306 }
3d2cf79f
RB
4307 token = peek ();
4308 }
4309 else
4310 fatal_at (token, "expected flag or type specifying identifier");
4311 }
4312
4313 if (token->type == CPP_ATSIGN
4314 && !(token->flags & PREV_WHITE))
96285749 4315 op = parse_capture (e, false);
44fc0a51
RB
4316 else if (force_capture)
4317 {
2eef1fc1
RB
4318 unsigned num = get_internal_capture_id ();
4319 op = new capture (token->src_loc, num, e, false);
44fc0a51 4320 }
3d2cf79f
RB
4321 else
4322 op = e;
4323 do
4324 {
4e4791ff 4325 token = peek ();
3d2cf79f
RB
4326 if (token->type == CPP_CLOSE_PAREN)
4327 {
4328 if (e->operation->nargs != -1
4329 && e->operation->nargs != (int) e->ops.length ())
4330 fatal_at (token, "'%s' expects %u operands, not %u",
4331 e->operation->id, e->operation->nargs, e->ops.length ());
4332 if (is_commutative)
4333 {
c566cc9f
RS
4334 if (e->ops.length () == 2
4335 || commutative_op (e->operation) >= 0)
3d2cf79f
RB
4336 e->is_commutative = true;
4337 else
c566cc9f
RS
4338 fatal_at (token, "only binary operators or functions with "
4339 "two arguments can be marked commutative, "
4340 "unless the operation is known to be inherently "
4341 "commutative");
3d2cf79f
RB
4342 }
4343 e->expr_type = expr_type;
4344 return op;
4345 }
2e96ac06
RB
4346 else if (!(token->flags & PREV_WHITE))
4347 fatal_at (token, "expected expression operand");
4348
3d2cf79f
RB
4349 e->append_op (parse_op ());
4350 }
4351 while (1);
4352}
4353
4354/* Lex native C code delimited by START recording the preprocessing tokens
4355 for later processing.
4356 c_expr = ('{'|'(') <pp token>... ('}'|')') */
4357
4358c_expr *
4359parser::parse_c_expr (cpp_ttype start)
4360{
4361 const cpp_token *token;
4362 cpp_ttype end;
4363 unsigned opencnt;
4364 vec<cpp_token> code = vNULL;
4365 unsigned nr_stmts = 0;
620e594b 4366 location_t loc = eat_token (start)->src_loc;
3d2cf79f
RB
4367 if (start == CPP_OPEN_PAREN)
4368 end = CPP_CLOSE_PAREN;
4369 else if (start == CPP_OPEN_BRACE)
4370 end = CPP_CLOSE_BRACE;
4371 else
4372 gcc_unreachable ();
4373 opencnt = 1;
4374 do
4375 {
4376 token = next ();
4377
4378 /* Count brace pairs to find the end of the expr to match. */
4379 if (token->type == start)
4380 opencnt++;
4381 else if (token->type == end
4382 && --opencnt == 0)
4383 break;
a5382f1c
RB
4384 else if (token->type == CPP_EOF)
4385 fatal_at (token, "unexpected end of file");
3d2cf79f
RB
4386
4387 /* This is a lame way of counting the number of statements. */
4388 if (token->type == CPP_SEMICOLON)
4389 nr_stmts++;
4390
72eb311d
RB
4391 /* If this is possibly a user-defined identifier mark it used. */
4392 if (token->type == CPP_NAME)
1c9b0448
RB
4393 {
4394 id_base *idb = get_operator ((const char *)CPP_HASHNODE
4395 (token->val.node.node)->ident.str);
4396 user_id *p;
4397 if (idb && (p = dyn_cast<user_id *> (idb)) && p->is_oper_list)
4398 record_operlist (token->src_loc, p);
4399 }
72eb311d 4400
3d2cf79f
RB
4401 /* Record the token. */
4402 code.safe_push (*token);
4403 }
4404 while (1);
0889c52f 4405 return new c_expr (r, loc, code, nr_stmts, vNULL, capture_ids);
3d2cf79f
RB
4406}
4407
4408/* Parse an operand which is either an expression, a predicate or
4409 a standalone capture.
4410 op = predicate | expr | c_expr | capture */
4411
99b1c316 4412class operand *
3d2cf79f
RB
4413parser::parse_op ()
4414{
4415 const cpp_token *token = peek ();
99b1c316 4416 class operand *op = NULL;
3d2cf79f
RB
4417 if (token->type == CPP_OPEN_PAREN)
4418 {
4419 eat_token (CPP_OPEN_PAREN);
4420 op = parse_expr ();
4421 eat_token (CPP_CLOSE_PAREN);
4422 }
4423 else if (token->type == CPP_OPEN_BRACE)
4424 {
4425 op = parse_c_expr (CPP_OPEN_BRACE);
4426 }
4427 else
4428 {
4429 /* Remaining ops are either empty or predicates */
4430 if (token->type == CPP_NAME)
4431 {
4432 const char *id = get_ident ();
4433 id_base *opr = get_operator (id);
4434 if (!opr)
4435 fatal_at (token, "expected predicate name");
4e4791ff 4436 if (operator_id *code1 = dyn_cast <operator_id *> (opr))
3d2cf79f 4437 {
4e4791ff 4438 if (code1->nargs != 0)
3d2cf79f
RB
4439 fatal_at (token, "using an operator with operands as predicate");
4440 /* Parse the zero-operand operator "predicates" as
4441 expression. */
0889c52f 4442 op = new expr (opr, token->src_loc);
3d2cf79f 4443 }
4e4791ff 4444 else if (user_id *code2 = dyn_cast <user_id *> (opr))
10230017 4445 {
4e4791ff 4446 if (code2->nargs != 0)
10230017
RB
4447 fatal_at (token, "using an operator with operands as predicate");
4448 /* Parse the zero-operand operator "predicates" as
4449 expression. */
0889c52f 4450 op = new expr (opr, token->src_loc);
10230017 4451 }
3d2cf79f 4452 else if (predicate_id *p = dyn_cast <predicate_id *> (opr))
0889c52f 4453 op = new predicate (p, token->src_loc);
3d2cf79f
RB
4454 else
4455 fatal_at (token, "using an unsupported operator as predicate");
72eb311d
RB
4456 if (!parsing_match_operand)
4457 fatal_at (token, "predicates are only allowed in match expression");
3d2cf79f
RB
4458 token = peek ();
4459 if (token->flags & PREV_WHITE)
4460 return op;
4461 }
4462 else if (token->type != CPP_COLON
4463 && token->type != CPP_ATSIGN)
4464 fatal_at (token, "expected expression or predicate");
4465 /* optionally followed by a capture and a predicate. */
4466 if (token->type == CPP_COLON)
4467 fatal_at (token, "not implemented: predicate on leaf operand");
4468 if (token->type == CPP_ATSIGN)
c56f494f 4469 op = parse_capture (op, !parsing_match_operand);
3d2cf79f
RB
4470 }
4471
4472 return op;
4473}
4474
1c9b0448
RB
4475/* Create a new simplify from the current parsing state and MATCH,
4476 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
4477
4478void
8fdc6c67
RB
4479parser::push_simplify (simplify::simplify_kind kind,
4480 vec<simplify *>& simplifiers,
0889c52f 4481 operand *match, operand *result)
1c9b0448 4482{
026c3cfd 4483 /* Build and push a temporary for operator list uses in expressions. */
1c9b0448
RB
4484 if (!oper_lists.is_empty ())
4485 active_fors.safe_push (oper_lists);
4486
4487 simplifiers.safe_push
6c35e5b0 4488 (new simplify (kind, last_id++, match, result,
8fdc6c67 4489 active_fors.copy (), capture_ids));
1c9b0448
RB
4490
4491 if (!oper_lists.is_empty ())
4492 active_fors.pop ();
4493}
4494
3d2cf79f 4495/* Parse
3d2cf79f
RB
4496 <result-op> = <op> | <if> | <with>
4497 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
4498 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
8fdc6c67
RB
4499 and return it. */
4500
4501operand *
4502parser::parse_result (operand *result, predicate_id *matcher)
4503{
4504 const cpp_token *token = peek ();
4505 if (token->type != CPP_OPEN_PAREN)
4506 return parse_op ();
4507
4508 eat_token (CPP_OPEN_PAREN);
4509 if (peek_ident ("if"))
4510 {
4511 eat_ident ("if");
0889c52f 4512 if_expr *ife = new if_expr (token->src_loc);
8fdc6c67
RB
4513 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4514 if (peek ()->type == CPP_OPEN_PAREN)
4515 {
4516 ife->trueexpr = parse_result (result, matcher);
4517 if (peek ()->type == CPP_OPEN_PAREN)
4518 ife->falseexpr = parse_result (result, matcher);
4519 else if (peek ()->type != CPP_CLOSE_PAREN)
4520 ife->falseexpr = parse_op ();
4521 }
4522 else if (peek ()->type != CPP_CLOSE_PAREN)
4523 {
4524 ife->trueexpr = parse_op ();
4525 if (peek ()->type == CPP_OPEN_PAREN)
4526 ife->falseexpr = parse_result (result, matcher);
4527 else if (peek ()->type != CPP_CLOSE_PAREN)
4528 ife->falseexpr = parse_op ();
4529 }
4530 /* If this if is immediately closed then it contains a
4531 manual matcher or is part of a predicate definition. */
4532 else /* if (peek ()->type == CPP_CLOSE_PAREN) */
4533 {
4534 if (!matcher)
4535 fatal_at (peek (), "manual transform not implemented");
d3b0b692 4536 ife->trueexpr = result;
8fdc6c67
RB
4537 }
4538 eat_token (CPP_CLOSE_PAREN);
4539 return ife;
4540 }
4541 else if (peek_ident ("with"))
4542 {
4543 eat_ident ("with");
0889c52f 4544 with_expr *withe = new with_expr (token->src_loc);
8fdc6c67
RB
4545 /* Parse (with c-expr expr) as (if-with (true) expr). */
4546 withe->with = parse_c_expr (CPP_OPEN_BRACE);
4547 withe->with->nr_stmts = 0;
4548 withe->subexpr = parse_result (result, matcher);
4549 eat_token (CPP_CLOSE_PAREN);
4550 return withe;
4551 }
64d3a1f0
RB
4552 else if (peek_ident ("switch"))
4553 {
4554 token = eat_ident ("switch");
620e594b 4555 location_t ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
64d3a1f0 4556 eat_ident ("if");
0889c52f 4557 if_expr *ife = new if_expr (ifloc);
64d3a1f0
RB
4558 operand *res = ife;
4559 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4560 if (peek ()->type == CPP_OPEN_PAREN)
4561 ife->trueexpr = parse_result (result, matcher);
4562 else
4563 ife->trueexpr = parse_op ();
4564 eat_token (CPP_CLOSE_PAREN);
4565 if (peek ()->type != CPP_OPEN_PAREN
4566 || !peek_ident ("if", 2))
4567 fatal_at (token, "switch can be implemented with a single if");
4568 while (peek ()->type != CPP_CLOSE_PAREN)
4569 {
4570 if (peek ()->type == CPP_OPEN_PAREN)
4571 {
4572 if (peek_ident ("if", 2))
4573 {
0889c52f 4574 ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
64d3a1f0 4575 eat_ident ("if");
0889c52f 4576 ife->falseexpr = new if_expr (ifloc);
64d3a1f0
RB
4577 ife = as_a <if_expr *> (ife->falseexpr);
4578 ife->cond = parse_c_expr (CPP_OPEN_PAREN);
4579 if (peek ()->type == CPP_OPEN_PAREN)
4580 ife->trueexpr = parse_result (result, matcher);
4581 else
4582 ife->trueexpr = parse_op ();
4583 eat_token (CPP_CLOSE_PAREN);
4584 }
4585 else
4586 {
4587 /* switch default clause */
4588 ife->falseexpr = parse_result (result, matcher);
4589 eat_token (CPP_CLOSE_PAREN);
4590 return res;
4591 }
4592 }
4593 else
4594 {
4595 /* switch default clause */
4596 ife->falseexpr = parse_op ();
4597 eat_token (CPP_CLOSE_PAREN);
4598 return res;
4599 }
4600 }
4601 eat_token (CPP_CLOSE_PAREN);
4602 return res;
4603 }
8fdc6c67
RB
4604 else
4605 {
4606 operand *op = result;
4607 if (!matcher)
4608 op = parse_expr ();
4609 eat_token (CPP_CLOSE_PAREN);
4610 return op;
4611 }
4612}
4613
4614/* Parse
4615 simplify = 'simplify' <expr> <result-op>
4616 or
4617 match = 'match' <ident> <expr> [<result-op>]
3d2cf79f
RB
4618 and fill SIMPLIFIERS with the results. */
4619
4620void
8fdc6c67 4621parser::parse_simplify (simplify::simplify_kind kind,
3d2cf79f 4622 vec<simplify *>& simplifiers, predicate_id *matcher,
8fdc6c67 4623 operand *result)
3d2cf79f
RB
4624{
4625 /* Reset the capture map. */
d0af2c65
RB
4626 if (!capture_ids)
4627 capture_ids = new cid_map_t;
1c9b0448
RB
4628 /* Reset oper_lists and set. */
4629 hash_set <user_id *> olist;
4630 oper_lists_set = &olist;
4631 oper_lists = vNULL;
3d2cf79f
RB
4632
4633 const cpp_token *loc = peek ();
72eb311d 4634 parsing_match_operand = true;
99b1c316 4635 class operand *match = parse_op ();
2eef1fc1 4636 finish_match_operand (match);
72eb311d 4637 parsing_match_operand = false;
3d2cf79f
RB
4638 if (match->type == operand::OP_CAPTURE && !matcher)
4639 fatal_at (loc, "outermost expression cannot be captured");
4640 if (match->type == operand::OP_EXPR
4641 && is_a <predicate_id *> (as_a <expr *> (match)->operation))
4642 fatal_at (loc, "outermost expression cannot be a predicate");
4643
8fdc6c67
RB
4644 /* Splice active_ifs onto result and continue parsing the
4645 "then" expr. */
4646 if_expr *active_if = NULL;
4647 for (int i = active_ifs.length (); i > 0; --i)
4648 {
0889c52f 4649 if_expr *ifc = new if_expr (active_ifs[i-1]->location);
8fdc6c67
RB
4650 ifc->cond = active_ifs[i-1];
4651 ifc->trueexpr = active_if;
4652 active_if = ifc;
4653 }
4654 if_expr *outermost_if = active_if;
4655 while (active_if && active_if->trueexpr)
4656 active_if = as_a <if_expr *> (active_if->trueexpr);
4657
3d2cf79f
RB
4658 const cpp_token *token = peek ();
4659
4660 /* If this if is immediately closed then it is part of a predicate
4661 definition. Push it. */
4662 if (token->type == CPP_CLOSE_PAREN)
4663 {
4664 if (!matcher)
4665 fatal_at (token, "expected transform expression");
8fdc6c67
RB
4666 if (active_if)
4667 {
4668 active_if->trueexpr = result;
4669 result = outermost_if;
4670 }
0889c52f 4671 push_simplify (kind, simplifiers, match, result);
3d2cf79f
RB
4672 return;
4673 }
4674
8fdc6c67
RB
4675 operand *tem = parse_result (result, matcher);
4676 if (active_if)
3d2cf79f 4677 {
8fdc6c67
RB
4678 active_if->trueexpr = tem;
4679 result = outermost_if;
3d2cf79f 4680 }
8fdc6c67
RB
4681 else
4682 result = tem;
4683
0889c52f 4684 push_simplify (kind, simplifiers, match, result);
3d2cf79f
RB
4685}
4686
4687/* Parsing of the outer control structures. */
4688
4689/* Parse a for expression
4690 for = '(' 'for' <subst>... <pattern> ')'
4691 subst = <ident> '(' <ident>... ')' */
4692
4693void
620e594b 4694parser::parse_for (location_t)
3d2cf79f 4695{
72eb311d 4696 auto_vec<const cpp_token *> user_id_tokens;
3d2cf79f
RB
4697 vec<user_id *> user_ids = vNULL;
4698 const cpp_token *token;
4699 unsigned min_n_opers = 0, max_n_opers = 0;
4700
4701 while (1)
4702 {
72eb311d
RB
4703 token = peek ();
4704 if (token->type != CPP_NAME)
3d2cf79f
RB
4705 break;
4706
4707 /* Insert the user defined operators into the operator hash. */
4708 const char *id = get_ident ();
fa74b47a 4709 if (get_operator (id, true) != NULL)
72eb311d 4710 fatal_at (token, "operator already defined");
3d2cf79f
RB
4711 user_id *op = new user_id (id);
4712 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
3d2cf79f
RB
4713 *slot = op;
4714 user_ids.safe_push (op);
72eb311d 4715 user_id_tokens.safe_push (token);
3d2cf79f
RB
4716
4717 eat_token (CPP_OPEN_PAREN);
4718
4719 int arity = -1;
4720 while ((token = peek_ident ()) != 0)
4721 {
4722 const char *oper = get_ident ();
fa74b47a 4723 id_base *idb = get_operator (oper, true);
3d2cf79f
RB
4724 if (idb == NULL)
4725 fatal_at (token, "no such operator '%s'", oper);
39791822
RB
4726 if (*idb == CONVERT0 || *idb == CONVERT1 || *idb == CONVERT2
4727 || *idb == VIEW_CONVERT0 || *idb == VIEW_CONVERT1
4728 || *idb == VIEW_CONVERT2)
3d2cf79f
RB
4729 fatal_at (token, "conditional operators cannot be used inside for");
4730
4731 if (arity == -1)
4732 arity = idb->nargs;
4733 else if (idb->nargs == -1)
4734 ;
4735 else if (idb->nargs != arity)
4736 fatal_at (token, "operator '%s' with arity %d does not match "
4737 "others with arity %d", oper, idb->nargs, arity);
4738
72eb311d 4739 user_id *p = dyn_cast<user_id *> (idb);
7523ca9b
PK
4740 if (p)
4741 {
4742 if (p->is_oper_list)
4743 op->substitutes.safe_splice (p->substitutes);
4744 else
4745 fatal_at (token, "iterator cannot be used as operator-list");
4746 }
72eb311d
RB
4747 else
4748 op->substitutes.safe_push (idb);
3d2cf79f
RB
4749 }
4750 op->nargs = arity;
4751 token = expect (CPP_CLOSE_PAREN);
4752
4753 unsigned nsubstitutes = op->substitutes.length ();
4754 if (nsubstitutes == 0)
4755 fatal_at (token, "A user-defined operator must have at least "
4756 "one substitution");
4757 if (max_n_opers == 0)
4758 {
4759 min_n_opers = nsubstitutes;
4760 max_n_opers = nsubstitutes;
4761 }
4762 else
4763 {
4764 if (nsubstitutes % min_n_opers != 0
4765 && min_n_opers % nsubstitutes != 0)
4766 fatal_at (token, "All user-defined identifiers must have a "
4767 "multiple number of operator substitutions of the "
4768 "smallest number of substitutions");
4769 if (nsubstitutes < min_n_opers)
4770 min_n_opers = nsubstitutes;
4771 else if (nsubstitutes > max_n_opers)
4772 max_n_opers = nsubstitutes;
4773 }
4774 }
4775
4776 unsigned n_ids = user_ids.length ();
4777 if (n_ids == 0)
4778 fatal_at (token, "for requires at least one user-defined identifier");
4779
4780 token = peek ();
4781 if (token->type == CPP_CLOSE_PAREN)
4782 fatal_at (token, "no pattern defined in for");
4783
4784 active_fors.safe_push (user_ids);
4785 while (1)
4786 {
4787 token = peek ();
4788 if (token->type == CPP_CLOSE_PAREN)
4789 break;
4790 parse_pattern ();
4791 }
4792 active_fors.pop ();
4793
4794 /* Remove user-defined operators from the hash again. */
4795 for (unsigned i = 0; i < user_ids.length (); ++i)
72eb311d
RB
4796 {
4797 if (!user_ids[i]->used)
4798 warning_at (user_id_tokens[i],
4799 "operator %s defined but not used", user_ids[i]->id);
4800 operators->remove_elt (user_ids[i]);
4801 }
4802}
4803
4804/* Parse an identifier associated with a list of operators.
4805 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
4806
4807void
620e594b 4808parser::parse_operator_list (location_t)
72eb311d
RB
4809{
4810 const cpp_token *token = peek ();
4811 const char *id = get_ident ();
4812
fa74b47a 4813 if (get_operator (id, true) != 0)
72eb311d
RB
4814 fatal_at (token, "operator %s already defined", id);
4815
4816 user_id *op = new user_id (id, true);
4817 int arity = -1;
4818
4819 while ((token = peek_ident ()) != 0)
4820 {
4821 token = peek ();
4822 const char *oper = get_ident ();
fa74b47a 4823 id_base *idb = get_operator (oper, true);
72eb311d
RB
4824
4825 if (idb == 0)
4826 fatal_at (token, "no such operator '%s'", oper);
4827
4828 if (arity == -1)
4829 arity = idb->nargs;
4830 else if (idb->nargs == -1)
4831 ;
4832 else if (arity != idb->nargs)
4833 fatal_at (token, "operator '%s' with arity %d does not match "
4834 "others with arity %d", oper, idb->nargs, arity);
4835
4836 /* We allow composition of multiple operator lists. */
4837 if (user_id *p = dyn_cast<user_id *> (idb))
4838 op->substitutes.safe_splice (p->substitutes);
4839 else
4840 op->substitutes.safe_push (idb);
4841 }
4842
b78be014
PK
4843 // Check that there is no junk after id-list
4844 token = peek();
4845 if (token->type != CPP_CLOSE_PAREN)
4846 fatal_at (token, "expected identifier got %s", cpp_type2name (token->type, 0));
4847
72eb311d
RB
4848 if (op->substitutes.length () == 0)
4849 fatal_at (token, "operator-list cannot be empty");
4850
4851 op->nargs = arity;
4852 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
4853 *slot = op;
3d2cf79f
RB
4854}
4855
4856/* Parse an outer if expression.
4857 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
4858
4859void
620e594b 4860parser::parse_if (location_t)
3d2cf79f 4861{
8fdc6c67 4862 c_expr *ifexpr = parse_c_expr (CPP_OPEN_PAREN);
3d2cf79f
RB
4863
4864 const cpp_token *token = peek ();
4865 if (token->type == CPP_CLOSE_PAREN)
4866 fatal_at (token, "no pattern defined in if");
4867
8fdc6c67 4868 active_ifs.safe_push (ifexpr);
3d2cf79f
RB
4869 while (1)
4870 {
4e4791ff 4871 token = peek ();
3d2cf79f
RB
4872 if (token->type == CPP_CLOSE_PAREN)
4873 break;
4874
4875 parse_pattern ();
4876 }
4877 active_ifs.pop ();
4878}
4879
4880/* Parse a list of predefined predicate identifiers.
4881 preds = '(' 'define_predicates' <ident>... ')' */
4882
4883void
620e594b 4884parser::parse_predicates (location_t)
3d2cf79f
RB
4885{
4886 do
4887 {
4888 const cpp_token *token = peek ();
4889 if (token->type != CPP_NAME)
4890 break;
4891
4892 add_predicate (get_ident ());
4893 }
4894 while (1);
4895}
4896
4897/* Parse outer control structures.
4898 pattern = <preds>|<for>|<if>|<simplify>|<match> */
4899
4900void
4901parser::parse_pattern ()
4902{
4903 /* All clauses start with '('. */
4904 eat_token (CPP_OPEN_PAREN);
4905 const cpp_token *token = peek ();
4906 const char *id = get_ident ();
4907 if (strcmp (id, "simplify") == 0)
d0af2c65 4908 {
0889c52f 4909 parse_simplify (simplify::SIMPLIFY, simplifiers, NULL, NULL);
d0af2c65
RB
4910 capture_ids = NULL;
4911 }
3d2cf79f
RB
4912 else if (strcmp (id, "match") == 0)
4913 {
4914 bool with_args = false;
620e594b 4915 location_t e_loc = peek ()->src_loc;
3d2cf79f
RB
4916 if (peek ()->type == CPP_OPEN_PAREN)
4917 {
4918 eat_token (CPP_OPEN_PAREN);
4919 with_args = true;
4920 }
4921 const char *name = get_ident ();
4e4791ff 4922 id_base *id1 = get_operator (name);
3d2cf79f 4923 predicate_id *p;
4e4791ff 4924 if (!id1)
3d2cf79f
RB
4925 {
4926 p = add_predicate (name);
4927 user_predicates.safe_push (p);
4928 }
4e4791ff 4929 else if ((p = dyn_cast <predicate_id *> (id1)))
3d2cf79f
RB
4930 ;
4931 else
4932 fatal_at (token, "cannot add a match to a non-predicate ID");
4933 /* Parse (match <id> <arg>... (match-expr)) here. */
4934 expr *e = NULL;
4935 if (with_args)
4936 {
d0af2c65 4937 capture_ids = new cid_map_t;
0889c52f 4938 e = new expr (p, e_loc);
3d2cf79f 4939 while (peek ()->type == CPP_ATSIGN)
c56f494f 4940 e->append_op (parse_capture (NULL, false));
3d2cf79f
RB
4941 eat_token (CPP_CLOSE_PAREN);
4942 }
4943 if (p->nargs != -1
4944 && ((e && e->ops.length () != (unsigned)p->nargs)
4945 || (!e && p->nargs != 0)))
4946 fatal_at (token, "non-matching number of match operands");
4947 p->nargs = e ? e->ops.length () : 0;
0889c52f 4948 parse_simplify (simplify::MATCH, p->matchers, p, e);
d0af2c65 4949 capture_ids = NULL;
3d2cf79f
RB
4950 }
4951 else if (strcmp (id, "for") == 0)
4952 parse_for (token->src_loc);
4953 else if (strcmp (id, "if") == 0)
4954 parse_if (token->src_loc);
4955 else if (strcmp (id, "define_predicates") == 0)
4956 {
4957 if (active_ifs.length () > 0
4958 || active_fors.length () > 0)
4959 fatal_at (token, "define_predicates inside if or for is not supported");
4960 parse_predicates (token->src_loc);
4961 }
72eb311d
RB
4962 else if (strcmp (id, "define_operator_list") == 0)
4963 {
4964 if (active_ifs.length () > 0
4965 || active_fors.length () > 0)
4966 fatal_at (token, "operator-list inside if or for is not supported");
4967 parse_operator_list (token->src_loc);
4968 }
3d2cf79f
RB
4969 else
4970 fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'",
4971 active_ifs.length () == 0 && active_fors.length () == 0
4972 ? "'define_predicates', " : "");
4973
4974 eat_token (CPP_CLOSE_PAREN);
4975}
4976
2eef1fc1
RB
4977/* Helper for finish_match_operand, collecting captures of OP in CPTS
4978 recursively. */
4979
4980static void
4981walk_captures (operand *op, vec<vec<capture *> > cpts)
4982{
4983 if (! op)
4984 return;
4985
4986 if (capture *c = dyn_cast <capture *> (op))
4987 {
4988 cpts[c->where].safe_push (c);
4989 walk_captures (c->what, cpts);
4990 }
4991 else if (expr *e = dyn_cast <expr *> (op))
4992 for (unsigned i = 0; i < e->ops.length (); ++i)
4993 walk_captures (e->ops[i], cpts);
4994}
4995
4996/* Finish up OP which is a match operand. */
4997
4998void
4999parser::finish_match_operand (operand *op)
5000{
5001 /* Look for matching captures, diagnose mis-uses of @@ and apply
5002 early lowering and distribution of value_match. */
5003 auto_vec<vec<capture *> > cpts;
5004 cpts.safe_grow_cleared (capture_ids->elements ());
5005 walk_captures (op, cpts);
5006 for (unsigned i = 0; i < cpts.length (); ++i)
5007 {
5008 capture *value_match = NULL;
5009 for (unsigned j = 0; j < cpts[i].length (); ++j)
5010 {
5011 if (cpts[i][j]->value_match)
5012 {
5013 if (value_match)
5014 fatal_at (cpts[i][j]->location, "duplicate @@");
5015 value_match = cpts[i][j];
5016 }
5017 }
5018 if (cpts[i].length () == 1 && value_match)
5019 fatal_at (value_match->location, "@@ without a matching capture");
5020 if (value_match)
5021 {
5022 /* Duplicate prevailing capture with the existing ID, create
5023 a fake ID and rewrite all captures to use it. This turns
5024 @@1 into @__<newid>@1 and @1 into @__<newid>. */
5025 value_match->what = new capture (value_match->location,
5026 value_match->where,
5027 value_match->what, false);
5028 /* Create a fake ID and rewrite all captures to use it. */
5029 unsigned newid = get_internal_capture_id ();
5030 for (unsigned j = 0; j < cpts[i].length (); ++j)
5031 {
5032 cpts[i][j]->where = newid;
5033 cpts[i][j]->value_match = true;
5034 }
5035 }
5036 cpts[i].release ();
5037 }
5038}
5039
3d2cf79f
RB
5040/* Main entry of the parser. Repeatedly parse outer control structures. */
5041
5042parser::parser (cpp_reader *r_)
5043{
5044 r = r_;
5045 active_ifs = vNULL;
5046 active_fors = vNULL;
5047 simplifiers = vNULL;
1c9b0448
RB
5048 oper_lists_set = NULL;
5049 oper_lists = vNULL;
d0af2c65 5050 capture_ids = NULL;
3d2cf79f 5051 user_predicates = vNULL;
72eb311d 5052 parsing_match_operand = false;
6c35e5b0 5053 last_id = 0;
3d2cf79f
RB
5054
5055 const cpp_token *token = next ();
5056 while (token->type != CPP_EOF)
5057 {
5058 _cpp_backup_tokens (r, 1);
5059 parse_pattern ();
5060 token = next ();
5061 }
5062}
5063
5064
5065/* Helper for the linemap code. */
5066
5067static size_t
5068round_alloc_size (size_t s)
5069{
5070 return s;
5071}
5072
5073
5074/* The genmatch generator progam. It reads from a pattern description
5075 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
5076
5077int
5078main (int argc, char **argv)
5079{
5080 cpp_reader *r;
5081
5082 progname = "genmatch";
5083
5084 if (argc < 2)
5085 return 1;
5086
5087 bool gimple = true;
3d2cf79f
RB
5088 char *input = argv[argc-1];
5089 for (int i = 1; i < argc - 1; ++i)
5090 {
5091 if (strcmp (argv[i], "--gimple") == 0)
5092 gimple = true;
5093 else if (strcmp (argv[i], "--generic") == 0)
5094 gimple = false;
5095 else if (strcmp (argv[i], "-v") == 0)
53a19317
RB
5096 verbose = 1;
5097 else if (strcmp (argv[i], "-vv") == 0)
5098 verbose = 2;
3d2cf79f
RB
5099 else
5100 {
5101 fprintf (stderr, "Usage: genmatch "
53a19317 5102 "[--gimple] [--generic] [-v[v]] input\n");
3d2cf79f
RB
5103 return 1;
5104 }
5105 }
5106
99b1c316 5107 line_table = XCNEW (class line_maps);
3d2cf79f
RB
5108 linemap_init (line_table, 0);
5109 line_table->reallocator = xrealloc;
5110 line_table->round_alloc_size = round_alloc_size;
5111
5112 r = cpp_create_reader (CLK_GNUC99, NULL, line_table);
5113 cpp_callbacks *cb = cpp_get_callbacks (r);
c24300ba 5114 cb->diagnostic = diagnostic_cb;
3d2cf79f 5115
b1dc4a20
RS
5116 /* Add the build directory to the #include "" search path. */
5117 cpp_dir *dir = XCNEW (cpp_dir);
5118 dir->name = getpwd ();
5119 if (!dir->name)
5120 dir->name = ASTRDUP (".");
5121 cpp_set_include_chains (r, dir, NULL, false);
5122
3d2cf79f
RB
5123 if (!cpp_read_main_file (r, input))
5124 return 1;
5125 cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1");
5126 cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0");
5127
fa74b47a
RS
5128 null_id = new id_base (id_base::NULL_ID, "null");
5129
3d2cf79f
RB
5130 /* Pre-seed operators. */
5131 operators = new hash_table<id_base> (1024);
5132#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
5133 add_operator (SYM, # SYM, # TYPE, NARGS);
5134#define END_OF_BASE_TREE_CODES
5135#include "tree.def"
c9e926ce
RS
5136add_operator (CONVERT0, "convert0", "tcc_unary", 1);
5137add_operator (CONVERT1, "convert1", "tcc_unary", 1);
5138add_operator (CONVERT2, "convert2", "tcc_unary", 1);
5139add_operator (VIEW_CONVERT0, "view_convert0", "tcc_unary", 1);
5140add_operator (VIEW_CONVERT1, "view_convert1", "tcc_unary", 1);
5141add_operator (VIEW_CONVERT2, "view_convert2", "tcc_unary", 1);
3d2cf79f
RB
5142#undef END_OF_BASE_TREE_CODES
5143#undef DEFTREECODE
5144
5145 /* Pre-seed builtin functions.
5146 ??? Cannot use N (name) as that is targetm.emultls.get_address
5147 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
5148#define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
c9e926ce 5149 add_function (ENUM, "CFN_" # ENUM);
3d2cf79f 5150#include "builtins.def"
3d2cf79f 5151
c9e926ce
RS
5152#define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \
5153 add_function (IFN_##CODE, "CFN_" #CODE);
5154#include "internal-fn.def"
5155
3d2cf79f
RB
5156 /* Parse ahead! */
5157 parser p (r);
5158
5159 if (gimple)
5160 write_header (stdout, "gimple-match-head.c");
5161 else
5162 write_header (stdout, "generic-match-head.c");
5163
5164 /* Go over all predicates defined with patterns and perform
5165 lowering and code generation. */
5166 for (unsigned i = 0; i < p.user_predicates.length (); ++i)
5167 {
5168 predicate_id *pred = p.user_predicates[i];
47b25362 5169 lower (pred->matchers, gimple);
3d2cf79f 5170
53a19317 5171 if (verbose == 2)
4e4791ff
BE
5172 for (unsigned j = 0; j < pred->matchers.length (); ++j)
5173 print_matches (pred->matchers[j]);
3d2cf79f
RB
5174
5175 decision_tree dt;
4e4791ff
BE
5176 for (unsigned j = 0; j < pred->matchers.length (); ++j)
5177 dt.insert (pred->matchers[j], j);
3d2cf79f 5178
53a19317 5179 if (verbose == 2)
3d2cf79f
RB
5180 dt.print (stderr);
5181
5182 write_predicate (stdout, pred, dt, gimple);
5183 }
5184
5185 /* Lower the main simplifiers and generate code for them. */
47b25362 5186 lower (p.simplifiers, gimple);
3d2cf79f 5187
53a19317 5188 if (verbose == 2)
3d2cf79f
RB
5189 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
5190 print_matches (p.simplifiers[i]);
5191
5192 decision_tree dt;
5193 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
5194 dt.insert (p.simplifiers[i], i);
5195
53a19317 5196 if (verbose == 2)
3d2cf79f
RB
5197 dt.print (stderr);
5198
b21ce9ce 5199 dt.gen (stdout, gimple);
3d2cf79f
RB
5200
5201 /* Finalize. */
5202 cpp_finish (r, NULL);
5203 cpp_destroy (r);
5204
5205 delete operators;
5206
5207 return 0;
5208}