genmatch.c (dt_node::gen_kids): Fix placement of break statement.
[gcc.git] / gcc / genmatch.c
1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
3
4 Copyright (C) 2014 Free Software Foundation, Inc.
5 Contributed by Richard Biener <rguenther@suse.de>
6 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 #include "bconfig.h"
25 #include <new>
26 #include "system.h"
27 #include "coretypes.h"
28 #include "ggc.h"
29 #include <cpplib.h>
30 #include "errors.h"
31 #include "hashtab.h"
32 #include "hash-table.h"
33 #include "hash-map.h"
34 #include "vec.h"
35 #include "is-a.h"
36
37
38 /* Stubs for GGC referenced through instantiations triggered by hash-map. */
39 void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
40 size_t, size_t MEM_STAT_DECL)
41 {
42 return NULL;
43 }
44 void ggc_free (void *)
45 {
46 }
47
48
49 /* libccp helpers. */
50
51 static struct line_maps *line_table;
52
53 static bool
54 #if GCC_VERSION >= 4001
55 __attribute__((format (printf, 6, 0)))
56 #endif
57 error_cb (cpp_reader *, int errtype, int, source_location location,
58 unsigned int, const char *msg, va_list *ap)
59 {
60 const line_map *map;
61 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
62 expanded_location loc = linemap_expand_location (line_table, map, location);
63 fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column,
64 (errtype == CPP_DL_WARNING) ? "warning" : "error");
65 vfprintf (stderr, msg, *ap);
66 fprintf (stderr, "\n");
67 FILE *f = fopen (loc.file, "r");
68 if (f)
69 {
70 char buf[128];
71 while (loc.line > 0)
72 {
73 if (!fgets (buf, 128, f))
74 goto notfound;
75 if (buf[strlen (buf) - 1] != '\n')
76 {
77 if (loc.line > 1)
78 loc.line++;
79 }
80 loc.line--;
81 }
82 fprintf (stderr, "%s", buf);
83 for (int i = 0; i < loc.column - 1; ++i)
84 fputc (' ', stderr);
85 fputc ('^', stderr);
86 fputc ('\n', stderr);
87 notfound:
88 fclose (f);
89 }
90
91 if (errtype == CPP_DL_FATAL)
92 exit (1);
93 return false;
94 }
95
96 static void
97 #if GCC_VERSION >= 4001
98 __attribute__((format (printf, 2, 3)))
99 #endif
100 fatal_at (const cpp_token *tk, const char *msg, ...)
101 {
102 va_list ap;
103 va_start (ap, msg);
104 error_cb (NULL, CPP_DL_FATAL, 0, tk->src_loc, 0, msg, &ap);
105 va_end (ap);
106 }
107
108 static void
109 #if GCC_VERSION >= 4001
110 __attribute__((format (printf, 2, 3)))
111 #endif
112 warning_at (const cpp_token *tk, const char *msg, ...)
113 {
114 va_list ap;
115 va_start (ap, msg);
116 error_cb (NULL, CPP_DL_WARNING, 0, tk->src_loc, 0, msg, &ap);
117 va_end (ap);
118 }
119
120 static void
121 output_line_directive (FILE *f, source_location location,
122 bool dumpfile = false)
123 {
124 const line_map *map;
125 linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
126 expanded_location loc = linemap_expand_location (line_table, map, location);
127 if (dumpfile)
128 {
129 /* When writing to a dumpfile only dump the filename. */
130 const char *file = strrchr (loc.file, DIR_SEPARATOR);
131 if (!file)
132 file = loc.file;
133 else
134 ++file;
135 fprintf (f, "%s:%d", file, loc.line);
136 }
137 else
138 /* Other gen programs really output line directives here, at least for
139 development it's right now more convenient to have line information
140 from the generated file. Still keep the directives as comment for now
141 to easily back-point to the meta-description. */
142 fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file);
143 }
144
145
146 /* Pull in tree codes and builtin function codes from their
147 definition files. */
148
149 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
150 enum tree_code {
151 #include "tree.def"
152 CONVERT0,
153 CONVERT1,
154 CONVERT2,
155 MAX_TREE_CODES
156 };
157 #undef DEFTREECODE
158
159 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
160 enum built_in_function {
161 #include "builtins.def"
162 END_BUILTINS
163 };
164 #undef DEF_BUILTIN
165
166
167 /* Base class for all identifiers the parser knows. */
168
169 struct id_base : typed_noop_remove<id_base>
170 {
171 enum id_kind { CODE, FN, PREDICATE, USER } kind;
172
173 id_base (id_kind, const char *, int = -1);
174
175 hashval_t hashval;
176 int nargs;
177 const char *id;
178
179 /* hash_table support. */
180 typedef id_base value_type;
181 typedef id_base compare_type;
182 static inline hashval_t hash (const value_type *);
183 static inline int equal (const value_type *, const compare_type *);
184 };
185
186 inline hashval_t
187 id_base::hash (const value_type *op)
188 {
189 return op->hashval;
190 }
191
192 inline int
193 id_base::equal (const value_type *op1,
194 const compare_type *op2)
195 {
196 return (op1->hashval == op2->hashval
197 && strcmp (op1->id, op2->id) == 0);
198 }
199
200 /* Hashtable of known pattern operators. This is pre-seeded from
201 all known tree codes and all known builtin function ids. */
202 static hash_table<id_base> *operators;
203
204 id_base::id_base (id_kind kind_, const char *id_, int nargs_)
205 {
206 kind = kind_;
207 id = id_;
208 nargs = nargs_;
209 hashval = htab_hash_string (id);
210 }
211
212 /* Identifier that maps to a tree code. */
213
214 struct operator_id : public id_base
215 {
216 operator_id (enum tree_code code_, const char *id_, unsigned nargs_,
217 const char *tcc_)
218 : id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {}
219 enum tree_code code;
220 const char *tcc;
221 };
222
223 /* Identifier that maps to a builtin function code. */
224
225 struct fn_id : public id_base
226 {
227 fn_id (enum built_in_function fn_, const char *id_)
228 : id_base (id_base::FN, id_), fn (fn_) {}
229 enum built_in_function fn;
230 };
231
232 struct simplify;
233
234 /* Identifier that maps to a user-defined predicate. */
235
236 struct predicate_id : public id_base
237 {
238 predicate_id (const char *id_)
239 : id_base (id_base::PREDICATE, id_), matchers (vNULL) {}
240 vec<simplify *> matchers;
241 };
242
243 /* Identifier that maps to a operator defined by a 'for' directive. */
244
245 struct user_id : public id_base
246 {
247 user_id (const char *id_, bool is_oper_list_ = false)
248 : id_base (id_base::USER, id_), substitutes (vNULL),
249 used (false), is_oper_list (is_oper_list_) {}
250 vec<id_base *> substitutes;
251 bool used;
252 bool is_oper_list;
253 };
254
255 template<>
256 template<>
257 inline bool
258 is_a_helper <fn_id *>::test (id_base *id)
259 {
260 return id->kind == id_base::FN;
261 }
262
263 template<>
264 template<>
265 inline bool
266 is_a_helper <operator_id *>::test (id_base *id)
267 {
268 return id->kind == id_base::CODE;
269 }
270
271 template<>
272 template<>
273 inline bool
274 is_a_helper <predicate_id *>::test (id_base *id)
275 {
276 return id->kind == id_base::PREDICATE;
277 }
278
279 template<>
280 template<>
281 inline bool
282 is_a_helper <user_id *>::test (id_base *id)
283 {
284 return id->kind == id_base::USER;
285 }
286
287 /* Add a predicate identifier to the hash. */
288
289 static predicate_id *
290 add_predicate (const char *id)
291 {
292 predicate_id *p = new predicate_id (id);
293 id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT);
294 if (*slot)
295 fatal ("duplicate id definition");
296 *slot = p;
297 return p;
298 }
299
300 /* Add a tree code identifier to the hash. */
301
302 static void
303 add_operator (enum tree_code code, const char *id,
304 const char *tcc, unsigned nargs)
305 {
306 if (strcmp (tcc, "tcc_unary") != 0
307 && strcmp (tcc, "tcc_binary") != 0
308 && strcmp (tcc, "tcc_comparison") != 0
309 && strcmp (tcc, "tcc_expression") != 0
310 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
311 && strcmp (tcc, "tcc_reference") != 0
312 /* To have INTEGER_CST and friends as "predicate operators". */
313 && strcmp (tcc, "tcc_constant") != 0)
314 return;
315 operator_id *op = new operator_id (code, id, nargs, tcc);
316 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
317 if (*slot)
318 fatal ("duplicate id definition");
319 *slot = op;
320 }
321
322 /* Add a builtin identifier to the hash. */
323
324 static void
325 add_builtin (enum built_in_function code, const char *id)
326 {
327 fn_id *fn = new fn_id (code, id);
328 id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT);
329 if (*slot)
330 fatal ("duplicate id definition");
331 *slot = fn;
332 }
333
334 /* Helper for easy comparing ID with tree code CODE. */
335
336 static bool
337 operator==(id_base &id, enum tree_code code)
338 {
339 if (operator_id *oid = dyn_cast <operator_id *> (&id))
340 return oid->code == code;
341 return false;
342 }
343
344 /* Lookup the identifier ID. */
345
346 id_base *
347 get_operator (const char *id)
348 {
349 id_base tem (id_base::CODE, id);
350
351 id_base *op = operators->find_with_hash (&tem, tem.hashval);
352 if (op)
353 {
354 /* If this is a user-defined identifier track whether it was used. */
355 if (user_id *uid = dyn_cast<user_id *> (op))
356 uid->used = true;
357 return op;
358 }
359
360 /* Try all-uppercase. */
361 char *id2 = xstrdup (id);
362 for (unsigned i = 0; i < strlen (id2); ++i)
363 id2[i] = TOUPPER (id2[i]);
364 new (&tem) id_base (id_base::CODE, id2);
365 op = operators->find_with_hash (&tem, tem.hashval);
366 if (op)
367 {
368 free (id2);
369 return op;
370 }
371
372 /* Try _EXPR appended. */
373 id2 = (char *)xrealloc (id2, strlen (id2) + sizeof ("_EXPR") + 1);
374 strcat (id2, "_EXPR");
375 new (&tem) id_base (id_base::CODE, id2);
376 op = operators->find_with_hash (&tem, tem.hashval);
377 if (op)
378 {
379 free (id2);
380 return op;
381 }
382
383 return 0;
384 }
385
386
387 /* Helper for the capture-id map. */
388
389 struct capture_id_map_hasher : default_hashmap_traits
390 {
391 static inline hashval_t hash (const char *);
392 static inline bool equal_keys (const char *, const char *);
393 };
394
395 inline hashval_t
396 capture_id_map_hasher::hash (const char *id)
397 {
398 return htab_hash_string (id);
399 }
400
401 inline bool
402 capture_id_map_hasher::equal_keys (const char *id1, const char *id2)
403 {
404 return strcmp (id1, id2) == 0;
405 }
406
407 typedef hash_map<const char *, unsigned, capture_id_map_hasher> cid_map_t;
408
409
410 /* The AST produced by parsing of the pattern definitions. */
411
412 struct dt_operand;
413 struct capture_info;
414
415 /* The base class for operands. */
416
417 struct operand {
418 enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR };
419 operand (enum op_type type_) : type (type_) {}
420 enum op_type type;
421 virtual void gen_transform (FILE *, const char *, bool, int,
422 const char *, capture_info *,
423 dt_operand ** = 0,
424 bool = true)
425 { gcc_unreachable (); }
426 };
427
428 /* A predicate operand. Predicates are leafs in the AST. */
429
430 struct predicate : public operand
431 {
432 predicate (predicate_id *p_) : operand (OP_PREDICATE), p (p_) {}
433 predicate_id *p;
434 };
435
436 /* An operand that constitutes an expression. Expressions include
437 function calls and user-defined predicate invocations. */
438
439 struct expr : public operand
440 {
441 expr (id_base *operation_, bool is_commutative_ = false)
442 : operand (OP_EXPR), operation (operation_),
443 ops (vNULL), expr_type (NULL), is_commutative (is_commutative_),
444 is_generic (false) {}
445 void append_op (operand *op) { ops.safe_push (op); }
446 /* The operator and its operands. */
447 id_base *operation;
448 vec<operand *> ops;
449 /* An explicitely specified type - used exclusively for conversions. */
450 const char *expr_type;
451 /* Whether the operation is to be applied commutatively. This is
452 later lowered to two separate patterns. */
453 bool is_commutative;
454 /* Whether the expression is expected to be in GENERIC form. */
455 bool is_generic;
456 virtual void gen_transform (FILE *f, const char *, bool, int,
457 const char *, capture_info *,
458 dt_operand ** = 0, bool = true);
459 };
460
461 /* An operator that is represented by native C code. This is always
462 a leaf operand in the AST. This class is also used to represent
463 the code to be generated for 'if' and 'with' expressions. */
464
465 struct c_expr : public operand
466 {
467 /* A mapping of an identifier and its replacement. Used to apply
468 'for' lowering. */
469 struct id_tab {
470 const char *id;
471 const char *oper;
472 id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {}
473 };
474
475 c_expr (cpp_reader *r_, vec<cpp_token> code_, unsigned nr_stmts_,
476 vec<id_tab> ids_, cid_map_t *capture_ids_)
477 : operand (OP_C_EXPR), r (r_), code (code_), capture_ids (capture_ids_),
478 nr_stmts (nr_stmts_), ids (ids_) {}
479 /* cpplib tokens and state to transform this back to source. */
480 cpp_reader *r;
481 vec<cpp_token> code;
482 cid_map_t *capture_ids;
483 /* The number of statements parsed (well, the number of ';'s). */
484 unsigned nr_stmts;
485 /* The identifier replacement vector. */
486 vec<id_tab> ids;
487 virtual void gen_transform (FILE *f, const char *, bool, int,
488 const char *, capture_info *,
489 dt_operand ** = 0, bool = true);
490 };
491
492 /* A wrapper around another operand that captures its value. */
493
494 struct capture : public operand
495 {
496 capture (unsigned where_, operand *what_)
497 : operand (OP_CAPTURE), where (where_), what (what_) {}
498 /* Identifier index for the value. */
499 unsigned where;
500 /* The captured value. */
501 operand *what;
502 virtual void gen_transform (FILE *f, const char *, bool, int,
503 const char *, capture_info *,
504 dt_operand ** = 0, bool = true);
505 };
506
507 template<>
508 template<>
509 inline bool
510 is_a_helper <capture *>::test (operand *op)
511 {
512 return op->type == operand::OP_CAPTURE;
513 }
514
515 template<>
516 template<>
517 inline bool
518 is_a_helper <predicate *>::test (operand *op)
519 {
520 return op->type == operand::OP_PREDICATE;
521 }
522
523 template<>
524 template<>
525 inline bool
526 is_a_helper <c_expr *>::test (operand *op)
527 {
528 return op->type == operand::OP_C_EXPR;
529 }
530
531 template<>
532 template<>
533 inline bool
534 is_a_helper <expr *>::test (operand *op)
535 {
536 return op->type == operand::OP_EXPR;
537 }
538
539 /* Helper to distinguish 'if' from 'with' expressions. */
540
541 struct if_or_with
542 {
543 if_or_with (operand *cexpr_, source_location location_, bool is_with_)
544 : location (location_), cexpr (cexpr_), is_with (is_with_) {}
545 source_location location;
546 operand *cexpr;
547 bool is_with;
548 };
549
550 /* The main class of a pattern and its transform. This is used to
551 represent both (simplify ...) and (match ...) kinds. The AST
552 duplicates all outer 'if' and 'for' expressions here so each
553 simplify can exist in isolation. */
554
555 struct simplify
556 {
557 simplify (operand *match_, source_location match_location_,
558 struct operand *result_, source_location result_location_,
559 vec<if_or_with> ifexpr_vec_, vec<vec<user_id *> > for_vec_,
560 cid_map_t *capture_ids_)
561 : match (match_), match_location (match_location_),
562 result (result_), result_location (result_location_),
563 ifexpr_vec (ifexpr_vec_), for_vec (for_vec_),
564 capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
565
566 /* The expression that is matched against the GENERIC or GIMPLE IL. */
567 operand *match;
568 source_location match_location;
569 /* For a (simplify ...) the expression produced when the pattern applies.
570 For a (match ...) either NULL if it is a simple predicate or the
571 single expression specifying the matched operands. */
572 struct operand *result;
573 source_location result_location;
574 /* Collected 'if' expressions that need to evaluate to true to make
575 the pattern apply. */
576 vec<if_or_with> ifexpr_vec;
577 /* Collected 'for' expression operators that have to be replaced
578 in the lowering phase. */
579 vec<vec<user_id *> > for_vec;
580 /* A map of capture identifiers to indexes. */
581 cid_map_t *capture_ids;
582 int capture_max;
583 };
584
585 /* Debugging routines for dumping the AST. */
586
587 DEBUG_FUNCTION void
588 print_operand (operand *o, FILE *f = stderr, bool flattened = false)
589 {
590 if (capture *c = dyn_cast<capture *> (o))
591 {
592 fprintf (f, "@%u", c->where);
593 if (c->what && flattened == false)
594 {
595 putc (':', f);
596 print_operand (c->what, f, flattened);
597 putc (' ', f);
598 }
599 }
600
601 else if (predicate *p = dyn_cast<predicate *> (o))
602 fprintf (f, "%s", p->p->id);
603
604 else if (is_a<c_expr *> (o))
605 fprintf (f, "c_expr");
606
607 else if (expr *e = dyn_cast<expr *> (o))
608 {
609 fprintf (f, "(%s", e->operation->id);
610
611 if (flattened == false)
612 {
613 putc (' ', f);
614 for (unsigned i = 0; i < e->ops.length (); ++i)
615 {
616 print_operand (e->ops[i], f, flattened);
617 putc (' ', f);
618 }
619 }
620 putc (')', f);
621 }
622
623 else
624 gcc_unreachable ();
625 }
626
627 DEBUG_FUNCTION void
628 print_matches (struct simplify *s, FILE *f = stderr)
629 {
630 fprintf (f, "for expression: ");
631 print_operand (s->match, f);
632 putc ('\n', f);
633 }
634
635
636 /* AST lowering. */
637
638 /* Lowering of commutative operators. */
639
640 static void
641 cartesian_product (const vec< vec<operand *> >& ops_vector,
642 vec< vec<operand *> >& result, vec<operand *>& v, unsigned n)
643 {
644 if (n == ops_vector.length ())
645 {
646 vec<operand *> xv = v.copy ();
647 result.safe_push (xv);
648 return;
649 }
650
651 for (unsigned i = 0; i < ops_vector[n].length (); ++i)
652 {
653 v[n] = ops_vector[n][i];
654 cartesian_product (ops_vector, result, v, n + 1);
655 }
656 }
657
658 /* Lower OP to two operands in case it is marked as commutative. */
659
660 static vec<operand *>
661 commutate (operand *op)
662 {
663 vec<operand *> ret = vNULL;
664
665 if (capture *c = dyn_cast <capture *> (op))
666 {
667 if (!c->what)
668 {
669 ret.safe_push (op);
670 return ret;
671 }
672 vec<operand *> v = commutate (c->what);
673 for (unsigned i = 0; i < v.length (); ++i)
674 {
675 capture *nc = new capture (c->where, v[i]);
676 ret.safe_push (nc);
677 }
678 return ret;
679 }
680
681 expr *e = dyn_cast <expr *> (op);
682 if (!e || e->ops.length () == 0)
683 {
684 ret.safe_push (op);
685 return ret;
686 }
687
688 vec< vec<operand *> > ops_vector = vNULL;
689 for (unsigned i = 0; i < e->ops.length (); ++i)
690 ops_vector.safe_push (commutate (e->ops[i]));
691
692 auto_vec< vec<operand *> > result;
693 auto_vec<operand *> v (e->ops.length ());
694 v.quick_grow_cleared (e->ops.length ());
695 cartesian_product (ops_vector, result, v, 0);
696
697
698 for (unsigned i = 0; i < result.length (); ++i)
699 {
700 expr *ne = new expr (e->operation);
701 for (unsigned j = 0; j < result[i].length (); ++j)
702 ne->append_op (result[i][j]);
703 ret.safe_push (ne);
704 }
705
706 if (!e->is_commutative)
707 return ret;
708
709 for (unsigned i = 0; i < result.length (); ++i)
710 {
711 expr *ne = new expr (e->operation);
712 // result[i].length () is 2 since e->operation is binary
713 for (unsigned j = result[i].length (); j; --j)
714 ne->append_op (result[i][j-1]);
715 ret.safe_push (ne);
716 }
717
718 return ret;
719 }
720
721 /* Lower operations marked as commutative in the AST of S and push
722 the resulting patterns to SIMPLIFIERS. */
723
724 static void
725 lower_commutative (simplify *s, vec<simplify *>& simplifiers)
726 {
727 vec<operand *> matchers = commutate (s->match);
728 for (unsigned i = 0; i < matchers.length (); ++i)
729 {
730 simplify *ns = new simplify (matchers[i], s->match_location,
731 s->result, s->result_location, s->ifexpr_vec,
732 s->for_vec, s->capture_ids);
733 simplifiers.safe_push (ns);
734 }
735 }
736
737 /* Strip conditional conversios using operator OPER from O and its
738 children if STRIP, else replace them with an unconditional convert. */
739
740 operand *
741 lower_opt_convert (operand *o, enum tree_code oper, bool strip)
742 {
743 if (capture *c = dyn_cast<capture *> (o))
744 {
745 if (c->what)
746 return new capture (c->where, lower_opt_convert (c->what, oper, strip));
747 else
748 return c;
749 }
750
751 expr *e = dyn_cast<expr *> (o);
752 if (!e)
753 return o;
754
755 if (*e->operation == oper)
756 {
757 if (strip)
758 return lower_opt_convert (e->ops[0], oper, strip);
759
760 expr *ne = new expr (get_operator ("CONVERT_EXPR"));
761 ne->append_op (lower_opt_convert (e->ops[0], oper, strip));
762 return ne;
763 }
764
765 expr *ne = new expr (e->operation, e->is_commutative);
766 for (unsigned i = 0; i < e->ops.length (); ++i)
767 ne->append_op (lower_opt_convert (e->ops[i], oper, strip));
768
769 return ne;
770 }
771
772 /* Determine whether O or its children uses the conditional conversion
773 operator OPER. */
774
775 static bool
776 has_opt_convert (operand *o, enum tree_code oper)
777 {
778 if (capture *c = dyn_cast<capture *> (o))
779 {
780 if (c->what)
781 return has_opt_convert (c->what, oper);
782 else
783 return false;
784 }
785
786 expr *e = dyn_cast<expr *> (o);
787 if (!e)
788 return false;
789
790 if (*e->operation == oper)
791 return true;
792
793 for (unsigned i = 0; i < e->ops.length (); ++i)
794 if (has_opt_convert (e->ops[i], oper))
795 return true;
796
797 return false;
798 }
799
800 /* Lower conditional convert operators in O, expanding it to a vector
801 if required. */
802
803 static vec<operand *>
804 lower_opt_convert (operand *o)
805 {
806 vec<operand *> v1 = vNULL, v2;
807
808 v1.safe_push (o);
809
810 enum tree_code opers[] = { CONVERT0, CONVERT1, CONVERT2 };
811
812 /* Conditional converts are lowered to a pattern with the
813 conversion and one without. The three different conditional
814 convert codes are lowered separately. */
815
816 for (unsigned i = 0; i < 3; ++i)
817 {
818 v2 = vNULL;
819 for (unsigned j = 0; j < v1.length (); ++j)
820 if (has_opt_convert (v1[j], opers[i]))
821 {
822 v2.safe_push (lower_opt_convert (v1[j], opers[i], false));
823 v2.safe_push (lower_opt_convert (v1[j], opers[i], true));
824 }
825
826 if (v2 != vNULL)
827 {
828 v1 = vNULL;
829 for (unsigned j = 0; j < v2.length (); ++j)
830 v1.safe_push (v2[j]);
831 }
832 }
833
834 return v1;
835 }
836
837 /* Lower conditional convert operators in the AST of S and push
838 the resulting multiple patterns to SIMPLIFIERS. */
839
840 static void
841 lower_opt_convert (simplify *s, vec<simplify *>& simplifiers)
842 {
843 vec<operand *> matchers = lower_opt_convert (s->match);
844 for (unsigned i = 0; i < matchers.length (); ++i)
845 {
846 simplify *ns = new simplify (matchers[i], s->match_location,
847 s->result, s->result_location, s->ifexpr_vec,
848 s->for_vec, s->capture_ids);
849 simplifiers.safe_push (ns);
850 }
851 }
852
853 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
854 GENERIC and a GIMPLE variant. */
855
856 static vec<operand *>
857 lower_cond (operand *o)
858 {
859 vec<operand *> ro = vNULL;
860
861 if (capture *c = dyn_cast<capture *> (o))
862 {
863 if (c->what)
864 {
865 vec<operand *> lop = vNULL;
866 lop = lower_cond (c->what);
867
868 for (unsigned i = 0; i < lop.length (); ++i)
869 ro.safe_push (new capture (c->where, lop[i]));
870 return ro;
871 }
872 }
873
874 expr *e = dyn_cast<expr *> (o);
875 if (!e || e->ops.length () == 0)
876 {
877 ro.safe_push (o);
878 return ro;
879 }
880
881 vec< vec<operand *> > ops_vector = vNULL;
882 for (unsigned i = 0; i < e->ops.length (); ++i)
883 ops_vector.safe_push (lower_cond (e->ops[i]));
884
885 auto_vec< vec<operand *> > result;
886 auto_vec<operand *> v (e->ops.length ());
887 v.quick_grow_cleared (e->ops.length ());
888 cartesian_product (ops_vector, result, v, 0);
889
890 for (unsigned i = 0; i < result.length (); ++i)
891 {
892 expr *ne = new expr (e->operation);
893 for (unsigned j = 0; j < result[i].length (); ++j)
894 ne->append_op (result[i][j]);
895 ro.safe_push (ne);
896 /* If this is a COND with a captured expression or an
897 expression with two operands then also match a GENERIC
898 form on the compare. */
899 if ((*e->operation == COND_EXPR
900 || *e->operation == VEC_COND_EXPR)
901 && ((is_a <capture *> (e->ops[0])
902 && as_a <capture *> (e->ops[0])->what
903 && is_a <expr *> (as_a <capture *> (e->ops[0])->what)
904 && as_a <expr *>
905 (as_a <capture *> (e->ops[0])->what)->ops.length () == 2)
906 || (is_a <expr *> (e->ops[0])
907 && as_a <expr *> (e->ops[0])->ops.length () == 2)))
908 {
909 expr *ne = new expr (e->operation);
910 for (unsigned j = 0; j < result[i].length (); ++j)
911 ne->append_op (result[i][j]);
912 if (capture *c = dyn_cast <capture *> (ne->ops[0]))
913 {
914 expr *ocmp = as_a <expr *> (c->what);
915 expr *cmp = new expr (ocmp->operation);
916 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
917 cmp->append_op (ocmp->ops[j]);
918 cmp->is_generic = true;
919 ne->ops[0] = new capture (c->where, cmp);
920 }
921 else
922 {
923 expr *ocmp = as_a <expr *> (ne->ops[0]);
924 expr *cmp = new expr (ocmp->operation);
925 for (unsigned j = 0; j < ocmp->ops.length (); ++j)
926 cmp->append_op (ocmp->ops[j]);
927 cmp->is_generic = true;
928 ne->ops[0] = cmp;
929 }
930 ro.safe_push (ne);
931 }
932 }
933
934 return ro;
935 }
936
937 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
938 GENERIC and a GIMPLE variant. */
939
940 static void
941 lower_cond (simplify *s, vec<simplify *>& simplifiers)
942 {
943 vec<operand *> matchers = lower_cond (s->match);
944 for (unsigned i = 0; i < matchers.length (); ++i)
945 {
946 simplify *ns = new simplify (matchers[i], s->match_location,
947 s->result, s->result_location, s->ifexpr_vec,
948 s->for_vec, s->capture_ids);
949 simplifiers.safe_push (ns);
950 }
951 }
952
953 /* In AST operand O replace operator ID with operator WITH. */
954
955 operand *
956 replace_id (operand *o, user_id *id, id_base *with)
957 {
958 /* Deep-copy captures and expressions, replacing operations as
959 needed. */
960 if (capture *c = dyn_cast<capture *> (o))
961 {
962 if (!c->what)
963 return c;
964 return new capture (c->where, replace_id (c->what, id, with));
965 }
966 else if (expr *e = dyn_cast<expr *> (o))
967 {
968 expr *ne = new expr (e->operation == id ? with : e->operation,
969 e->is_commutative);
970 for (unsigned i = 0; i < e->ops.length (); ++i)
971 ne->append_op (replace_id (e->ops[i], id, with));
972 return ne;
973 }
974
975 /* For c_expr we simply record a string replacement table which is
976 applied at code-generation time. */
977 if (c_expr *ce = dyn_cast<c_expr *> (o))
978 {
979 vec<c_expr::id_tab> ids = ce->ids.copy ();
980 ids.safe_push (c_expr::id_tab (id->id, with->id));
981 return new c_expr (ce->r, ce->code, ce->nr_stmts, ids, ce->capture_ids);
982 }
983
984 return o;
985 }
986
987 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
988
989 static void
990 lower_for (simplify *sin, vec<simplify *>& simplifiers)
991 {
992 vec<vec<user_id *> >& for_vec = sin->for_vec;
993 unsigned worklist_start = 0;
994 auto_vec<simplify *> worklist;
995 worklist.safe_push (sin);
996
997 /* Lower each recorded for separately, operating on the
998 set of simplifiers created by the previous one.
999 Lower inner-to-outer so inner for substitutes can refer
1000 to operators replaced by outer fors. */
1001 for (int fi = for_vec.length () - 1; fi >= 0; --fi)
1002 {
1003 vec<user_id *>& ids = for_vec[fi];
1004 unsigned n_ids = ids.length ();
1005 unsigned max_n_opers = 0;
1006 for (unsigned i = 0; i < n_ids; ++i)
1007 if (ids[i]->substitutes.length () > max_n_opers)
1008 max_n_opers = ids[i]->substitutes.length ();
1009
1010 unsigned worklist_end = worklist.length ();
1011 for (unsigned si = worklist_start; si < worklist_end; ++si)
1012 {
1013 simplify *s = worklist[si];
1014 for (unsigned j = 0; j < max_n_opers; ++j)
1015 {
1016 operand *match_op = s->match;
1017 operand *result_op = s->result;
1018 vec<if_or_with> ifexpr_vec = s->ifexpr_vec.copy ();
1019
1020 for (unsigned i = 0; i < n_ids; ++i)
1021 {
1022 user_id *id = ids[i];
1023 id_base *oper = id->substitutes[j % id->substitutes.length ()];
1024 match_op = replace_id (match_op, id, oper);
1025 if (result_op)
1026 result_op = replace_id (result_op, id, oper);
1027 for (unsigned k = 0; k < s->ifexpr_vec.length (); ++k)
1028 ifexpr_vec[k].cexpr = replace_id (ifexpr_vec[k].cexpr,
1029 id, oper);
1030 }
1031 simplify *ns = new simplify (match_op, s->match_location,
1032 result_op, s->result_location,
1033 ifexpr_vec, vNULL, s->capture_ids);
1034 worklist.safe_push (ns);
1035 }
1036 }
1037 worklist_start = worklist_end;
1038 }
1039
1040 /* Copy out the result from the last for lowering. */
1041 for (unsigned i = worklist_start; i < worklist.length (); ++i)
1042 simplifiers.safe_push (worklist[i]);
1043 }
1044
1045 /* Lower the AST for everything in SIMPLIFIERS. */
1046
1047 static void
1048 lower (vec<simplify *>& simplifiers, bool gimple)
1049 {
1050 auto_vec<simplify *> out_simplifiers;
1051 for (unsigned i = 0; i < simplifiers.length (); ++i)
1052 lower_opt_convert (simplifiers[i], out_simplifiers);
1053
1054 simplifiers.truncate (0);
1055 for (unsigned i = 0; i < out_simplifiers.length (); ++i)
1056 lower_commutative (out_simplifiers[i], simplifiers);
1057
1058 out_simplifiers.truncate (0);
1059 if (gimple)
1060 for (unsigned i = 0; i < simplifiers.length (); ++i)
1061 lower_cond (simplifiers[i], out_simplifiers);
1062 else
1063 out_simplifiers.safe_splice (simplifiers);
1064
1065
1066 simplifiers.truncate (0);
1067 for (unsigned i = 0; i < out_simplifiers.length (); ++i)
1068 lower_for (out_simplifiers[i], simplifiers);
1069 }
1070
1071
1072
1073
1074 /* The decision tree built for generating GIMPLE and GENERIC pattern
1075 matching code. It represents the 'match' expression of all
1076 simplifies and has those as its leafs. */
1077
1078 /* Decision tree base class, used for DT_TRUE and DT_NODE. */
1079
1080 struct dt_node
1081 {
1082 enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY };
1083
1084 enum dt_type type;
1085 unsigned level;
1086 vec<dt_node *> kids;
1087
1088 dt_node (enum dt_type type_): type (type_), level (0), kids (vNULL) {}
1089
1090 dt_node *append_node (dt_node *);
1091 dt_node *append_op (operand *, dt_node *parent = 0, unsigned pos = 0);
1092 dt_node *append_true_op (dt_node *parent = 0, unsigned pos = 0);
1093 dt_node *append_match_op (dt_operand *, dt_node *parent = 0, unsigned pos = 0);
1094 dt_node *append_simplify (simplify *, unsigned, dt_operand **);
1095
1096 virtual void gen (FILE *, bool) {}
1097
1098 void gen_kids (FILE *, bool);
1099 };
1100
1101 /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */
1102
1103 struct dt_operand : public dt_node
1104 {
1105 operand *op;
1106 dt_operand *match_dop;
1107 dt_operand *parent;
1108 unsigned pos;
1109
1110 dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_,
1111 dt_operand *parent_ = 0, unsigned pos_ = 0)
1112 : dt_node (type), op (op_), match_dop (match_dop_),
1113 parent (parent_), pos (pos_) {}
1114
1115 void gen (FILE *, bool);
1116 unsigned gen_predicate (FILE *, const char *, bool);
1117 unsigned gen_match_op (FILE *, const char *);
1118
1119 unsigned gen_gimple_expr (FILE *);
1120 unsigned gen_generic_expr (FILE *, const char *);
1121
1122 char *get_name (char *);
1123 void gen_opname (char *, unsigned);
1124 };
1125
1126 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1127
1128 struct dt_simplify : public dt_node
1129 {
1130 simplify *s;
1131 unsigned pattern_no;
1132 dt_operand **indexes;
1133
1134 dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_)
1135 : dt_node (DT_SIMPLIFY), s (s_), pattern_no (pattern_no_),
1136 indexes (indexes_) {}
1137
1138 void gen (FILE *f, bool);
1139 };
1140
1141 template<>
1142 template<>
1143 inline bool
1144 is_a_helper <dt_operand *>::test (dt_node *n)
1145 {
1146 return (n->type == dt_node::DT_OPERAND
1147 || n->type == dt_node::DT_MATCH);
1148 }
1149
1150 /* A container for the actual decision tree. */
1151
1152 struct decision_tree
1153 {
1154 dt_node *root;
1155
1156 void insert (struct simplify *, unsigned);
1157 void gen_gimple (FILE *f = stderr);
1158 void gen_generic (FILE *f = stderr);
1159 void print (FILE *f = stderr);
1160
1161 decision_tree () { root = new dt_node (dt_node::DT_NODE); }
1162
1163 static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes,
1164 unsigned pos = 0, dt_node *parent = 0);
1165 static dt_node *find_node (vec<dt_node *>&, dt_node *);
1166 static bool cmp_node (dt_node *, dt_node *);
1167 static void print_node (dt_node *, FILE *f = stderr, unsigned = 0);
1168 };
1169
1170 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1171
1172 bool
1173 cmp_operand (operand *o1, operand *o2)
1174 {
1175 if (!o1 || !o2 || o1->type != o2->type)
1176 return false;
1177
1178 if (o1->type == operand::OP_PREDICATE)
1179 {
1180 predicate *p1 = as_a<predicate *>(o1);
1181 predicate *p2 = as_a<predicate *>(o2);
1182 return p1->p == p2->p;
1183 }
1184 else if (o1->type == operand::OP_EXPR)
1185 {
1186 expr *e1 = static_cast<expr *>(o1);
1187 expr *e2 = static_cast<expr *>(o2);
1188 return (e1->operation == e2->operation
1189 && e1->is_generic == e2->is_generic);
1190 }
1191 else
1192 return false;
1193 }
1194
1195 /* Compare two decision tree nodes N1 and N2 and return true if they
1196 are equal. */
1197
1198 bool
1199 decision_tree::cmp_node (dt_node *n1, dt_node *n2)
1200 {
1201 if (!n1 || !n2 || n1->type != n2->type)
1202 return false;
1203
1204 if (n1 == n2 || n1->type == dt_node::DT_TRUE)
1205 return true;
1206
1207 if (n1->type == dt_node::DT_OPERAND)
1208 return cmp_operand ((as_a<dt_operand *> (n1))->op,
1209 (as_a<dt_operand *> (n2))->op);
1210 else if (n1->type == dt_node::DT_MATCH)
1211 return ((as_a<dt_operand *> (n1))->match_dop
1212 == (as_a<dt_operand *> (n2))->match_dop);
1213 return false;
1214 }
1215
1216 /* Search OPS for a decision tree node like P and return it if found. */
1217
1218 dt_node *
1219 decision_tree::find_node (vec<dt_node *>& ops, dt_node *p)
1220 {
1221 for (unsigned i = 0; i < ops.length (); ++i)
1222 if (decision_tree::cmp_node (ops[i], p))
1223 return ops[i];
1224
1225 return NULL;
1226 }
1227
1228 /* Append N to the decision tree if it there is not already an existing
1229 identical child. */
1230
1231 dt_node *
1232 dt_node::append_node (dt_node *n)
1233 {
1234 dt_node *kid;
1235
1236 kid = decision_tree::find_node (kids, n);
1237 if (kid)
1238 return kid;
1239
1240 kids.safe_push (n);
1241 n->level = this->level + 1;
1242
1243 unsigned len = kids.length ();
1244
1245 if (len > 1 && kids[len - 2]->type == dt_node::DT_TRUE)
1246 {
1247 dt_node *p = kids[len - 2];
1248 kids[len - 2] = kids[len - 1];
1249 kids[len - 1] = p;
1250 }
1251
1252 return n;
1253 }
1254
1255 /* Append OP to the decision tree. */
1256
1257 dt_node *
1258 dt_node::append_op (operand *op, dt_node *parent, unsigned pos)
1259 {
1260 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
1261 dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos);
1262 return append_node (n);
1263 }
1264
1265 /* Append a DT_TRUE decision tree node. */
1266
1267 dt_node *
1268 dt_node::append_true_op (dt_node *parent, unsigned pos)
1269 {
1270 dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
1271 dt_operand *n = new dt_operand (DT_TRUE, 0, 0, parent_, pos);
1272 return append_node (n);
1273 }
1274
1275 /* Append a DT_MATCH decision tree node. */
1276
1277 dt_node *
1278 dt_node::append_match_op (dt_operand *match_dop, dt_node *parent, unsigned pos)
1279 {
1280 dt_operand *parent_ = as_a<dt_operand *> (parent);
1281 dt_operand *n = new dt_operand (DT_MATCH, 0, match_dop, parent_, pos);
1282 return append_node (n);
1283 }
1284
1285 /* Append S to the decision tree. */
1286
1287 dt_node *
1288 dt_node::append_simplify (simplify *s, unsigned pattern_no,
1289 dt_operand **indexes)
1290 {
1291 dt_simplify *n = new dt_simplify (s, pattern_no, indexes);
1292 return append_node (n);
1293 }
1294
1295 /* Insert O into the decision tree and return the decision tree node found
1296 or created. */
1297
1298 dt_node *
1299 decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes,
1300 unsigned pos, dt_node *parent)
1301 {
1302 dt_node *q, *elm = 0;
1303
1304 if (capture *c = dyn_cast<capture *> (o))
1305 {
1306 unsigned capt_index = c->where;
1307
1308 if (indexes[capt_index] == 0)
1309 {
1310 if (c->what)
1311 q = insert_operand (p, c->what, indexes, pos, parent);
1312 else
1313 {
1314 q = elm = p->append_true_op (parent, pos);
1315 goto at_assert_elm;
1316 }
1317 // get to the last capture
1318 for (operand *what = c->what;
1319 what && is_a<capture *> (what);
1320 c = as_a<capture *> (what), what = c->what)
1321 ;
1322
1323 if (!c->what)
1324 {
1325 unsigned cc_index = c->where;
1326 dt_operand *match_op = indexes[cc_index];
1327
1328 dt_operand temp (dt_node::DT_TRUE, 0, 0);
1329 elm = decision_tree::find_node (p->kids, &temp);
1330
1331 if (elm == 0)
1332 {
1333 dt_operand temp (dt_node::DT_MATCH, 0, match_op);
1334 elm = decision_tree::find_node (p->kids, &temp);
1335 }
1336 }
1337 else
1338 {
1339 dt_operand temp (dt_node::DT_OPERAND, c->what, 0);
1340 elm = decision_tree::find_node (p->kids, &temp);
1341 }
1342
1343 at_assert_elm:
1344 gcc_assert (elm->type == dt_node::DT_TRUE
1345 || elm->type == dt_node::DT_OPERAND
1346 || elm->type == dt_node::DT_MATCH);
1347 indexes[capt_index] = static_cast<dt_operand *> (elm);
1348 return q;
1349 }
1350 else
1351 {
1352 p = p->append_match_op (indexes[capt_index], parent, pos);
1353 if (c->what)
1354 return insert_operand (p, c->what, indexes, 0, p);
1355 else
1356 return p;
1357 }
1358 }
1359 p = p->append_op (o, parent, pos);
1360 q = p;
1361
1362 if (expr *e = dyn_cast <expr *>(o))
1363 {
1364 for (unsigned i = 0; i < e->ops.length (); ++i)
1365 q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p);
1366 }
1367
1368 return q;
1369 }
1370
1371 /* Insert S into the decision tree. */
1372
1373 void
1374 decision_tree::insert (struct simplify *s, unsigned pattern_no)
1375 {
1376 dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1);
1377 dt_node *p = decision_tree::insert_operand (root, s->match, indexes);
1378 p->append_simplify (s, pattern_no, indexes);
1379 }
1380
1381 /* Debug functions to dump the decision tree. */
1382
1383 DEBUG_FUNCTION void
1384 decision_tree::print_node (dt_node *p, FILE *f, unsigned indent)
1385 {
1386 if (p->type == dt_node::DT_NODE)
1387 fprintf (f, "root");
1388 else
1389 {
1390 fprintf (f, "|");
1391 for (unsigned i = 0; i < indent; i++)
1392 fprintf (f, "-");
1393
1394 if (p->type == dt_node::DT_OPERAND)
1395 {
1396 dt_operand *dop = static_cast<dt_operand *>(p);
1397 print_operand (dop->op, f, true);
1398 }
1399 else if (p->type == dt_node::DT_TRUE)
1400 fprintf (f, "true");
1401 else if (p->type == dt_node::DT_MATCH)
1402 fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop));
1403 else if (p->type == dt_node::DT_SIMPLIFY)
1404 {
1405 dt_simplify *s = static_cast<dt_simplify *> (p);
1406 fprintf (f, "simplify_%u { ", s->pattern_no);
1407 for (int i = 0; i <= s->s->capture_max; ++i)
1408 fprintf (f, "%p, ", (void *) s->indexes[i]);
1409 fprintf (f, " } ");
1410 }
1411 }
1412
1413 fprintf (stderr, " (%p), %u, %u\n", (void *) p, p->level, p->kids.length ());
1414
1415 for (unsigned i = 0; i < p->kids.length (); ++i)
1416 decision_tree::print_node (p->kids[i], f, indent + 2);
1417 }
1418
1419 DEBUG_FUNCTION void
1420 decision_tree::print (FILE *f)
1421 {
1422 return decision_tree::print_node (root, f);
1423 }
1424
1425
1426 /* For GENERIC we have to take care of wrapping multiple-used
1427 expressions with side-effects in save_expr and preserve side-effects
1428 of expressions with omit_one_operand. Analyze captures in
1429 match, result and with expressions and perform early-outs
1430 on the outermost match expression operands for cases we cannot
1431 handle. */
1432
1433 struct capture_info
1434 {
1435 capture_info (simplify *s);
1436 void walk_match (operand *o, unsigned toplevel_arg, bool, bool);
1437 void walk_result (operand *o, bool);
1438 void walk_c_expr (c_expr *);
1439
1440 struct cinfo
1441 {
1442 bool expr_p;
1443 bool cse_p;
1444 bool force_no_side_effects_p;
1445 bool cond_expr_cond_p;
1446 unsigned long toplevel_msk;
1447 int result_use_count;
1448 };
1449
1450 auto_vec<cinfo> info;
1451 unsigned long force_no_side_effects;
1452 };
1453
1454 /* Analyze captures in S. */
1455
1456 capture_info::capture_info (simplify *s)
1457 {
1458 expr *e;
1459 if (!s->result
1460 || ((e = dyn_cast <expr *> (s->result))
1461 && is_a <predicate_id *> (e->operation)))
1462 {
1463 force_no_side_effects = -1;
1464 return;
1465 }
1466
1467 force_no_side_effects = 0;
1468 info.safe_grow_cleared (s->capture_max + 1);
1469 e = as_a <expr *> (s->match);
1470 for (unsigned i = 0; i < e->ops.length (); ++i)
1471 walk_match (e->ops[i], i,
1472 (i != 0 && *e->operation == COND_EXPR)
1473 || *e->operation == TRUTH_ANDIF_EXPR
1474 || *e->operation == TRUTH_ORIF_EXPR,
1475 i == 0
1476 && (*e->operation == COND_EXPR
1477 || *e->operation == VEC_COND_EXPR));
1478
1479 walk_result (s->result, false);
1480
1481 for (unsigned i = 0; i < s->ifexpr_vec.length (); ++i)
1482 if (s->ifexpr_vec[i].is_with)
1483 walk_c_expr (as_a <c_expr *>(s->ifexpr_vec[i].cexpr));
1484 }
1485
1486 /* Analyze captures in the match expression piece O. */
1487
1488 void
1489 capture_info::walk_match (operand *o, unsigned toplevel_arg,
1490 bool conditional_p, bool cond_expr_cond_p)
1491 {
1492 if (capture *c = dyn_cast <capture *> (o))
1493 {
1494 info[c->where].toplevel_msk |= 1 << toplevel_arg;
1495 info[c->where].force_no_side_effects_p |= conditional_p;
1496 info[c->where].cond_expr_cond_p |= cond_expr_cond_p;
1497 /* Mark expr (non-leaf) captures and recurse. */
1498 if (c->what
1499 && is_a <expr *> (c->what))
1500 {
1501 info[c->where].expr_p = true;
1502 walk_match (c->what, toplevel_arg, conditional_p, false);
1503 }
1504 }
1505 else if (expr *e = dyn_cast <expr *> (o))
1506 {
1507 for (unsigned i = 0; i < e->ops.length (); ++i)
1508 {
1509 bool cond_p = conditional_p;
1510 bool cond_expr_cond_p = false;
1511 if (i != 0 && *e->operation == COND_EXPR)
1512 cond_p = true;
1513 else if (*e->operation == TRUTH_ANDIF_EXPR
1514 || *e->operation == TRUTH_ORIF_EXPR)
1515 cond_p = true;
1516 if (i == 0
1517 && (*e->operation == COND_EXPR
1518 || *e->operation == VEC_COND_EXPR))
1519 cond_expr_cond_p = true;
1520 walk_match (e->ops[i], toplevel_arg, cond_p, cond_expr_cond_p);
1521 }
1522 }
1523 else if (is_a <predicate *> (o))
1524 {
1525 /* Mark non-captured leafs toplevel arg for checking. */
1526 force_no_side_effects |= 1 << toplevel_arg;
1527 }
1528 else
1529 gcc_unreachable ();
1530 }
1531
1532 /* Analyze captures in the result expression piece O. */
1533
1534 void
1535 capture_info::walk_result (operand *o, bool conditional_p)
1536 {
1537 if (capture *c = dyn_cast <capture *> (o))
1538 {
1539 info[c->where].result_use_count++;
1540 /* If we substitute an expression capture we don't know
1541 which captures this will end up using (well, we don't
1542 compute that). Force the uses to be side-effect free
1543 which means forcing the toplevels that reach the
1544 expression side-effect free. */
1545 if (info[c->where].expr_p)
1546 force_no_side_effects |= info[c->where].toplevel_msk;
1547 /* Mark CSE capture capture uses as forced to have
1548 no side-effects. */
1549 if (c->what
1550 && is_a <expr *> (c->what))
1551 {
1552 info[c->where].cse_p = true;
1553 walk_result (c->what, true);
1554 }
1555 }
1556 else if (expr *e = dyn_cast <expr *> (o))
1557 {
1558 for (unsigned i = 0; i < e->ops.length (); ++i)
1559 {
1560 bool cond_p = conditional_p;
1561 if (i != 0 && *e->operation == COND_EXPR)
1562 cond_p = true;
1563 else if (*e->operation == TRUTH_ANDIF_EXPR
1564 || *e->operation == TRUTH_ORIF_EXPR)
1565 cond_p = true;
1566 walk_result (e->ops[i], cond_p);
1567 }
1568 }
1569 else if (c_expr *e = dyn_cast <c_expr *> (o))
1570 walk_c_expr (e);
1571 else
1572 gcc_unreachable ();
1573 }
1574
1575 /* Look for captures in the C expr E. */
1576
1577 void
1578 capture_info::walk_c_expr (c_expr *e)
1579 {
1580 /* Give up for C exprs mentioning captures not inside TREE_TYPE (). */
1581 unsigned p_depth = 0;
1582 for (unsigned i = 0; i < e->code.length (); ++i)
1583 {
1584 const cpp_token *t = &e->code[i];
1585 const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL;
1586 if (t->type == CPP_NAME
1587 && strcmp ((const char *)CPP_HASHNODE
1588 (t->val.node.node)->ident.str, "TREE_TYPE") == 0
1589 && n->type == CPP_OPEN_PAREN)
1590 p_depth++;
1591 else if (t->type == CPP_CLOSE_PAREN
1592 && p_depth > 0)
1593 p_depth--;
1594 else if (p_depth == 0
1595 && t->type == CPP_ATSIGN
1596 && (n->type == CPP_NUMBER
1597 || n->type == CPP_NAME)
1598 && !(n->flags & PREV_WHITE))
1599 {
1600 const char *id;
1601 if (n->type == CPP_NUMBER)
1602 id = (const char *)n->val.str.text;
1603 else
1604 id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
1605 info[*e->capture_ids->get(id)].force_no_side_effects_p = true;
1606 }
1607 }
1608 }
1609
1610
1611 /* Code generation off the decision tree and the refered AST nodes. */
1612
1613 bool
1614 is_conversion (id_base *op)
1615 {
1616 return (*op == CONVERT_EXPR
1617 || *op == NOP_EXPR
1618 || *op == FLOAT_EXPR
1619 || *op == FIX_TRUNC_EXPR
1620 || *op == VIEW_CONVERT_EXPR);
1621 }
1622
1623 /* Get the type to be used for generating operands of OP from the
1624 various sources. */
1625
1626 static const char *
1627 get_operand_type (id_base *op, const char *in_type,
1628 const char *expr_type,
1629 const char *other_oprnd_type)
1630 {
1631 /* Generally operands whose type does not match the type of the
1632 expression generated need to know their types but match and
1633 thus can fall back to 'other_oprnd_type'. */
1634 if (is_conversion (op))
1635 return other_oprnd_type;
1636 else if (*op == REALPART_EXPR
1637 || *op == IMAGPART_EXPR)
1638 return other_oprnd_type;
1639 else if (is_a <operator_id *> (op)
1640 && strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0)
1641 return other_oprnd_type;
1642 else
1643 {
1644 /* Otherwise all types should match - choose one in order of
1645 preference. */
1646 if (expr_type)
1647 return expr_type;
1648 else if (in_type)
1649 return in_type;
1650 else
1651 return other_oprnd_type;
1652 }
1653 }
1654
1655 /* Generate transform code for an expression. */
1656
1657 void
1658 expr::gen_transform (FILE *f, const char *dest, bool gimple, int depth,
1659 const char *in_type, capture_info *cinfo,
1660 dt_operand **indexes, bool)
1661 {
1662 bool conversion_p = is_conversion (operation);
1663 const char *type = expr_type;
1664 char optype[64];
1665 if (type)
1666 /* If there was a type specification in the pattern use it. */
1667 ;
1668 else if (conversion_p)
1669 /* For conversions we need to build the expression using the
1670 outer type passed in. */
1671 type = in_type;
1672 else if (*operation == REALPART_EXPR
1673 || *operation == IMAGPART_EXPR)
1674 {
1675 /* __real and __imag use the component type of its operand. */
1676 sprintf (optype, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth);
1677 type = optype;
1678 }
1679 else if (is_a <operator_id *> (operation)
1680 && !strcmp (as_a <operator_id *> (operation)->tcc, "tcc_comparison"))
1681 {
1682 /* comparisons use boolean_type_node (or what gets in), but
1683 their operands need to figure out the types themselves. */
1684 sprintf (optype, "boolean_type_node");
1685 type = optype;
1686 }
1687 else
1688 {
1689 /* Other operations are of the same type as their first operand. */
1690 sprintf (optype, "TREE_TYPE (ops%d[0])", depth);
1691 type = optype;
1692 }
1693 if (!type)
1694 fatal ("two conversions in a row");
1695
1696 fprintf (f, "{\n");
1697 fprintf (f, " tree ops%d[%u], res;\n", depth, ops.length ());
1698 char op0type[64];
1699 snprintf (op0type, 64, "TREE_TYPE (ops%d[0])", depth);
1700 for (unsigned i = 0; i < ops.length (); ++i)
1701 {
1702 char dest[32];
1703 snprintf (dest, 32, " ops%d[%u]", depth, i);
1704 const char *optype
1705 = get_operand_type (operation, in_type, expr_type,
1706 i == 0 ? NULL : op0type);
1707 ops[i]->gen_transform (f, dest, gimple, depth + 1, optype, cinfo, indexes,
1708 ((!(*operation == COND_EXPR)
1709 && !(*operation == VEC_COND_EXPR))
1710 || i != 0));
1711 }
1712
1713 const char *opr;
1714 if (*operation == CONVERT_EXPR)
1715 opr = "NOP_EXPR";
1716 else
1717 opr = operation->id;
1718
1719 if (gimple)
1720 {
1721 /* ??? Have another helper that is like gimple_build but may
1722 fail if seq == NULL. */
1723 fprintf (f, " if (!seq)\n"
1724 " {\n"
1725 " res = gimple_simplify (%s, %s", opr, type);
1726 for (unsigned i = 0; i < ops.length (); ++i)
1727 fprintf (f, ", ops%d[%u]", depth, i);
1728 fprintf (f, ", seq, valueize);\n");
1729 fprintf (f, " if (!res) return false;\n");
1730 fprintf (f, " }\n");
1731 fprintf (f, " else\n");
1732 fprintf (f, " res = gimple_build (seq, UNKNOWN_LOCATION, %s, %s",
1733 opr, type);
1734 for (unsigned i = 0; i < ops.length (); ++i)
1735 fprintf (f, ", ops%d[%u]", depth, i);
1736 fprintf (f, ", valueize);\n");
1737 }
1738 else
1739 {
1740 if (operation->kind == id_base::CODE)
1741 fprintf (f, " res = fold_build%d_loc (loc, %s, %s",
1742 ops.length(), opr, type);
1743 else
1744 fprintf (f, " res = build_call_expr_loc (loc, "
1745 "builtin_decl_implicit (%s), %d", opr, ops.length());
1746 for (unsigned i = 0; i < ops.length (); ++i)
1747 fprintf (f, ", ops%d[%u]", depth, i);
1748 fprintf (f, ");\n");
1749 }
1750 fprintf (f, " %s = res;\n", dest);
1751 fprintf (f, "}\n");
1752 }
1753
1754 /* Generate code for a c_expr which is either the expression inside
1755 an if statement or a sequence of statements which computes a
1756 result to be stored to DEST. */
1757
1758 void
1759 c_expr::gen_transform (FILE *f, const char *dest,
1760 bool, int, const char *, capture_info *,
1761 dt_operand **, bool)
1762 {
1763 if (dest && nr_stmts == 1)
1764 fprintf (f, "%s = ", dest);
1765
1766 unsigned stmt_nr = 1;
1767 for (unsigned i = 0; i < code.length (); ++i)
1768 {
1769 const cpp_token *token = &code[i];
1770
1771 /* Replace captures for code-gen. */
1772 if (token->type == CPP_ATSIGN)
1773 {
1774 const cpp_token *n = &code[i+1];
1775 if ((n->type == CPP_NUMBER
1776 || n->type == CPP_NAME)
1777 && !(n->flags & PREV_WHITE))
1778 {
1779 if (token->flags & PREV_WHITE)
1780 fputc (' ', f);
1781 const char *id;
1782 if (n->type == CPP_NUMBER)
1783 id = (const char *)n->val.str.text;
1784 else
1785 id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
1786 fprintf (f, "captures[%u]", *capture_ids->get(id));
1787 ++i;
1788 continue;
1789 }
1790 }
1791
1792 if (token->flags & PREV_WHITE)
1793 fputc (' ', f);
1794
1795 if (token->type == CPP_NAME)
1796 {
1797 const char *id = (const char *) NODE_NAME (token->val.node.node);
1798 unsigned j;
1799 for (j = 0; j < ids.length (); ++j)
1800 {
1801 if (strcmp (id, ids[j].id) == 0)
1802 {
1803 fprintf (f, "%s", ids[j].oper);
1804 break;
1805 }
1806 }
1807 if (j < ids.length ())
1808 continue;
1809 }
1810
1811 /* Output the token as string. */
1812 char *tk = (char *)cpp_token_as_text (r, token);
1813 fputs (tk, f);
1814
1815 if (token->type == CPP_SEMICOLON)
1816 {
1817 stmt_nr++;
1818 if (dest && stmt_nr == nr_stmts)
1819 fprintf (f, "\n %s = ", dest);
1820 else
1821 fputc ('\n', f);
1822 }
1823 }
1824 }
1825
1826 /* Generate transform code for a capture. */
1827
1828 void
1829 capture::gen_transform (FILE *f, const char *dest, bool gimple, int depth,
1830 const char *in_type, capture_info *cinfo,
1831 dt_operand **indexes, bool expand_compares)
1832 {
1833 if (what && is_a<expr *> (what))
1834 {
1835 if (indexes[where] == 0)
1836 {
1837 char buf[20];
1838 sprintf (buf, "captures[%u]", where);
1839 what->gen_transform (f, buf, gimple, depth, in_type, cinfo, NULL);
1840 }
1841 }
1842
1843 fprintf (f, "%s = captures[%u];\n", dest, where);
1844
1845 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
1846 with substituting a capture of that.
1847 ??? Returning false here will also not allow any other patterns
1848 to match. */
1849 if (gimple && expand_compares
1850 && cinfo->info[where].cond_expr_cond_p)
1851 fprintf (f, "if (COMPARISON_CLASS_P (%s))\n"
1852 " {\n"
1853 " if (!seq) return false;\n"
1854 " %s = gimple_build (seq, TREE_CODE (%s),"
1855 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
1856 " TREE_OPERAND (%s, 1));\n"
1857 " }\n", dest, dest, dest, dest, dest, dest);
1858 }
1859
1860 /* Return the name of the operand representing the decision tree node.
1861 Use NAME as space to generate it. */
1862
1863 char *
1864 dt_operand::get_name (char *name)
1865 {
1866 if (!parent)
1867 sprintf (name, "t");
1868 else if (parent->level == 1)
1869 sprintf (name, "op%u", pos);
1870 else if (parent->type == dt_node::DT_MATCH)
1871 return parent->get_name (name);
1872 else
1873 sprintf (name, "o%u%u", parent->level, pos);
1874 return name;
1875 }
1876
1877 /* Fill NAME with the operand name at position POS. */
1878
1879 void
1880 dt_operand::gen_opname (char *name, unsigned pos)
1881 {
1882 if (!parent)
1883 sprintf (name, "op%u", pos);
1884 else
1885 sprintf (name, "o%u%u", level, pos);
1886 }
1887
1888 /* Generate matching code for the decision tree operand which is
1889 a predicate. */
1890
1891 unsigned
1892 dt_operand::gen_predicate (FILE *f, const char *opname, bool gimple)
1893 {
1894 predicate *p = as_a <predicate *> (op);
1895
1896 if (p->p->matchers.exists ())
1897 {
1898 /* If this is a predicate generated from a pattern mangle its
1899 name and pass on the valueize hook. */
1900 if (gimple)
1901 fprintf (f, "if (gimple_%s (%s, valueize))\n", p->p->id, opname);
1902 else
1903 fprintf (f, "if (tree_%s (%s))\n", p->p->id, opname);
1904 }
1905 else
1906 fprintf (f, "if (%s (%s))\n", p->p->id, opname);
1907 fprintf (f, "{\n");
1908 return 1;
1909 }
1910
1911 /* Generate matching code for the decision tree operand which is
1912 a capture-match. */
1913
1914 unsigned
1915 dt_operand::gen_match_op (FILE *f, const char *opname)
1916 {
1917 char match_opname[20];
1918 match_dop->get_name (match_opname);
1919 fprintf (f, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
1920 opname, match_opname, opname, match_opname);
1921 fprintf (f, "{\n");
1922 return 1;
1923 }
1924
1925 /* Generate GIMPLE matching code for the decision tree operand. */
1926
1927 unsigned
1928 dt_operand::gen_gimple_expr (FILE *f)
1929 {
1930 expr *e = static_cast<expr *> (op);
1931 id_base *id = e->operation;
1932 unsigned n_ops = e->ops.length ();
1933
1934 for (unsigned i = 0; i < n_ops; ++i)
1935 {
1936 char child_opname[20];
1937 gen_opname (child_opname, i);
1938
1939 if (id->kind == id_base::CODE)
1940 {
1941 if (e->is_generic
1942 || *id == REALPART_EXPR || *id == IMAGPART_EXPR
1943 || *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR)
1944 {
1945 /* ??? If this is a memory operation we can't (and should not)
1946 match this. The only sensible operand types are
1947 SSA names and invariants. */
1948 fprintf (f, "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), %i);\n",
1949 child_opname, i);
1950 fprintf (f, "if ((TREE_CODE (%s) == SSA_NAME\n"
1951 "|| is_gimple_min_invariant (%s))\n"
1952 "&& (%s = do_valueize (valueize, %s)))\n"
1953 "{\n", child_opname, child_opname, child_opname,
1954 child_opname);
1955 continue;
1956 }
1957 else
1958 fprintf (f, "tree %s = gimple_assign_rhs%u (def_stmt);\n",
1959 child_opname, i + 1);
1960 }
1961 else
1962 fprintf (f, "tree %s = gimple_call_arg (def_stmt, %u);\n",
1963 child_opname, i);
1964 fprintf (f, "if ((%s = do_valueize (valueize, %s)))\n",
1965 child_opname, child_opname);
1966 fprintf (f, "{\n");
1967 }
1968
1969 return n_ops;
1970 }
1971
1972 /* Generate GENERIC matching code for the decision tree operand. */
1973
1974 unsigned
1975 dt_operand::gen_generic_expr (FILE *f, const char *opname)
1976 {
1977 expr *e = static_cast<expr *> (op);
1978 unsigned n_ops = e->ops.length ();
1979
1980 for (unsigned i = 0; i < n_ops; ++i)
1981 {
1982 char child_opname[20];
1983 gen_opname (child_opname, i);
1984
1985 if (e->operation->kind == id_base::CODE)
1986 fprintf (f, "tree %s = TREE_OPERAND (%s, %u);\n",
1987 child_opname, opname, i);
1988 else
1989 fprintf (f, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
1990 child_opname, opname, i);
1991 }
1992
1993 return 0;
1994 }
1995
1996 /* Generate matching code for the children of the decision tree node. */
1997
1998 void
1999 dt_node::gen_kids (FILE *f, bool gimple)
2000 {
2001 auto_vec<dt_operand *> gimple_exprs;
2002 auto_vec<dt_operand *> generic_exprs;
2003 auto_vec<dt_operand *> fns;
2004 auto_vec<dt_operand *> generic_fns;
2005 auto_vec<dt_operand *> preds;
2006 auto_vec<dt_node *> others;
2007 dt_node *true_operand = NULL;
2008
2009 for (unsigned i = 0; i < kids.length (); ++i)
2010 {
2011 if (kids[i]->type == dt_node::DT_OPERAND)
2012 {
2013 dt_operand *op = as_a<dt_operand *> (kids[i]);
2014 if (expr *e = dyn_cast <expr *> (op->op))
2015 {
2016 if (e->ops.length () == 0)
2017 generic_exprs.safe_push (op);
2018 else if (e->operation->kind == id_base::FN)
2019 {
2020 if (gimple)
2021 fns.safe_push (op);
2022 else
2023 generic_fns.safe_push (op);
2024 }
2025 else if (e->operation->kind == id_base::PREDICATE)
2026 preds.safe_push (op);
2027 else
2028 {
2029 if (gimple)
2030 gimple_exprs.safe_push (op);
2031 else
2032 generic_exprs.safe_push (op);
2033 }
2034 }
2035 else if (op->op->type == operand::OP_PREDICATE)
2036 others.safe_push (kids[i]);
2037 else
2038 gcc_unreachable ();
2039 }
2040 else if (kids[i]->type == dt_node::DT_MATCH
2041 || kids[i]->type == dt_node::DT_SIMPLIFY)
2042 others.safe_push (kids[i]);
2043 else if (kids[i]->type == dt_node::DT_TRUE)
2044 true_operand = kids[i];
2045 else
2046 gcc_unreachable ();
2047 }
2048
2049 char buf[128];
2050 char *kid_opname = buf;
2051
2052 unsigned exprs_len = gimple_exprs.length ();
2053 unsigned gexprs_len = generic_exprs.length ();
2054 unsigned fns_len = fns.length ();
2055 unsigned gfns_len = generic_fns.length ();
2056
2057 if (exprs_len || fns_len || gexprs_len || gfns_len)
2058 {
2059 if (exprs_len)
2060 gimple_exprs[0]->get_name (kid_opname);
2061 else if (fns_len)
2062 fns[0]->get_name (kid_opname);
2063 else if (gfns_len)
2064 generic_fns[0]->get_name (kid_opname);
2065 else
2066 generic_exprs[0]->get_name (kid_opname);
2067
2068 fprintf (f, "switch (TREE_CODE (%s))\n"
2069 "{\n", kid_opname);
2070 }
2071
2072 if (exprs_len || fns_len)
2073 {
2074 fprintf (f, "case SSA_NAME:\n");
2075 fprintf (f, "if (do_valueize (valueize, %s) != NULL_TREE)\n", kid_opname);
2076 fprintf (f, "{\n");
2077 fprintf (f, "gimple def_stmt = SSA_NAME_DEF_STMT (%s);\n", kid_opname);
2078
2079 if (exprs_len)
2080 {
2081 fprintf (f, "if (is_gimple_assign (def_stmt))\n");
2082 fprintf (f, "switch (gimple_assign_rhs_code (def_stmt))\n"
2083 "{\n");
2084 for (unsigned i = 0; i < exprs_len; ++i)
2085 {
2086 expr *e = as_a <expr *> (gimple_exprs[i]->op);
2087 id_base *op = e->operation;
2088 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
2089 fprintf (f, "CASE_CONVERT:\n");
2090 else
2091 fprintf (f, "case %s:\n", op->id);
2092 fprintf (f, "{\n");
2093 gimple_exprs[i]->gen (f, true);
2094 fprintf (f, "break;\n"
2095 "}\n");
2096 }
2097 fprintf (f, "default:;\n"
2098 "}\n");
2099 }
2100
2101 if (fns_len)
2102 {
2103 if (exprs_len)
2104 fprintf (f, "else ");
2105
2106 fprintf (f, "if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL))\n"
2107 "{\n"
2108 "tree fndecl = gimple_call_fndecl (def_stmt);\n"
2109 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2110 "{\n");
2111
2112 for (unsigned i = 0; i < fns_len; ++i)
2113 {
2114 expr *e = as_a <expr *>(fns[i]->op);
2115 fprintf (f, "case %s:\n"
2116 "{\n", e->operation->id);
2117 fns[i]->gen (f, true);
2118 fprintf (f, "break;\n"
2119 "}\n");
2120 }
2121
2122 fprintf (f, "default:;\n"
2123 "}\n"
2124 "}\n");
2125 }
2126
2127 fprintf (f, "}\n"
2128 "break;\n");
2129 }
2130
2131 for (unsigned i = 0; i < generic_exprs.length (); ++i)
2132 {
2133 expr *e = as_a <expr *>(generic_exprs[i]->op);
2134 id_base *op = e->operation;
2135 if (*op == CONVERT_EXPR || *op == NOP_EXPR)
2136 fprintf (f, "CASE_CONVERT:\n");
2137 else
2138 fprintf (f, "case %s:\n", op->id);
2139 fprintf (f, "{\n");
2140 generic_exprs[i]->gen (f, gimple);
2141 fprintf (f, "break;\n"
2142 "}\n");
2143 }
2144
2145 if (gfns_len)
2146 {
2147 fprintf (f, "case CALL_EXPR:\n"
2148 "{\n"
2149 "tree fndecl = get_callee_fndecl (%s);\n"
2150 "if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)\n"
2151 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2152 "{\n", kid_opname);
2153
2154 for (unsigned j = 0; j < generic_fns.length (); ++j)
2155 {
2156 expr *e = as_a <expr *>(generic_fns[j]->op);
2157 gcc_assert (e->operation->kind == id_base::FN);
2158
2159 fprintf (f, "case %s:\n"
2160 "{\n", e->operation->id);
2161 generic_fns[j]->gen (f, false);
2162 fprintf (f, "break;\n"
2163 "}\n");
2164 }
2165
2166 fprintf (f, "default:;\n"
2167 "}\n"
2168 "break;\n"
2169 "}\n");
2170 }
2171
2172 /* Close switch (TREE_CODE ()). */
2173 if (exprs_len || fns_len || gexprs_len || gfns_len)
2174 fprintf (f, "default:;\n"
2175 "}\n");
2176
2177 for (unsigned i = 0; i < preds.length (); ++i)
2178 {
2179 expr *e = as_a <expr *> (preds[i]->op);
2180 predicate_id *p = as_a <predicate_id *> (e->operation);
2181 preds[i]->get_name (kid_opname);
2182 fprintf (f, "tree %s_pops[%d];\n", kid_opname, p->nargs);
2183 fprintf (f, "if (%s_%s (%s, %s_pops%s))\n",
2184 gimple ? "gimple" : "tree",
2185 p->id, kid_opname, kid_opname,
2186 gimple ? ", valueize" : "");
2187 fprintf (f, "{\n");
2188 for (int j = 0; j < p->nargs; ++j)
2189 {
2190 char child_opname[20];
2191 preds[i]->gen_opname (child_opname, j);
2192 fprintf (f, "tree %s = %s_pops[%d];\n", child_opname, kid_opname, j);
2193 }
2194 preds[i]->gen_kids (f, gimple);
2195 fprintf (f, "}\n");
2196 }
2197
2198 for (unsigned i = 0; i < others.length (); ++i)
2199 others[i]->gen (f, gimple);
2200
2201 if (true_operand)
2202 true_operand->gen (f, gimple);
2203 }
2204
2205 /* Generate matching code for the decision tree operand. */
2206
2207 void
2208 dt_operand::gen (FILE *f, bool gimple)
2209 {
2210 char opname[20];
2211 get_name (opname);
2212
2213 unsigned n_braces = 0;
2214
2215 if (type == DT_OPERAND)
2216 switch (op->type)
2217 {
2218 case operand::OP_PREDICATE:
2219 n_braces = gen_predicate (f, opname, gimple);
2220 break;
2221
2222 case operand::OP_EXPR:
2223 if (gimple)
2224 n_braces = gen_gimple_expr (f);
2225 else
2226 n_braces = gen_generic_expr (f, opname);
2227 break;
2228
2229 default:
2230 gcc_unreachable ();
2231 }
2232 else if (type == DT_TRUE)
2233 ;
2234 else if (type == DT_MATCH)
2235 n_braces = gen_match_op (f, opname);
2236 else
2237 gcc_unreachable ();
2238
2239 gen_kids (f, gimple);
2240
2241 for (unsigned i = 0; i < n_braces; ++i)
2242 fprintf (f, "}\n");
2243 }
2244
2245
2246
2247 /* Generate code for the '(if ...)', '(with ..)' and actual transform
2248 step of a '(simplify ...)' or '(match ...)'. This handles everything
2249 that is not part of the decision tree (simplify->match). */
2250
2251 void
2252 dt_simplify::gen (FILE *f, bool gimple)
2253 {
2254 fprintf (f, "{\n");
2255 output_line_directive (f, s->result_location);
2256 if (s->capture_max >= 0)
2257 fprintf (f, "tree captures[%u] ATTRIBUTE_UNUSED = {};\n",
2258 s->capture_max + 1);
2259
2260 for (int i = 0; i <= s->capture_max; ++i)
2261 if (indexes[i])
2262 {
2263 char opname[20];
2264 fprintf (f, "captures[%u] = %s;\n", i, indexes[i]->get_name (opname));
2265 }
2266
2267 unsigned n_braces = 0;
2268 if (s->ifexpr_vec != vNULL)
2269 {
2270 for (unsigned i = 0; i < s->ifexpr_vec.length (); ++i)
2271 {
2272 if_or_with &w = s->ifexpr_vec[i];
2273 if (w.is_with)
2274 {
2275 fprintf (f, "{\n");
2276 output_line_directive (f, w.location);
2277 w.cexpr->gen_transform (f, NULL, true, 1, "type", NULL);
2278 n_braces++;
2279 }
2280 else
2281 {
2282 output_line_directive (f, w.location);
2283 fprintf (f, "if (");
2284 if (i == s->ifexpr_vec.length () - 1
2285 || s->ifexpr_vec[i+1].is_with)
2286 w.cexpr->gen_transform (f, NULL, true, 1, "type", NULL);
2287 else
2288 {
2289 unsigned j = i;
2290 do
2291 {
2292 if (j != i)
2293 {
2294 fprintf (f, "\n");
2295 output_line_directive (f, s->ifexpr_vec[j].location);
2296 fprintf (f, "&& ");
2297 }
2298 fprintf (f, "(");
2299 s->ifexpr_vec[j].cexpr->gen_transform (f, NULL,
2300 true, 1, "type",
2301 NULL);
2302 fprintf (f, ")");
2303 ++j;
2304 }
2305 while (j < s->ifexpr_vec.length ()
2306 && !s->ifexpr_vec[j].is_with);
2307 i = j - 1;
2308 }
2309 fprintf (f, ")\n");
2310 }
2311 }
2312 fprintf (f, "{\n");
2313 n_braces++;
2314 }
2315
2316 /* Analyze captures and perform early-outs on the incoming arguments
2317 that cover cases we cannot handle. */
2318 capture_info cinfo (s);
2319 expr *e;
2320 if (!gimple
2321 && s->result
2322 && !((e = dyn_cast <expr *> (s->result))
2323 && is_a <predicate_id *> (e->operation)))
2324 {
2325 for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
2326 if (cinfo.force_no_side_effects & (1 << i))
2327 fprintf (f, "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n", i);
2328 for (int i = 0; i <= s->capture_max; ++i)
2329 if (cinfo.info[i].cse_p)
2330 ;
2331 else if (cinfo.info[i].force_no_side_effects_p
2332 && (cinfo.info[i].toplevel_msk
2333 & cinfo.force_no_side_effects) == 0)
2334 fprintf (f, "if (TREE_SIDE_EFFECTS (captures[%d])) "
2335 "return NULL_TREE;\n", i);
2336 else if ((cinfo.info[i].toplevel_msk
2337 & cinfo.force_no_side_effects) != 0)
2338 /* Mark capture as having no side-effects if we had to verify
2339 that via forced toplevel operand checks. */
2340 cinfo.info[i].force_no_side_effects_p = true;
2341 }
2342
2343 fprintf (f, "if (dump_file && (dump_flags & TDF_DETAILS)) "
2344 "fprintf (dump_file, \"Applying pattern ");
2345 output_line_directive (f, s->result_location, true);
2346 fprintf (f, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
2347
2348 operand *result = s->result;
2349 if (!result)
2350 {
2351 /* If there is no result then this is a predicate implementation. */
2352 fprintf (f, "return true;\n");
2353 }
2354 else if (gimple)
2355 {
2356 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
2357 in outermost position). */
2358 if (result->type == operand::OP_EXPR
2359 && *as_a <expr *> (result)->operation == NON_LVALUE_EXPR)
2360 result = as_a <expr *> (result)->ops[0];
2361 if (result->type == operand::OP_EXPR)
2362 {
2363 expr *e = as_a <expr *> (result);
2364 bool is_predicate = is_a <predicate_id *> (e->operation);
2365 if (!is_predicate)
2366 fprintf (f, "*res_code = %s;\n",
2367 *e->operation == CONVERT_EXPR
2368 ? "NOP_EXPR" : e->operation->id);
2369 for (unsigned j = 0; j < e->ops.length (); ++j)
2370 {
2371 char dest[32];
2372 snprintf (dest, 32, " res_ops[%d]", j);
2373 const char *optype
2374 = get_operand_type (e->operation,
2375 "type", e->expr_type,
2376 j == 0
2377 ? NULL : "TREE_TYPE (res_ops[0])");
2378 /* We need to expand GENERIC conditions we captured from
2379 COND_EXPRs. */
2380 bool expand_generic_cond_exprs_p
2381 = (!is_predicate
2382 /* But avoid doing that if the GENERIC condition is
2383 valid - which it is in the first operand of COND_EXPRs
2384 and VEC_COND_EXRPs. */
2385 && ((!(*e->operation == COND_EXPR)
2386 && !(*e->operation == VEC_COND_EXPR))
2387 || j != 0));
2388 e->ops[j]->gen_transform (f, dest, true, 1, optype, &cinfo,
2389 indexes, expand_generic_cond_exprs_p);
2390 }
2391
2392 /* Re-fold the toplevel result. It's basically an embedded
2393 gimple_build w/o actually building the stmt. */
2394 if (!is_predicate)
2395 fprintf (f, "gimple_resimplify%d (seq, res_code, type, "
2396 "res_ops, valueize);\n", e->ops.length ());
2397 }
2398 else if (result->type == operand::OP_CAPTURE
2399 || result->type == operand::OP_C_EXPR)
2400 {
2401 result->gen_transform (f, "res_ops[0]", true, 1, "type",
2402 &cinfo, indexes, false);
2403 fprintf (f, "*res_code = TREE_CODE (res_ops[0]);\n");
2404 if (is_a <capture *> (result)
2405 && cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p)
2406 {
2407 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2408 with substituting a capture of that. */
2409 fprintf (f, "if (COMPARISON_CLASS_P (res_ops[0]))\n"
2410 " {\n"
2411 " tree tem = res_ops[0];\n"
2412 " res_ops[0] = TREE_OPERAND (tem, 0);\n"
2413 " res_ops[1] = TREE_OPERAND (tem, 1);\n"
2414 " }\n");
2415 }
2416 }
2417 else
2418 gcc_unreachable ();
2419 fprintf (f, "return true;\n");
2420 }
2421 else /* GENERIC */
2422 {
2423 bool is_predicate = false;
2424 if (result->type == operand::OP_EXPR)
2425 {
2426 expr *e = as_a <expr *> (result);
2427 is_predicate = is_a <predicate_id *> (e->operation);
2428 /* Search for captures used multiple times in the result expression
2429 and dependent on TREE_SIDE_EFFECTS emit a SAVE_EXPR. */
2430 if (!is_predicate)
2431 for (int i = 0; i < s->capture_max + 1; ++i)
2432 {
2433 if (!cinfo.info[i].force_no_side_effects_p
2434 && cinfo.info[i].result_use_count > 1)
2435 fprintf (f, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2436 " captures[%d] = save_expr (captures[%d]);\n",
2437 i, i, i);
2438 }
2439 for (unsigned j = 0; j < e->ops.length (); ++j)
2440 {
2441 char dest[32];
2442 if (is_predicate)
2443 snprintf (dest, 32, "res_ops[%d]", j);
2444 else
2445 {
2446 fprintf (f, " tree res_op%d;\n", j);
2447 snprintf (dest, 32, " res_op%d", j);
2448 }
2449 const char *optype
2450 = get_operand_type (e->operation,
2451 "type", e->expr_type,
2452 j == 0
2453 ? NULL : "TREE_TYPE (res_op0)");
2454 e->ops[j]->gen_transform (f, dest, false, 1, optype,
2455 &cinfo, indexes);
2456 }
2457 if (is_predicate)
2458 fprintf (f, "return true;\n");
2459 else
2460 {
2461 fprintf (f, " tree res;\n");
2462 /* Re-fold the toplevel result. Use non_lvalue to
2463 build NON_LVALUE_EXPRs so they get properly
2464 ignored when in GIMPLE form. */
2465 if (*e->operation == NON_LVALUE_EXPR)
2466 fprintf (f, " res = non_lvalue_loc (loc, res_op0);\n");
2467 else
2468 {
2469 if (e->operation->kind == id_base::CODE)
2470 fprintf (f, " res = fold_build%d_loc (loc, %s, type",
2471 e->ops.length (),
2472 *e->operation == CONVERT_EXPR
2473 ? "NOP_EXPR" : e->operation->id);
2474 else
2475 fprintf (f, " res = build_call_expr_loc "
2476 "(loc, builtin_decl_implicit (%s), %d",
2477 e->operation->id, e->ops.length());
2478 for (unsigned j = 0; j < e->ops.length (); ++j)
2479 fprintf (f, ", res_op%d", j);
2480 fprintf (f, ");\n");
2481 }
2482 }
2483 }
2484 else if (result->type == operand::OP_CAPTURE
2485 || result->type == operand::OP_C_EXPR)
2486
2487 {
2488 fprintf (f, " tree res;\n");
2489 s->result->gen_transform (f, " res", false, 1, "type",
2490 &cinfo, indexes);
2491 }
2492 else
2493 gcc_unreachable ();
2494 if (!is_predicate)
2495 {
2496 /* Search for captures not used in the result expression and dependent
2497 on TREE_SIDE_EFFECTS emit omit_one_operand. */
2498 for (int i = 0; i < s->capture_max + 1; ++i)
2499 {
2500 if (!cinfo.info[i].force_no_side_effects_p
2501 && !cinfo.info[i].expr_p
2502 && cinfo.info[i].result_use_count == 0)
2503 fprintf (f, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2504 " res = build2_loc (loc, COMPOUND_EXPR, type,"
2505 " fold_ignored_result (captures[%d]), res);\n",
2506 i, i);
2507 }
2508 fprintf (f, " return res;\n");
2509 }
2510 }
2511
2512 for (unsigned i = 0; i < n_braces; ++i)
2513 fprintf (f, "}\n");
2514
2515 fprintf (f, "}\n");
2516 }
2517
2518 /* Main entry to generate code for matching GIMPLE IL off the decision
2519 tree. */
2520
2521 void
2522 decision_tree::gen_gimple (FILE *f)
2523 {
2524 for (unsigned n = 1; n <= 3; ++n)
2525 {
2526 fprintf (f, "\nstatic bool\n"
2527 "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
2528 " gimple_seq *seq, tree (*valueize)(tree),\n"
2529 " code_helper code, tree type");
2530 for (unsigned i = 0; i < n; ++i)
2531 fprintf (f, ", tree op%d", i);
2532 fprintf (f, ")\n");
2533 fprintf (f, "{\n");
2534
2535 fprintf (f, "switch (code.get_rep())\n"
2536 "{\n");
2537 for (unsigned i = 0; i < root->kids.length (); i++)
2538 {
2539 dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
2540 expr *e = static_cast<expr *>(dop->op);
2541 if (e->ops.length () != n)
2542 continue;
2543
2544 if (*e->operation == CONVERT_EXPR
2545 || *e->operation == NOP_EXPR)
2546 fprintf (f, "CASE_CONVERT:\n");
2547 else
2548 fprintf (f, "case %s%s:\n",
2549 is_a <fn_id *> (e->operation) ? "-" : "",
2550 e->operation->id);
2551 fprintf (f, "{\n");
2552 dop->gen_kids (f, true);
2553 fprintf (f, "break;\n");
2554 fprintf (f, "}\n");
2555 }
2556 fprintf (f, "default:;\n"
2557 "}\n");
2558
2559 fprintf (f, "return false;\n");
2560 fprintf (f, "}\n");
2561 }
2562 }
2563
2564 /* Main entry to generate code for matching GENERIC IL off the decision
2565 tree. */
2566
2567 void
2568 decision_tree::gen_generic (FILE *f)
2569 {
2570 for (unsigned n = 1; n <= 3; ++n)
2571 {
2572 fprintf (f, "\ntree\n"
2573 "generic_simplify (location_t loc, enum tree_code code, "
2574 "tree type ATTRIBUTE_UNUSED");
2575 for (unsigned i = 0; i < n; ++i)
2576 fprintf (f, ", tree op%d", i);
2577 fprintf (f, ")\n");
2578 fprintf (f, "{\n");
2579
2580 fprintf (f, "switch (code)\n"
2581 "{\n");
2582 for (unsigned i = 0; i < root->kids.length (); i++)
2583 {
2584 dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
2585 expr *e = static_cast<expr *>(dop->op);
2586 if (e->ops.length () != n
2587 /* Builtin simplifications are somewhat premature on
2588 GENERIC. The following drops patterns with outermost
2589 calls. It's easy to emit overloads for function code
2590 though if necessary. */
2591 || e->operation->kind != id_base::CODE)
2592 continue;
2593
2594 operator_id *op_id = static_cast <operator_id *> (e->operation);
2595 if (op_id->code == NOP_EXPR || op_id->code == CONVERT_EXPR)
2596 fprintf (f, "CASE_CONVERT:\n");
2597 else
2598 fprintf (f, "case %s:\n", e->operation->id);
2599 fprintf (f, "{\n");
2600 dop->gen_kids (f, false);
2601 fprintf (f, "break;\n"
2602 "}\n");
2603 }
2604 fprintf (f, "default:;\n"
2605 "}\n");
2606
2607 fprintf (f, "return NULL_TREE;\n");
2608 fprintf (f, "}\n");
2609 }
2610 }
2611
2612 /* Output code to implement the predicate P from the decision tree DT. */
2613
2614 void
2615 write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
2616 {
2617 fprintf (f, "\nbool\n"
2618 "%s%s (tree t%s%s)\n"
2619 "{\n", gimple ? "gimple_" : "tree_", p->id,
2620 p->nargs > 0 ? ", tree *res_ops" : "",
2621 gimple ? ", tree (*valueize)(tree)" : "");
2622 /* Conveniently make 'type' available. */
2623 fprintf (f, "tree type = TREE_TYPE (t);\n");
2624
2625 if (!gimple)
2626 fprintf (f, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
2627 dt.root->gen_kids (f, gimple);
2628
2629 fprintf (f, "return false;\n"
2630 "}\n");
2631 }
2632
2633 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
2634
2635 static void
2636 write_header (FILE *f, const char *head)
2637 {
2638 fprintf (f, "/* Generated automatically by the program `genmatch' from\n");
2639 fprintf (f, " a IL pattern matching and simplification description. */\n");
2640
2641 /* Include the header instead of writing it awkwardly quoted here. */
2642 fprintf (f, "\n#include \"%s\"\n", head);
2643 }
2644
2645
2646
2647 /* AST parsing. */
2648
2649 class parser
2650 {
2651 public:
2652 parser (cpp_reader *);
2653
2654 private:
2655 const cpp_token *next ();
2656 const cpp_token *peek ();
2657 const cpp_token *peek_ident (const char * = NULL);
2658 const cpp_token *expect (enum cpp_ttype);
2659 void eat_token (enum cpp_ttype);
2660 const char *get_string ();
2661 const char *get_ident ();
2662 void eat_ident (const char *);
2663 const char *get_number ();
2664
2665 id_base *parse_operation ();
2666 operand *parse_capture (operand *);
2667 operand *parse_expr ();
2668 c_expr *parse_c_expr (cpp_ttype);
2669 operand *parse_op ();
2670
2671 void parse_pattern ();
2672 void parse_simplify (source_location, vec<simplify *>&, predicate_id *,
2673 expr *);
2674 void parse_for (source_location);
2675 void parse_if (source_location);
2676 void parse_predicates (source_location);
2677 void parse_operator_list (source_location);
2678
2679 cpp_reader *r;
2680 vec<if_or_with> active_ifs;
2681 vec<vec<user_id *> > active_fors;
2682
2683 cid_map_t *capture_ids;
2684
2685 public:
2686 vec<simplify *> simplifiers;
2687 vec<predicate_id *> user_predicates;
2688 bool parsing_match_operand;
2689 };
2690
2691 /* Lexing helpers. */
2692
2693 /* Read the next non-whitespace token from R. */
2694
2695 const cpp_token *
2696 parser::next ()
2697 {
2698 const cpp_token *token;
2699 do
2700 {
2701 token = cpp_get_token (r);
2702 }
2703 while (token->type == CPP_PADDING
2704 && token->type != CPP_EOF);
2705 return token;
2706 }
2707
2708 /* Peek at the next non-whitespace token from R. */
2709
2710 const cpp_token *
2711 parser::peek ()
2712 {
2713 const cpp_token *token;
2714 unsigned i = 0;
2715 do
2716 {
2717 token = cpp_peek_token (r, i++);
2718 }
2719 while (token->type == CPP_PADDING
2720 && token->type != CPP_EOF);
2721 /* If we peek at EOF this is a fatal error as it leaves the
2722 cpp_reader in unusable state. Assume we really wanted a
2723 token and thus this EOF is unexpected. */
2724 if (token->type == CPP_EOF)
2725 fatal_at (token, "unexpected end of file");
2726 return token;
2727 }
2728
2729 /* Peek at the next identifier token (or return NULL if the next
2730 token is not an identifier or equal to ID if supplied). */
2731
2732 const cpp_token *
2733 parser::peek_ident (const char *id)
2734 {
2735 const cpp_token *token = peek ();
2736 if (token->type != CPP_NAME)
2737 return 0;
2738
2739 if (id == 0)
2740 return token;
2741
2742 const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
2743 if (strcmp (id, t) == 0)
2744 return token;
2745
2746 return 0;
2747 }
2748
2749 /* Read the next token from R and assert it is of type TK. */
2750
2751 const cpp_token *
2752 parser::expect (enum cpp_ttype tk)
2753 {
2754 const cpp_token *token = next ();
2755 if (token->type != tk)
2756 fatal_at (token, "expected %s, got %s",
2757 cpp_type2name (tk, 0), cpp_type2name (token->type, 0));
2758
2759 return token;
2760 }
2761
2762 /* Consume the next token from R and assert it is of type TK. */
2763
2764 void
2765 parser::eat_token (enum cpp_ttype tk)
2766 {
2767 expect (tk);
2768 }
2769
2770 /* Read the next token from R and assert it is of type CPP_STRING and
2771 return its value. */
2772
2773 const char *
2774 parser::get_string ()
2775 {
2776 const cpp_token *token = expect (CPP_STRING);
2777 return (const char *)token->val.str.text;
2778 }
2779
2780 /* Read the next token from R and assert it is of type CPP_NAME and
2781 return its value. */
2782
2783 const char *
2784 parser::get_ident ()
2785 {
2786 const cpp_token *token = expect (CPP_NAME);
2787 return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
2788 }
2789
2790 /* Eat an identifier token with value S from R. */
2791
2792 void
2793 parser::eat_ident (const char *s)
2794 {
2795 const cpp_token *token = peek ();
2796 const char *t = get_ident ();
2797 if (strcmp (s, t) != 0)
2798 fatal_at (token, "expected '%s' got '%s'\n", s, t);
2799 }
2800
2801 /* Read the next token from R and assert it is of type CPP_NUMBER and
2802 return its value. */
2803
2804 const char *
2805 parser::get_number ()
2806 {
2807 const cpp_token *token = expect (CPP_NUMBER);
2808 return (const char *)token->val.str.text;
2809 }
2810
2811
2812 /* Parse the operator ID, special-casing convert?, convert1? and
2813 convert2? */
2814
2815 id_base *
2816 parser::parse_operation ()
2817 {
2818 const cpp_token *id_tok = peek ();
2819 const char *id = get_ident ();
2820 const cpp_token *token = peek ();
2821 if (strcmp (id, "convert0") == 0)
2822 fatal_at (id_tok, "use 'convert?' here");
2823 if (token->type == CPP_QUERY
2824 && !(token->flags & PREV_WHITE))
2825 {
2826 if (strcmp (id, "convert") == 0)
2827 id = "convert0";
2828 else if (strcmp (id, "convert1") == 0)
2829 ;
2830 else if (strcmp (id, "convert2") == 0)
2831 ;
2832 else
2833 fatal_at (id_tok, "non-convert operator conditionalized");
2834
2835 if (!parsing_match_operand)
2836 fatal_at (id_tok, "conditional convert can only be used in "
2837 "match expression");
2838 eat_token (CPP_QUERY);
2839 }
2840 else if (strcmp (id, "convert1") == 0
2841 || strcmp (id, "convert2") == 0)
2842 fatal_at (id_tok, "expected '?' after conditional operator");
2843 id_base *op = get_operator (id);
2844 if (!op)
2845 fatal_at (id_tok, "unknown operator %s", id);
2846
2847 user_id *p = dyn_cast<user_id *> (op);
2848 if (p && p->is_oper_list)
2849 fatal_at (id_tok, "operator-list not allowed in expression");
2850
2851 return op;
2852 }
2853
2854 /* Parse a capture.
2855 capture = '@'<number> */
2856
2857 struct operand *
2858 parser::parse_capture (operand *op)
2859 {
2860 eat_token (CPP_ATSIGN);
2861 const cpp_token *token = peek ();
2862 const char *id = NULL;
2863 if (token->type == CPP_NUMBER)
2864 id = get_number ();
2865 else if (token->type == CPP_NAME)
2866 id = get_ident ();
2867 else
2868 fatal_at (token, "expected number or identifier");
2869 unsigned next_id = capture_ids->elements ();
2870 bool existed;
2871 unsigned &num = capture_ids->get_or_insert (id, &existed);
2872 if (!existed)
2873 num = next_id;
2874 return new capture (num, op);
2875 }
2876
2877 /* Parse an expression
2878 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
2879
2880 struct operand *
2881 parser::parse_expr ()
2882 {
2883 expr *e = new expr (parse_operation ());
2884 const cpp_token *token = peek ();
2885 operand *op;
2886 bool is_commutative = false;
2887 const char *expr_type = NULL;
2888
2889 if (token->type == CPP_COLON
2890 && !(token->flags & PREV_WHITE))
2891 {
2892 eat_token (CPP_COLON);
2893 token = peek ();
2894 if (token->type == CPP_NAME
2895 && !(token->flags & PREV_WHITE))
2896 {
2897 const char *s = get_ident ();
2898 if (s[0] == 'c' && !s[1])
2899 {
2900 if (!parsing_match_operand)
2901 fatal_at (token,
2902 "flag 'c' can only be used in match expression");
2903 is_commutative = true;
2904 }
2905 else if (s[1] != '\0')
2906 {
2907 if (parsing_match_operand)
2908 fatal_at (token, "type can only be used in result expression");
2909 expr_type = s;
2910 }
2911 else
2912 fatal_at (token, "flag %s not recognized", s);
2913
2914 token = peek ();
2915 }
2916 else
2917 fatal_at (token, "expected flag or type specifying identifier");
2918 }
2919
2920 if (token->type == CPP_ATSIGN
2921 && !(token->flags & PREV_WHITE))
2922 op = parse_capture (e);
2923 else
2924 op = e;
2925 do
2926 {
2927 const cpp_token *token = peek ();
2928 if (token->type == CPP_CLOSE_PAREN)
2929 {
2930 if (e->operation->nargs != -1
2931 && e->operation->nargs != (int) e->ops.length ())
2932 fatal_at (token, "'%s' expects %u operands, not %u",
2933 e->operation->id, e->operation->nargs, e->ops.length ());
2934 if (is_commutative)
2935 {
2936 if (e->ops.length () == 2)
2937 e->is_commutative = true;
2938 else
2939 fatal_at (token, "only binary operators or function with "
2940 "two arguments can be marked commutative");
2941 }
2942 e->expr_type = expr_type;
2943 return op;
2944 }
2945 e->append_op (parse_op ());
2946 }
2947 while (1);
2948 }
2949
2950 /* Lex native C code delimited by START recording the preprocessing tokens
2951 for later processing.
2952 c_expr = ('{'|'(') <pp token>... ('}'|')') */
2953
2954 c_expr *
2955 parser::parse_c_expr (cpp_ttype start)
2956 {
2957 const cpp_token *token;
2958 cpp_ttype end;
2959 unsigned opencnt;
2960 vec<cpp_token> code = vNULL;
2961 unsigned nr_stmts = 0;
2962 eat_token (start);
2963 if (start == CPP_OPEN_PAREN)
2964 end = CPP_CLOSE_PAREN;
2965 else if (start == CPP_OPEN_BRACE)
2966 end = CPP_CLOSE_BRACE;
2967 else
2968 gcc_unreachable ();
2969 opencnt = 1;
2970 do
2971 {
2972 token = next ();
2973
2974 /* Count brace pairs to find the end of the expr to match. */
2975 if (token->type == start)
2976 opencnt++;
2977 else if (token->type == end
2978 && --opencnt == 0)
2979 break;
2980
2981 /* This is a lame way of counting the number of statements. */
2982 if (token->type == CPP_SEMICOLON)
2983 nr_stmts++;
2984
2985 /* If this is possibly a user-defined identifier mark it used. */
2986 if (token->type == CPP_NAME)
2987 get_operator ((const char *)CPP_HASHNODE
2988 (token->val.node.node)->ident.str);
2989
2990 /* Record the token. */
2991 code.safe_push (*token);
2992 }
2993 while (1);
2994 return new c_expr (r, code, nr_stmts, vNULL, capture_ids);
2995 }
2996
2997 /* Parse an operand which is either an expression, a predicate or
2998 a standalone capture.
2999 op = predicate | expr | c_expr | capture */
3000
3001 struct operand *
3002 parser::parse_op ()
3003 {
3004 const cpp_token *token = peek ();
3005 struct operand *op = NULL;
3006 if (token->type == CPP_OPEN_PAREN)
3007 {
3008 eat_token (CPP_OPEN_PAREN);
3009 op = parse_expr ();
3010 eat_token (CPP_CLOSE_PAREN);
3011 }
3012 else if (token->type == CPP_OPEN_BRACE)
3013 {
3014 op = parse_c_expr (CPP_OPEN_BRACE);
3015 }
3016 else
3017 {
3018 /* Remaining ops are either empty or predicates */
3019 if (token->type == CPP_NAME)
3020 {
3021 const char *id = get_ident ();
3022 id_base *opr = get_operator (id);
3023 if (!opr)
3024 fatal_at (token, "expected predicate name");
3025 if (operator_id *code = dyn_cast <operator_id *> (opr))
3026 {
3027 if (code->nargs != 0)
3028 fatal_at (token, "using an operator with operands as predicate");
3029 /* Parse the zero-operand operator "predicates" as
3030 expression. */
3031 op = new expr (opr);
3032 }
3033 else if (predicate_id *p = dyn_cast <predicate_id *> (opr))
3034 op = new predicate (p);
3035 else
3036 fatal_at (token, "using an unsupported operator as predicate");
3037 if (!parsing_match_operand)
3038 fatal_at (token, "predicates are only allowed in match expression");
3039 token = peek ();
3040 if (token->flags & PREV_WHITE)
3041 return op;
3042 }
3043 else if (token->type != CPP_COLON
3044 && token->type != CPP_ATSIGN)
3045 fatal_at (token, "expected expression or predicate");
3046 /* optionally followed by a capture and a predicate. */
3047 if (token->type == CPP_COLON)
3048 fatal_at (token, "not implemented: predicate on leaf operand");
3049 if (token->type == CPP_ATSIGN)
3050 op = parse_capture (op);
3051 }
3052
3053 return op;
3054 }
3055
3056 /* Parse
3057 simplify = 'simplify' <expr> <result-op>
3058 or
3059 match = 'match' <ident> <expr> [<result-op>]
3060 with
3061 <result-op> = <op> | <if> | <with>
3062 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
3063 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
3064 and fill SIMPLIFIERS with the results. */
3065
3066 void
3067 parser::parse_simplify (source_location match_location,
3068 vec<simplify *>& simplifiers, predicate_id *matcher,
3069 expr *result)
3070 {
3071 /* Reset the capture map. */
3072 capture_ids = new cid_map_t;
3073
3074 const cpp_token *loc = peek ();
3075 parsing_match_operand = true;
3076 struct operand *match = parse_op ();
3077 parsing_match_operand = false;
3078 if (match->type == operand::OP_CAPTURE && !matcher)
3079 fatal_at (loc, "outermost expression cannot be captured");
3080 if (match->type == operand::OP_EXPR
3081 && is_a <predicate_id *> (as_a <expr *> (match)->operation))
3082 fatal_at (loc, "outermost expression cannot be a predicate");
3083
3084 const cpp_token *token = peek ();
3085
3086 /* If this if is immediately closed then it is part of a predicate
3087 definition. Push it. */
3088 if (token->type == CPP_CLOSE_PAREN)
3089 {
3090 if (!matcher)
3091 fatal_at (token, "expected transform expression");
3092 simplifiers.safe_push
3093 (new simplify (match, match_location, result, token->src_loc,
3094 active_ifs.copy (), active_fors.copy (),
3095 capture_ids));
3096 return;
3097 }
3098
3099 unsigned active_ifs_len = active_ifs.length ();
3100 while (1)
3101 {
3102 if (token->type == CPP_OPEN_PAREN)
3103 {
3104 source_location paren_loc = token->src_loc;
3105 eat_token (CPP_OPEN_PAREN);
3106 if (peek_ident ("if"))
3107 {
3108 eat_ident ("if");
3109 active_ifs.safe_push (if_or_with (parse_c_expr (CPP_OPEN_PAREN),
3110 token->src_loc, false));
3111 /* If this if is immediately closed then it contains a
3112 manual matcher or is part of a predicate definition.
3113 Push it. */
3114 if (peek ()->type == CPP_CLOSE_PAREN)
3115 {
3116 if (!matcher)
3117 fatal_at (token, "manual transform not implemented");
3118 simplifiers.safe_push
3119 (new simplify (match, match_location, result,
3120 paren_loc, active_ifs.copy (),
3121 active_fors.copy (), capture_ids));
3122 }
3123 }
3124 else if (peek_ident ("with"))
3125 {
3126 eat_ident ("with");
3127 /* Parse (with c-expr expr) as (if-with (true) expr). */
3128 c_expr *e = parse_c_expr (CPP_OPEN_BRACE);
3129 e->nr_stmts = 0;
3130 active_ifs.safe_push (if_or_with (e, token->src_loc, true));
3131 }
3132 else
3133 {
3134 operand *op = result;
3135 if (!matcher)
3136 op = parse_expr ();
3137 simplifiers.safe_push
3138 (new simplify (match, match_location, op,
3139 token->src_loc, active_ifs.copy (),
3140 active_fors.copy (), capture_ids));
3141 eat_token (CPP_CLOSE_PAREN);
3142 /* A "default" result closes the enclosing scope. */
3143 if (active_ifs.length () > active_ifs_len)
3144 {
3145 eat_token (CPP_CLOSE_PAREN);
3146 active_ifs.pop ();
3147 }
3148 else
3149 return;
3150 }
3151 }
3152 else if (token->type == CPP_CLOSE_PAREN)
3153 {
3154 /* Close a scope if requested. */
3155 if (active_ifs.length () > active_ifs_len)
3156 {
3157 eat_token (CPP_CLOSE_PAREN);
3158 active_ifs.pop ();
3159 token = peek ();
3160 }
3161 else
3162 return;
3163 }
3164 else
3165 {
3166 if (matcher)
3167 fatal_at (token, "expected match operand expression");
3168 simplifiers.safe_push
3169 (new simplify (match, match_location,
3170 matcher ? result : parse_op (),
3171 token->src_loc, active_ifs.copy (),
3172 active_fors.copy (), capture_ids));
3173 /* A "default" result closes the enclosing scope. */
3174 if (active_ifs.length () > active_ifs_len)
3175 {
3176 eat_token (CPP_CLOSE_PAREN);
3177 active_ifs.pop ();
3178 }
3179 else
3180 return;
3181 }
3182 token = peek ();
3183 }
3184 }
3185
3186 /* Parsing of the outer control structures. */
3187
3188 /* Parse a for expression
3189 for = '(' 'for' <subst>... <pattern> ')'
3190 subst = <ident> '(' <ident>... ')' */
3191
3192 void
3193 parser::parse_for (source_location)
3194 {
3195 auto_vec<const cpp_token *> user_id_tokens;
3196 vec<user_id *> user_ids = vNULL;
3197 const cpp_token *token;
3198 unsigned min_n_opers = 0, max_n_opers = 0;
3199
3200 while (1)
3201 {
3202 token = peek ();
3203 if (token->type != CPP_NAME)
3204 break;
3205
3206 /* Insert the user defined operators into the operator hash. */
3207 const char *id = get_ident ();
3208 if (get_operator (id) != NULL)
3209 fatal_at (token, "operator already defined");
3210 user_id *op = new user_id (id);
3211 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
3212 *slot = op;
3213 user_ids.safe_push (op);
3214 user_id_tokens.safe_push (token);
3215
3216 eat_token (CPP_OPEN_PAREN);
3217
3218 int arity = -1;
3219 while ((token = peek_ident ()) != 0)
3220 {
3221 const char *oper = get_ident ();
3222 id_base *idb = get_operator (oper);
3223 if (idb == NULL)
3224 fatal_at (token, "no such operator '%s'", oper);
3225 if (*idb == CONVERT0 || *idb == CONVERT1 || *idb == CONVERT2)
3226 fatal_at (token, "conditional operators cannot be used inside for");
3227
3228 if (arity == -1)
3229 arity = idb->nargs;
3230 else if (idb->nargs == -1)
3231 ;
3232 else if (idb->nargs != arity)
3233 fatal_at (token, "operator '%s' with arity %d does not match "
3234 "others with arity %d", oper, idb->nargs, arity);
3235
3236 user_id *p = dyn_cast<user_id *> (idb);
3237 if (p && p->is_oper_list)
3238 op->substitutes.safe_splice (p->substitutes);
3239 else
3240 op->substitutes.safe_push (idb);
3241 }
3242 op->nargs = arity;
3243 token = expect (CPP_CLOSE_PAREN);
3244
3245 unsigned nsubstitutes = op->substitutes.length ();
3246 if (nsubstitutes == 0)
3247 fatal_at (token, "A user-defined operator must have at least "
3248 "one substitution");
3249 if (max_n_opers == 0)
3250 {
3251 min_n_opers = nsubstitutes;
3252 max_n_opers = nsubstitutes;
3253 }
3254 else
3255 {
3256 if (nsubstitutes % min_n_opers != 0
3257 && min_n_opers % nsubstitutes != 0)
3258 fatal_at (token, "All user-defined identifiers must have a "
3259 "multiple number of operator substitutions of the "
3260 "smallest number of substitutions");
3261 if (nsubstitutes < min_n_opers)
3262 min_n_opers = nsubstitutes;
3263 else if (nsubstitutes > max_n_opers)
3264 max_n_opers = nsubstitutes;
3265 }
3266 }
3267
3268 unsigned n_ids = user_ids.length ();
3269 if (n_ids == 0)
3270 fatal_at (token, "for requires at least one user-defined identifier");
3271
3272 token = peek ();
3273 if (token->type == CPP_CLOSE_PAREN)
3274 fatal_at (token, "no pattern defined in for");
3275
3276 active_fors.safe_push (user_ids);
3277 while (1)
3278 {
3279 token = peek ();
3280 if (token->type == CPP_CLOSE_PAREN)
3281 break;
3282 parse_pattern ();
3283 }
3284 active_fors.pop ();
3285
3286 /* Remove user-defined operators from the hash again. */
3287 for (unsigned i = 0; i < user_ids.length (); ++i)
3288 {
3289 if (!user_ids[i]->used)
3290 warning_at (user_id_tokens[i],
3291 "operator %s defined but not used", user_ids[i]->id);
3292 operators->remove_elt (user_ids[i]);
3293 }
3294 }
3295
3296 /* Parse an identifier associated with a list of operators.
3297 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
3298
3299 void
3300 parser::parse_operator_list (source_location)
3301 {
3302 const cpp_token *token = peek ();
3303 const char *id = get_ident ();
3304
3305 if (get_operator (id) != 0)
3306 fatal_at (token, "operator %s already defined", id);
3307
3308 user_id *op = new user_id (id, true);
3309 int arity = -1;
3310
3311 while ((token = peek_ident ()) != 0)
3312 {
3313 token = peek ();
3314 const char *oper = get_ident ();
3315 id_base *idb = get_operator (oper);
3316
3317 if (idb == 0)
3318 fatal_at (token, "no such operator '%s'", oper);
3319
3320 if (arity == -1)
3321 arity = idb->nargs;
3322 else if (idb->nargs == -1)
3323 ;
3324 else if (arity != idb->nargs)
3325 fatal_at (token, "operator '%s' with arity %d does not match "
3326 "others with arity %d", oper, idb->nargs, arity);
3327
3328 /* We allow composition of multiple operator lists. */
3329 if (user_id *p = dyn_cast<user_id *> (idb))
3330 op->substitutes.safe_splice (p->substitutes);
3331 else
3332 op->substitutes.safe_push (idb);
3333 }
3334
3335 if (op->substitutes.length () == 0)
3336 fatal_at (token, "operator-list cannot be empty");
3337
3338 op->nargs = arity;
3339 id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
3340 *slot = op;
3341 }
3342
3343 /* Parse an outer if expression.
3344 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
3345
3346 void
3347 parser::parse_if (source_location loc)
3348 {
3349 operand *ifexpr = parse_c_expr (CPP_OPEN_PAREN);
3350
3351 const cpp_token *token = peek ();
3352 if (token->type == CPP_CLOSE_PAREN)
3353 fatal_at (token, "no pattern defined in if");
3354
3355 active_ifs.safe_push (if_or_with (ifexpr, loc, false));
3356 while (1)
3357 {
3358 const cpp_token *token = peek ();
3359 if (token->type == CPP_CLOSE_PAREN)
3360 break;
3361
3362 parse_pattern ();
3363 }
3364 active_ifs.pop ();
3365 }
3366
3367 /* Parse a list of predefined predicate identifiers.
3368 preds = '(' 'define_predicates' <ident>... ')' */
3369
3370 void
3371 parser::parse_predicates (source_location)
3372 {
3373 do
3374 {
3375 const cpp_token *token = peek ();
3376 if (token->type != CPP_NAME)
3377 break;
3378
3379 add_predicate (get_ident ());
3380 }
3381 while (1);
3382 }
3383
3384 /* Parse outer control structures.
3385 pattern = <preds>|<for>|<if>|<simplify>|<match> */
3386
3387 void
3388 parser::parse_pattern ()
3389 {
3390 /* All clauses start with '('. */
3391 eat_token (CPP_OPEN_PAREN);
3392 const cpp_token *token = peek ();
3393 const char *id = get_ident ();
3394 if (strcmp (id, "simplify") == 0)
3395 parse_simplify (token->src_loc, simplifiers, NULL, NULL);
3396 else if (strcmp (id, "match") == 0)
3397 {
3398 bool with_args = false;
3399 if (peek ()->type == CPP_OPEN_PAREN)
3400 {
3401 eat_token (CPP_OPEN_PAREN);
3402 with_args = true;
3403 }
3404 const char *name = get_ident ();
3405 id_base *id = get_operator (name);
3406 predicate_id *p;
3407 if (!id)
3408 {
3409 p = add_predicate (name);
3410 user_predicates.safe_push (p);
3411 }
3412 else if ((p = dyn_cast <predicate_id *> (id)))
3413 ;
3414 else
3415 fatal_at (token, "cannot add a match to a non-predicate ID");
3416 /* Parse (match <id> <arg>... (match-expr)) here. */
3417 expr *e = NULL;
3418 if (with_args)
3419 {
3420 e = new expr (p);
3421 while (peek ()->type == CPP_ATSIGN)
3422 e->append_op (parse_capture (NULL));
3423 eat_token (CPP_CLOSE_PAREN);
3424 }
3425 if (p->nargs != -1
3426 && ((e && e->ops.length () != (unsigned)p->nargs)
3427 || (!e && p->nargs != 0)))
3428 fatal_at (token, "non-matching number of match operands");
3429 p->nargs = e ? e->ops.length () : 0;
3430 parse_simplify (token->src_loc, p->matchers, p, e);
3431 }
3432 else if (strcmp (id, "for") == 0)
3433 parse_for (token->src_loc);
3434 else if (strcmp (id, "if") == 0)
3435 parse_if (token->src_loc);
3436 else if (strcmp (id, "define_predicates") == 0)
3437 {
3438 if (active_ifs.length () > 0
3439 || active_fors.length () > 0)
3440 fatal_at (token, "define_predicates inside if or for is not supported");
3441 parse_predicates (token->src_loc);
3442 }
3443 else if (strcmp (id, "define_operator_list") == 0)
3444 {
3445 if (active_ifs.length () > 0
3446 || active_fors.length () > 0)
3447 fatal_at (token, "operator-list inside if or for is not supported");
3448 parse_operator_list (token->src_loc);
3449 }
3450 else
3451 fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'",
3452 active_ifs.length () == 0 && active_fors.length () == 0
3453 ? "'define_predicates', " : "");
3454
3455 eat_token (CPP_CLOSE_PAREN);
3456 }
3457
3458 /* Main entry of the parser. Repeatedly parse outer control structures. */
3459
3460 parser::parser (cpp_reader *r_)
3461 {
3462 r = r_;
3463 active_ifs = vNULL;
3464 active_fors = vNULL;
3465 simplifiers = vNULL;
3466 user_predicates = vNULL;
3467 parsing_match_operand = false;
3468
3469 const cpp_token *token = next ();
3470 while (token->type != CPP_EOF)
3471 {
3472 _cpp_backup_tokens (r, 1);
3473 parse_pattern ();
3474 token = next ();
3475 }
3476 }
3477
3478
3479 /* Helper for the linemap code. */
3480
3481 static size_t
3482 round_alloc_size (size_t s)
3483 {
3484 return s;
3485 }
3486
3487
3488 /* The genmatch generator progam. It reads from a pattern description
3489 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
3490
3491 int
3492 main (int argc, char **argv)
3493 {
3494 cpp_reader *r;
3495
3496 progname = "genmatch";
3497
3498 if (argc < 2)
3499 return 1;
3500
3501 bool gimple = true;
3502 bool verbose = false;
3503 char *input = argv[argc-1];
3504 for (int i = 1; i < argc - 1; ++i)
3505 {
3506 if (strcmp (argv[i], "--gimple") == 0)
3507 gimple = true;
3508 else if (strcmp (argv[i], "--generic") == 0)
3509 gimple = false;
3510 else if (strcmp (argv[i], "-v") == 0)
3511 verbose = true;
3512 else
3513 {
3514 fprintf (stderr, "Usage: genmatch "
3515 "[--gimple] [--generic] [-v] input\n");
3516 return 1;
3517 }
3518 }
3519
3520 line_table = XCNEW (struct line_maps);
3521 linemap_init (line_table, 0);
3522 line_table->reallocator = xrealloc;
3523 line_table->round_alloc_size = round_alloc_size;
3524
3525 r = cpp_create_reader (CLK_GNUC99, NULL, line_table);
3526 cpp_callbacks *cb = cpp_get_callbacks (r);
3527 cb->error = error_cb;
3528
3529 if (!cpp_read_main_file (r, input))
3530 return 1;
3531 cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1");
3532 cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0");
3533
3534 /* Pre-seed operators. */
3535 operators = new hash_table<id_base> (1024);
3536 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
3537 add_operator (SYM, # SYM, # TYPE, NARGS);
3538 #define END_OF_BASE_TREE_CODES
3539 #include "tree.def"
3540 add_operator (CONVERT0, "CONVERT0", "tcc_unary", 1);
3541 add_operator (CONVERT1, "CONVERT1", "tcc_unary", 1);
3542 add_operator (CONVERT2, "CONVERT2", "tcc_unary", 1);
3543 #undef END_OF_BASE_TREE_CODES
3544 #undef DEFTREECODE
3545
3546 /* Pre-seed builtin functions.
3547 ??? Cannot use N (name) as that is targetm.emultls.get_address
3548 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
3549 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
3550 add_builtin (ENUM, # ENUM);
3551 #include "builtins.def"
3552 #undef DEF_BUILTIN
3553
3554 /* Parse ahead! */
3555 parser p (r);
3556
3557 if (gimple)
3558 write_header (stdout, "gimple-match-head.c");
3559 else
3560 write_header (stdout, "generic-match-head.c");
3561
3562 /* Go over all predicates defined with patterns and perform
3563 lowering and code generation. */
3564 for (unsigned i = 0; i < p.user_predicates.length (); ++i)
3565 {
3566 predicate_id *pred = p.user_predicates[i];
3567 lower (pred->matchers, gimple);
3568
3569 if (verbose)
3570 for (unsigned i = 0; i < pred->matchers.length (); ++i)
3571 print_matches (pred->matchers[i]);
3572
3573 decision_tree dt;
3574 for (unsigned i = 0; i < pred->matchers.length (); ++i)
3575 dt.insert (pred->matchers[i], i);
3576
3577 if (verbose)
3578 dt.print (stderr);
3579
3580 write_predicate (stdout, pred, dt, gimple);
3581 }
3582
3583 /* Lower the main simplifiers and generate code for them. */
3584 lower (p.simplifiers, gimple);
3585
3586 if (verbose)
3587 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
3588 print_matches (p.simplifiers[i]);
3589
3590 decision_tree dt;
3591 for (unsigned i = 0; i < p.simplifiers.length (); ++i)
3592 dt.insert (p.simplifiers[i], i);
3593
3594 if (verbose)
3595 dt.print (stderr);
3596
3597 if (gimple)
3598 dt.gen_gimple (stdout);
3599 else
3600 dt.gen_generic (stdout);
3601
3602 /* Finalize. */
3603 cpp_finish (r, NULL);
3604 cpp_destroy (r);
3605
3606 delete operators;
3607
3608 return 0;
3609 }