1 /* Generate pattern matching and transform code shared between
2 GENERIC and GIMPLE folding code from match-and-simplify description.
4 Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 Contributed by Richard Biener <rguenther@suse.de>
6 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
32 #include "hash-table.h"
39 /* Stubs for GGC referenced through instantiations triggered by hash-map. */
40 void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
41 size_t, size_t MEM_STAT_DECL
)
45 void ggc_free (void *)
52 static struct line_maps
*line_table
;
55 #if GCC_VERSION >= 4001
56 __attribute__((format (printf
, 6, 0)))
58 error_cb (cpp_reader
*, int errtype
, int, source_location location
,
59 unsigned int, const char *msg
, va_list *ap
)
61 const line_map_ordinary
*map
;
62 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
63 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
64 fprintf (stderr
, "%s:%d:%d %s: ", loc
.file
, loc
.line
, loc
.column
,
65 (errtype
== CPP_DL_WARNING
) ? "warning" : "error");
66 vfprintf (stderr
, msg
, *ap
);
67 fprintf (stderr
, "\n");
68 FILE *f
= fopen (loc
.file
, "r");
74 if (!fgets (buf
, 128, f
))
76 if (buf
[strlen (buf
) - 1] != '\n')
83 fprintf (stderr
, "%s", buf
);
84 for (int i
= 0; i
< loc
.column
- 1; ++i
)
92 if (errtype
== CPP_DL_FATAL
)
98 #if GCC_VERSION >= 4001
99 __attribute__((format (printf
, 2, 3)))
101 fatal_at (const cpp_token
*tk
, const char *msg
, ...)
105 error_cb (NULL
, CPP_DL_FATAL
, 0, tk
->src_loc
, 0, msg
, &ap
);
110 #if GCC_VERSION >= 4001
111 __attribute__((format (printf
, 2, 3)))
113 fatal_at (source_location loc
, const char *msg
, ...)
117 error_cb (NULL
, CPP_DL_FATAL
, 0, loc
, 0, msg
, &ap
);
122 #if GCC_VERSION >= 4001
123 __attribute__((format (printf
, 2, 3)))
125 warning_at (const cpp_token
*tk
, const char *msg
, ...)
129 error_cb (NULL
, CPP_DL_WARNING
, 0, tk
->src_loc
, 0, msg
, &ap
);
134 output_line_directive (FILE *f
, source_location location
,
135 bool dumpfile
= false)
137 const line_map_ordinary
*map
;
138 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
139 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
142 /* When writing to a dumpfile only dump the filename. */
143 const char *file
= strrchr (loc
.file
, DIR_SEPARATOR
);
148 fprintf (f
, "%s:%d", file
, loc
.line
);
151 /* Other gen programs really output line directives here, at least for
152 development it's right now more convenient to have line information
153 from the generated file. Still keep the directives as comment for now
154 to easily back-point to the meta-description. */
155 fprintf (f
, "/* #line %d \"%s\" */\n", loc
.line
, loc
.file
);
159 /* Pull in tree codes and builtin function codes from their
162 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
172 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
173 enum built_in_function
{
174 #include "builtins.def"
180 /* Base class for all identifiers the parser knows. */
182 struct id_base
: typed_noop_remove
<id_base
>
184 enum id_kind
{ CODE
, FN
, PREDICATE
, USER
} kind
;
186 id_base (id_kind
, const char *, int = -1);
192 /* hash_table support. */
193 typedef id_base
*value_type
;
194 typedef id_base
*compare_type
;
195 static inline hashval_t
hash (const id_base
*);
196 static inline int equal (const id_base
*, const id_base
*);
200 id_base::hash (const id_base
*op
)
206 id_base::equal (const id_base
*op1
,
209 return (op1
->hashval
== op2
->hashval
210 && strcmp (op1
->id
, op2
->id
) == 0);
213 /* Hashtable of known pattern operators. This is pre-seeded from
214 all known tree codes and all known builtin function ids. */
215 static hash_table
<id_base
> *operators
;
217 id_base::id_base (id_kind kind_
, const char *id_
, int nargs_
)
222 hashval
= htab_hash_string (id
);
225 /* Identifier that maps to a tree code. */
227 struct operator_id
: public id_base
229 operator_id (enum tree_code code_
, const char *id_
, unsigned nargs_
,
231 : id_base (id_base::CODE
, id_
, nargs_
), code (code_
), tcc (tcc_
) {}
236 /* Identifier that maps to a builtin function code. */
238 struct fn_id
: public id_base
240 fn_id (enum built_in_function fn_
, const char *id_
)
241 : id_base (id_base::FN
, id_
), fn (fn_
) {}
242 enum built_in_function fn
;
247 /* Identifier that maps to a user-defined predicate. */
249 struct predicate_id
: public id_base
251 predicate_id (const char *id_
)
252 : id_base (id_base::PREDICATE
, id_
), matchers (vNULL
) {}
253 vec
<simplify
*> matchers
;
256 /* Identifier that maps to a operator defined by a 'for' directive. */
258 struct user_id
: public id_base
260 user_id (const char *id_
, bool is_oper_list_
= false)
261 : id_base (id_base::USER
, id_
), substitutes (vNULL
),
262 used (false), is_oper_list (is_oper_list_
) {}
263 vec
<id_base
*> substitutes
;
271 is_a_helper
<fn_id
*>::test (id_base
*id
)
273 return id
->kind
== id_base::FN
;
279 is_a_helper
<operator_id
*>::test (id_base
*id
)
281 return id
->kind
== id_base::CODE
;
287 is_a_helper
<predicate_id
*>::test (id_base
*id
)
289 return id
->kind
== id_base::PREDICATE
;
295 is_a_helper
<user_id
*>::test (id_base
*id
)
297 return id
->kind
== id_base::USER
;
300 /* Add a predicate identifier to the hash. */
302 static predicate_id
*
303 add_predicate (const char *id
)
305 predicate_id
*p
= new predicate_id (id
);
306 id_base
**slot
= operators
->find_slot_with_hash (p
, p
->hashval
, INSERT
);
308 fatal ("duplicate id definition");
313 /* Add a tree code identifier to the hash. */
316 add_operator (enum tree_code code
, const char *id
,
317 const char *tcc
, unsigned nargs
)
319 if (strcmp (tcc
, "tcc_unary") != 0
320 && strcmp (tcc
, "tcc_binary") != 0
321 && strcmp (tcc
, "tcc_comparison") != 0
322 && strcmp (tcc
, "tcc_expression") != 0
323 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
324 && strcmp (tcc
, "tcc_reference") != 0
325 /* To have INTEGER_CST and friends as "predicate operators". */
326 && strcmp (tcc
, "tcc_constant") != 0
327 /* And allow CONSTRUCTOR for vector initializers. */
328 && !(code
== CONSTRUCTOR
))
330 operator_id
*op
= new operator_id (code
, id
, nargs
, tcc
);
331 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
333 fatal ("duplicate id definition");
337 /* Add a builtin identifier to the hash. */
340 add_builtin (enum built_in_function code
, const char *id
)
342 fn_id
*fn
= new fn_id (code
, id
);
343 id_base
**slot
= operators
->find_slot_with_hash (fn
, fn
->hashval
, INSERT
);
345 fatal ("duplicate id definition");
349 /* Helper for easy comparing ID with tree code CODE. */
352 operator==(id_base
&id
, enum tree_code code
)
354 if (operator_id
*oid
= dyn_cast
<operator_id
*> (&id
))
355 return oid
->code
== code
;
359 /* Lookup the identifier ID. */
362 get_operator (const char *id
)
364 id_base
tem (id_base::CODE
, id
);
366 id_base
*op
= operators
->find_with_hash (&tem
, tem
.hashval
);
369 /* If this is a user-defined identifier track whether it was used. */
370 if (user_id
*uid
= dyn_cast
<user_id
*> (op
))
375 /* Try all-uppercase. */
376 char *id2
= xstrdup (id
);
377 for (unsigned i
= 0; i
< strlen (id2
); ++i
)
378 id2
[i
] = TOUPPER (id2
[i
]);
379 new (&tem
) id_base (id_base::CODE
, id2
);
380 op
= operators
->find_with_hash (&tem
, tem
.hashval
);
387 /* Try _EXPR appended. */
388 id2
= (char *)xrealloc (id2
, strlen (id2
) + sizeof ("_EXPR") + 1);
389 strcat (id2
, "_EXPR");
390 new (&tem
) id_base (id_base::CODE
, id2
);
391 op
= operators
->find_with_hash (&tem
, tem
.hashval
);
402 /* Helper for the capture-id map. */
404 struct capture_id_map_hasher
: default_hashmap_traits
406 static inline hashval_t
hash (const char *);
407 static inline bool equal_keys (const char *, const char *);
411 capture_id_map_hasher::hash (const char *id
)
413 return htab_hash_string (id
);
417 capture_id_map_hasher::equal_keys (const char *id1
, const char *id2
)
419 return strcmp (id1
, id2
) == 0;
422 typedef hash_map
<const char *, unsigned, capture_id_map_hasher
> cid_map_t
;
425 /* The AST produced by parsing of the pattern definitions. */
430 /* The base class for operands. */
433 enum op_type
{ OP_PREDICATE
, OP_EXPR
, OP_CAPTURE
, OP_C_EXPR
};
434 operand (enum op_type type_
) : type (type_
) {}
436 virtual void gen_transform (FILE *, const char *, bool, int,
437 const char *, capture_info
*,
440 { gcc_unreachable (); }
443 /* A predicate operand. Predicates are leafs in the AST. */
445 struct predicate
: public operand
447 predicate (predicate_id
*p_
) : operand (OP_PREDICATE
), p (p_
) {}
451 /* An operand that constitutes an expression. Expressions include
452 function calls and user-defined predicate invocations. */
454 struct expr
: public operand
456 expr (id_base
*operation_
, bool is_commutative_
= false)
457 : operand (OP_EXPR
), operation (operation_
),
458 ops (vNULL
), expr_type (NULL
), is_commutative (is_commutative_
),
459 is_generic (false) {}
460 void append_op (operand
*op
) { ops
.safe_push (op
); }
461 /* The operator and its operands. */
464 /* An explicitely specified type - used exclusively for conversions. */
465 const char *expr_type
;
466 /* Whether the operation is to be applied commutatively. This is
467 later lowered to two separate patterns. */
469 /* Whether the expression is expected to be in GENERIC form. */
471 virtual void gen_transform (FILE *f
, const char *, bool, int,
472 const char *, capture_info
*,
473 dt_operand
** = 0, bool = true);
476 /* An operator that is represented by native C code. This is always
477 a leaf operand in the AST. This class is also used to represent
478 the code to be generated for 'if' and 'with' expressions. */
480 struct c_expr
: public operand
482 /* A mapping of an identifier and its replacement. Used to apply
487 id_tab (const char *id_
, const char *oper_
): id (id_
), oper (oper_
) {}
490 c_expr (cpp_reader
*r_
, vec
<cpp_token
> code_
, unsigned nr_stmts_
,
491 vec
<id_tab
> ids_
, cid_map_t
*capture_ids_
)
492 : operand (OP_C_EXPR
), r (r_
), code (code_
), capture_ids (capture_ids_
),
493 nr_stmts (nr_stmts_
), ids (ids_
) {}
494 /* cpplib tokens and state to transform this back to source. */
497 cid_map_t
*capture_ids
;
498 /* The number of statements parsed (well, the number of ';'s). */
500 /* The identifier replacement vector. */
502 virtual void gen_transform (FILE *f
, const char *, bool, int,
503 const char *, capture_info
*,
504 dt_operand
** = 0, bool = true);
507 /* A wrapper around another operand that captures its value. */
509 struct capture
: public operand
511 capture (unsigned where_
, operand
*what_
)
512 : operand (OP_CAPTURE
), where (where_
), what (what_
) {}
513 /* Identifier index for the value. */
515 /* The captured value. */
517 virtual void gen_transform (FILE *f
, const char *, bool, int,
518 const char *, capture_info
*,
519 dt_operand
** = 0, bool = true);
525 is_a_helper
<capture
*>::test (operand
*op
)
527 return op
->type
== operand::OP_CAPTURE
;
533 is_a_helper
<predicate
*>::test (operand
*op
)
535 return op
->type
== operand::OP_PREDICATE
;
541 is_a_helper
<c_expr
*>::test (operand
*op
)
543 return op
->type
== operand::OP_C_EXPR
;
549 is_a_helper
<expr
*>::test (operand
*op
)
551 return op
->type
== operand::OP_EXPR
;
554 /* Helper to distinguish 'if' from 'with' expressions. */
558 if_or_with (operand
*cexpr_
, source_location location_
, bool is_with_
)
559 : location (location_
), cexpr (cexpr_
), is_with (is_with_
) {}
560 source_location location
;
565 /* The main class of a pattern and its transform. This is used to
566 represent both (simplify ...) and (match ...) kinds. The AST
567 duplicates all outer 'if' and 'for' expressions here so each
568 simplify can exist in isolation. */
572 simplify (operand
*match_
, source_location match_location_
,
573 struct operand
*result_
, source_location result_location_
,
574 vec
<if_or_with
> ifexpr_vec_
, vec
<vec
<user_id
*> > for_vec_
,
575 cid_map_t
*capture_ids_
)
576 : match (match_
), match_location (match_location_
),
577 result (result_
), result_location (result_location_
),
578 ifexpr_vec (ifexpr_vec_
), for_vec (for_vec_
),
579 capture_ids (capture_ids_
), capture_max (capture_ids_
->elements () - 1) {}
581 /* The expression that is matched against the GENERIC or GIMPLE IL. */
583 source_location match_location
;
584 /* For a (simplify ...) the expression produced when the pattern applies.
585 For a (match ...) either NULL if it is a simple predicate or the
586 single expression specifying the matched operands. */
587 struct operand
*result
;
588 source_location result_location
;
589 /* Collected 'if' expressions that need to evaluate to true to make
590 the pattern apply. */
591 vec
<if_or_with
> ifexpr_vec
;
592 /* Collected 'for' expression operators that have to be replaced
593 in the lowering phase. */
594 vec
<vec
<user_id
*> > for_vec
;
595 /* A map of capture identifiers to indexes. */
596 cid_map_t
*capture_ids
;
600 /* Debugging routines for dumping the AST. */
603 print_operand (operand
*o
, FILE *f
= stderr
, bool flattened
= false)
605 if (capture
*c
= dyn_cast
<capture
*> (o
))
607 fprintf (f
, "@%u", c
->where
);
608 if (c
->what
&& flattened
== false)
611 print_operand (c
->what
, f
, flattened
);
616 else if (predicate
*p
= dyn_cast
<predicate
*> (o
))
617 fprintf (f
, "%s", p
->p
->id
);
619 else if (is_a
<c_expr
*> (o
))
620 fprintf (f
, "c_expr");
622 else if (expr
*e
= dyn_cast
<expr
*> (o
))
624 fprintf (f
, "(%s", e
->operation
->id
);
626 if (flattened
== false)
629 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
631 print_operand (e
->ops
[i
], f
, flattened
);
643 print_matches (struct simplify
*s
, FILE *f
= stderr
)
645 fprintf (f
, "for expression: ");
646 print_operand (s
->match
, f
);
653 /* Lowering of commutative operators. */
656 cartesian_product (const vec
< vec
<operand
*> >& ops_vector
,
657 vec
< vec
<operand
*> >& result
, vec
<operand
*>& v
, unsigned n
)
659 if (n
== ops_vector
.length ())
661 vec
<operand
*> xv
= v
.copy ();
662 result
.safe_push (xv
);
666 for (unsigned i
= 0; i
< ops_vector
[n
].length (); ++i
)
668 v
[n
] = ops_vector
[n
][i
];
669 cartesian_product (ops_vector
, result
, v
, n
+ 1);
673 /* Lower OP to two operands in case it is marked as commutative. */
675 static vec
<operand
*>
676 commutate (operand
*op
)
678 vec
<operand
*> ret
= vNULL
;
680 if (capture
*c
= dyn_cast
<capture
*> (op
))
687 vec
<operand
*> v
= commutate (c
->what
);
688 for (unsigned i
= 0; i
< v
.length (); ++i
)
690 capture
*nc
= new capture (c
->where
, v
[i
]);
696 expr
*e
= dyn_cast
<expr
*> (op
);
697 if (!e
|| e
->ops
.length () == 0)
703 vec
< vec
<operand
*> > ops_vector
= vNULL
;
704 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
705 ops_vector
.safe_push (commutate (e
->ops
[i
]));
707 auto_vec
< vec
<operand
*> > result
;
708 auto_vec
<operand
*> v (e
->ops
.length ());
709 v
.quick_grow_cleared (e
->ops
.length ());
710 cartesian_product (ops_vector
, result
, v
, 0);
713 for (unsigned i
= 0; i
< result
.length (); ++i
)
715 expr
*ne
= new expr (e
->operation
);
716 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
717 ne
->append_op (result
[i
][j
]);
721 if (!e
->is_commutative
)
724 for (unsigned i
= 0; i
< result
.length (); ++i
)
726 expr
*ne
= new expr (e
->operation
);
727 // result[i].length () is 2 since e->operation is binary
728 for (unsigned j
= result
[i
].length (); j
; --j
)
729 ne
->append_op (result
[i
][j
-1]);
736 /* Lower operations marked as commutative in the AST of S and push
737 the resulting patterns to SIMPLIFIERS. */
740 lower_commutative (simplify
*s
, vec
<simplify
*>& simplifiers
)
742 vec
<operand
*> matchers
= commutate (s
->match
);
743 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
745 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
746 s
->result
, s
->result_location
, s
->ifexpr_vec
,
747 s
->for_vec
, s
->capture_ids
);
748 simplifiers
.safe_push (ns
);
752 /* Strip conditional conversios using operator OPER from O and its
753 children if STRIP, else replace them with an unconditional convert. */
756 lower_opt_convert (operand
*o
, enum tree_code oper
, bool strip
)
758 if (capture
*c
= dyn_cast
<capture
*> (o
))
761 return new capture (c
->where
, lower_opt_convert (c
->what
, oper
, strip
));
766 expr
*e
= dyn_cast
<expr
*> (o
);
770 if (*e
->operation
== oper
)
773 return lower_opt_convert (e
->ops
[0], oper
, strip
);
775 expr
*ne
= new expr (get_operator ("CONVERT_EXPR"));
776 ne
->append_op (lower_opt_convert (e
->ops
[0], oper
, strip
));
780 expr
*ne
= new expr (e
->operation
, e
->is_commutative
);
781 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
782 ne
->append_op (lower_opt_convert (e
->ops
[i
], oper
, strip
));
787 /* Determine whether O or its children uses the conditional conversion
791 has_opt_convert (operand
*o
, enum tree_code oper
)
793 if (capture
*c
= dyn_cast
<capture
*> (o
))
796 return has_opt_convert (c
->what
, oper
);
801 expr
*e
= dyn_cast
<expr
*> (o
);
805 if (*e
->operation
== oper
)
808 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
809 if (has_opt_convert (e
->ops
[i
], oper
))
815 /* Lower conditional convert operators in O, expanding it to a vector
818 static vec
<operand
*>
819 lower_opt_convert (operand
*o
)
821 vec
<operand
*> v1
= vNULL
, v2
;
825 enum tree_code opers
[] = { CONVERT0
, CONVERT1
, CONVERT2
};
827 /* Conditional converts are lowered to a pattern with the
828 conversion and one without. The three different conditional
829 convert codes are lowered separately. */
831 for (unsigned i
= 0; i
< 3; ++i
)
834 for (unsigned j
= 0; j
< v1
.length (); ++j
)
835 if (has_opt_convert (v1
[j
], opers
[i
]))
837 v2
.safe_push (lower_opt_convert (v1
[j
], opers
[i
], false));
838 v2
.safe_push (lower_opt_convert (v1
[j
], opers
[i
], true));
844 for (unsigned j
= 0; j
< v2
.length (); ++j
)
845 v1
.safe_push (v2
[j
]);
852 /* Lower conditional convert operators in the AST of S and push
853 the resulting multiple patterns to SIMPLIFIERS. */
856 lower_opt_convert (simplify
*s
, vec
<simplify
*>& simplifiers
)
858 vec
<operand
*> matchers
= lower_opt_convert (s
->match
);
859 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
861 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
862 s
->result
, s
->result_location
, s
->ifexpr_vec
,
863 s
->for_vec
, s
->capture_ids
);
864 simplifiers
.safe_push (ns
);
868 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
869 GENERIC and a GIMPLE variant. */
871 static vec
<operand
*>
872 lower_cond (operand
*o
)
874 vec
<operand
*> ro
= vNULL
;
876 if (capture
*c
= dyn_cast
<capture
*> (o
))
880 vec
<operand
*> lop
= vNULL
;
881 lop
= lower_cond (c
->what
);
883 for (unsigned i
= 0; i
< lop
.length (); ++i
)
884 ro
.safe_push (new capture (c
->where
, lop
[i
]));
889 expr
*e
= dyn_cast
<expr
*> (o
);
890 if (!e
|| e
->ops
.length () == 0)
896 vec
< vec
<operand
*> > ops_vector
= vNULL
;
897 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
898 ops_vector
.safe_push (lower_cond (e
->ops
[i
]));
900 auto_vec
< vec
<operand
*> > result
;
901 auto_vec
<operand
*> v (e
->ops
.length ());
902 v
.quick_grow_cleared (e
->ops
.length ());
903 cartesian_product (ops_vector
, result
, v
, 0);
905 for (unsigned i
= 0; i
< result
.length (); ++i
)
907 expr
*ne
= new expr (e
->operation
);
908 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
909 ne
->append_op (result
[i
][j
]);
911 /* If this is a COND with a captured expression or an
912 expression with two operands then also match a GENERIC
913 form on the compare. */
914 if ((*e
->operation
== COND_EXPR
915 || *e
->operation
== VEC_COND_EXPR
)
916 && ((is_a
<capture
*> (e
->ops
[0])
917 && as_a
<capture
*> (e
->ops
[0])->what
918 && is_a
<expr
*> (as_a
<capture
*> (e
->ops
[0])->what
)
920 (as_a
<capture
*> (e
->ops
[0])->what
)->ops
.length () == 2)
921 || (is_a
<expr
*> (e
->ops
[0])
922 && as_a
<expr
*> (e
->ops
[0])->ops
.length () == 2)))
924 expr
*ne
= new expr (e
->operation
);
925 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
926 ne
->append_op (result
[i
][j
]);
927 if (capture
*c
= dyn_cast
<capture
*> (ne
->ops
[0]))
929 expr
*ocmp
= as_a
<expr
*> (c
->what
);
930 expr
*cmp
= new expr (ocmp
->operation
);
931 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
932 cmp
->append_op (ocmp
->ops
[j
]);
933 cmp
->is_generic
= true;
934 ne
->ops
[0] = new capture (c
->where
, cmp
);
938 expr
*ocmp
= as_a
<expr
*> (ne
->ops
[0]);
939 expr
*cmp
= new expr (ocmp
->operation
);
940 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
941 cmp
->append_op (ocmp
->ops
[j
]);
942 cmp
->is_generic
= true;
952 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
953 GENERIC and a GIMPLE variant. */
956 lower_cond (simplify
*s
, vec
<simplify
*>& simplifiers
)
958 vec
<operand
*> matchers
= lower_cond (s
->match
);
959 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
961 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
962 s
->result
, s
->result_location
, s
->ifexpr_vec
,
963 s
->for_vec
, s
->capture_ids
);
964 simplifiers
.safe_push (ns
);
968 /* In AST operand O replace operator ID with operator WITH. */
971 replace_id (operand
*o
, user_id
*id
, id_base
*with
)
973 /* Deep-copy captures and expressions, replacing operations as
975 if (capture
*c
= dyn_cast
<capture
*> (o
))
979 return new capture (c
->where
, replace_id (c
->what
, id
, with
));
981 else if (expr
*e
= dyn_cast
<expr
*> (o
))
983 expr
*ne
= new expr (e
->operation
== id
? with
: e
->operation
,
985 ne
->expr_type
= e
->expr_type
;
986 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
987 ne
->append_op (replace_id (e
->ops
[i
], id
, with
));
991 /* For c_expr we simply record a string replacement table which is
992 applied at code-generation time. */
993 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
995 vec
<c_expr::id_tab
> ids
= ce
->ids
.copy ();
996 ids
.safe_push (c_expr::id_tab (id
->id
, with
->id
));
997 return new c_expr (ce
->r
, ce
->code
, ce
->nr_stmts
, ids
, ce
->capture_ids
);
1003 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1006 lower_for (simplify
*sin
, vec
<simplify
*>& simplifiers
)
1008 vec
<vec
<user_id
*> >& for_vec
= sin
->for_vec
;
1009 unsigned worklist_start
= 0;
1010 auto_vec
<simplify
*> worklist
;
1011 worklist
.safe_push (sin
);
1013 /* Lower each recorded for separately, operating on the
1014 set of simplifiers created by the previous one.
1015 Lower inner-to-outer so inner for substitutes can refer
1016 to operators replaced by outer fors. */
1017 for (int fi
= for_vec
.length () - 1; fi
>= 0; --fi
)
1019 vec
<user_id
*>& ids
= for_vec
[fi
];
1020 unsigned n_ids
= ids
.length ();
1021 unsigned max_n_opers
= 0;
1022 for (unsigned i
= 0; i
< n_ids
; ++i
)
1023 if (ids
[i
]->substitutes
.length () > max_n_opers
)
1024 max_n_opers
= ids
[i
]->substitutes
.length ();
1026 unsigned worklist_end
= worklist
.length ();
1027 for (unsigned si
= worklist_start
; si
< worklist_end
; ++si
)
1029 simplify
*s
= worklist
[si
];
1030 for (unsigned j
= 0; j
< max_n_opers
; ++j
)
1032 operand
*match_op
= s
->match
;
1033 operand
*result_op
= s
->result
;
1034 vec
<if_or_with
> ifexpr_vec
= s
->ifexpr_vec
.copy ();
1036 for (unsigned i
= 0; i
< n_ids
; ++i
)
1038 user_id
*id
= ids
[i
];
1039 id_base
*oper
= id
->substitutes
[j
% id
->substitutes
.length ()];
1040 match_op
= replace_id (match_op
, id
, oper
);
1042 result_op
= replace_id (result_op
, id
, oper
);
1043 for (unsigned k
= 0; k
< s
->ifexpr_vec
.length (); ++k
)
1044 ifexpr_vec
[k
].cexpr
= replace_id (ifexpr_vec
[k
].cexpr
,
1047 simplify
*ns
= new simplify (match_op
, s
->match_location
,
1048 result_op
, s
->result_location
,
1049 ifexpr_vec
, vNULL
, s
->capture_ids
);
1050 worklist
.safe_push (ns
);
1053 worklist_start
= worklist_end
;
1056 /* Copy out the result from the last for lowering. */
1057 for (unsigned i
= worklist_start
; i
< worklist
.length (); ++i
)
1058 simplifiers
.safe_push (worklist
[i
]);
1061 /* Lower the AST for everything in SIMPLIFIERS. */
1064 lower (vec
<simplify
*>& simplifiers
, bool gimple
)
1066 auto_vec
<simplify
*> out_simplifiers
;
1067 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1068 lower_opt_convert (simplifiers
[i
], out_simplifiers
);
1070 simplifiers
.truncate (0);
1071 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1072 lower_commutative (out_simplifiers
[i
], simplifiers
);
1074 out_simplifiers
.truncate (0);
1076 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1077 lower_cond (simplifiers
[i
], out_simplifiers
);
1079 out_simplifiers
.safe_splice (simplifiers
);
1082 simplifiers
.truncate (0);
1083 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1084 lower_for (out_simplifiers
[i
], simplifiers
);
1090 /* The decision tree built for generating GIMPLE and GENERIC pattern
1091 matching code. It represents the 'match' expression of all
1092 simplifies and has those as its leafs. */
1094 /* Decision tree base class, used for DT_TRUE and DT_NODE. */
1098 enum dt_type
{ DT_NODE
, DT_OPERAND
, DT_TRUE
, DT_MATCH
, DT_SIMPLIFY
};
1102 vec
<dt_node
*> kids
;
1104 dt_node (enum dt_type type_
): type (type_
), level (0), kids (vNULL
) {}
1106 dt_node
*append_node (dt_node
*);
1107 dt_node
*append_op (operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1108 dt_node
*append_true_op (dt_node
*parent
= 0, unsigned pos
= 0);
1109 dt_node
*append_match_op (dt_operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1110 dt_node
*append_simplify (simplify
*, unsigned, dt_operand
**);
1112 virtual void gen (FILE *, bool) {}
1114 void gen_kids (FILE *, bool);
1115 void gen_kids_1 (FILE *, bool,
1116 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_operand
*>,
1117 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_node
*>);
1120 /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */
1122 struct dt_operand
: public dt_node
1125 dt_operand
*match_dop
;
1129 dt_operand (enum dt_type type
, operand
*op_
, dt_operand
*match_dop_
,
1130 dt_operand
*parent_
= 0, unsigned pos_
= 0)
1131 : dt_node (type
), op (op_
), match_dop (match_dop_
),
1132 parent (parent_
), pos (pos_
) {}
1134 void gen (FILE *, bool);
1135 unsigned gen_predicate (FILE *, const char *, bool);
1136 unsigned gen_match_op (FILE *, const char *);
1138 unsigned gen_gimple_expr (FILE *);
1139 unsigned gen_generic_expr (FILE *, const char *);
1141 char *get_name (char *);
1142 void gen_opname (char *, unsigned);
1145 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1147 struct dt_simplify
: public dt_node
1150 unsigned pattern_no
;
1151 dt_operand
**indexes
;
1153 dt_simplify (simplify
*s_
, unsigned pattern_no_
, dt_operand
**indexes_
)
1154 : dt_node (DT_SIMPLIFY
), s (s_
), pattern_no (pattern_no_
),
1155 indexes (indexes_
) {}
1157 void gen (FILE *f
, bool);
1163 is_a_helper
<dt_operand
*>::test (dt_node
*n
)
1165 return (n
->type
== dt_node::DT_OPERAND
1166 || n
->type
== dt_node::DT_MATCH
);
1169 /* A container for the actual decision tree. */
1171 struct decision_tree
1175 void insert (struct simplify
*, unsigned);
1176 void gen_gimple (FILE *f
= stderr
);
1177 void gen_generic (FILE *f
= stderr
);
1178 void print (FILE *f
= stderr
);
1180 decision_tree () { root
= new dt_node (dt_node::DT_NODE
); }
1182 static dt_node
*insert_operand (dt_node
*, operand
*, dt_operand
**indexes
,
1183 unsigned pos
= 0, dt_node
*parent
= 0);
1184 static dt_node
*find_node (vec
<dt_node
*>&, dt_node
*);
1185 static bool cmp_node (dt_node
*, dt_node
*);
1186 static void print_node (dt_node
*, FILE *f
= stderr
, unsigned = 0);
1189 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1192 cmp_operand (operand
*o1
, operand
*o2
)
1194 if (!o1
|| !o2
|| o1
->type
!= o2
->type
)
1197 if (o1
->type
== operand::OP_PREDICATE
)
1199 predicate
*p1
= as_a
<predicate
*>(o1
);
1200 predicate
*p2
= as_a
<predicate
*>(o2
);
1201 return p1
->p
== p2
->p
;
1203 else if (o1
->type
== operand::OP_EXPR
)
1205 expr
*e1
= static_cast<expr
*>(o1
);
1206 expr
*e2
= static_cast<expr
*>(o2
);
1207 return (e1
->operation
== e2
->operation
1208 && e1
->is_generic
== e2
->is_generic
);
1214 /* Compare two decision tree nodes N1 and N2 and return true if they
1218 decision_tree::cmp_node (dt_node
*n1
, dt_node
*n2
)
1220 if (!n1
|| !n2
|| n1
->type
!= n2
->type
)
1226 if (n1
->type
== dt_node::DT_TRUE
)
1229 if (n1
->type
== dt_node::DT_OPERAND
)
1230 return cmp_operand ((as_a
<dt_operand
*> (n1
))->op
,
1231 (as_a
<dt_operand
*> (n2
))->op
);
1232 else if (n1
->type
== dt_node::DT_MATCH
)
1233 return ((as_a
<dt_operand
*> (n1
))->match_dop
1234 == (as_a
<dt_operand
*> (n2
))->match_dop
);
1238 /* Search OPS for a decision tree node like P and return it if found. */
1241 decision_tree::find_node (vec
<dt_node
*>& ops
, dt_node
*p
)
1243 /* We can merge adjacent DT_TRUE. */
1244 if (p
->type
== dt_node::DT_TRUE
1246 && ops
.last ()->type
== dt_node::DT_TRUE
)
1248 for (int i
= ops
.length () - 1; i
>= 0; --i
)
1250 /* But we can't merge across DT_TRUE nodes as they serve as
1251 pattern order barriers to make sure that patterns apply
1252 in order of appearance in case multiple matches are possible. */
1253 if (ops
[i
]->type
== dt_node::DT_TRUE
)
1255 if (decision_tree::cmp_node (ops
[i
], p
))
1261 /* Append N to the decision tree if it there is not already an existing
1265 dt_node::append_node (dt_node
*n
)
1269 kid
= decision_tree::find_node (kids
, n
);
1274 n
->level
= this->level
+ 1;
1279 /* Append OP to the decision tree. */
1282 dt_node::append_op (operand
*op
, dt_node
*parent
, unsigned pos
)
1284 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1285 dt_operand
*n
= new dt_operand (DT_OPERAND
, op
, 0, parent_
, pos
);
1286 return append_node (n
);
1289 /* Append a DT_TRUE decision tree node. */
1292 dt_node::append_true_op (dt_node
*parent
, unsigned pos
)
1294 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1295 dt_operand
*n
= new dt_operand (DT_TRUE
, 0, 0, parent_
, pos
);
1296 return append_node (n
);
1299 /* Append a DT_MATCH decision tree node. */
1302 dt_node::append_match_op (dt_operand
*match_dop
, dt_node
*parent
, unsigned pos
)
1304 dt_operand
*parent_
= as_a
<dt_operand
*> (parent
);
1305 dt_operand
*n
= new dt_operand (DT_MATCH
, 0, match_dop
, parent_
, pos
);
1306 return append_node (n
);
1309 /* Append S to the decision tree. */
1312 dt_node::append_simplify (simplify
*s
, unsigned pattern_no
,
1313 dt_operand
**indexes
)
1315 dt_simplify
*n
= new dt_simplify (s
, pattern_no
, indexes
);
1316 return append_node (n
);
1319 /* Insert O into the decision tree and return the decision tree node found
1323 decision_tree::insert_operand (dt_node
*p
, operand
*o
, dt_operand
**indexes
,
1324 unsigned pos
, dt_node
*parent
)
1326 dt_node
*q
, *elm
= 0;
1328 if (capture
*c
= dyn_cast
<capture
*> (o
))
1330 unsigned capt_index
= c
->where
;
1332 if (indexes
[capt_index
] == 0)
1335 q
= insert_operand (p
, c
->what
, indexes
, pos
, parent
);
1338 q
= elm
= p
->append_true_op (parent
, pos
);
1341 // get to the last capture
1342 for (operand
*what
= c
->what
;
1343 what
&& is_a
<capture
*> (what
);
1344 c
= as_a
<capture
*> (what
), what
= c
->what
)
1349 unsigned cc_index
= c
->where
;
1350 dt_operand
*match_op
= indexes
[cc_index
];
1352 dt_operand
temp (dt_node::DT_TRUE
, 0, 0);
1353 elm
= decision_tree::find_node (p
->kids
, &temp
);
1357 dt_operand
temp (dt_node::DT_MATCH
, 0, match_op
);
1358 elm
= decision_tree::find_node (p
->kids
, &temp
);
1363 dt_operand
temp (dt_node::DT_OPERAND
, c
->what
, 0);
1364 elm
= decision_tree::find_node (p
->kids
, &temp
);
1368 gcc_assert (elm
->type
== dt_node::DT_TRUE
1369 || elm
->type
== dt_node::DT_OPERAND
1370 || elm
->type
== dt_node::DT_MATCH
);
1371 indexes
[capt_index
] = static_cast<dt_operand
*> (elm
);
1376 p
= p
->append_match_op (indexes
[capt_index
], parent
, pos
);
1378 return insert_operand (p
, c
->what
, indexes
, 0, p
);
1383 p
= p
->append_op (o
, parent
, pos
);
1386 if (expr
*e
= dyn_cast
<expr
*>(o
))
1388 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1389 q
= decision_tree::insert_operand (q
, e
->ops
[i
], indexes
, i
, p
);
1395 /* Insert S into the decision tree. */
1398 decision_tree::insert (struct simplify
*s
, unsigned pattern_no
)
1400 dt_operand
**indexes
= XCNEWVEC (dt_operand
*, s
->capture_max
+ 1);
1401 dt_node
*p
= decision_tree::insert_operand (root
, s
->match
, indexes
);
1402 p
->append_simplify (s
, pattern_no
, indexes
);
1405 /* Debug functions to dump the decision tree. */
1408 decision_tree::print_node (dt_node
*p
, FILE *f
, unsigned indent
)
1410 if (p
->type
== dt_node::DT_NODE
)
1411 fprintf (f
, "root");
1415 for (unsigned i
= 0; i
< indent
; i
++)
1418 if (p
->type
== dt_node::DT_OPERAND
)
1420 dt_operand
*dop
= static_cast<dt_operand
*>(p
);
1421 print_operand (dop
->op
, f
, true);
1423 else if (p
->type
== dt_node::DT_TRUE
)
1424 fprintf (f
, "true");
1425 else if (p
->type
== dt_node::DT_MATCH
)
1426 fprintf (f
, "match (%p)", (void *)((as_a
<dt_operand
*>(p
))->match_dop
));
1427 else if (p
->type
== dt_node::DT_SIMPLIFY
)
1429 dt_simplify
*s
= static_cast<dt_simplify
*> (p
);
1430 fprintf (f
, "simplify_%u { ", s
->pattern_no
);
1431 for (int i
= 0; i
<= s
->s
->capture_max
; ++i
)
1432 fprintf (f
, "%p, ", (void *) s
->indexes
[i
]);
1437 fprintf (stderr
, " (%p), %u, %u\n", (void *) p
, p
->level
, p
->kids
.length ());
1439 for (unsigned i
= 0; i
< p
->kids
.length (); ++i
)
1440 decision_tree::print_node (p
->kids
[i
], f
, indent
+ 2);
1444 decision_tree::print (FILE *f
)
1446 return decision_tree::print_node (root
, f
);
1450 /* For GENERIC we have to take care of wrapping multiple-used
1451 expressions with side-effects in save_expr and preserve side-effects
1452 of expressions with omit_one_operand. Analyze captures in
1453 match, result and with expressions and perform early-outs
1454 on the outermost match expression operands for cases we cannot
1459 capture_info (simplify
*s
);
1460 void walk_match (operand
*o
, unsigned toplevel_arg
, bool, bool);
1461 void walk_result (operand
*o
, bool);
1462 void walk_c_expr (c_expr
*);
1468 bool force_no_side_effects_p
;
1469 bool cond_expr_cond_p
;
1470 unsigned long toplevel_msk
;
1471 int result_use_count
;
1474 auto_vec
<cinfo
> info
;
1475 unsigned long force_no_side_effects
;
1478 /* Analyze captures in S. */
1480 capture_info::capture_info (simplify
*s
)
1484 || ((e
= dyn_cast
<expr
*> (s
->result
))
1485 && is_a
<predicate_id
*> (e
->operation
)))
1487 force_no_side_effects
= -1;
1491 force_no_side_effects
= 0;
1492 info
.safe_grow_cleared (s
->capture_max
+ 1);
1493 e
= as_a
<expr
*> (s
->match
);
1494 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1495 walk_match (e
->ops
[i
], i
,
1496 (i
!= 0 && *e
->operation
== COND_EXPR
)
1497 || *e
->operation
== TRUTH_ANDIF_EXPR
1498 || *e
->operation
== TRUTH_ORIF_EXPR
,
1500 && (*e
->operation
== COND_EXPR
1501 || *e
->operation
== VEC_COND_EXPR
));
1503 walk_result (s
->result
, false);
1505 for (unsigned i
= 0; i
< s
->ifexpr_vec
.length (); ++i
)
1506 if (s
->ifexpr_vec
[i
].is_with
)
1507 walk_c_expr (as_a
<c_expr
*>(s
->ifexpr_vec
[i
].cexpr
));
1510 /* Analyze captures in the match expression piece O. */
1513 capture_info::walk_match (operand
*o
, unsigned toplevel_arg
,
1514 bool conditional_p
, bool cond_expr_cond_p
)
1516 if (capture
*c
= dyn_cast
<capture
*> (o
))
1518 info
[c
->where
].toplevel_msk
|= 1 << toplevel_arg
;
1519 info
[c
->where
].force_no_side_effects_p
|= conditional_p
;
1520 info
[c
->where
].cond_expr_cond_p
|= cond_expr_cond_p
;
1521 /* Mark expr (non-leaf) captures and recurse. */
1523 && is_a
<expr
*> (c
->what
))
1525 info
[c
->where
].expr_p
= true;
1526 walk_match (c
->what
, toplevel_arg
, conditional_p
, false);
1529 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1531 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1533 bool cond_p
= conditional_p
;
1534 bool cond_expr_cond_p
= false;
1535 if (i
!= 0 && *e
->operation
== COND_EXPR
)
1537 else if (*e
->operation
== TRUTH_ANDIF_EXPR
1538 || *e
->operation
== TRUTH_ORIF_EXPR
)
1541 && (*e
->operation
== COND_EXPR
1542 || *e
->operation
== VEC_COND_EXPR
))
1543 cond_expr_cond_p
= true;
1544 walk_match (e
->ops
[i
], toplevel_arg
, cond_p
, cond_expr_cond_p
);
1547 else if (is_a
<predicate
*> (o
))
1549 /* Mark non-captured leafs toplevel arg for checking. */
1550 force_no_side_effects
|= 1 << toplevel_arg
;
1556 /* Analyze captures in the result expression piece O. */
1559 capture_info::walk_result (operand
*o
, bool conditional_p
)
1561 if (capture
*c
= dyn_cast
<capture
*> (o
))
1563 info
[c
->where
].result_use_count
++;
1564 /* If we substitute an expression capture we don't know
1565 which captures this will end up using (well, we don't
1566 compute that). Force the uses to be side-effect free
1567 which means forcing the toplevels that reach the
1568 expression side-effect free. */
1569 if (info
[c
->where
].expr_p
)
1570 force_no_side_effects
|= info
[c
->where
].toplevel_msk
;
1571 /* Mark CSE capture capture uses as forced to have
1574 && is_a
<expr
*> (c
->what
))
1576 info
[c
->where
].cse_p
= true;
1577 walk_result (c
->what
, true);
1580 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1582 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1584 bool cond_p
= conditional_p
;
1585 if (i
!= 0 && *e
->operation
== COND_EXPR
)
1587 else if (*e
->operation
== TRUTH_ANDIF_EXPR
1588 || *e
->operation
== TRUTH_ORIF_EXPR
)
1590 walk_result (e
->ops
[i
], cond_p
);
1593 else if (c_expr
*e
= dyn_cast
<c_expr
*> (o
))
1599 /* Look for captures in the C expr E. */
1602 capture_info::walk_c_expr (c_expr
*e
)
1604 /* Give up for C exprs mentioning captures not inside TREE_TYPE (). */
1605 unsigned p_depth
= 0;
1606 for (unsigned i
= 0; i
< e
->code
.length (); ++i
)
1608 const cpp_token
*t
= &e
->code
[i
];
1609 const cpp_token
*n
= i
< e
->code
.length () - 1 ? &e
->code
[i
+1] : NULL
;
1610 if (t
->type
== CPP_NAME
1611 && strcmp ((const char *)CPP_HASHNODE
1612 (t
->val
.node
.node
)->ident
.str
, "TREE_TYPE") == 0
1613 && n
->type
== CPP_OPEN_PAREN
)
1615 else if (t
->type
== CPP_CLOSE_PAREN
1618 else if (p_depth
== 0
1619 && t
->type
== CPP_ATSIGN
1620 && (n
->type
== CPP_NUMBER
1621 || n
->type
== CPP_NAME
)
1622 && !(n
->flags
& PREV_WHITE
))
1625 if (n
->type
== CPP_NUMBER
)
1626 id
= (const char *)n
->val
.str
.text
;
1628 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
1629 info
[*e
->capture_ids
->get(id
)].force_no_side_effects_p
= true;
1635 /* Code generation off the decision tree and the refered AST nodes. */
1638 is_conversion (id_base
*op
)
1640 return (*op
== CONVERT_EXPR
1642 || *op
== FLOAT_EXPR
1643 || *op
== FIX_TRUNC_EXPR
1644 || *op
== VIEW_CONVERT_EXPR
);
1647 /* Get the type to be used for generating operands of OP from the
1651 get_operand_type (id_base
*op
, const char *in_type
,
1652 const char *expr_type
,
1653 const char *other_oprnd_type
)
1655 /* Generally operands whose type does not match the type of the
1656 expression generated need to know their types but match and
1657 thus can fall back to 'other_oprnd_type'. */
1658 if (is_conversion (op
))
1659 return other_oprnd_type
;
1660 else if (*op
== REALPART_EXPR
1661 || *op
== IMAGPART_EXPR
)
1662 return other_oprnd_type
;
1663 else if (is_a
<operator_id
*> (op
)
1664 && strcmp (as_a
<operator_id
*> (op
)->tcc
, "tcc_comparison") == 0)
1665 return other_oprnd_type
;
1668 /* Otherwise all types should match - choose one in order of
1675 return other_oprnd_type
;
1679 /* Generate transform code for an expression. */
1682 expr::gen_transform (FILE *f
, const char *dest
, bool gimple
, int depth
,
1683 const char *in_type
, capture_info
*cinfo
,
1684 dt_operand
**indexes
, bool)
1686 bool conversion_p
= is_conversion (operation
);
1687 const char *type
= expr_type
;
1690 /* If there was a type specification in the pattern use it. */
1692 else if (conversion_p
)
1693 /* For conversions we need to build the expression using the
1694 outer type passed in. */
1696 else if (*operation
== REALPART_EXPR
1697 || *operation
== IMAGPART_EXPR
)
1699 /* __real and __imag use the component type of its operand. */
1700 sprintf (optype
, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth
);
1703 else if (is_a
<operator_id
*> (operation
)
1704 && !strcmp (as_a
<operator_id
*> (operation
)->tcc
, "tcc_comparison"))
1706 /* comparisons use boolean_type_node (or what gets in), but
1707 their operands need to figure out the types themselves. */
1708 sprintf (optype
, "boolean_type_node");
1713 /* Other operations are of the same type as their first operand. */
1714 sprintf (optype
, "TREE_TYPE (ops%d[0])", depth
);
1718 fatal ("two conversions in a row");
1721 fprintf (f
, " tree ops%d[%u], res;\n", depth
, ops
.length ());
1723 snprintf (op0type
, 64, "TREE_TYPE (ops%d[0])", depth
);
1724 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1727 snprintf (dest
, 32, " ops%d[%u]", depth
, i
);
1729 = get_operand_type (operation
, in_type
, expr_type
,
1730 i
== 0 ? NULL
: op0type
);
1731 ops
[i
]->gen_transform (f
, dest
, gimple
, depth
+ 1, optype
, cinfo
, indexes
,
1732 ((!(*operation
== COND_EXPR
)
1733 && !(*operation
== VEC_COND_EXPR
))
1738 if (*operation
== CONVERT_EXPR
)
1741 opr
= operation
->id
;
1745 /* ??? Building a stmt can fail for various reasons here, seq being
1746 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
1747 So if we fail here we should continue matching other patterns. */
1748 fprintf (f
, " code_helper tem_code = %s;\n"
1749 " tree tem_ops[3] = { ", opr
);
1750 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1751 fprintf (f
, "ops%d[%u]%s", depth
, i
,
1752 i
== ops
.length () - 1 ? " };\n" : ", ");
1753 fprintf (f
, " gimple_resimplify%d (seq, &tem_code, %s, tem_ops, valueize);\n",
1754 ops
.length (), type
);
1755 fprintf (f
, " res = maybe_push_res_to_seq (tem_code, %s, tem_ops, seq);\n"
1756 " if (!res) return false;\n", type
);
1760 if (operation
->kind
== id_base::CODE
)
1761 fprintf (f
, " res = fold_build%d_loc (loc, %s, %s",
1762 ops
.length(), opr
, type
);
1764 fprintf (f
, " res = build_call_expr_loc (loc, "
1765 "builtin_decl_implicit (%s), %d", opr
, ops
.length());
1766 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1767 fprintf (f
, ", ops%d[%u]", depth
, i
);
1768 fprintf (f
, ");\n");
1770 fprintf (f
, "%s = res;\n", dest
);
1774 /* Generate code for a c_expr which is either the expression inside
1775 an if statement or a sequence of statements which computes a
1776 result to be stored to DEST. */
1779 c_expr::gen_transform (FILE *f
, const char *dest
,
1780 bool, int, const char *, capture_info
*,
1781 dt_operand
**, bool)
1783 if (dest
&& nr_stmts
== 1)
1784 fprintf (f
, "%s = ", dest
);
1786 unsigned stmt_nr
= 1;
1787 for (unsigned i
= 0; i
< code
.length (); ++i
)
1789 const cpp_token
*token
= &code
[i
];
1791 /* Replace captures for code-gen. */
1792 if (token
->type
== CPP_ATSIGN
)
1794 const cpp_token
*n
= &code
[i
+1];
1795 if ((n
->type
== CPP_NUMBER
1796 || n
->type
== CPP_NAME
)
1797 && !(n
->flags
& PREV_WHITE
))
1799 if (token
->flags
& PREV_WHITE
)
1802 if (n
->type
== CPP_NUMBER
)
1803 id
= (const char *)n
->val
.str
.text
;
1805 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
1806 fprintf (f
, "captures[%u]", *capture_ids
->get(id
));
1812 if (token
->flags
& PREV_WHITE
)
1815 if (token
->type
== CPP_NAME
)
1817 const char *id
= (const char *) NODE_NAME (token
->val
.node
.node
);
1819 for (j
= 0; j
< ids
.length (); ++j
)
1821 if (strcmp (id
, ids
[j
].id
) == 0)
1823 fprintf (f
, "%s", ids
[j
].oper
);
1827 if (j
< ids
.length ())
1831 /* Output the token as string. */
1832 char *tk
= (char *)cpp_token_as_text (r
, token
);
1835 if (token
->type
== CPP_SEMICOLON
)
1838 if (dest
&& stmt_nr
== nr_stmts
)
1839 fprintf (f
, "\n %s = ", dest
);
1846 /* Generate transform code for a capture. */
1849 capture::gen_transform (FILE *f
, const char *dest
, bool gimple
, int depth
,
1850 const char *in_type
, capture_info
*cinfo
,
1851 dt_operand
**indexes
, bool expand_compares
)
1853 if (what
&& is_a
<expr
*> (what
))
1855 if (indexes
[where
] == 0)
1858 sprintf (buf
, "captures[%u]", where
);
1859 what
->gen_transform (f
, buf
, gimple
, depth
, in_type
, cinfo
, NULL
);
1863 fprintf (f
, "%s = captures[%u];\n", dest
, where
);
1865 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
1866 with substituting a capture of that.
1867 ??? Returning false here will also not allow any other patterns
1869 if (gimple
&& expand_compares
1870 && cinfo
->info
[where
].cond_expr_cond_p
)
1871 fprintf (f
, "if (COMPARISON_CLASS_P (%s))\n"
1873 " if (!seq) return false;\n"
1874 " %s = gimple_build (seq, TREE_CODE (%s),"
1875 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
1876 " TREE_OPERAND (%s, 1));\n"
1877 " }\n", dest
, dest
, dest
, dest
, dest
, dest
);
1880 /* Return the name of the operand representing the decision tree node.
1881 Use NAME as space to generate it. */
1884 dt_operand::get_name (char *name
)
1887 sprintf (name
, "t");
1888 else if (parent
->level
== 1)
1889 sprintf (name
, "op%u", pos
);
1890 else if (parent
->type
== dt_node::DT_MATCH
)
1891 return parent
->get_name (name
);
1893 sprintf (name
, "o%u%u", parent
->level
, pos
);
1897 /* Fill NAME with the operand name at position POS. */
1900 dt_operand::gen_opname (char *name
, unsigned pos
)
1903 sprintf (name
, "op%u", pos
);
1905 sprintf (name
, "o%u%u", level
, pos
);
1908 /* Generate matching code for the decision tree operand which is
1912 dt_operand::gen_predicate (FILE *f
, const char *opname
, bool gimple
)
1914 predicate
*p
= as_a
<predicate
*> (op
);
1916 if (p
->p
->matchers
.exists ())
1918 /* If this is a predicate generated from a pattern mangle its
1919 name and pass on the valueize hook. */
1921 fprintf (f
, "if (gimple_%s (%s, valueize))\n", p
->p
->id
, opname
);
1923 fprintf (f
, "if (tree_%s (%s))\n", p
->p
->id
, opname
);
1926 fprintf (f
, "if (%s (%s))\n", p
->p
->id
, opname
);
1931 /* Generate matching code for the decision tree operand which is
1935 dt_operand::gen_match_op (FILE *f
, const char *opname
)
1937 char match_opname
[20];
1938 match_dop
->get_name (match_opname
);
1939 fprintf (f
, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
1940 opname
, match_opname
, opname
, match_opname
);
1945 /* Generate GIMPLE matching code for the decision tree operand. */
1948 dt_operand::gen_gimple_expr (FILE *f
)
1950 expr
*e
= static_cast<expr
*> (op
);
1951 id_base
*id
= e
->operation
;
1952 unsigned n_ops
= e
->ops
.length ();
1954 for (unsigned i
= 0; i
< n_ops
; ++i
)
1956 char child_opname
[20];
1957 gen_opname (child_opname
, i
);
1959 if (id
->kind
== id_base::CODE
)
1962 || *id
== REALPART_EXPR
|| *id
== IMAGPART_EXPR
1963 || *id
== BIT_FIELD_REF
|| *id
== VIEW_CONVERT_EXPR
)
1965 /* ??? If this is a memory operation we can't (and should not)
1966 match this. The only sensible operand types are
1967 SSA names and invariants. */
1968 fprintf (f
, "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), %i);\n",
1970 fprintf (f
, "if ((TREE_CODE (%s) == SSA_NAME\n"
1971 "|| is_gimple_min_invariant (%s))\n"
1972 "&& (%s = do_valueize (valueize, %s)))\n"
1973 "{\n", child_opname
, child_opname
, child_opname
,
1978 fprintf (f
, "tree %s = gimple_assign_rhs%u (def_stmt);\n",
1979 child_opname
, i
+ 1);
1982 fprintf (f
, "tree %s = gimple_call_arg (def_stmt, %u);\n",
1984 fprintf (f
, "if ((%s = do_valueize (valueize, %s)))\n",
1985 child_opname
, child_opname
);
1992 /* Generate GENERIC matching code for the decision tree operand. */
1995 dt_operand::gen_generic_expr (FILE *f
, const char *opname
)
1997 expr
*e
= static_cast<expr
*> (op
);
1998 unsigned n_ops
= e
->ops
.length ();
2000 for (unsigned i
= 0; i
< n_ops
; ++i
)
2002 char child_opname
[20];
2003 gen_opname (child_opname
, i
);
2005 if (e
->operation
->kind
== id_base::CODE
)
2006 fprintf (f
, "tree %s = TREE_OPERAND (%s, %u);\n",
2007 child_opname
, opname
, i
);
2009 fprintf (f
, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2010 child_opname
, opname
, i
);
2016 /* Generate matching code for the children of the decision tree node. */
2019 dt_node::gen_kids (FILE *f
, bool gimple
)
2021 auto_vec
<dt_operand
*> gimple_exprs
;
2022 auto_vec
<dt_operand
*> generic_exprs
;
2023 auto_vec
<dt_operand
*> fns
;
2024 auto_vec
<dt_operand
*> generic_fns
;
2025 auto_vec
<dt_operand
*> preds
;
2026 auto_vec
<dt_node
*> others
;
2028 for (unsigned i
= 0; i
< kids
.length (); ++i
)
2030 if (kids
[i
]->type
== dt_node::DT_OPERAND
)
2032 dt_operand
*op
= as_a
<dt_operand
*> (kids
[i
]);
2033 if (expr
*e
= dyn_cast
<expr
*> (op
->op
))
2035 if (e
->ops
.length () == 0
2036 && (!gimple
|| !(*e
->operation
== CONSTRUCTOR
)))
2037 generic_exprs
.safe_push (op
);
2038 else if (e
->operation
->kind
== id_base::FN
)
2043 generic_fns
.safe_push (op
);
2045 else if (e
->operation
->kind
== id_base::PREDICATE
)
2046 preds
.safe_push (op
);
2050 gimple_exprs
.safe_push (op
);
2052 generic_exprs
.safe_push (op
);
2055 else if (op
->op
->type
== operand::OP_PREDICATE
)
2056 others
.safe_push (kids
[i
]);
2060 else if (kids
[i
]->type
== dt_node::DT_MATCH
2061 || kids
[i
]->type
== dt_node::DT_SIMPLIFY
)
2062 others
.safe_push (kids
[i
]);
2063 else if (kids
[i
]->type
== dt_node::DT_TRUE
)
2065 /* A DT_TRUE operand serves as a barrier - generate code now
2066 for what we have collected sofar. */
2067 gen_kids_1 (f
, gimple
, gimple_exprs
, generic_exprs
,
2068 fns
, generic_fns
, preds
, others
);
2069 /* And output the true operand itself. */
2070 kids
[i
]->gen (f
, gimple
);
2071 gimple_exprs
.truncate (0);
2072 generic_exprs
.truncate (0);
2074 generic_fns
.truncate (0);
2076 others
.truncate (0);
2082 /* Generate code for the remains. */
2083 gen_kids_1 (f
, gimple
, gimple_exprs
, generic_exprs
,
2084 fns
, generic_fns
, preds
, others
);
2087 /* Generate matching code for the children of the decision tree node. */
2090 dt_node::gen_kids_1 (FILE *f
, bool gimple
,
2091 vec
<dt_operand
*> gimple_exprs
,
2092 vec
<dt_operand
*> generic_exprs
,
2093 vec
<dt_operand
*> fns
,
2094 vec
<dt_operand
*> generic_fns
,
2095 vec
<dt_operand
*> preds
,
2096 vec
<dt_node
*> others
)
2099 char *kid_opname
= buf
;
2101 unsigned exprs_len
= gimple_exprs
.length ();
2102 unsigned gexprs_len
= generic_exprs
.length ();
2103 unsigned fns_len
= fns
.length ();
2104 unsigned gfns_len
= generic_fns
.length ();
2106 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2109 gimple_exprs
[0]->get_name (kid_opname
);
2111 fns
[0]->get_name (kid_opname
);
2113 generic_fns
[0]->get_name (kid_opname
);
2115 generic_exprs
[0]->get_name (kid_opname
);
2117 fprintf (f
, "switch (TREE_CODE (%s))\n"
2121 if (exprs_len
|| fns_len
)
2123 fprintf (f
, "case SSA_NAME:\n");
2124 fprintf (f
, "if (do_valueize (valueize, %s) != NULL_TREE)\n", kid_opname
);
2126 fprintf (f
, "gimple def_stmt = SSA_NAME_DEF_STMT (%s);\n", kid_opname
);
2130 fprintf (f
, "if (is_gimple_assign (def_stmt))\n");
2131 fprintf (f
, "switch (gimple_assign_rhs_code (def_stmt))\n"
2133 for (unsigned i
= 0; i
< exprs_len
; ++i
)
2135 expr
*e
= as_a
<expr
*> (gimple_exprs
[i
]->op
);
2136 id_base
*op
= e
->operation
;
2137 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2138 fprintf (f
, "CASE_CONVERT:\n");
2140 fprintf (f
, "case %s:\n", op
->id
);
2142 gimple_exprs
[i
]->gen (f
, true);
2143 fprintf (f
, "break;\n"
2146 fprintf (f
, "default:;\n"
2153 fprintf (f
, "else ");
2155 fprintf (f
, "if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL))\n"
2157 "tree fndecl = gimple_call_fndecl (def_stmt);\n"
2158 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2161 for (unsigned i
= 0; i
< fns_len
; ++i
)
2163 expr
*e
= as_a
<expr
*>(fns
[i
]->op
);
2164 fprintf (f
, "case %s:\n"
2165 "{\n", e
->operation
->id
);
2166 fns
[i
]->gen (f
, true);
2167 fprintf (f
, "break;\n"
2171 fprintf (f
, "default:;\n"
2180 for (unsigned i
= 0; i
< generic_exprs
.length (); ++i
)
2182 expr
*e
= as_a
<expr
*>(generic_exprs
[i
]->op
);
2183 id_base
*op
= e
->operation
;
2184 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2185 fprintf (f
, "CASE_CONVERT:\n");
2187 fprintf (f
, "case %s:\n", op
->id
);
2189 generic_exprs
[i
]->gen (f
, gimple
);
2190 fprintf (f
, "break;\n"
2196 fprintf (f
, "case CALL_EXPR:\n"
2198 "tree fndecl = get_callee_fndecl (%s);\n"
2199 "if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)\n"
2200 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2203 for (unsigned j
= 0; j
< generic_fns
.length (); ++j
)
2205 expr
*e
= as_a
<expr
*>(generic_fns
[j
]->op
);
2206 gcc_assert (e
->operation
->kind
== id_base::FN
);
2208 fprintf (f
, "case %s:\n"
2209 "{\n", e
->operation
->id
);
2210 generic_fns
[j
]->gen (f
, false);
2211 fprintf (f
, "break;\n"
2215 fprintf (f
, "default:;\n"
2221 /* Close switch (TREE_CODE ()). */
2222 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2223 fprintf (f
, "default:;\n"
2226 for (unsigned i
= 0; i
< preds
.length (); ++i
)
2228 expr
*e
= as_a
<expr
*> (preds
[i
]->op
);
2229 predicate_id
*p
= as_a
<predicate_id
*> (e
->operation
);
2230 preds
[i
]->get_name (kid_opname
);
2231 fprintf (f
, "tree %s_pops[%d];\n", kid_opname
, p
->nargs
);
2232 fprintf (f
, "if (%s_%s (%s, %s_pops%s))\n",
2233 gimple
? "gimple" : "tree",
2234 p
->id
, kid_opname
, kid_opname
,
2235 gimple
? ", valueize" : "");
2237 for (int j
= 0; j
< p
->nargs
; ++j
)
2239 char child_opname
[20];
2240 preds
[i
]->gen_opname (child_opname
, j
);
2241 fprintf (f
, "tree %s = %s_pops[%d];\n", child_opname
, kid_opname
, j
);
2243 preds
[i
]->gen_kids (f
, gimple
);
2247 for (unsigned i
= 0; i
< others
.length (); ++i
)
2248 others
[i
]->gen (f
, gimple
);
2251 /* Generate matching code for the decision tree operand. */
2254 dt_operand::gen (FILE *f
, bool gimple
)
2259 unsigned n_braces
= 0;
2261 if (type
== DT_OPERAND
)
2264 case operand::OP_PREDICATE
:
2265 n_braces
= gen_predicate (f
, opname
, gimple
);
2268 case operand::OP_EXPR
:
2270 n_braces
= gen_gimple_expr (f
);
2272 n_braces
= gen_generic_expr (f
, opname
);
2278 else if (type
== DT_TRUE
)
2280 else if (type
== DT_MATCH
)
2281 n_braces
= gen_match_op (f
, opname
);
2285 gen_kids (f
, gimple
);
2287 for (unsigned i
= 0; i
< n_braces
; ++i
)
2293 /* Generate code for the '(if ...)', '(with ..)' and actual transform
2294 step of a '(simplify ...)' or '(match ...)'. This handles everything
2295 that is not part of the decision tree (simplify->match). */
2298 dt_simplify::gen (FILE *f
, bool gimple
)
2301 output_line_directive (f
, s
->result_location
);
2302 if (s
->capture_max
>= 0)
2303 fprintf (f
, "tree captures[%u] ATTRIBUTE_UNUSED = {};\n",
2304 s
->capture_max
+ 1);
2306 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2310 fprintf (f
, "captures[%u] = %s;\n", i
, indexes
[i
]->get_name (opname
));
2313 unsigned n_braces
= 0;
2314 if (s
->ifexpr_vec
!= vNULL
)
2316 for (unsigned i
= 0; i
< s
->ifexpr_vec
.length (); ++i
)
2318 if_or_with
&w
= s
->ifexpr_vec
[i
];
2322 output_line_directive (f
, w
.location
);
2323 w
.cexpr
->gen_transform (f
, NULL
, true, 1, "type", NULL
);
2328 output_line_directive (f
, w
.location
);
2329 fprintf (f
, "if (");
2330 if (i
== s
->ifexpr_vec
.length () - 1
2331 || s
->ifexpr_vec
[i
+1].is_with
)
2332 w
.cexpr
->gen_transform (f
, NULL
, true, 1, "type", NULL
);
2341 output_line_directive (f
, s
->ifexpr_vec
[j
].location
);
2345 s
->ifexpr_vec
[j
].cexpr
->gen_transform (f
, NULL
,
2351 while (j
< s
->ifexpr_vec
.length ()
2352 && !s
->ifexpr_vec
[j
].is_with
);
2362 /* Analyze captures and perform early-outs on the incoming arguments
2363 that cover cases we cannot handle. */
2364 capture_info
cinfo (s
);
2368 && !((e
= dyn_cast
<expr
*> (s
->result
))
2369 && is_a
<predicate_id
*> (e
->operation
)))
2371 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
2372 if (cinfo
.force_no_side_effects
& (1 << i
))
2373 fprintf (f
, "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n", i
);
2374 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2375 if (cinfo
.info
[i
].cse_p
)
2377 else if (cinfo
.info
[i
].force_no_side_effects_p
2378 && (cinfo
.info
[i
].toplevel_msk
2379 & cinfo
.force_no_side_effects
) == 0)
2380 fprintf (f
, "if (TREE_SIDE_EFFECTS (captures[%d])) "
2381 "return NULL_TREE;\n", i
);
2382 else if ((cinfo
.info
[i
].toplevel_msk
2383 & cinfo
.force_no_side_effects
) != 0)
2384 /* Mark capture as having no side-effects if we had to verify
2385 that via forced toplevel operand checks. */
2386 cinfo
.info
[i
].force_no_side_effects_p
= true;
2389 fprintf (f
, "if (dump_file && (dump_flags & TDF_DETAILS)) "
2390 "fprintf (dump_file, \"Applying pattern ");
2391 output_line_directive (f
, s
->result_location
, true);
2392 fprintf (f
, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
2394 operand
*result
= s
->result
;
2397 /* If there is no result then this is a predicate implementation. */
2398 fprintf (f
, "return true;\n");
2402 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
2403 in outermost position). */
2404 if (result
->type
== operand::OP_EXPR
2405 && *as_a
<expr
*> (result
)->operation
== NON_LVALUE_EXPR
)
2406 result
= as_a
<expr
*> (result
)->ops
[0];
2407 if (result
->type
== operand::OP_EXPR
)
2409 expr
*e
= as_a
<expr
*> (result
);
2410 bool is_predicate
= is_a
<predicate_id
*> (e
->operation
);
2412 fprintf (f
, "*res_code = %s;\n",
2413 *e
->operation
== CONVERT_EXPR
2414 ? "NOP_EXPR" : e
->operation
->id
);
2415 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2418 snprintf (dest
, 32, " res_ops[%d]", j
);
2420 = get_operand_type (e
->operation
,
2421 "type", e
->expr_type
,
2423 ? NULL
: "TREE_TYPE (res_ops[0])");
2424 /* We need to expand GENERIC conditions we captured from
2426 bool expand_generic_cond_exprs_p
2428 /* But avoid doing that if the GENERIC condition is
2429 valid - which it is in the first operand of COND_EXPRs
2430 and VEC_COND_EXRPs. */
2431 && ((!(*e
->operation
== COND_EXPR
)
2432 && !(*e
->operation
== VEC_COND_EXPR
))
2434 e
->ops
[j
]->gen_transform (f
, dest
, true, 1, optype
, &cinfo
,
2435 indexes
, expand_generic_cond_exprs_p
);
2438 /* Re-fold the toplevel result. It's basically an embedded
2439 gimple_build w/o actually building the stmt. */
2441 fprintf (f
, "gimple_resimplify%d (seq, res_code, type, "
2442 "res_ops, valueize);\n", e
->ops
.length ());
2444 else if (result
->type
== operand::OP_CAPTURE
2445 || result
->type
== operand::OP_C_EXPR
)
2447 result
->gen_transform (f
, "res_ops[0]", true, 1, "type",
2448 &cinfo
, indexes
, false);
2449 fprintf (f
, "*res_code = TREE_CODE (res_ops[0]);\n");
2450 if (is_a
<capture
*> (result
)
2451 && cinfo
.info
[as_a
<capture
*> (result
)->where
].cond_expr_cond_p
)
2453 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2454 with substituting a capture of that. */
2455 fprintf (f
, "if (COMPARISON_CLASS_P (res_ops[0]))\n"
2457 " tree tem = res_ops[0];\n"
2458 " res_ops[0] = TREE_OPERAND (tem, 0);\n"
2459 " res_ops[1] = TREE_OPERAND (tem, 1);\n"
2465 fprintf (f
, "return true;\n");
2469 bool is_predicate
= false;
2470 if (result
->type
== operand::OP_EXPR
)
2472 expr
*e
= as_a
<expr
*> (result
);
2473 is_predicate
= is_a
<predicate_id
*> (e
->operation
);
2474 /* Search for captures used multiple times in the result expression
2475 and dependent on TREE_SIDE_EFFECTS emit a SAVE_EXPR. */
2477 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
2479 if (!cinfo
.info
[i
].force_no_side_effects_p
2480 && cinfo
.info
[i
].result_use_count
> 1)
2481 fprintf (f
, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2482 " captures[%d] = save_expr (captures[%d]);\n",
2485 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2489 snprintf (dest
, 32, "res_ops[%d]", j
);
2492 fprintf (f
, " tree res_op%d;\n", j
);
2493 snprintf (dest
, 32, " res_op%d", j
);
2496 = get_operand_type (e
->operation
,
2497 "type", e
->expr_type
,
2499 ? NULL
: "TREE_TYPE (res_op0)");
2500 e
->ops
[j
]->gen_transform (f
, dest
, false, 1, optype
,
2504 fprintf (f
, "return true;\n");
2507 fprintf (f
, " tree res;\n");
2508 /* Re-fold the toplevel result. Use non_lvalue to
2509 build NON_LVALUE_EXPRs so they get properly
2510 ignored when in GIMPLE form. */
2511 if (*e
->operation
== NON_LVALUE_EXPR
)
2512 fprintf (f
, " res = non_lvalue_loc (loc, res_op0);\n");
2515 if (e
->operation
->kind
== id_base::CODE
)
2516 fprintf (f
, " res = fold_build%d_loc (loc, %s, type",
2518 *e
->operation
== CONVERT_EXPR
2519 ? "NOP_EXPR" : e
->operation
->id
);
2521 fprintf (f
, " res = build_call_expr_loc "
2522 "(loc, builtin_decl_implicit (%s), %d",
2523 e
->operation
->id
, e
->ops
.length());
2524 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2525 fprintf (f
, ", res_op%d", j
);
2526 fprintf (f
, ");\n");
2530 else if (result
->type
== operand::OP_CAPTURE
2531 || result
->type
== operand::OP_C_EXPR
)
2534 fprintf (f
, " tree res;\n");
2535 s
->result
->gen_transform (f
, " res", false, 1, "type",
2542 /* Search for captures not used in the result expression and dependent
2543 on TREE_SIDE_EFFECTS emit omit_one_operand. */
2544 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
2546 if (!cinfo
.info
[i
].force_no_side_effects_p
2547 && !cinfo
.info
[i
].expr_p
2548 && cinfo
.info
[i
].result_use_count
== 0)
2549 fprintf (f
, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2550 " res = build2_loc (loc, COMPOUND_EXPR, type,"
2551 " fold_ignored_result (captures[%d]), res);\n",
2554 fprintf (f
, " return res;\n");
2558 for (unsigned i
= 0; i
< n_braces
; ++i
)
2564 /* Main entry to generate code for matching GIMPLE IL off the decision
2568 decision_tree::gen_gimple (FILE *f
)
2570 for (unsigned n
= 1; n
<= 3; ++n
)
2572 fprintf (f
, "\nstatic bool\n"
2573 "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
2574 " gimple_seq *seq, tree (*valueize)(tree),\n"
2575 " code_helper code, tree type");
2576 for (unsigned i
= 0; i
< n
; ++i
)
2577 fprintf (f
, ", tree op%d", i
);
2581 fprintf (f
, "switch (code.get_rep())\n"
2583 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
2585 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
2586 expr
*e
= static_cast<expr
*>(dop
->op
);
2587 if (e
->ops
.length () != n
)
2590 if (*e
->operation
== CONVERT_EXPR
2591 || *e
->operation
== NOP_EXPR
)
2592 fprintf (f
, "CASE_CONVERT:\n");
2594 fprintf (f
, "case %s%s:\n",
2595 is_a
<fn_id
*> (e
->operation
) ? "-" : "",
2598 dop
->gen_kids (f
, true);
2599 fprintf (f
, "break;\n");
2602 fprintf (f
, "default:;\n"
2605 fprintf (f
, "return false;\n");
2610 /* Main entry to generate code for matching GENERIC IL off the decision
2614 decision_tree::gen_generic (FILE *f
)
2616 for (unsigned n
= 1; n
<= 3; ++n
)
2618 fprintf (f
, "\ntree\n"
2619 "generic_simplify (location_t loc, enum tree_code code, "
2620 "tree type ATTRIBUTE_UNUSED");
2621 for (unsigned i
= 0; i
< n
; ++i
)
2622 fprintf (f
, ", tree op%d", i
);
2626 fprintf (f
, "switch (code)\n"
2628 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
2630 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
2631 expr
*e
= static_cast<expr
*>(dop
->op
);
2632 if (e
->ops
.length () != n
2633 /* Builtin simplifications are somewhat premature on
2634 GENERIC. The following drops patterns with outermost
2635 calls. It's easy to emit overloads for function code
2636 though if necessary. */
2637 || e
->operation
->kind
!= id_base::CODE
)
2640 operator_id
*op_id
= static_cast <operator_id
*> (e
->operation
);
2641 if (op_id
->code
== NOP_EXPR
|| op_id
->code
== CONVERT_EXPR
)
2642 fprintf (f
, "CASE_CONVERT:\n");
2644 fprintf (f
, "case %s:\n", e
->operation
->id
);
2646 dop
->gen_kids (f
, false);
2647 fprintf (f
, "break;\n"
2650 fprintf (f
, "default:;\n"
2653 fprintf (f
, "return NULL_TREE;\n");
2658 /* Output code to implement the predicate P from the decision tree DT. */
2661 write_predicate (FILE *f
, predicate_id
*p
, decision_tree
&dt
, bool gimple
)
2663 fprintf (f
, "\nbool\n"
2664 "%s%s (tree t%s%s)\n"
2665 "{\n", gimple
? "gimple_" : "tree_", p
->id
,
2666 p
->nargs
> 0 ? ", tree *res_ops" : "",
2667 gimple
? ", tree (*valueize)(tree)" : "");
2668 /* Conveniently make 'type' available. */
2669 fprintf (f
, "tree type = TREE_TYPE (t);\n");
2672 fprintf (f
, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
2673 dt
.root
->gen_kids (f
, gimple
);
2675 fprintf (f
, "return false;\n"
2679 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
2682 write_header (FILE *f
, const char *head
)
2684 fprintf (f
, "/* Generated automatically by the program `genmatch' from\n");
2685 fprintf (f
, " a IL pattern matching and simplification description. */\n");
2687 /* Include the header instead of writing it awkwardly quoted here. */
2688 fprintf (f
, "\n#include \"%s\"\n", head
);
2698 parser (cpp_reader
*);
2701 const cpp_token
*next ();
2702 const cpp_token
*peek ();
2703 const cpp_token
*peek_ident (const char * = NULL
);
2704 const cpp_token
*expect (enum cpp_ttype
);
2705 void eat_token (enum cpp_ttype
);
2706 const char *get_string ();
2707 const char *get_ident ();
2708 void eat_ident (const char *);
2709 const char *get_number ();
2711 id_base
*parse_operation ();
2712 operand
*parse_capture (operand
*);
2713 operand
*parse_expr ();
2714 c_expr
*parse_c_expr (cpp_ttype
);
2715 operand
*parse_op ();
2717 void record_operlist (source_location
, user_id
*);
2719 void parse_pattern ();
2720 void push_simplify (vec
<simplify
*>&, operand
*, source_location
,
2721 operand
*, source_location
);
2722 void parse_simplify (source_location
, vec
<simplify
*>&, predicate_id
*,
2724 void parse_for (source_location
);
2725 void parse_if (source_location
);
2726 void parse_predicates (source_location
);
2727 void parse_operator_list (source_location
);
2730 vec
<if_or_with
> active_ifs
;
2731 vec
<vec
<user_id
*> > active_fors
;
2732 hash_set
<user_id
*> *oper_lists_set
;
2733 vec
<user_id
*> oper_lists
;
2735 cid_map_t
*capture_ids
;
2738 vec
<simplify
*> simplifiers
;
2739 vec
<predicate_id
*> user_predicates
;
2740 bool parsing_match_operand
;
2743 /* Lexing helpers. */
2745 /* Read the next non-whitespace token from R. */
2750 const cpp_token
*token
;
2753 token
= cpp_get_token (r
);
2755 while (token
->type
== CPP_PADDING
2756 && token
->type
!= CPP_EOF
);
2760 /* Peek at the next non-whitespace token from R. */
2765 const cpp_token
*token
;
2769 token
= cpp_peek_token (r
, i
++);
2771 while (token
->type
== CPP_PADDING
2772 && token
->type
!= CPP_EOF
);
2773 /* If we peek at EOF this is a fatal error as it leaves the
2774 cpp_reader in unusable state. Assume we really wanted a
2775 token and thus this EOF is unexpected. */
2776 if (token
->type
== CPP_EOF
)
2777 fatal_at (token
, "unexpected end of file");
2781 /* Peek at the next identifier token (or return NULL if the next
2782 token is not an identifier or equal to ID if supplied). */
2785 parser::peek_ident (const char *id
)
2787 const cpp_token
*token
= peek ();
2788 if (token
->type
!= CPP_NAME
)
2794 const char *t
= (const char *) CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
2795 if (strcmp (id
, t
) == 0)
2801 /* Read the next token from R and assert it is of type TK. */
2804 parser::expect (enum cpp_ttype tk
)
2806 const cpp_token
*token
= next ();
2807 if (token
->type
!= tk
)
2808 fatal_at (token
, "expected %s, got %s",
2809 cpp_type2name (tk
, 0), cpp_type2name (token
->type
, 0));
2814 /* Consume the next token from R and assert it is of type TK. */
2817 parser::eat_token (enum cpp_ttype tk
)
2822 /* Read the next token from R and assert it is of type CPP_STRING and
2823 return its value. */
2826 parser::get_string ()
2828 const cpp_token
*token
= expect (CPP_STRING
);
2829 return (const char *)token
->val
.str
.text
;
2832 /* Read the next token from R and assert it is of type CPP_NAME and
2833 return its value. */
2836 parser::get_ident ()
2838 const cpp_token
*token
= expect (CPP_NAME
);
2839 return (const char *)CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
2842 /* Eat an identifier token with value S from R. */
2845 parser::eat_ident (const char *s
)
2847 const cpp_token
*token
= peek ();
2848 const char *t
= get_ident ();
2849 if (strcmp (s
, t
) != 0)
2850 fatal_at (token
, "expected '%s' got '%s'\n", s
, t
);
2853 /* Read the next token from R and assert it is of type CPP_NUMBER and
2854 return its value. */
2857 parser::get_number ()
2859 const cpp_token
*token
= expect (CPP_NUMBER
);
2860 return (const char *)token
->val
.str
.text
;
2864 /* Record an operator-list use for transparent for handling. */
2867 parser::record_operlist (source_location loc
, user_id
*p
)
2869 if (!oper_lists_set
->add (p
))
2871 if (!oper_lists
.is_empty ()
2872 && oper_lists
[0]->substitutes
.length () != p
->substitutes
.length ())
2873 fatal_at (loc
, "User-defined operator list does not have the "
2874 "same number of entries as others used in the pattern");
2875 oper_lists
.safe_push (p
);
2879 /* Parse the operator ID, special-casing convert?, convert1? and
2883 parser::parse_operation ()
2885 const cpp_token
*id_tok
= peek ();
2886 const char *id
= get_ident ();
2887 const cpp_token
*token
= peek ();
2888 if (strcmp (id
, "convert0") == 0)
2889 fatal_at (id_tok
, "use 'convert?' here");
2890 if (token
->type
== CPP_QUERY
2891 && !(token
->flags
& PREV_WHITE
))
2893 if (strcmp (id
, "convert") == 0)
2895 else if (strcmp (id
, "convert1") == 0)
2897 else if (strcmp (id
, "convert2") == 0)
2900 fatal_at (id_tok
, "non-convert operator conditionalized");
2902 if (!parsing_match_operand
)
2903 fatal_at (id_tok
, "conditional convert can only be used in "
2904 "match expression");
2905 eat_token (CPP_QUERY
);
2907 else if (strcmp (id
, "convert1") == 0
2908 || strcmp (id
, "convert2") == 0)
2909 fatal_at (id_tok
, "expected '?' after conditional operator");
2910 id_base
*op
= get_operator (id
);
2912 fatal_at (id_tok
, "unknown operator %s", id
);
2914 user_id
*p
= dyn_cast
<user_id
*> (op
);
2915 if (p
&& p
->is_oper_list
)
2917 if (active_fors
.length() == 0)
2918 record_operlist (id_tok
->src_loc
, p
);
2920 fatal_at (id_tok
, "operator-list %s cannot be exapnded inside 'for'", id
);
2926 capture = '@'<number> */
2929 parser::parse_capture (operand
*op
)
2931 eat_token (CPP_ATSIGN
);
2932 const cpp_token
*token
= peek ();
2933 const char *id
= NULL
;
2934 if (token
->type
== CPP_NUMBER
)
2936 else if (token
->type
== CPP_NAME
)
2939 fatal_at (token
, "expected number or identifier");
2940 unsigned next_id
= capture_ids
->elements ();
2942 unsigned &num
= capture_ids
->get_or_insert (id
, &existed
);
2945 return new capture (num
, op
);
2948 /* Parse an expression
2949 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
2952 parser::parse_expr ()
2954 expr
*e
= new expr (parse_operation ());
2955 const cpp_token
*token
= peek ();
2957 bool is_commutative
= false;
2958 const char *expr_type
= NULL
;
2960 if (token
->type
== CPP_COLON
2961 && !(token
->flags
& PREV_WHITE
))
2963 eat_token (CPP_COLON
);
2965 if (token
->type
== CPP_NAME
2966 && !(token
->flags
& PREV_WHITE
))
2968 const char *s
= get_ident ();
2969 if (s
[0] == 'c' && !s
[1])
2971 if (!parsing_match_operand
)
2973 "flag 'c' can only be used in match expression");
2974 is_commutative
= true;
2976 else if (s
[1] != '\0')
2978 if (parsing_match_operand
)
2979 fatal_at (token
, "type can only be used in result expression");
2983 fatal_at (token
, "flag %s not recognized", s
);
2988 fatal_at (token
, "expected flag or type specifying identifier");
2991 if (token
->type
== CPP_ATSIGN
2992 && !(token
->flags
& PREV_WHITE
))
2993 op
= parse_capture (e
);
2998 const cpp_token
*token
= peek ();
2999 if (token
->type
== CPP_CLOSE_PAREN
)
3001 if (e
->operation
->nargs
!= -1
3002 && e
->operation
->nargs
!= (int) e
->ops
.length ())
3003 fatal_at (token
, "'%s' expects %u operands, not %u",
3004 e
->operation
->id
, e
->operation
->nargs
, e
->ops
.length ());
3007 if (e
->ops
.length () == 2)
3008 e
->is_commutative
= true;
3010 fatal_at (token
, "only binary operators or function with "
3011 "two arguments can be marked commutative");
3013 e
->expr_type
= expr_type
;
3016 e
->append_op (parse_op ());
3021 /* Lex native C code delimited by START recording the preprocessing tokens
3022 for later processing.
3023 c_expr = ('{'|'(') <pp token>... ('}'|')') */
3026 parser::parse_c_expr (cpp_ttype start
)
3028 const cpp_token
*token
;
3031 vec
<cpp_token
> code
= vNULL
;
3032 unsigned nr_stmts
= 0;
3034 if (start
== CPP_OPEN_PAREN
)
3035 end
= CPP_CLOSE_PAREN
;
3036 else if (start
== CPP_OPEN_BRACE
)
3037 end
= CPP_CLOSE_BRACE
;
3045 /* Count brace pairs to find the end of the expr to match. */
3046 if (token
->type
== start
)
3048 else if (token
->type
== end
3052 /* This is a lame way of counting the number of statements. */
3053 if (token
->type
== CPP_SEMICOLON
)
3056 /* If this is possibly a user-defined identifier mark it used. */
3057 if (token
->type
== CPP_NAME
)
3059 id_base
*idb
= get_operator ((const char *)CPP_HASHNODE
3060 (token
->val
.node
.node
)->ident
.str
);
3062 if (idb
&& (p
= dyn_cast
<user_id
*> (idb
)) && p
->is_oper_list
)
3063 record_operlist (token
->src_loc
, p
);
3066 /* Record the token. */
3067 code
.safe_push (*token
);
3070 return new c_expr (r
, code
, nr_stmts
, vNULL
, capture_ids
);
3073 /* Parse an operand which is either an expression, a predicate or
3074 a standalone capture.
3075 op = predicate | expr | c_expr | capture */
3080 const cpp_token
*token
= peek ();
3081 struct operand
*op
= NULL
;
3082 if (token
->type
== CPP_OPEN_PAREN
)
3084 eat_token (CPP_OPEN_PAREN
);
3086 eat_token (CPP_CLOSE_PAREN
);
3088 else if (token
->type
== CPP_OPEN_BRACE
)
3090 op
= parse_c_expr (CPP_OPEN_BRACE
);
3094 /* Remaining ops are either empty or predicates */
3095 if (token
->type
== CPP_NAME
)
3097 const char *id
= get_ident ();
3098 id_base
*opr
= get_operator (id
);
3100 fatal_at (token
, "expected predicate name");
3101 if (operator_id
*code
= dyn_cast
<operator_id
*> (opr
))
3103 if (code
->nargs
!= 0)
3104 fatal_at (token
, "using an operator with operands as predicate");
3105 /* Parse the zero-operand operator "predicates" as
3107 op
= new expr (opr
);
3109 else if (user_id
*code
= dyn_cast
<user_id
*> (opr
))
3111 if (code
->nargs
!= 0)
3112 fatal_at (token
, "using an operator with operands as predicate");
3113 /* Parse the zero-operand operator "predicates" as
3115 op
= new expr (opr
);
3117 else if (predicate_id
*p
= dyn_cast
<predicate_id
*> (opr
))
3118 op
= new predicate (p
);
3120 fatal_at (token
, "using an unsupported operator as predicate");
3121 if (!parsing_match_operand
)
3122 fatal_at (token
, "predicates are only allowed in match expression");
3124 if (token
->flags
& PREV_WHITE
)
3127 else if (token
->type
!= CPP_COLON
3128 && token
->type
!= CPP_ATSIGN
)
3129 fatal_at (token
, "expected expression or predicate");
3130 /* optionally followed by a capture and a predicate. */
3131 if (token
->type
== CPP_COLON
)
3132 fatal_at (token
, "not implemented: predicate on leaf operand");
3133 if (token
->type
== CPP_ATSIGN
)
3134 op
= parse_capture (op
);
3140 /* Create a new simplify from the current parsing state and MATCH,
3141 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
3144 parser::push_simplify (vec
<simplify
*>& simplifiers
,
3145 operand
*match
, source_location match_loc
,
3146 operand
*result
, source_location result_loc
)
3148 /* Build and push a temporary for for operator list uses in expressions. */
3149 if (!oper_lists
.is_empty ())
3150 active_fors
.safe_push (oper_lists
);
3152 simplifiers
.safe_push
3153 (new simplify (match
, match_loc
, result
, result_loc
,
3154 active_ifs
.copy (), active_fors
.copy (), capture_ids
));
3156 if (!oper_lists
.is_empty ())
3161 simplify = 'simplify' <expr> <result-op>
3163 match = 'match' <ident> <expr> [<result-op>]
3165 <result-op> = <op> | <if> | <with>
3166 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
3167 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
3168 and fill SIMPLIFIERS with the results. */
3171 parser::parse_simplify (source_location match_location
,
3172 vec
<simplify
*>& simplifiers
, predicate_id
*matcher
,
3175 /* Reset the capture map. */
3177 capture_ids
= new cid_map_t
;
3178 /* Reset oper_lists and set. */
3179 hash_set
<user_id
*> olist
;
3180 oper_lists_set
= &olist
;
3183 const cpp_token
*loc
= peek ();
3184 parsing_match_operand
= true;
3185 struct operand
*match
= parse_op ();
3186 parsing_match_operand
= false;
3187 if (match
->type
== operand::OP_CAPTURE
&& !matcher
)
3188 fatal_at (loc
, "outermost expression cannot be captured");
3189 if (match
->type
== operand::OP_EXPR
3190 && is_a
<predicate_id
*> (as_a
<expr
*> (match
)->operation
))
3191 fatal_at (loc
, "outermost expression cannot be a predicate");
3193 const cpp_token
*token
= peek ();
3195 /* If this if is immediately closed then it is part of a predicate
3196 definition. Push it. */
3197 if (token
->type
== CPP_CLOSE_PAREN
)
3200 fatal_at (token
, "expected transform expression");
3201 push_simplify (simplifiers
, match
, match_location
,
3202 result
, token
->src_loc
);
3206 unsigned active_ifs_len
= active_ifs
.length ();
3209 if (token
->type
== CPP_OPEN_PAREN
)
3211 source_location paren_loc
= token
->src_loc
;
3212 eat_token (CPP_OPEN_PAREN
);
3213 if (peek_ident ("if"))
3216 active_ifs
.safe_push (if_or_with (parse_c_expr (CPP_OPEN_PAREN
),
3217 token
->src_loc
, false));
3218 /* If this if is immediately closed then it contains a
3219 manual matcher or is part of a predicate definition.
3221 if (peek ()->type
== CPP_CLOSE_PAREN
)
3224 fatal_at (token
, "manual transform not implemented");
3225 push_simplify (simplifiers
, match
, match_location
,
3229 else if (peek_ident ("with"))
3232 /* Parse (with c-expr expr) as (if-with (true) expr). */
3233 c_expr
*e
= parse_c_expr (CPP_OPEN_BRACE
);
3235 active_ifs
.safe_push (if_or_with (e
, token
->src_loc
, true));
3239 operand
*op
= result
;
3242 push_simplify (simplifiers
, match
, match_location
,
3243 op
, token
->src_loc
);
3244 eat_token (CPP_CLOSE_PAREN
);
3245 /* A "default" result closes the enclosing scope. */
3246 if (active_ifs
.length () > active_ifs_len
)
3248 eat_token (CPP_CLOSE_PAREN
);
3255 else if (token
->type
== CPP_CLOSE_PAREN
)
3257 /* Close a scope if requested. */
3258 if (active_ifs
.length () > active_ifs_len
)
3260 eat_token (CPP_CLOSE_PAREN
);
3270 fatal_at (token
, "expected match operand expression");
3271 push_simplify (simplifiers
, match
, match_location
,
3272 matcher
? result
: parse_op (), token
->src_loc
);
3273 /* A "default" result closes the enclosing scope. */
3274 if (active_ifs
.length () > active_ifs_len
)
3276 eat_token (CPP_CLOSE_PAREN
);
3286 /* Parsing of the outer control structures. */
3288 /* Parse a for expression
3289 for = '(' 'for' <subst>... <pattern> ')'
3290 subst = <ident> '(' <ident>... ')' */
3293 parser::parse_for (source_location
)
3295 auto_vec
<const cpp_token
*> user_id_tokens
;
3296 vec
<user_id
*> user_ids
= vNULL
;
3297 const cpp_token
*token
;
3298 unsigned min_n_opers
= 0, max_n_opers
= 0;
3303 if (token
->type
!= CPP_NAME
)
3306 /* Insert the user defined operators into the operator hash. */
3307 const char *id
= get_ident ();
3308 if (get_operator (id
) != NULL
)
3309 fatal_at (token
, "operator already defined");
3310 user_id
*op
= new user_id (id
);
3311 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
3313 user_ids
.safe_push (op
);
3314 user_id_tokens
.safe_push (token
);
3316 eat_token (CPP_OPEN_PAREN
);
3319 while ((token
= peek_ident ()) != 0)
3321 const char *oper
= get_ident ();
3322 id_base
*idb
= get_operator (oper
);
3324 fatal_at (token
, "no such operator '%s'", oper
);
3325 if (*idb
== CONVERT0
|| *idb
== CONVERT1
|| *idb
== CONVERT2
)
3326 fatal_at (token
, "conditional operators cannot be used inside for");
3330 else if (idb
->nargs
== -1)
3332 else if (idb
->nargs
!= arity
)
3333 fatal_at (token
, "operator '%s' with arity %d does not match "
3334 "others with arity %d", oper
, idb
->nargs
, arity
);
3336 user_id
*p
= dyn_cast
<user_id
*> (idb
);
3339 if (p
->is_oper_list
)
3340 op
->substitutes
.safe_splice (p
->substitutes
);
3342 fatal_at (token
, "iterator cannot be used as operator-list");
3345 op
->substitutes
.safe_push (idb
);
3348 token
= expect (CPP_CLOSE_PAREN
);
3350 unsigned nsubstitutes
= op
->substitutes
.length ();
3351 if (nsubstitutes
== 0)
3352 fatal_at (token
, "A user-defined operator must have at least "
3353 "one substitution");
3354 if (max_n_opers
== 0)
3356 min_n_opers
= nsubstitutes
;
3357 max_n_opers
= nsubstitutes
;
3361 if (nsubstitutes
% min_n_opers
!= 0
3362 && min_n_opers
% nsubstitutes
!= 0)
3363 fatal_at (token
, "All user-defined identifiers must have a "
3364 "multiple number of operator substitutions of the "
3365 "smallest number of substitutions");
3366 if (nsubstitutes
< min_n_opers
)
3367 min_n_opers
= nsubstitutes
;
3368 else if (nsubstitutes
> max_n_opers
)
3369 max_n_opers
= nsubstitutes
;
3373 unsigned n_ids
= user_ids
.length ();
3375 fatal_at (token
, "for requires at least one user-defined identifier");
3378 if (token
->type
== CPP_CLOSE_PAREN
)
3379 fatal_at (token
, "no pattern defined in for");
3381 active_fors
.safe_push (user_ids
);
3385 if (token
->type
== CPP_CLOSE_PAREN
)
3391 /* Remove user-defined operators from the hash again. */
3392 for (unsigned i
= 0; i
< user_ids
.length (); ++i
)
3394 if (!user_ids
[i
]->used
)
3395 warning_at (user_id_tokens
[i
],
3396 "operator %s defined but not used", user_ids
[i
]->id
);
3397 operators
->remove_elt (user_ids
[i
]);
3401 /* Parse an identifier associated with a list of operators.
3402 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
3405 parser::parse_operator_list (source_location
)
3407 const cpp_token
*token
= peek ();
3408 const char *id
= get_ident ();
3410 if (get_operator (id
) != 0)
3411 fatal_at (token
, "operator %s already defined", id
);
3413 user_id
*op
= new user_id (id
, true);
3416 while ((token
= peek_ident ()) != 0)
3419 const char *oper
= get_ident ();
3420 id_base
*idb
= get_operator (oper
);
3423 fatal_at (token
, "no such operator '%s'", oper
);
3427 else if (idb
->nargs
== -1)
3429 else if (arity
!= idb
->nargs
)
3430 fatal_at (token
, "operator '%s' with arity %d does not match "
3431 "others with arity %d", oper
, idb
->nargs
, arity
);
3433 /* We allow composition of multiple operator lists. */
3434 if (user_id
*p
= dyn_cast
<user_id
*> (idb
))
3435 op
->substitutes
.safe_splice (p
->substitutes
);
3437 op
->substitutes
.safe_push (idb
);
3440 // Check that there is no junk after id-list
3442 if (token
->type
!= CPP_CLOSE_PAREN
)
3443 fatal_at (token
, "expected identifier got %s", cpp_type2name (token
->type
, 0));
3445 if (op
->substitutes
.length () == 0)
3446 fatal_at (token
, "operator-list cannot be empty");
3449 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
3453 /* Parse an outer if expression.
3454 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
3457 parser::parse_if (source_location loc
)
3459 operand
*ifexpr
= parse_c_expr (CPP_OPEN_PAREN
);
3461 const cpp_token
*token
= peek ();
3462 if (token
->type
== CPP_CLOSE_PAREN
)
3463 fatal_at (token
, "no pattern defined in if");
3465 active_ifs
.safe_push (if_or_with (ifexpr
, loc
, false));
3468 const cpp_token
*token
= peek ();
3469 if (token
->type
== CPP_CLOSE_PAREN
)
3477 /* Parse a list of predefined predicate identifiers.
3478 preds = '(' 'define_predicates' <ident>... ')' */
3481 parser::parse_predicates (source_location
)
3485 const cpp_token
*token
= peek ();
3486 if (token
->type
!= CPP_NAME
)
3489 add_predicate (get_ident ());
3494 /* Parse outer control structures.
3495 pattern = <preds>|<for>|<if>|<simplify>|<match> */
3498 parser::parse_pattern ()
3500 /* All clauses start with '('. */
3501 eat_token (CPP_OPEN_PAREN
);
3502 const cpp_token
*token
= peek ();
3503 const char *id
= get_ident ();
3504 if (strcmp (id
, "simplify") == 0)
3506 parse_simplify (token
->src_loc
, simplifiers
, NULL
, NULL
);
3509 else if (strcmp (id
, "match") == 0)
3511 bool with_args
= false;
3512 if (peek ()->type
== CPP_OPEN_PAREN
)
3514 eat_token (CPP_OPEN_PAREN
);
3517 const char *name
= get_ident ();
3518 id_base
*id
= get_operator (name
);
3522 p
= add_predicate (name
);
3523 user_predicates
.safe_push (p
);
3525 else if ((p
= dyn_cast
<predicate_id
*> (id
)))
3528 fatal_at (token
, "cannot add a match to a non-predicate ID");
3529 /* Parse (match <id> <arg>... (match-expr)) here. */
3533 capture_ids
= new cid_map_t
;
3535 while (peek ()->type
== CPP_ATSIGN
)
3536 e
->append_op (parse_capture (NULL
));
3537 eat_token (CPP_CLOSE_PAREN
);
3540 && ((e
&& e
->ops
.length () != (unsigned)p
->nargs
)
3541 || (!e
&& p
->nargs
!= 0)))
3542 fatal_at (token
, "non-matching number of match operands");
3543 p
->nargs
= e
? e
->ops
.length () : 0;
3544 parse_simplify (token
->src_loc
, p
->matchers
, p
, e
);
3547 else if (strcmp (id
, "for") == 0)
3548 parse_for (token
->src_loc
);
3549 else if (strcmp (id
, "if") == 0)
3550 parse_if (token
->src_loc
);
3551 else if (strcmp (id
, "define_predicates") == 0)
3553 if (active_ifs
.length () > 0
3554 || active_fors
.length () > 0)
3555 fatal_at (token
, "define_predicates inside if or for is not supported");
3556 parse_predicates (token
->src_loc
);
3558 else if (strcmp (id
, "define_operator_list") == 0)
3560 if (active_ifs
.length () > 0
3561 || active_fors
.length () > 0)
3562 fatal_at (token
, "operator-list inside if or for is not supported");
3563 parse_operator_list (token
->src_loc
);
3566 fatal_at (token
, "expected %s'simplify', 'match', 'for' or 'if'",
3567 active_ifs
.length () == 0 && active_fors
.length () == 0
3568 ? "'define_predicates', " : "");
3570 eat_token (CPP_CLOSE_PAREN
);
3573 /* Main entry of the parser. Repeatedly parse outer control structures. */
3575 parser::parser (cpp_reader
*r_
)
3579 active_fors
= vNULL
;
3580 simplifiers
= vNULL
;
3581 oper_lists_set
= NULL
;
3584 user_predicates
= vNULL
;
3585 parsing_match_operand
= false;
3587 const cpp_token
*token
= next ();
3588 while (token
->type
!= CPP_EOF
)
3590 _cpp_backup_tokens (r
, 1);
3597 /* Helper for the linemap code. */
3600 round_alloc_size (size_t s
)
3606 /* The genmatch generator progam. It reads from a pattern description
3607 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
3610 main (int argc
, char **argv
)
3614 progname
= "genmatch";
3620 bool verbose
= false;
3621 char *input
= argv
[argc
-1];
3622 for (int i
= 1; i
< argc
- 1; ++i
)
3624 if (strcmp (argv
[i
], "--gimple") == 0)
3626 else if (strcmp (argv
[i
], "--generic") == 0)
3628 else if (strcmp (argv
[i
], "-v") == 0)
3632 fprintf (stderr
, "Usage: genmatch "
3633 "[--gimple] [--generic] [-v] input\n");
3638 line_table
= XCNEW (struct line_maps
);
3639 linemap_init (line_table
, 0);
3640 line_table
->reallocator
= xrealloc
;
3641 line_table
->round_alloc_size
= round_alloc_size
;
3643 r
= cpp_create_reader (CLK_GNUC99
, NULL
, line_table
);
3644 cpp_callbacks
*cb
= cpp_get_callbacks (r
);
3645 cb
->error
= error_cb
;
3647 if (!cpp_read_main_file (r
, input
))
3649 cpp_define (r
, gimple
? "GIMPLE=1": "GENERIC=1");
3650 cpp_define (r
, gimple
? "GENERIC=0": "GIMPLE=0");
3652 /* Pre-seed operators. */
3653 operators
= new hash_table
<id_base
> (1024);
3654 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
3655 add_operator (SYM, # SYM, # TYPE, NARGS);
3656 #define END_OF_BASE_TREE_CODES
3658 add_operator (CONVERT0
, "CONVERT0", "tcc_unary", 1);
3659 add_operator (CONVERT1
, "CONVERT1", "tcc_unary", 1);
3660 add_operator (CONVERT2
, "CONVERT2", "tcc_unary", 1);
3661 #undef END_OF_BASE_TREE_CODES
3664 /* Pre-seed builtin functions.
3665 ??? Cannot use N (name) as that is targetm.emultls.get_address
3666 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
3667 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
3668 add_builtin (ENUM, # ENUM);
3669 #include "builtins.def"
3676 write_header (stdout
, "gimple-match-head.c");
3678 write_header (stdout
, "generic-match-head.c");
3680 /* Go over all predicates defined with patterns and perform
3681 lowering and code generation. */
3682 for (unsigned i
= 0; i
< p
.user_predicates
.length (); ++i
)
3684 predicate_id
*pred
= p
.user_predicates
[i
];
3685 lower (pred
->matchers
, gimple
);
3688 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
3689 print_matches (pred
->matchers
[i
]);
3692 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
3693 dt
.insert (pred
->matchers
[i
], i
);
3698 write_predicate (stdout
, pred
, dt
, gimple
);
3701 /* Lower the main simplifiers and generate code for them. */
3702 lower (p
.simplifiers
, gimple
);
3705 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
3706 print_matches (p
.simplifiers
[i
]);
3709 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
3710 dt
.insert (p
.simplifiers
[i
], i
);
3716 dt
.gen_gimple (stdout
);
3718 dt
.gen_generic (stdout
);
3721 cpp_finish (r
, NULL
);