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"
30 #include "hash-table.h"
35 /* Stubs for GGC referenced through instantiations triggered by hash-map. */
36 void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
37 size_t, size_t MEM_STAT_DECL
)
41 void ggc_free (void *)
48 static struct line_maps
*line_table
;
51 #if GCC_VERSION >= 4001
52 __attribute__((format (printf
, 6, 0)))
54 error_cb (cpp_reader
*, int errtype
, int, source_location location
,
55 unsigned int, const char *msg
, va_list *ap
)
57 const line_map_ordinary
*map
;
58 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
59 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
60 fprintf (stderr
, "%s:%d:%d %s: ", loc
.file
, loc
.line
, loc
.column
,
61 (errtype
== CPP_DL_WARNING
) ? "warning" : "error");
62 vfprintf (stderr
, msg
, *ap
);
63 fprintf (stderr
, "\n");
64 FILE *f
= fopen (loc
.file
, "r");
70 if (!fgets (buf
, 128, f
))
72 if (buf
[strlen (buf
) - 1] != '\n')
79 fprintf (stderr
, "%s", buf
);
80 for (int i
= 0; i
< loc
.column
- 1; ++i
)
88 if (errtype
== CPP_DL_FATAL
)
94 #if GCC_VERSION >= 4001
95 __attribute__((format (printf
, 2, 3)))
97 fatal_at (const cpp_token
*tk
, const char *msg
, ...)
101 error_cb (NULL
, CPP_DL_FATAL
, 0, tk
->src_loc
, 0, msg
, &ap
);
106 #if GCC_VERSION >= 4001
107 __attribute__((format (printf
, 2, 3)))
109 fatal_at (source_location loc
, const char *msg
, ...)
113 error_cb (NULL
, CPP_DL_FATAL
, 0, loc
, 0, msg
, &ap
);
118 #if GCC_VERSION >= 4001
119 __attribute__((format (printf
, 2, 3)))
121 warning_at (const cpp_token
*tk
, const char *msg
, ...)
125 error_cb (NULL
, CPP_DL_WARNING
, 0, tk
->src_loc
, 0, msg
, &ap
);
130 output_line_directive (FILE *f
, source_location location
,
131 bool dumpfile
= false)
133 const line_map_ordinary
*map
;
134 linemap_resolve_location (line_table
, location
, LRK_SPELLING_LOCATION
, &map
);
135 expanded_location loc
= linemap_expand_location (line_table
, map
, location
);
138 /* When writing to a dumpfile only dump the filename. */
139 const char *file
= strrchr (loc
.file
, DIR_SEPARATOR
);
144 fprintf (f
, "%s:%d", file
, loc
.line
);
147 /* Other gen programs really output line directives here, at least for
148 development it's right now more convenient to have line information
149 from the generated file. Still keep the directives as comment for now
150 to easily back-point to the meta-description. */
151 fprintf (f
, "/* #line %d \"%s\" */\n", loc
.line
, loc
.file
);
155 /* Pull in tree codes and builtin function codes from their
158 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
168 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
169 enum built_in_function
{
170 #include "builtins.def"
176 /* Base class for all identifiers the parser knows. */
178 struct id_base
: typed_noop_remove
<id_base
>
180 enum id_kind
{ CODE
, FN
, PREDICATE
, USER
} kind
;
182 id_base (id_kind
, const char *, int = -1);
188 /* hash_table support. */
189 typedef id_base
*value_type
;
190 typedef id_base
*compare_type
;
191 static inline hashval_t
hash (const id_base
*);
192 static inline int equal (const id_base
*, const id_base
*);
196 id_base::hash (const id_base
*op
)
202 id_base::equal (const id_base
*op1
,
205 return (op1
->hashval
== op2
->hashval
206 && strcmp (op1
->id
, op2
->id
) == 0);
209 /* Hashtable of known pattern operators. This is pre-seeded from
210 all known tree codes and all known builtin function ids. */
211 static hash_table
<id_base
> *operators
;
213 id_base::id_base (id_kind kind_
, const char *id_
, int nargs_
)
218 hashval
= htab_hash_string (id
);
221 /* Identifier that maps to a tree code. */
223 struct operator_id
: public id_base
225 operator_id (enum tree_code code_
, const char *id_
, unsigned nargs_
,
227 : id_base (id_base::CODE
, id_
, nargs_
), code (code_
), tcc (tcc_
) {}
232 /* Identifier that maps to a builtin function code. */
234 struct fn_id
: public id_base
236 fn_id (enum built_in_function fn_
, const char *id_
)
237 : id_base (id_base::FN
, id_
), fn (fn_
) {}
238 enum built_in_function fn
;
243 /* Identifier that maps to a user-defined predicate. */
245 struct predicate_id
: public id_base
247 predicate_id (const char *id_
)
248 : id_base (id_base::PREDICATE
, id_
), matchers (vNULL
) {}
249 vec
<simplify
*> matchers
;
252 /* Identifier that maps to a operator defined by a 'for' directive. */
254 struct user_id
: public id_base
256 user_id (const char *id_
, bool is_oper_list_
= false)
257 : id_base (id_base::USER
, id_
), substitutes (vNULL
),
258 used (false), is_oper_list (is_oper_list_
) {}
259 vec
<id_base
*> substitutes
;
267 is_a_helper
<fn_id
*>::test (id_base
*id
)
269 return id
->kind
== id_base::FN
;
275 is_a_helper
<operator_id
*>::test (id_base
*id
)
277 return id
->kind
== id_base::CODE
;
283 is_a_helper
<predicate_id
*>::test (id_base
*id
)
285 return id
->kind
== id_base::PREDICATE
;
291 is_a_helper
<user_id
*>::test (id_base
*id
)
293 return id
->kind
== id_base::USER
;
296 /* Add a predicate identifier to the hash. */
298 static predicate_id
*
299 add_predicate (const char *id
)
301 predicate_id
*p
= new predicate_id (id
);
302 id_base
**slot
= operators
->find_slot_with_hash (p
, p
->hashval
, INSERT
);
304 fatal ("duplicate id definition");
309 /* Add a tree code identifier to the hash. */
312 add_operator (enum tree_code code
, const char *id
,
313 const char *tcc
, unsigned nargs
)
315 if (strcmp (tcc
, "tcc_unary") != 0
316 && strcmp (tcc
, "tcc_binary") != 0
317 && strcmp (tcc
, "tcc_comparison") != 0
318 && strcmp (tcc
, "tcc_expression") != 0
319 /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
320 && strcmp (tcc
, "tcc_reference") != 0
321 /* To have INTEGER_CST and friends as "predicate operators". */
322 && strcmp (tcc
, "tcc_constant") != 0
323 /* And allow CONSTRUCTOR for vector initializers. */
324 && !(code
== CONSTRUCTOR
))
326 operator_id
*op
= new operator_id (code
, id
, nargs
, tcc
);
327 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
329 fatal ("duplicate id definition");
333 /* Add a builtin identifier to the hash. */
336 add_builtin (enum built_in_function code
, const char *id
)
338 fn_id
*fn
= new fn_id (code
, id
);
339 id_base
**slot
= operators
->find_slot_with_hash (fn
, fn
->hashval
, INSERT
);
341 fatal ("duplicate id definition");
345 /* Helper for easy comparing ID with tree code CODE. */
348 operator==(id_base
&id
, enum tree_code code
)
350 if (operator_id
*oid
= dyn_cast
<operator_id
*> (&id
))
351 return oid
->code
== code
;
355 /* Lookup the identifier ID. */
358 get_operator (const char *id
)
360 id_base
tem (id_base::CODE
, id
);
362 id_base
*op
= operators
->find_with_hash (&tem
, tem
.hashval
);
365 /* If this is a user-defined identifier track whether it was used. */
366 if (user_id
*uid
= dyn_cast
<user_id
*> (op
))
371 /* Try all-uppercase. */
372 char *id2
= xstrdup (id
);
373 for (unsigned i
= 0; i
< strlen (id2
); ++i
)
374 id2
[i
] = TOUPPER (id2
[i
]);
375 new (&tem
) id_base (id_base::CODE
, id2
);
376 op
= operators
->find_with_hash (&tem
, tem
.hashval
);
383 /* Try _EXPR appended. */
384 id2
= (char *)xrealloc (id2
, strlen (id2
) + sizeof ("_EXPR") + 1);
385 strcat (id2
, "_EXPR");
386 new (&tem
) id_base (id_base::CODE
, id2
);
387 op
= operators
->find_with_hash (&tem
, tem
.hashval
);
398 /* Helper for the capture-id map. */
400 struct capture_id_map_hasher
: default_hashmap_traits
402 static inline hashval_t
hash (const char *);
403 static inline bool equal_keys (const char *, const char *);
407 capture_id_map_hasher::hash (const char *id
)
409 return htab_hash_string (id
);
413 capture_id_map_hasher::equal_keys (const char *id1
, const char *id2
)
415 return strcmp (id1
, id2
) == 0;
418 typedef hash_map
<const char *, unsigned, capture_id_map_hasher
> cid_map_t
;
421 /* The AST produced by parsing of the pattern definitions. */
426 /* The base class for operands. */
429 enum op_type
{ OP_PREDICATE
, OP_EXPR
, OP_CAPTURE
, OP_C_EXPR
};
430 operand (enum op_type type_
) : type (type_
) {}
432 virtual void gen_transform (FILE *, const char *, bool, int,
433 const char *, capture_info
*,
436 { gcc_unreachable (); }
439 /* A predicate operand. Predicates are leafs in the AST. */
441 struct predicate
: public operand
443 predicate (predicate_id
*p_
) : operand (OP_PREDICATE
), p (p_
) {}
447 /* An operand that constitutes an expression. Expressions include
448 function calls and user-defined predicate invocations. */
450 struct expr
: public operand
452 expr (id_base
*operation_
, bool is_commutative_
= false)
453 : operand (OP_EXPR
), operation (operation_
),
454 ops (vNULL
), expr_type (NULL
), is_commutative (is_commutative_
),
455 is_generic (false) {}
456 void append_op (operand
*op
) { ops
.safe_push (op
); }
457 /* The operator and its operands. */
460 /* An explicitely specified type - used exclusively for conversions. */
461 const char *expr_type
;
462 /* Whether the operation is to be applied commutatively. This is
463 later lowered to two separate patterns. */
465 /* Whether the expression is expected to be in GENERIC form. */
467 virtual void gen_transform (FILE *f
, const char *, bool, int,
468 const char *, capture_info
*,
469 dt_operand
** = 0, bool = true);
472 /* An operator that is represented by native C code. This is always
473 a leaf operand in the AST. This class is also used to represent
474 the code to be generated for 'if' and 'with' expressions. */
476 struct c_expr
: public operand
478 /* A mapping of an identifier and its replacement. Used to apply
483 id_tab (const char *id_
, const char *oper_
): id (id_
), oper (oper_
) {}
486 c_expr (cpp_reader
*r_
, vec
<cpp_token
> code_
, unsigned nr_stmts_
,
487 vec
<id_tab
> ids_
, cid_map_t
*capture_ids_
)
488 : operand (OP_C_EXPR
), r (r_
), code (code_
), capture_ids (capture_ids_
),
489 nr_stmts (nr_stmts_
), ids (ids_
) {}
490 /* cpplib tokens and state to transform this back to source. */
493 cid_map_t
*capture_ids
;
494 /* The number of statements parsed (well, the number of ';'s). */
496 /* The identifier replacement vector. */
498 virtual void gen_transform (FILE *f
, const char *, bool, int,
499 const char *, capture_info
*,
500 dt_operand
** = 0, bool = true);
503 /* A wrapper around another operand that captures its value. */
505 struct capture
: public operand
507 capture (unsigned where_
, operand
*what_
)
508 : operand (OP_CAPTURE
), where (where_
), what (what_
) {}
509 /* Identifier index for the value. */
511 /* The captured value. */
513 virtual void gen_transform (FILE *f
, const char *, bool, int,
514 const char *, capture_info
*,
515 dt_operand
** = 0, bool = true);
521 is_a_helper
<capture
*>::test (operand
*op
)
523 return op
->type
== operand::OP_CAPTURE
;
529 is_a_helper
<predicate
*>::test (operand
*op
)
531 return op
->type
== operand::OP_PREDICATE
;
537 is_a_helper
<c_expr
*>::test (operand
*op
)
539 return op
->type
== operand::OP_C_EXPR
;
545 is_a_helper
<expr
*>::test (operand
*op
)
547 return op
->type
== operand::OP_EXPR
;
550 /* Helper to distinguish 'if' from 'with' expressions. */
554 if_or_with (operand
*cexpr_
, source_location location_
, bool is_with_
)
555 : location (location_
), cexpr (cexpr_
), is_with (is_with_
) {}
556 source_location location
;
561 /* The main class of a pattern and its transform. This is used to
562 represent both (simplify ...) and (match ...) kinds. The AST
563 duplicates all outer 'if' and 'for' expressions here so each
564 simplify can exist in isolation. */
568 simplify (operand
*match_
, source_location match_location_
,
569 struct operand
*result_
, source_location result_location_
,
570 vec
<if_or_with
> ifexpr_vec_
, vec
<vec
<user_id
*> > for_vec_
,
571 cid_map_t
*capture_ids_
)
572 : match (match_
), match_location (match_location_
),
573 result (result_
), result_location (result_location_
),
574 ifexpr_vec (ifexpr_vec_
), for_vec (for_vec_
),
575 capture_ids (capture_ids_
), capture_max (capture_ids_
->elements () - 1) {}
577 /* The expression that is matched against the GENERIC or GIMPLE IL. */
579 source_location match_location
;
580 /* For a (simplify ...) the expression produced when the pattern applies.
581 For a (match ...) either NULL if it is a simple predicate or the
582 single expression specifying the matched operands. */
583 struct operand
*result
;
584 source_location result_location
;
585 /* Collected 'if' expressions that need to evaluate to true to make
586 the pattern apply. */
587 vec
<if_or_with
> ifexpr_vec
;
588 /* Collected 'for' expression operators that have to be replaced
589 in the lowering phase. */
590 vec
<vec
<user_id
*> > for_vec
;
591 /* A map of capture identifiers to indexes. */
592 cid_map_t
*capture_ids
;
596 /* Debugging routines for dumping the AST. */
599 print_operand (operand
*o
, FILE *f
= stderr
, bool flattened
= false)
601 if (capture
*c
= dyn_cast
<capture
*> (o
))
603 fprintf (f
, "@%u", c
->where
);
604 if (c
->what
&& flattened
== false)
607 print_operand (c
->what
, f
, flattened
);
612 else if (predicate
*p
= dyn_cast
<predicate
*> (o
))
613 fprintf (f
, "%s", p
->p
->id
);
615 else if (is_a
<c_expr
*> (o
))
616 fprintf (f
, "c_expr");
618 else if (expr
*e
= dyn_cast
<expr
*> (o
))
620 fprintf (f
, "(%s", e
->operation
->id
);
622 if (flattened
== false)
625 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
627 print_operand (e
->ops
[i
], f
, flattened
);
639 print_matches (struct simplify
*s
, FILE *f
= stderr
)
641 fprintf (f
, "for expression: ");
642 print_operand (s
->match
, f
);
649 /* Lowering of commutative operators. */
652 cartesian_product (const vec
< vec
<operand
*> >& ops_vector
,
653 vec
< vec
<operand
*> >& result
, vec
<operand
*>& v
, unsigned n
)
655 if (n
== ops_vector
.length ())
657 vec
<operand
*> xv
= v
.copy ();
658 result
.safe_push (xv
);
662 for (unsigned i
= 0; i
< ops_vector
[n
].length (); ++i
)
664 v
[n
] = ops_vector
[n
][i
];
665 cartesian_product (ops_vector
, result
, v
, n
+ 1);
669 /* Lower OP to two operands in case it is marked as commutative. */
671 static vec
<operand
*>
672 commutate (operand
*op
)
674 vec
<operand
*> ret
= vNULL
;
676 if (capture
*c
= dyn_cast
<capture
*> (op
))
683 vec
<operand
*> v
= commutate (c
->what
);
684 for (unsigned i
= 0; i
< v
.length (); ++i
)
686 capture
*nc
= new capture (c
->where
, v
[i
]);
692 expr
*e
= dyn_cast
<expr
*> (op
);
693 if (!e
|| e
->ops
.length () == 0)
699 vec
< vec
<operand
*> > ops_vector
= vNULL
;
700 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
701 ops_vector
.safe_push (commutate (e
->ops
[i
]));
703 auto_vec
< vec
<operand
*> > result
;
704 auto_vec
<operand
*> v (e
->ops
.length ());
705 v
.quick_grow_cleared (e
->ops
.length ());
706 cartesian_product (ops_vector
, result
, v
, 0);
709 for (unsigned i
= 0; i
< result
.length (); ++i
)
711 expr
*ne
= new expr (e
->operation
);
712 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
713 ne
->append_op (result
[i
][j
]);
717 if (!e
->is_commutative
)
720 for (unsigned i
= 0; i
< result
.length (); ++i
)
722 expr
*ne
= new expr (e
->operation
);
723 // result[i].length () is 2 since e->operation is binary
724 for (unsigned j
= result
[i
].length (); j
; --j
)
725 ne
->append_op (result
[i
][j
-1]);
732 /* Lower operations marked as commutative in the AST of S and push
733 the resulting patterns to SIMPLIFIERS. */
736 lower_commutative (simplify
*s
, vec
<simplify
*>& simplifiers
)
738 vec
<operand
*> matchers
= commutate (s
->match
);
739 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
741 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
742 s
->result
, s
->result_location
, s
->ifexpr_vec
,
743 s
->for_vec
, s
->capture_ids
);
744 simplifiers
.safe_push (ns
);
748 /* Strip conditional conversios using operator OPER from O and its
749 children if STRIP, else replace them with an unconditional convert. */
752 lower_opt_convert (operand
*o
, enum tree_code oper
, bool strip
)
754 if (capture
*c
= dyn_cast
<capture
*> (o
))
757 return new capture (c
->where
, lower_opt_convert (c
->what
, oper
, strip
));
762 expr
*e
= dyn_cast
<expr
*> (o
);
766 if (*e
->operation
== oper
)
769 return lower_opt_convert (e
->ops
[0], oper
, strip
);
771 expr
*ne
= new expr (get_operator ("CONVERT_EXPR"));
772 ne
->append_op (lower_opt_convert (e
->ops
[0], oper
, strip
));
776 expr
*ne
= new expr (e
->operation
, e
->is_commutative
);
777 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
778 ne
->append_op (lower_opt_convert (e
->ops
[i
], oper
, strip
));
783 /* Determine whether O or its children uses the conditional conversion
787 has_opt_convert (operand
*o
, enum tree_code oper
)
789 if (capture
*c
= dyn_cast
<capture
*> (o
))
792 return has_opt_convert (c
->what
, oper
);
797 expr
*e
= dyn_cast
<expr
*> (o
);
801 if (*e
->operation
== oper
)
804 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
805 if (has_opt_convert (e
->ops
[i
], oper
))
811 /* Lower conditional convert operators in O, expanding it to a vector
814 static vec
<operand
*>
815 lower_opt_convert (operand
*o
)
817 vec
<operand
*> v1
= vNULL
, v2
;
821 enum tree_code opers
[] = { CONVERT0
, CONVERT1
, CONVERT2
};
823 /* Conditional converts are lowered to a pattern with the
824 conversion and one without. The three different conditional
825 convert codes are lowered separately. */
827 for (unsigned i
= 0; i
< 3; ++i
)
830 for (unsigned j
= 0; j
< v1
.length (); ++j
)
831 if (has_opt_convert (v1
[j
], opers
[i
]))
833 v2
.safe_push (lower_opt_convert (v1
[j
], opers
[i
], false));
834 v2
.safe_push (lower_opt_convert (v1
[j
], opers
[i
], true));
840 for (unsigned j
= 0; j
< v2
.length (); ++j
)
841 v1
.safe_push (v2
[j
]);
848 /* Lower conditional convert operators in the AST of S and push
849 the resulting multiple patterns to SIMPLIFIERS. */
852 lower_opt_convert (simplify
*s
, vec
<simplify
*>& simplifiers
)
854 vec
<operand
*> matchers
= lower_opt_convert (s
->match
);
855 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
857 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
858 s
->result
, s
->result_location
, s
->ifexpr_vec
,
859 s
->for_vec
, s
->capture_ids
);
860 simplifiers
.safe_push (ns
);
864 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
865 GENERIC and a GIMPLE variant. */
867 static vec
<operand
*>
868 lower_cond (operand
*o
)
870 vec
<operand
*> ro
= vNULL
;
872 if (capture
*c
= dyn_cast
<capture
*> (o
))
876 vec
<operand
*> lop
= vNULL
;
877 lop
= lower_cond (c
->what
);
879 for (unsigned i
= 0; i
< lop
.length (); ++i
)
880 ro
.safe_push (new capture (c
->where
, lop
[i
]));
885 expr
*e
= dyn_cast
<expr
*> (o
);
886 if (!e
|| e
->ops
.length () == 0)
892 vec
< vec
<operand
*> > ops_vector
= vNULL
;
893 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
894 ops_vector
.safe_push (lower_cond (e
->ops
[i
]));
896 auto_vec
< vec
<operand
*> > result
;
897 auto_vec
<operand
*> v (e
->ops
.length ());
898 v
.quick_grow_cleared (e
->ops
.length ());
899 cartesian_product (ops_vector
, result
, v
, 0);
901 for (unsigned i
= 0; i
< result
.length (); ++i
)
903 expr
*ne
= new expr (e
->operation
);
904 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
905 ne
->append_op (result
[i
][j
]);
907 /* If this is a COND with a captured expression or an
908 expression with two operands then also match a GENERIC
909 form on the compare. */
910 if ((*e
->operation
== COND_EXPR
911 || *e
->operation
== VEC_COND_EXPR
)
912 && ((is_a
<capture
*> (e
->ops
[0])
913 && as_a
<capture
*> (e
->ops
[0])->what
914 && is_a
<expr
*> (as_a
<capture
*> (e
->ops
[0])->what
)
916 (as_a
<capture
*> (e
->ops
[0])->what
)->ops
.length () == 2)
917 || (is_a
<expr
*> (e
->ops
[0])
918 && as_a
<expr
*> (e
->ops
[0])->ops
.length () == 2)))
920 expr
*ne
= new expr (e
->operation
);
921 for (unsigned j
= 0; j
< result
[i
].length (); ++j
)
922 ne
->append_op (result
[i
][j
]);
923 if (capture
*c
= dyn_cast
<capture
*> (ne
->ops
[0]))
925 expr
*ocmp
= as_a
<expr
*> (c
->what
);
926 expr
*cmp
= new expr (ocmp
->operation
);
927 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
928 cmp
->append_op (ocmp
->ops
[j
]);
929 cmp
->is_generic
= true;
930 ne
->ops
[0] = new capture (c
->where
, cmp
);
934 expr
*ocmp
= as_a
<expr
*> (ne
->ops
[0]);
935 expr
*cmp
= new expr (ocmp
->operation
);
936 for (unsigned j
= 0; j
< ocmp
->ops
.length (); ++j
)
937 cmp
->append_op (ocmp
->ops
[j
]);
938 cmp
->is_generic
= true;
948 /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
949 GENERIC and a GIMPLE variant. */
952 lower_cond (simplify
*s
, vec
<simplify
*>& simplifiers
)
954 vec
<operand
*> matchers
= lower_cond (s
->match
);
955 for (unsigned i
= 0; i
< matchers
.length (); ++i
)
957 simplify
*ns
= new simplify (matchers
[i
], s
->match_location
,
958 s
->result
, s
->result_location
, s
->ifexpr_vec
,
959 s
->for_vec
, s
->capture_ids
);
960 simplifiers
.safe_push (ns
);
964 /* In AST operand O replace operator ID with operator WITH. */
967 replace_id (operand
*o
, user_id
*id
, id_base
*with
)
969 /* Deep-copy captures and expressions, replacing operations as
971 if (capture
*c
= dyn_cast
<capture
*> (o
))
975 return new capture (c
->where
, replace_id (c
->what
, id
, with
));
977 else if (expr
*e
= dyn_cast
<expr
*> (o
))
979 expr
*ne
= new expr (e
->operation
== id
? with
: e
->operation
,
981 ne
->expr_type
= e
->expr_type
;
982 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
983 ne
->append_op (replace_id (e
->ops
[i
], id
, with
));
987 /* For c_expr we simply record a string replacement table which is
988 applied at code-generation time. */
989 if (c_expr
*ce
= dyn_cast
<c_expr
*> (o
))
991 vec
<c_expr::id_tab
> ids
= ce
->ids
.copy ();
992 ids
.safe_push (c_expr::id_tab (id
->id
, with
->id
));
993 return new c_expr (ce
->r
, ce
->code
, ce
->nr_stmts
, ids
, ce
->capture_ids
);
999 /* Lower recorded fors for SIN and output to SIMPLIFIERS. */
1002 lower_for (simplify
*sin
, vec
<simplify
*>& simplifiers
)
1004 vec
<vec
<user_id
*> >& for_vec
= sin
->for_vec
;
1005 unsigned worklist_start
= 0;
1006 auto_vec
<simplify
*> worklist
;
1007 worklist
.safe_push (sin
);
1009 /* Lower each recorded for separately, operating on the
1010 set of simplifiers created by the previous one.
1011 Lower inner-to-outer so inner for substitutes can refer
1012 to operators replaced by outer fors. */
1013 for (int fi
= for_vec
.length () - 1; fi
>= 0; --fi
)
1015 vec
<user_id
*>& ids
= for_vec
[fi
];
1016 unsigned n_ids
= ids
.length ();
1017 unsigned max_n_opers
= 0;
1018 for (unsigned i
= 0; i
< n_ids
; ++i
)
1019 if (ids
[i
]->substitutes
.length () > max_n_opers
)
1020 max_n_opers
= ids
[i
]->substitutes
.length ();
1022 unsigned worklist_end
= worklist
.length ();
1023 for (unsigned si
= worklist_start
; si
< worklist_end
; ++si
)
1025 simplify
*s
= worklist
[si
];
1026 for (unsigned j
= 0; j
< max_n_opers
; ++j
)
1028 operand
*match_op
= s
->match
;
1029 operand
*result_op
= s
->result
;
1030 vec
<if_or_with
> ifexpr_vec
= s
->ifexpr_vec
.copy ();
1032 for (unsigned i
= 0; i
< n_ids
; ++i
)
1034 user_id
*id
= ids
[i
];
1035 id_base
*oper
= id
->substitutes
[j
% id
->substitutes
.length ()];
1036 match_op
= replace_id (match_op
, id
, oper
);
1038 result_op
= replace_id (result_op
, id
, oper
);
1039 for (unsigned k
= 0; k
< s
->ifexpr_vec
.length (); ++k
)
1040 ifexpr_vec
[k
].cexpr
= replace_id (ifexpr_vec
[k
].cexpr
,
1043 simplify
*ns
= new simplify (match_op
, s
->match_location
,
1044 result_op
, s
->result_location
,
1045 ifexpr_vec
, vNULL
, s
->capture_ids
);
1046 worklist
.safe_push (ns
);
1049 worklist_start
= worklist_end
;
1052 /* Copy out the result from the last for lowering. */
1053 for (unsigned i
= worklist_start
; i
< worklist
.length (); ++i
)
1054 simplifiers
.safe_push (worklist
[i
]);
1057 /* Lower the AST for everything in SIMPLIFIERS. */
1060 lower (vec
<simplify
*>& simplifiers
, bool gimple
)
1062 auto_vec
<simplify
*> out_simplifiers
;
1063 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1064 lower_opt_convert (simplifiers
[i
], out_simplifiers
);
1066 simplifiers
.truncate (0);
1067 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1068 lower_commutative (out_simplifiers
[i
], simplifiers
);
1070 out_simplifiers
.truncate (0);
1072 for (unsigned i
= 0; i
< simplifiers
.length (); ++i
)
1073 lower_cond (simplifiers
[i
], out_simplifiers
);
1075 out_simplifiers
.safe_splice (simplifiers
);
1078 simplifiers
.truncate (0);
1079 for (unsigned i
= 0; i
< out_simplifiers
.length (); ++i
)
1080 lower_for (out_simplifiers
[i
], simplifiers
);
1086 /* The decision tree built for generating GIMPLE and GENERIC pattern
1087 matching code. It represents the 'match' expression of all
1088 simplifies and has those as its leafs. */
1090 /* Decision tree base class, used for DT_TRUE and DT_NODE. */
1094 enum dt_type
{ DT_NODE
, DT_OPERAND
, DT_TRUE
, DT_MATCH
, DT_SIMPLIFY
};
1098 vec
<dt_node
*> kids
;
1100 dt_node (enum dt_type type_
): type (type_
), level (0), kids (vNULL
) {}
1102 dt_node
*append_node (dt_node
*);
1103 dt_node
*append_op (operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1104 dt_node
*append_true_op (dt_node
*parent
= 0, unsigned pos
= 0);
1105 dt_node
*append_match_op (dt_operand
*, dt_node
*parent
= 0, unsigned pos
= 0);
1106 dt_node
*append_simplify (simplify
*, unsigned, dt_operand
**);
1108 virtual void gen (FILE *, bool) {}
1110 void gen_kids (FILE *, bool);
1111 void gen_kids_1 (FILE *, bool,
1112 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_operand
*>,
1113 vec
<dt_operand
*>, vec
<dt_operand
*>, vec
<dt_node
*>);
1116 /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */
1118 struct dt_operand
: public dt_node
1121 dt_operand
*match_dop
;
1125 dt_operand (enum dt_type type
, operand
*op_
, dt_operand
*match_dop_
,
1126 dt_operand
*parent_
= 0, unsigned pos_
= 0)
1127 : dt_node (type
), op (op_
), match_dop (match_dop_
),
1128 parent (parent_
), pos (pos_
) {}
1130 void gen (FILE *, bool);
1131 unsigned gen_predicate (FILE *, const char *, bool);
1132 unsigned gen_match_op (FILE *, const char *);
1134 unsigned gen_gimple_expr (FILE *);
1135 unsigned gen_generic_expr (FILE *, const char *);
1137 char *get_name (char *);
1138 void gen_opname (char *, unsigned);
1141 /* Leaf node of the decision tree, used for DT_SIMPLIFY. */
1143 struct dt_simplify
: public dt_node
1146 unsigned pattern_no
;
1147 dt_operand
**indexes
;
1149 dt_simplify (simplify
*s_
, unsigned pattern_no_
, dt_operand
**indexes_
)
1150 : dt_node (DT_SIMPLIFY
), s (s_
), pattern_no (pattern_no_
),
1151 indexes (indexes_
) {}
1153 void gen (FILE *f
, bool);
1159 is_a_helper
<dt_operand
*>::test (dt_node
*n
)
1161 return (n
->type
== dt_node::DT_OPERAND
1162 || n
->type
== dt_node::DT_MATCH
);
1165 /* A container for the actual decision tree. */
1167 struct decision_tree
1171 void insert (struct simplify
*, unsigned);
1172 void gen_gimple (FILE *f
= stderr
);
1173 void gen_generic (FILE *f
= stderr
);
1174 void print (FILE *f
= stderr
);
1176 decision_tree () { root
= new dt_node (dt_node::DT_NODE
); }
1178 static dt_node
*insert_operand (dt_node
*, operand
*, dt_operand
**indexes
,
1179 unsigned pos
= 0, dt_node
*parent
= 0);
1180 static dt_node
*find_node (vec
<dt_node
*>&, dt_node
*);
1181 static bool cmp_node (dt_node
*, dt_node
*);
1182 static void print_node (dt_node
*, FILE *f
= stderr
, unsigned = 0);
1185 /* Compare two AST operands O1 and O2 and return true if they are equal. */
1188 cmp_operand (operand
*o1
, operand
*o2
)
1190 if (!o1
|| !o2
|| o1
->type
!= o2
->type
)
1193 if (o1
->type
== operand::OP_PREDICATE
)
1195 predicate
*p1
= as_a
<predicate
*>(o1
);
1196 predicate
*p2
= as_a
<predicate
*>(o2
);
1197 return p1
->p
== p2
->p
;
1199 else if (o1
->type
== operand::OP_EXPR
)
1201 expr
*e1
= static_cast<expr
*>(o1
);
1202 expr
*e2
= static_cast<expr
*>(o2
);
1203 return (e1
->operation
== e2
->operation
1204 && e1
->is_generic
== e2
->is_generic
);
1210 /* Compare two decision tree nodes N1 and N2 and return true if they
1214 decision_tree::cmp_node (dt_node
*n1
, dt_node
*n2
)
1216 if (!n1
|| !n2
|| n1
->type
!= n2
->type
)
1222 if (n1
->type
== dt_node::DT_TRUE
)
1225 if (n1
->type
== dt_node::DT_OPERAND
)
1226 return cmp_operand ((as_a
<dt_operand
*> (n1
))->op
,
1227 (as_a
<dt_operand
*> (n2
))->op
);
1228 else if (n1
->type
== dt_node::DT_MATCH
)
1229 return ((as_a
<dt_operand
*> (n1
))->match_dop
1230 == (as_a
<dt_operand
*> (n2
))->match_dop
);
1234 /* Search OPS for a decision tree node like P and return it if found. */
1237 decision_tree::find_node (vec
<dt_node
*>& ops
, dt_node
*p
)
1239 /* We can merge adjacent DT_TRUE. */
1240 if (p
->type
== dt_node::DT_TRUE
1242 && ops
.last ()->type
== dt_node::DT_TRUE
)
1244 for (int i
= ops
.length () - 1; i
>= 0; --i
)
1246 /* But we can't merge across DT_TRUE nodes as they serve as
1247 pattern order barriers to make sure that patterns apply
1248 in order of appearance in case multiple matches are possible. */
1249 if (ops
[i
]->type
== dt_node::DT_TRUE
)
1251 if (decision_tree::cmp_node (ops
[i
], p
))
1257 /* Append N to the decision tree if it there is not already an existing
1261 dt_node::append_node (dt_node
*n
)
1265 kid
= decision_tree::find_node (kids
, n
);
1270 n
->level
= this->level
+ 1;
1275 /* Append OP to the decision tree. */
1278 dt_node::append_op (operand
*op
, dt_node
*parent
, unsigned pos
)
1280 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1281 dt_operand
*n
= new dt_operand (DT_OPERAND
, op
, 0, parent_
, pos
);
1282 return append_node (n
);
1285 /* Append a DT_TRUE decision tree node. */
1288 dt_node::append_true_op (dt_node
*parent
, unsigned pos
)
1290 dt_operand
*parent_
= safe_as_a
<dt_operand
*> (parent
);
1291 dt_operand
*n
= new dt_operand (DT_TRUE
, 0, 0, parent_
, pos
);
1292 return append_node (n
);
1295 /* Append a DT_MATCH decision tree node. */
1298 dt_node::append_match_op (dt_operand
*match_dop
, dt_node
*parent
, unsigned pos
)
1300 dt_operand
*parent_
= as_a
<dt_operand
*> (parent
);
1301 dt_operand
*n
= new dt_operand (DT_MATCH
, 0, match_dop
, parent_
, pos
);
1302 return append_node (n
);
1305 /* Append S to the decision tree. */
1308 dt_node::append_simplify (simplify
*s
, unsigned pattern_no
,
1309 dt_operand
**indexes
)
1311 dt_simplify
*n
= new dt_simplify (s
, pattern_no
, indexes
);
1312 return append_node (n
);
1315 /* Insert O into the decision tree and return the decision tree node found
1319 decision_tree::insert_operand (dt_node
*p
, operand
*o
, dt_operand
**indexes
,
1320 unsigned pos
, dt_node
*parent
)
1322 dt_node
*q
, *elm
= 0;
1324 if (capture
*c
= dyn_cast
<capture
*> (o
))
1326 unsigned capt_index
= c
->where
;
1328 if (indexes
[capt_index
] == 0)
1331 q
= insert_operand (p
, c
->what
, indexes
, pos
, parent
);
1334 q
= elm
= p
->append_true_op (parent
, pos
);
1337 // get to the last capture
1338 for (operand
*what
= c
->what
;
1339 what
&& is_a
<capture
*> (what
);
1340 c
= as_a
<capture
*> (what
), what
= c
->what
)
1345 unsigned cc_index
= c
->where
;
1346 dt_operand
*match_op
= indexes
[cc_index
];
1348 dt_operand
temp (dt_node::DT_TRUE
, 0, 0);
1349 elm
= decision_tree::find_node (p
->kids
, &temp
);
1353 dt_operand
temp (dt_node::DT_MATCH
, 0, match_op
);
1354 elm
= decision_tree::find_node (p
->kids
, &temp
);
1359 dt_operand
temp (dt_node::DT_OPERAND
, c
->what
, 0);
1360 elm
= decision_tree::find_node (p
->kids
, &temp
);
1364 gcc_assert (elm
->type
== dt_node::DT_TRUE
1365 || elm
->type
== dt_node::DT_OPERAND
1366 || elm
->type
== dt_node::DT_MATCH
);
1367 indexes
[capt_index
] = static_cast<dt_operand
*> (elm
);
1372 p
= p
->append_match_op (indexes
[capt_index
], parent
, pos
);
1374 return insert_operand (p
, c
->what
, indexes
, 0, p
);
1379 p
= p
->append_op (o
, parent
, pos
);
1382 if (expr
*e
= dyn_cast
<expr
*>(o
))
1384 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1385 q
= decision_tree::insert_operand (q
, e
->ops
[i
], indexes
, i
, p
);
1391 /* Insert S into the decision tree. */
1394 decision_tree::insert (struct simplify
*s
, unsigned pattern_no
)
1396 dt_operand
**indexes
= XCNEWVEC (dt_operand
*, s
->capture_max
+ 1);
1397 dt_node
*p
= decision_tree::insert_operand (root
, s
->match
, indexes
);
1398 p
->append_simplify (s
, pattern_no
, indexes
);
1401 /* Debug functions to dump the decision tree. */
1404 decision_tree::print_node (dt_node
*p
, FILE *f
, unsigned indent
)
1406 if (p
->type
== dt_node::DT_NODE
)
1407 fprintf (f
, "root");
1411 for (unsigned i
= 0; i
< indent
; i
++)
1414 if (p
->type
== dt_node::DT_OPERAND
)
1416 dt_operand
*dop
= static_cast<dt_operand
*>(p
);
1417 print_operand (dop
->op
, f
, true);
1419 else if (p
->type
== dt_node::DT_TRUE
)
1420 fprintf (f
, "true");
1421 else if (p
->type
== dt_node::DT_MATCH
)
1422 fprintf (f
, "match (%p)", (void *)((as_a
<dt_operand
*>(p
))->match_dop
));
1423 else if (p
->type
== dt_node::DT_SIMPLIFY
)
1425 dt_simplify
*s
= static_cast<dt_simplify
*> (p
);
1426 fprintf (f
, "simplify_%u { ", s
->pattern_no
);
1427 for (int i
= 0; i
<= s
->s
->capture_max
; ++i
)
1428 fprintf (f
, "%p, ", (void *) s
->indexes
[i
]);
1433 fprintf (stderr
, " (%p), %u, %u\n", (void *) p
, p
->level
, p
->kids
.length ());
1435 for (unsigned i
= 0; i
< p
->kids
.length (); ++i
)
1436 decision_tree::print_node (p
->kids
[i
], f
, indent
+ 2);
1440 decision_tree::print (FILE *f
)
1442 return decision_tree::print_node (root
, f
);
1446 /* For GENERIC we have to take care of wrapping multiple-used
1447 expressions with side-effects in save_expr and preserve side-effects
1448 of expressions with omit_one_operand. Analyze captures in
1449 match, result and with expressions and perform early-outs
1450 on the outermost match expression operands for cases we cannot
1455 capture_info (simplify
*s
);
1456 void walk_match (operand
*o
, unsigned toplevel_arg
, bool, bool);
1457 void walk_result (operand
*o
, bool);
1458 void walk_c_expr (c_expr
*);
1464 bool force_no_side_effects_p
;
1465 bool cond_expr_cond_p
;
1466 unsigned long toplevel_msk
;
1467 int result_use_count
;
1470 auto_vec
<cinfo
> info
;
1471 unsigned long force_no_side_effects
;
1474 /* Analyze captures in S. */
1476 capture_info::capture_info (simplify
*s
)
1480 || ((e
= dyn_cast
<expr
*> (s
->result
))
1481 && is_a
<predicate_id
*> (e
->operation
)))
1483 force_no_side_effects
= -1;
1487 force_no_side_effects
= 0;
1488 info
.safe_grow_cleared (s
->capture_max
+ 1);
1489 e
= as_a
<expr
*> (s
->match
);
1490 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1491 walk_match (e
->ops
[i
], i
,
1492 (i
!= 0 && *e
->operation
== COND_EXPR
)
1493 || *e
->operation
== TRUTH_ANDIF_EXPR
1494 || *e
->operation
== TRUTH_ORIF_EXPR
,
1496 && (*e
->operation
== COND_EXPR
1497 || *e
->operation
== VEC_COND_EXPR
));
1499 walk_result (s
->result
, false);
1501 for (unsigned i
= 0; i
< s
->ifexpr_vec
.length (); ++i
)
1502 if (s
->ifexpr_vec
[i
].is_with
)
1503 walk_c_expr (as_a
<c_expr
*>(s
->ifexpr_vec
[i
].cexpr
));
1506 /* Analyze captures in the match expression piece O. */
1509 capture_info::walk_match (operand
*o
, unsigned toplevel_arg
,
1510 bool conditional_p
, bool cond_expr_cond_p
)
1512 if (capture
*c
= dyn_cast
<capture
*> (o
))
1514 info
[c
->where
].toplevel_msk
|= 1 << toplevel_arg
;
1515 info
[c
->where
].force_no_side_effects_p
|= conditional_p
;
1516 info
[c
->where
].cond_expr_cond_p
|= cond_expr_cond_p
;
1517 /* Mark expr (non-leaf) captures and recurse. */
1519 && is_a
<expr
*> (c
->what
))
1521 info
[c
->where
].expr_p
= true;
1522 walk_match (c
->what
, toplevel_arg
, conditional_p
, false);
1525 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1527 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1529 bool cond_p
= conditional_p
;
1530 bool cond_expr_cond_p
= false;
1531 if (i
!= 0 && *e
->operation
== COND_EXPR
)
1533 else if (*e
->operation
== TRUTH_ANDIF_EXPR
1534 || *e
->operation
== TRUTH_ORIF_EXPR
)
1537 && (*e
->operation
== COND_EXPR
1538 || *e
->operation
== VEC_COND_EXPR
))
1539 cond_expr_cond_p
= true;
1540 walk_match (e
->ops
[i
], toplevel_arg
, cond_p
, cond_expr_cond_p
);
1543 else if (is_a
<predicate
*> (o
))
1545 /* Mark non-captured leafs toplevel arg for checking. */
1546 force_no_side_effects
|= 1 << toplevel_arg
;
1552 /* Analyze captures in the result expression piece O. */
1555 capture_info::walk_result (operand
*o
, bool conditional_p
)
1557 if (capture
*c
= dyn_cast
<capture
*> (o
))
1559 info
[c
->where
].result_use_count
++;
1560 /* If we substitute an expression capture we don't know
1561 which captures this will end up using (well, we don't
1562 compute that). Force the uses to be side-effect free
1563 which means forcing the toplevels that reach the
1564 expression side-effect free. */
1565 if (info
[c
->where
].expr_p
)
1566 force_no_side_effects
|= info
[c
->where
].toplevel_msk
;
1567 /* Mark CSE capture capture uses as forced to have
1570 && is_a
<expr
*> (c
->what
))
1572 info
[c
->where
].cse_p
= true;
1573 walk_result (c
->what
, true);
1576 else if (expr
*e
= dyn_cast
<expr
*> (o
))
1578 for (unsigned i
= 0; i
< e
->ops
.length (); ++i
)
1580 bool cond_p
= conditional_p
;
1581 if (i
!= 0 && *e
->operation
== COND_EXPR
)
1583 else if (*e
->operation
== TRUTH_ANDIF_EXPR
1584 || *e
->operation
== TRUTH_ORIF_EXPR
)
1586 walk_result (e
->ops
[i
], cond_p
);
1589 else if (c_expr
*e
= dyn_cast
<c_expr
*> (o
))
1595 /* Look for captures in the C expr E. */
1598 capture_info::walk_c_expr (c_expr
*e
)
1600 /* Give up for C exprs mentioning captures not inside TREE_TYPE (). */
1601 unsigned p_depth
= 0;
1602 for (unsigned i
= 0; i
< e
->code
.length (); ++i
)
1604 const cpp_token
*t
= &e
->code
[i
];
1605 const cpp_token
*n
= i
< e
->code
.length () - 1 ? &e
->code
[i
+1] : NULL
;
1606 if (t
->type
== CPP_NAME
1607 && strcmp ((const char *)CPP_HASHNODE
1608 (t
->val
.node
.node
)->ident
.str
, "TREE_TYPE") == 0
1609 && n
->type
== CPP_OPEN_PAREN
)
1611 else if (t
->type
== CPP_CLOSE_PAREN
1614 else if (p_depth
== 0
1615 && t
->type
== CPP_ATSIGN
1616 && (n
->type
== CPP_NUMBER
1617 || n
->type
== CPP_NAME
)
1618 && !(n
->flags
& PREV_WHITE
))
1621 if (n
->type
== CPP_NUMBER
)
1622 id
= (const char *)n
->val
.str
.text
;
1624 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
1625 info
[*e
->capture_ids
->get(id
)].force_no_side_effects_p
= true;
1631 /* Code generation off the decision tree and the refered AST nodes. */
1634 is_conversion (id_base
*op
)
1636 return (*op
== CONVERT_EXPR
1638 || *op
== FLOAT_EXPR
1639 || *op
== FIX_TRUNC_EXPR
1640 || *op
== VIEW_CONVERT_EXPR
);
1643 /* Get the type to be used for generating operands of OP from the
1647 get_operand_type (id_base
*op
, const char *in_type
,
1648 const char *expr_type
,
1649 const char *other_oprnd_type
)
1651 /* Generally operands whose type does not match the type of the
1652 expression generated need to know their types but match and
1653 thus can fall back to 'other_oprnd_type'. */
1654 if (is_conversion (op
))
1655 return other_oprnd_type
;
1656 else if (*op
== REALPART_EXPR
1657 || *op
== IMAGPART_EXPR
)
1658 return other_oprnd_type
;
1659 else if (is_a
<operator_id
*> (op
)
1660 && strcmp (as_a
<operator_id
*> (op
)->tcc
, "tcc_comparison") == 0)
1661 return other_oprnd_type
;
1664 /* Otherwise all types should match - choose one in order of
1671 return other_oprnd_type
;
1675 /* Generate transform code for an expression. */
1678 expr::gen_transform (FILE *f
, const char *dest
, bool gimple
, int depth
,
1679 const char *in_type
, capture_info
*cinfo
,
1680 dt_operand
**indexes
, bool)
1682 bool conversion_p
= is_conversion (operation
);
1683 const char *type
= expr_type
;
1686 /* If there was a type specification in the pattern use it. */
1688 else if (conversion_p
)
1689 /* For conversions we need to build the expression using the
1690 outer type passed in. */
1692 else if (*operation
== REALPART_EXPR
1693 || *operation
== IMAGPART_EXPR
)
1695 /* __real and __imag use the component type of its operand. */
1696 sprintf (optype
, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth
);
1699 else if (is_a
<operator_id
*> (operation
)
1700 && !strcmp (as_a
<operator_id
*> (operation
)->tcc
, "tcc_comparison"))
1702 /* comparisons use boolean_type_node (or what gets in), but
1703 their operands need to figure out the types themselves. */
1704 sprintf (optype
, "boolean_type_node");
1707 else if (*operation
== COND_EXPR
1708 || *operation
== VEC_COND_EXPR
)
1710 /* Conditions are of the same type as their first alternative. */
1711 sprintf (optype
, "TREE_TYPE (ops%d[1])", depth
);
1716 /* Other operations are of the same type as their first operand. */
1717 sprintf (optype
, "TREE_TYPE (ops%d[0])", depth
);
1721 fatal ("two conversions in a row");
1724 fprintf (f
, " tree ops%d[%u], res;\n", depth
, ops
.length ());
1726 snprintf (op0type
, 64, "TREE_TYPE (ops%d[0])", depth
);
1727 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1730 snprintf (dest
, 32, " ops%d[%u]", depth
, i
);
1732 = get_operand_type (operation
, in_type
, expr_type
,
1733 i
== 0 ? NULL
: op0type
);
1734 ops
[i
]->gen_transform (f
, dest
, gimple
, depth
+ 1, optype
, cinfo
, indexes
,
1735 ((!(*operation
== COND_EXPR
)
1736 && !(*operation
== VEC_COND_EXPR
))
1741 if (*operation
== CONVERT_EXPR
)
1744 opr
= operation
->id
;
1748 /* ??? Building a stmt can fail for various reasons here, seq being
1749 NULL or the stmt referencing SSA names occuring in abnormal PHIs.
1750 So if we fail here we should continue matching other patterns. */
1751 fprintf (f
, " code_helper tem_code = %s;\n"
1752 " tree tem_ops[3] = { ", opr
);
1753 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1754 fprintf (f
, "ops%d[%u]%s", depth
, i
,
1755 i
== ops
.length () - 1 ? " };\n" : ", ");
1756 fprintf (f
, " gimple_resimplify%d (seq, &tem_code, %s, tem_ops, valueize);\n",
1757 ops
.length (), type
);
1758 fprintf (f
, " res = maybe_push_res_to_seq (tem_code, %s, tem_ops, seq);\n"
1759 " if (!res) return false;\n", type
);
1763 if (operation
->kind
== id_base::CODE
)
1764 fprintf (f
, " res = fold_build%d_loc (loc, %s, %s",
1765 ops
.length(), opr
, type
);
1767 fprintf (f
, " res = build_call_expr_loc (loc, "
1768 "builtin_decl_implicit (%s), %d", opr
, ops
.length());
1769 for (unsigned i
= 0; i
< ops
.length (); ++i
)
1770 fprintf (f
, ", ops%d[%u]", depth
, i
);
1771 fprintf (f
, ");\n");
1773 fprintf (f
, "%s = res;\n", dest
);
1777 /* Generate code for a c_expr which is either the expression inside
1778 an if statement or a sequence of statements which computes a
1779 result to be stored to DEST. */
1782 c_expr::gen_transform (FILE *f
, const char *dest
,
1783 bool, int, const char *, capture_info
*,
1784 dt_operand
**, bool)
1786 if (dest
&& nr_stmts
== 1)
1787 fprintf (f
, "%s = ", dest
);
1789 unsigned stmt_nr
= 1;
1790 for (unsigned i
= 0; i
< code
.length (); ++i
)
1792 const cpp_token
*token
= &code
[i
];
1794 /* Replace captures for code-gen. */
1795 if (token
->type
== CPP_ATSIGN
)
1797 const cpp_token
*n
= &code
[i
+1];
1798 if ((n
->type
== CPP_NUMBER
1799 || n
->type
== CPP_NAME
)
1800 && !(n
->flags
& PREV_WHITE
))
1802 if (token
->flags
& PREV_WHITE
)
1805 if (n
->type
== CPP_NUMBER
)
1806 id
= (const char *)n
->val
.str
.text
;
1808 id
= (const char *)CPP_HASHNODE (n
->val
.node
.node
)->ident
.str
;
1809 fprintf (f
, "captures[%u]", *capture_ids
->get(id
));
1815 if (token
->flags
& PREV_WHITE
)
1818 if (token
->type
== CPP_NAME
)
1820 const char *id
= (const char *) NODE_NAME (token
->val
.node
.node
);
1822 for (j
= 0; j
< ids
.length (); ++j
)
1824 if (strcmp (id
, ids
[j
].id
) == 0)
1826 fprintf (f
, "%s", ids
[j
].oper
);
1830 if (j
< ids
.length ())
1834 /* Output the token as string. */
1835 char *tk
= (char *)cpp_token_as_text (r
, token
);
1838 if (token
->type
== CPP_SEMICOLON
)
1841 if (dest
&& stmt_nr
== nr_stmts
)
1842 fprintf (f
, "\n %s = ", dest
);
1849 /* Generate transform code for a capture. */
1852 capture::gen_transform (FILE *f
, const char *dest
, bool gimple
, int depth
,
1853 const char *in_type
, capture_info
*cinfo
,
1854 dt_operand
**indexes
, bool expand_compares
)
1856 if (what
&& is_a
<expr
*> (what
))
1858 if (indexes
[where
] == 0)
1861 sprintf (buf
, "captures[%u]", where
);
1862 what
->gen_transform (f
, buf
, gimple
, depth
, in_type
, cinfo
, NULL
);
1866 fprintf (f
, "%s = captures[%u];\n", dest
, where
);
1868 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
1869 with substituting a capture of that.
1870 ??? Returning false here will also not allow any other patterns
1872 if (gimple
&& expand_compares
1873 && cinfo
->info
[where
].cond_expr_cond_p
)
1874 fprintf (f
, "if (COMPARISON_CLASS_P (%s))\n"
1876 " if (!seq) return false;\n"
1877 " %s = gimple_build (seq, TREE_CODE (%s),"
1878 " TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
1879 " TREE_OPERAND (%s, 1));\n"
1880 " }\n", dest
, dest
, dest
, dest
, dest
, dest
);
1883 /* Return the name of the operand representing the decision tree node.
1884 Use NAME as space to generate it. */
1887 dt_operand::get_name (char *name
)
1890 sprintf (name
, "t");
1891 else if (parent
->level
== 1)
1892 sprintf (name
, "op%u", pos
);
1893 else if (parent
->type
== dt_node::DT_MATCH
)
1894 return parent
->get_name (name
);
1896 sprintf (name
, "o%u%u", parent
->level
, pos
);
1900 /* Fill NAME with the operand name at position POS. */
1903 dt_operand::gen_opname (char *name
, unsigned pos
)
1906 sprintf (name
, "op%u", pos
);
1908 sprintf (name
, "o%u%u", level
, pos
);
1911 /* Generate matching code for the decision tree operand which is
1915 dt_operand::gen_predicate (FILE *f
, const char *opname
, bool gimple
)
1917 predicate
*p
= as_a
<predicate
*> (op
);
1919 if (p
->p
->matchers
.exists ())
1921 /* If this is a predicate generated from a pattern mangle its
1922 name and pass on the valueize hook. */
1924 fprintf (f
, "if (gimple_%s (%s, valueize))\n", p
->p
->id
, opname
);
1926 fprintf (f
, "if (tree_%s (%s))\n", p
->p
->id
, opname
);
1929 fprintf (f
, "if (%s (%s))\n", p
->p
->id
, opname
);
1934 /* Generate matching code for the decision tree operand which is
1938 dt_operand::gen_match_op (FILE *f
, const char *opname
)
1940 char match_opname
[20];
1941 match_dop
->get_name (match_opname
);
1942 fprintf (f
, "if (%s == %s || operand_equal_p (%s, %s, 0))\n",
1943 opname
, match_opname
, opname
, match_opname
);
1948 /* Generate GIMPLE matching code for the decision tree operand. */
1951 dt_operand::gen_gimple_expr (FILE *f
)
1953 expr
*e
= static_cast<expr
*> (op
);
1954 id_base
*id
= e
->operation
;
1955 unsigned n_ops
= e
->ops
.length ();
1957 for (unsigned i
= 0; i
< n_ops
; ++i
)
1959 char child_opname
[20];
1960 gen_opname (child_opname
, i
);
1962 if (id
->kind
== id_base::CODE
)
1965 || *id
== REALPART_EXPR
|| *id
== IMAGPART_EXPR
1966 || *id
== BIT_FIELD_REF
|| *id
== VIEW_CONVERT_EXPR
)
1968 /* ??? If this is a memory operation we can't (and should not)
1969 match this. The only sensible operand types are
1970 SSA names and invariants. */
1971 fprintf (f
, "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), %i);\n",
1973 fprintf (f
, "if ((TREE_CODE (%s) == SSA_NAME\n"
1974 "|| is_gimple_min_invariant (%s))\n"
1975 "&& (%s = do_valueize (valueize, %s)))\n"
1976 "{\n", child_opname
, child_opname
, child_opname
,
1981 fprintf (f
, "tree %s = gimple_assign_rhs%u (def_stmt);\n",
1982 child_opname
, i
+ 1);
1985 fprintf (f
, "tree %s = gimple_call_arg (def_stmt, %u);\n",
1987 fprintf (f
, "if ((%s = do_valueize (valueize, %s)))\n",
1988 child_opname
, child_opname
);
1995 /* Generate GENERIC matching code for the decision tree operand. */
1998 dt_operand::gen_generic_expr (FILE *f
, const char *opname
)
2000 expr
*e
= static_cast<expr
*> (op
);
2001 unsigned n_ops
= e
->ops
.length ();
2003 for (unsigned i
= 0; i
< n_ops
; ++i
)
2005 char child_opname
[20];
2006 gen_opname (child_opname
, i
);
2008 if (e
->operation
->kind
== id_base::CODE
)
2009 fprintf (f
, "tree %s = TREE_OPERAND (%s, %u);\n",
2010 child_opname
, opname
, i
);
2012 fprintf (f
, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
2013 child_opname
, opname
, i
);
2019 /* Generate matching code for the children of the decision tree node. */
2022 dt_node::gen_kids (FILE *f
, bool gimple
)
2024 auto_vec
<dt_operand
*> gimple_exprs
;
2025 auto_vec
<dt_operand
*> generic_exprs
;
2026 auto_vec
<dt_operand
*> fns
;
2027 auto_vec
<dt_operand
*> generic_fns
;
2028 auto_vec
<dt_operand
*> preds
;
2029 auto_vec
<dt_node
*> others
;
2031 for (unsigned i
= 0; i
< kids
.length (); ++i
)
2033 if (kids
[i
]->type
== dt_node::DT_OPERAND
)
2035 dt_operand
*op
= as_a
<dt_operand
*> (kids
[i
]);
2036 if (expr
*e
= dyn_cast
<expr
*> (op
->op
))
2038 if (e
->ops
.length () == 0
2039 && (!gimple
|| !(*e
->operation
== CONSTRUCTOR
)))
2040 generic_exprs
.safe_push (op
);
2041 else if (e
->operation
->kind
== id_base::FN
)
2046 generic_fns
.safe_push (op
);
2048 else if (e
->operation
->kind
== id_base::PREDICATE
)
2049 preds
.safe_push (op
);
2053 gimple_exprs
.safe_push (op
);
2055 generic_exprs
.safe_push (op
);
2058 else if (op
->op
->type
== operand::OP_PREDICATE
)
2059 others
.safe_push (kids
[i
]);
2063 else if (kids
[i
]->type
== dt_node::DT_MATCH
2064 || kids
[i
]->type
== dt_node::DT_SIMPLIFY
)
2065 others
.safe_push (kids
[i
]);
2066 else if (kids
[i
]->type
== dt_node::DT_TRUE
)
2068 /* A DT_TRUE operand serves as a barrier - generate code now
2069 for what we have collected sofar. */
2070 gen_kids_1 (f
, gimple
, gimple_exprs
, generic_exprs
,
2071 fns
, generic_fns
, preds
, others
);
2072 /* And output the true operand itself. */
2073 kids
[i
]->gen (f
, gimple
);
2074 gimple_exprs
.truncate (0);
2075 generic_exprs
.truncate (0);
2077 generic_fns
.truncate (0);
2079 others
.truncate (0);
2085 /* Generate code for the remains. */
2086 gen_kids_1 (f
, gimple
, gimple_exprs
, generic_exprs
,
2087 fns
, generic_fns
, preds
, others
);
2090 /* Generate matching code for the children of the decision tree node. */
2093 dt_node::gen_kids_1 (FILE *f
, bool gimple
,
2094 vec
<dt_operand
*> gimple_exprs
,
2095 vec
<dt_operand
*> generic_exprs
,
2096 vec
<dt_operand
*> fns
,
2097 vec
<dt_operand
*> generic_fns
,
2098 vec
<dt_operand
*> preds
,
2099 vec
<dt_node
*> others
)
2102 char *kid_opname
= buf
;
2104 unsigned exprs_len
= gimple_exprs
.length ();
2105 unsigned gexprs_len
= generic_exprs
.length ();
2106 unsigned fns_len
= fns
.length ();
2107 unsigned gfns_len
= generic_fns
.length ();
2109 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2112 gimple_exprs
[0]->get_name (kid_opname
);
2114 fns
[0]->get_name (kid_opname
);
2116 generic_fns
[0]->get_name (kid_opname
);
2118 generic_exprs
[0]->get_name (kid_opname
);
2120 fprintf (f
, "switch (TREE_CODE (%s))\n"
2124 if (exprs_len
|| fns_len
)
2126 fprintf (f
, "case SSA_NAME:\n");
2127 fprintf (f
, "if (do_valueize (valueize, %s) != NULL_TREE)\n", kid_opname
);
2129 fprintf (f
, "gimple def_stmt = SSA_NAME_DEF_STMT (%s);\n", kid_opname
);
2133 fprintf (f
, "if (is_gimple_assign (def_stmt))\n");
2134 fprintf (f
, "switch (gimple_assign_rhs_code (def_stmt))\n"
2136 for (unsigned i
= 0; i
< exprs_len
; ++i
)
2138 expr
*e
= as_a
<expr
*> (gimple_exprs
[i
]->op
);
2139 id_base
*op
= e
->operation
;
2140 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2141 fprintf (f
, "CASE_CONVERT:\n");
2143 fprintf (f
, "case %s:\n", op
->id
);
2145 gimple_exprs
[i
]->gen (f
, true);
2146 fprintf (f
, "break;\n"
2149 fprintf (f
, "default:;\n"
2156 fprintf (f
, "else ");
2158 fprintf (f
, "if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL))\n"
2160 "tree fndecl = gimple_call_fndecl (def_stmt);\n"
2161 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2164 for (unsigned i
= 0; i
< fns_len
; ++i
)
2166 expr
*e
= as_a
<expr
*>(fns
[i
]->op
);
2167 fprintf (f
, "case %s:\n"
2168 "{\n", e
->operation
->id
);
2169 fns
[i
]->gen (f
, true);
2170 fprintf (f
, "break;\n"
2174 fprintf (f
, "default:;\n"
2183 for (unsigned i
= 0; i
< generic_exprs
.length (); ++i
)
2185 expr
*e
= as_a
<expr
*>(generic_exprs
[i
]->op
);
2186 id_base
*op
= e
->operation
;
2187 if (*op
== CONVERT_EXPR
|| *op
== NOP_EXPR
)
2188 fprintf (f
, "CASE_CONVERT:\n");
2190 fprintf (f
, "case %s:\n", op
->id
);
2192 generic_exprs
[i
]->gen (f
, gimple
);
2193 fprintf (f
, "break;\n"
2199 fprintf (f
, "case CALL_EXPR:\n"
2201 "tree fndecl = get_callee_fndecl (%s);\n"
2202 "if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)\n"
2203 "switch (DECL_FUNCTION_CODE (fndecl))\n"
2206 for (unsigned j
= 0; j
< generic_fns
.length (); ++j
)
2208 expr
*e
= as_a
<expr
*>(generic_fns
[j
]->op
);
2209 gcc_assert (e
->operation
->kind
== id_base::FN
);
2211 fprintf (f
, "case %s:\n"
2212 "{\n", e
->operation
->id
);
2213 generic_fns
[j
]->gen (f
, false);
2214 fprintf (f
, "break;\n"
2218 fprintf (f
, "default:;\n"
2224 /* Close switch (TREE_CODE ()). */
2225 if (exprs_len
|| fns_len
|| gexprs_len
|| gfns_len
)
2226 fprintf (f
, "default:;\n"
2229 for (unsigned i
= 0; i
< preds
.length (); ++i
)
2231 expr
*e
= as_a
<expr
*> (preds
[i
]->op
);
2232 predicate_id
*p
= as_a
<predicate_id
*> (e
->operation
);
2233 preds
[i
]->get_name (kid_opname
);
2234 fprintf (f
, "tree %s_pops[%d];\n", kid_opname
, p
->nargs
);
2235 fprintf (f
, "if (%s_%s (%s, %s_pops%s))\n",
2236 gimple
? "gimple" : "tree",
2237 p
->id
, kid_opname
, kid_opname
,
2238 gimple
? ", valueize" : "");
2240 for (int j
= 0; j
< p
->nargs
; ++j
)
2242 char child_opname
[20];
2243 preds
[i
]->gen_opname (child_opname
, j
);
2244 fprintf (f
, "tree %s = %s_pops[%d];\n", child_opname
, kid_opname
, j
);
2246 preds
[i
]->gen_kids (f
, gimple
);
2250 for (unsigned i
= 0; i
< others
.length (); ++i
)
2251 others
[i
]->gen (f
, gimple
);
2254 /* Generate matching code for the decision tree operand. */
2257 dt_operand::gen (FILE *f
, bool gimple
)
2262 unsigned n_braces
= 0;
2264 if (type
== DT_OPERAND
)
2267 case operand::OP_PREDICATE
:
2268 n_braces
= gen_predicate (f
, opname
, gimple
);
2271 case operand::OP_EXPR
:
2273 n_braces
= gen_gimple_expr (f
);
2275 n_braces
= gen_generic_expr (f
, opname
);
2281 else if (type
== DT_TRUE
)
2283 else if (type
== DT_MATCH
)
2284 n_braces
= gen_match_op (f
, opname
);
2288 gen_kids (f
, gimple
);
2290 for (unsigned i
= 0; i
< n_braces
; ++i
)
2296 /* Generate code for the '(if ...)', '(with ..)' and actual transform
2297 step of a '(simplify ...)' or '(match ...)'. This handles everything
2298 that is not part of the decision tree (simplify->match). */
2301 dt_simplify::gen (FILE *f
, bool gimple
)
2304 output_line_directive (f
, s
->result_location
);
2305 if (s
->capture_max
>= 0)
2306 fprintf (f
, "tree captures[%u] ATTRIBUTE_UNUSED = {};\n",
2307 s
->capture_max
+ 1);
2309 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2313 fprintf (f
, "captures[%u] = %s;\n", i
, indexes
[i
]->get_name (opname
));
2316 unsigned n_braces
= 0;
2317 if (s
->ifexpr_vec
!= vNULL
)
2319 for (unsigned i
= 0; i
< s
->ifexpr_vec
.length (); ++i
)
2321 if_or_with
&w
= s
->ifexpr_vec
[i
];
2325 output_line_directive (f
, w
.location
);
2326 w
.cexpr
->gen_transform (f
, NULL
, true, 1, "type", NULL
);
2331 output_line_directive (f
, w
.location
);
2332 fprintf (f
, "if (");
2333 if (i
== s
->ifexpr_vec
.length () - 1
2334 || s
->ifexpr_vec
[i
+1].is_with
)
2335 w
.cexpr
->gen_transform (f
, NULL
, true, 1, "type", NULL
);
2344 output_line_directive (f
, s
->ifexpr_vec
[j
].location
);
2348 s
->ifexpr_vec
[j
].cexpr
->gen_transform (f
, NULL
,
2354 while (j
< s
->ifexpr_vec
.length ()
2355 && !s
->ifexpr_vec
[j
].is_with
);
2365 /* Analyze captures and perform early-outs on the incoming arguments
2366 that cover cases we cannot handle. */
2367 capture_info
cinfo (s
);
2371 && !((e
= dyn_cast
<expr
*> (s
->result
))
2372 && is_a
<predicate_id
*> (e
->operation
)))
2374 for (unsigned i
= 0; i
< as_a
<expr
*> (s
->match
)->ops
.length (); ++i
)
2375 if (cinfo
.force_no_side_effects
& (1 << i
))
2376 fprintf (f
, "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n", i
);
2377 for (int i
= 0; i
<= s
->capture_max
; ++i
)
2378 if (cinfo
.info
[i
].cse_p
)
2380 else if (cinfo
.info
[i
].force_no_side_effects_p
2381 && (cinfo
.info
[i
].toplevel_msk
2382 & cinfo
.force_no_side_effects
) == 0)
2383 fprintf (f
, "if (TREE_SIDE_EFFECTS (captures[%d])) "
2384 "return NULL_TREE;\n", i
);
2385 else if ((cinfo
.info
[i
].toplevel_msk
2386 & cinfo
.force_no_side_effects
) != 0)
2387 /* Mark capture as having no side-effects if we had to verify
2388 that via forced toplevel operand checks. */
2389 cinfo
.info
[i
].force_no_side_effects_p
= true;
2392 fprintf (f
, "if (dump_file && (dump_flags & TDF_DETAILS)) "
2393 "fprintf (dump_file, \"Applying pattern ");
2394 output_line_directive (f
, s
->result_location
, true);
2395 fprintf (f
, ", %%s:%%d\\n\", __FILE__, __LINE__);\n");
2397 operand
*result
= s
->result
;
2400 /* If there is no result then this is a predicate implementation. */
2401 fprintf (f
, "return true;\n");
2405 /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
2406 in outermost position). */
2407 if (result
->type
== operand::OP_EXPR
2408 && *as_a
<expr
*> (result
)->operation
== NON_LVALUE_EXPR
)
2409 result
= as_a
<expr
*> (result
)->ops
[0];
2410 if (result
->type
== operand::OP_EXPR
)
2412 expr
*e
= as_a
<expr
*> (result
);
2413 bool is_predicate
= is_a
<predicate_id
*> (e
->operation
);
2415 fprintf (f
, "*res_code = %s;\n",
2416 *e
->operation
== CONVERT_EXPR
2417 ? "NOP_EXPR" : e
->operation
->id
);
2418 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2421 snprintf (dest
, 32, " res_ops[%d]", j
);
2423 = get_operand_type (e
->operation
,
2424 "type", e
->expr_type
,
2426 ? NULL
: "TREE_TYPE (res_ops[0])");
2427 /* We need to expand GENERIC conditions we captured from
2429 bool expand_generic_cond_exprs_p
2431 /* But avoid doing that if the GENERIC condition is
2432 valid - which it is in the first operand of COND_EXPRs
2433 and VEC_COND_EXRPs. */
2434 && ((!(*e
->operation
== COND_EXPR
)
2435 && !(*e
->operation
== VEC_COND_EXPR
))
2437 e
->ops
[j
]->gen_transform (f
, dest
, true, 1, optype
, &cinfo
,
2438 indexes
, expand_generic_cond_exprs_p
);
2441 /* Re-fold the toplevel result. It's basically an embedded
2442 gimple_build w/o actually building the stmt. */
2444 fprintf (f
, "gimple_resimplify%d (seq, res_code, type, "
2445 "res_ops, valueize);\n", e
->ops
.length ());
2447 else if (result
->type
== operand::OP_CAPTURE
2448 || result
->type
== operand::OP_C_EXPR
)
2450 result
->gen_transform (f
, "res_ops[0]", true, 1, "type",
2451 &cinfo
, indexes
, false);
2452 fprintf (f
, "*res_code = TREE_CODE (res_ops[0]);\n");
2453 if (is_a
<capture
*> (result
)
2454 && cinfo
.info
[as_a
<capture
*> (result
)->where
].cond_expr_cond_p
)
2456 /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
2457 with substituting a capture of that. */
2458 fprintf (f
, "if (COMPARISON_CLASS_P (res_ops[0]))\n"
2460 " tree tem = res_ops[0];\n"
2461 " res_ops[0] = TREE_OPERAND (tem, 0);\n"
2462 " res_ops[1] = TREE_OPERAND (tem, 1);\n"
2468 fprintf (f
, "return true;\n");
2472 bool is_predicate
= false;
2473 if (result
->type
== operand::OP_EXPR
)
2475 expr
*e
= as_a
<expr
*> (result
);
2476 is_predicate
= is_a
<predicate_id
*> (e
->operation
);
2477 /* Search for captures used multiple times in the result expression
2478 and dependent on TREE_SIDE_EFFECTS emit a SAVE_EXPR. */
2480 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
2482 if (!cinfo
.info
[i
].force_no_side_effects_p
2483 && cinfo
.info
[i
].result_use_count
> 1)
2484 fprintf (f
, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2485 " captures[%d] = save_expr (captures[%d]);\n",
2488 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2492 snprintf (dest
, 32, "res_ops[%d]", j
);
2495 fprintf (f
, " tree res_op%d;\n", j
);
2496 snprintf (dest
, 32, " res_op%d", j
);
2499 = get_operand_type (e
->operation
,
2500 "type", e
->expr_type
,
2502 ? NULL
: "TREE_TYPE (res_op0)");
2503 e
->ops
[j
]->gen_transform (f
, dest
, false, 1, optype
,
2507 fprintf (f
, "return true;\n");
2510 fprintf (f
, " tree res;\n");
2511 /* Re-fold the toplevel result. Use non_lvalue to
2512 build NON_LVALUE_EXPRs so they get properly
2513 ignored when in GIMPLE form. */
2514 if (*e
->operation
== NON_LVALUE_EXPR
)
2515 fprintf (f
, " res = non_lvalue_loc (loc, res_op0);\n");
2518 if (e
->operation
->kind
== id_base::CODE
)
2519 fprintf (f
, " res = fold_build%d_loc (loc, %s, type",
2521 *e
->operation
== CONVERT_EXPR
2522 ? "NOP_EXPR" : e
->operation
->id
);
2524 fprintf (f
, " res = build_call_expr_loc "
2525 "(loc, builtin_decl_implicit (%s), %d",
2526 e
->operation
->id
, e
->ops
.length());
2527 for (unsigned j
= 0; j
< e
->ops
.length (); ++j
)
2528 fprintf (f
, ", res_op%d", j
);
2529 fprintf (f
, ");\n");
2533 else if (result
->type
== operand::OP_CAPTURE
2534 || result
->type
== operand::OP_C_EXPR
)
2537 fprintf (f
, " tree res;\n");
2538 s
->result
->gen_transform (f
, " res", false, 1, "type",
2545 /* Search for captures not used in the result expression and dependent
2546 on TREE_SIDE_EFFECTS emit omit_one_operand. */
2547 for (int i
= 0; i
< s
->capture_max
+ 1; ++i
)
2549 if (!cinfo
.info
[i
].force_no_side_effects_p
2550 && !cinfo
.info
[i
].expr_p
2551 && cinfo
.info
[i
].result_use_count
== 0)
2552 fprintf (f
, " if (TREE_SIDE_EFFECTS (captures[%d]))\n"
2553 " res = build2_loc (loc, COMPOUND_EXPR, type,"
2554 " fold_ignored_result (captures[%d]), res);\n",
2557 fprintf (f
, " return res;\n");
2561 for (unsigned i
= 0; i
< n_braces
; ++i
)
2567 /* Main entry to generate code for matching GIMPLE IL off the decision
2571 decision_tree::gen_gimple (FILE *f
)
2573 for (unsigned n
= 1; n
<= 3; ++n
)
2575 fprintf (f
, "\nstatic bool\n"
2576 "gimple_simplify (code_helper *res_code, tree *res_ops,\n"
2577 " gimple_seq *seq, tree (*valueize)(tree),\n"
2578 " code_helper code, tree type");
2579 for (unsigned i
= 0; i
< n
; ++i
)
2580 fprintf (f
, ", tree op%d", i
);
2584 fprintf (f
, "switch (code.get_rep())\n"
2586 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
2588 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
2589 expr
*e
= static_cast<expr
*>(dop
->op
);
2590 if (e
->ops
.length () != n
)
2593 if (*e
->operation
== CONVERT_EXPR
2594 || *e
->operation
== NOP_EXPR
)
2595 fprintf (f
, "CASE_CONVERT:\n");
2597 fprintf (f
, "case %s%s:\n",
2598 is_a
<fn_id
*> (e
->operation
) ? "-" : "",
2601 dop
->gen_kids (f
, true);
2602 fprintf (f
, "break;\n");
2605 fprintf (f
, "default:;\n"
2608 fprintf (f
, "return false;\n");
2613 /* Main entry to generate code for matching GENERIC IL off the decision
2617 decision_tree::gen_generic (FILE *f
)
2619 for (unsigned n
= 1; n
<= 3; ++n
)
2621 fprintf (f
, "\ntree\n"
2622 "generic_simplify (location_t loc, enum tree_code code, "
2623 "tree type ATTRIBUTE_UNUSED");
2624 for (unsigned i
= 0; i
< n
; ++i
)
2625 fprintf (f
, ", tree op%d", i
);
2629 fprintf (f
, "switch (code)\n"
2631 for (unsigned i
= 0; i
< root
->kids
.length (); i
++)
2633 dt_operand
*dop
= static_cast<dt_operand
*>(root
->kids
[i
]);
2634 expr
*e
= static_cast<expr
*>(dop
->op
);
2635 if (e
->ops
.length () != n
2636 /* Builtin simplifications are somewhat premature on
2637 GENERIC. The following drops patterns with outermost
2638 calls. It's easy to emit overloads for function code
2639 though if necessary. */
2640 || e
->operation
->kind
!= id_base::CODE
)
2643 operator_id
*op_id
= static_cast <operator_id
*> (e
->operation
);
2644 if (op_id
->code
== NOP_EXPR
|| op_id
->code
== CONVERT_EXPR
)
2645 fprintf (f
, "CASE_CONVERT:\n");
2647 fprintf (f
, "case %s:\n", e
->operation
->id
);
2649 dop
->gen_kids (f
, false);
2650 fprintf (f
, "break;\n"
2653 fprintf (f
, "default:;\n"
2656 fprintf (f
, "return NULL_TREE;\n");
2661 /* Output code to implement the predicate P from the decision tree DT. */
2664 write_predicate (FILE *f
, predicate_id
*p
, decision_tree
&dt
, bool gimple
)
2666 fprintf (f
, "\nbool\n"
2667 "%s%s (tree t%s%s)\n"
2668 "{\n", gimple
? "gimple_" : "tree_", p
->id
,
2669 p
->nargs
> 0 ? ", tree *res_ops" : "",
2670 gimple
? ", tree (*valueize)(tree)" : "");
2671 /* Conveniently make 'type' available. */
2672 fprintf (f
, "tree type = TREE_TYPE (t);\n");
2675 fprintf (f
, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
2676 dt
.root
->gen_kids (f
, gimple
);
2678 fprintf (f
, "return false;\n"
2682 /* Write the common header for the GIMPLE/GENERIC IL matching routines. */
2685 write_header (FILE *f
, const char *head
)
2687 fprintf (f
, "/* Generated automatically by the program `genmatch' from\n");
2688 fprintf (f
, " a IL pattern matching and simplification description. */\n");
2690 /* Include the header instead of writing it awkwardly quoted here. */
2691 fprintf (f
, "\n#include \"%s\"\n", head
);
2701 parser (cpp_reader
*);
2704 const cpp_token
*next ();
2705 const cpp_token
*peek ();
2706 const cpp_token
*peek_ident (const char * = NULL
);
2707 const cpp_token
*expect (enum cpp_ttype
);
2708 void eat_token (enum cpp_ttype
);
2709 const char *get_string ();
2710 const char *get_ident ();
2711 void eat_ident (const char *);
2712 const char *get_number ();
2714 id_base
*parse_operation ();
2715 operand
*parse_capture (operand
*);
2716 operand
*parse_expr ();
2717 c_expr
*parse_c_expr (cpp_ttype
);
2718 operand
*parse_op ();
2720 void record_operlist (source_location
, user_id
*);
2722 void parse_pattern ();
2723 void push_simplify (vec
<simplify
*>&, operand
*, source_location
,
2724 operand
*, source_location
);
2725 void parse_simplify (source_location
, vec
<simplify
*>&, predicate_id
*,
2727 void parse_for (source_location
);
2728 void parse_if (source_location
);
2729 void parse_predicates (source_location
);
2730 void parse_operator_list (source_location
);
2733 vec
<if_or_with
> active_ifs
;
2734 vec
<vec
<user_id
*> > active_fors
;
2735 hash_set
<user_id
*> *oper_lists_set
;
2736 vec
<user_id
*> oper_lists
;
2738 cid_map_t
*capture_ids
;
2741 vec
<simplify
*> simplifiers
;
2742 vec
<predicate_id
*> user_predicates
;
2743 bool parsing_match_operand
;
2746 /* Lexing helpers. */
2748 /* Read the next non-whitespace token from R. */
2753 const cpp_token
*token
;
2756 token
= cpp_get_token (r
);
2758 while (token
->type
== CPP_PADDING
2759 && token
->type
!= CPP_EOF
);
2763 /* Peek at the next non-whitespace token from R. */
2768 const cpp_token
*token
;
2772 token
= cpp_peek_token (r
, i
++);
2774 while (token
->type
== CPP_PADDING
2775 && token
->type
!= CPP_EOF
);
2776 /* If we peek at EOF this is a fatal error as it leaves the
2777 cpp_reader in unusable state. Assume we really wanted a
2778 token and thus this EOF is unexpected. */
2779 if (token
->type
== CPP_EOF
)
2780 fatal_at (token
, "unexpected end of file");
2784 /* Peek at the next identifier token (or return NULL if the next
2785 token is not an identifier or equal to ID if supplied). */
2788 parser::peek_ident (const char *id
)
2790 const cpp_token
*token
= peek ();
2791 if (token
->type
!= CPP_NAME
)
2797 const char *t
= (const char *) CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
2798 if (strcmp (id
, t
) == 0)
2804 /* Read the next token from R and assert it is of type TK. */
2807 parser::expect (enum cpp_ttype tk
)
2809 const cpp_token
*token
= next ();
2810 if (token
->type
!= tk
)
2811 fatal_at (token
, "expected %s, got %s",
2812 cpp_type2name (tk
, 0), cpp_type2name (token
->type
, 0));
2817 /* Consume the next token from R and assert it is of type TK. */
2820 parser::eat_token (enum cpp_ttype tk
)
2825 /* Read the next token from R and assert it is of type CPP_STRING and
2826 return its value. */
2829 parser::get_string ()
2831 const cpp_token
*token
= expect (CPP_STRING
);
2832 return (const char *)token
->val
.str
.text
;
2835 /* Read the next token from R and assert it is of type CPP_NAME and
2836 return its value. */
2839 parser::get_ident ()
2841 const cpp_token
*token
= expect (CPP_NAME
);
2842 return (const char *)CPP_HASHNODE (token
->val
.node
.node
)->ident
.str
;
2845 /* Eat an identifier token with value S from R. */
2848 parser::eat_ident (const char *s
)
2850 const cpp_token
*token
= peek ();
2851 const char *t
= get_ident ();
2852 if (strcmp (s
, t
) != 0)
2853 fatal_at (token
, "expected '%s' got '%s'\n", s
, t
);
2856 /* Read the next token from R and assert it is of type CPP_NUMBER and
2857 return its value. */
2860 parser::get_number ()
2862 const cpp_token
*token
= expect (CPP_NUMBER
);
2863 return (const char *)token
->val
.str
.text
;
2867 /* Record an operator-list use for transparent for handling. */
2870 parser::record_operlist (source_location loc
, user_id
*p
)
2872 if (!oper_lists_set
->add (p
))
2874 if (!oper_lists
.is_empty ()
2875 && oper_lists
[0]->substitutes
.length () != p
->substitutes
.length ())
2876 fatal_at (loc
, "User-defined operator list does not have the "
2877 "same number of entries as others used in the pattern");
2878 oper_lists
.safe_push (p
);
2882 /* Parse the operator ID, special-casing convert?, convert1? and
2886 parser::parse_operation ()
2888 const cpp_token
*id_tok
= peek ();
2889 const char *id
= get_ident ();
2890 const cpp_token
*token
= peek ();
2891 if (strcmp (id
, "convert0") == 0)
2892 fatal_at (id_tok
, "use 'convert?' here");
2893 if (token
->type
== CPP_QUERY
2894 && !(token
->flags
& PREV_WHITE
))
2896 if (strcmp (id
, "convert") == 0)
2898 else if (strcmp (id
, "convert1") == 0)
2900 else if (strcmp (id
, "convert2") == 0)
2903 fatal_at (id_tok
, "non-convert operator conditionalized");
2905 if (!parsing_match_operand
)
2906 fatal_at (id_tok
, "conditional convert can only be used in "
2907 "match expression");
2908 eat_token (CPP_QUERY
);
2910 else if (strcmp (id
, "convert1") == 0
2911 || strcmp (id
, "convert2") == 0)
2912 fatal_at (id_tok
, "expected '?' after conditional operator");
2913 id_base
*op
= get_operator (id
);
2915 fatal_at (id_tok
, "unknown operator %s", id
);
2917 user_id
*p
= dyn_cast
<user_id
*> (op
);
2918 if (p
&& p
->is_oper_list
)
2920 if (active_fors
.length() == 0)
2921 record_operlist (id_tok
->src_loc
, p
);
2923 fatal_at (id_tok
, "operator-list %s cannot be exapnded inside 'for'", id
);
2929 capture = '@'<number> */
2932 parser::parse_capture (operand
*op
)
2934 eat_token (CPP_ATSIGN
);
2935 const cpp_token
*token
= peek ();
2936 const char *id
= NULL
;
2937 if (token
->type
== CPP_NUMBER
)
2939 else if (token
->type
== CPP_NAME
)
2942 fatal_at (token
, "expected number or identifier");
2943 unsigned next_id
= capture_ids
->elements ();
2945 unsigned &num
= capture_ids
->get_or_insert (id
, &existed
);
2948 return new capture (num
, op
);
2951 /* Parse an expression
2952 expr = '(' <operation>[capture][flag][type] <operand>... ')' */
2955 parser::parse_expr ()
2957 expr
*e
= new expr (parse_operation ());
2958 const cpp_token
*token
= peek ();
2960 bool is_commutative
= false;
2961 const char *expr_type
= NULL
;
2963 if (token
->type
== CPP_COLON
2964 && !(token
->flags
& PREV_WHITE
))
2966 eat_token (CPP_COLON
);
2968 if (token
->type
== CPP_NAME
2969 && !(token
->flags
& PREV_WHITE
))
2971 const char *s
= get_ident ();
2972 if (s
[0] == 'c' && !s
[1])
2974 if (!parsing_match_operand
)
2976 "flag 'c' can only be used in match expression");
2977 is_commutative
= true;
2979 else if (s
[1] != '\0')
2981 if (parsing_match_operand
)
2982 fatal_at (token
, "type can only be used in result expression");
2986 fatal_at (token
, "flag %s not recognized", s
);
2991 fatal_at (token
, "expected flag or type specifying identifier");
2994 if (token
->type
== CPP_ATSIGN
2995 && !(token
->flags
& PREV_WHITE
))
2996 op
= parse_capture (e
);
3001 const cpp_token
*token
= peek ();
3002 if (token
->type
== CPP_CLOSE_PAREN
)
3004 if (e
->operation
->nargs
!= -1
3005 && e
->operation
->nargs
!= (int) e
->ops
.length ())
3006 fatal_at (token
, "'%s' expects %u operands, not %u",
3007 e
->operation
->id
, e
->operation
->nargs
, e
->ops
.length ());
3010 if (e
->ops
.length () == 2)
3011 e
->is_commutative
= true;
3013 fatal_at (token
, "only binary operators or function with "
3014 "two arguments can be marked commutative");
3016 e
->expr_type
= expr_type
;
3019 e
->append_op (parse_op ());
3024 /* Lex native C code delimited by START recording the preprocessing tokens
3025 for later processing.
3026 c_expr = ('{'|'(') <pp token>... ('}'|')') */
3029 parser::parse_c_expr (cpp_ttype start
)
3031 const cpp_token
*token
;
3034 vec
<cpp_token
> code
= vNULL
;
3035 unsigned nr_stmts
= 0;
3037 if (start
== CPP_OPEN_PAREN
)
3038 end
= CPP_CLOSE_PAREN
;
3039 else if (start
== CPP_OPEN_BRACE
)
3040 end
= CPP_CLOSE_BRACE
;
3048 /* Count brace pairs to find the end of the expr to match. */
3049 if (token
->type
== start
)
3051 else if (token
->type
== end
3055 /* This is a lame way of counting the number of statements. */
3056 if (token
->type
== CPP_SEMICOLON
)
3059 /* If this is possibly a user-defined identifier mark it used. */
3060 if (token
->type
== CPP_NAME
)
3062 id_base
*idb
= get_operator ((const char *)CPP_HASHNODE
3063 (token
->val
.node
.node
)->ident
.str
);
3065 if (idb
&& (p
= dyn_cast
<user_id
*> (idb
)) && p
->is_oper_list
)
3066 record_operlist (token
->src_loc
, p
);
3069 /* Record the token. */
3070 code
.safe_push (*token
);
3073 return new c_expr (r
, code
, nr_stmts
, vNULL
, capture_ids
);
3076 /* Parse an operand which is either an expression, a predicate or
3077 a standalone capture.
3078 op = predicate | expr | c_expr | capture */
3083 const cpp_token
*token
= peek ();
3084 struct operand
*op
= NULL
;
3085 if (token
->type
== CPP_OPEN_PAREN
)
3087 eat_token (CPP_OPEN_PAREN
);
3089 eat_token (CPP_CLOSE_PAREN
);
3091 else if (token
->type
== CPP_OPEN_BRACE
)
3093 op
= parse_c_expr (CPP_OPEN_BRACE
);
3097 /* Remaining ops are either empty or predicates */
3098 if (token
->type
== CPP_NAME
)
3100 const char *id
= get_ident ();
3101 id_base
*opr
= get_operator (id
);
3103 fatal_at (token
, "expected predicate name");
3104 if (operator_id
*code
= dyn_cast
<operator_id
*> (opr
))
3106 if (code
->nargs
!= 0)
3107 fatal_at (token
, "using an operator with operands as predicate");
3108 /* Parse the zero-operand operator "predicates" as
3110 op
= new expr (opr
);
3112 else if (user_id
*code
= dyn_cast
<user_id
*> (opr
))
3114 if (code
->nargs
!= 0)
3115 fatal_at (token
, "using an operator with operands as predicate");
3116 /* Parse the zero-operand operator "predicates" as
3118 op
= new expr (opr
);
3120 else if (predicate_id
*p
= dyn_cast
<predicate_id
*> (opr
))
3121 op
= new predicate (p
);
3123 fatal_at (token
, "using an unsupported operator as predicate");
3124 if (!parsing_match_operand
)
3125 fatal_at (token
, "predicates are only allowed in match expression");
3127 if (token
->flags
& PREV_WHITE
)
3130 else if (token
->type
!= CPP_COLON
3131 && token
->type
!= CPP_ATSIGN
)
3132 fatal_at (token
, "expected expression or predicate");
3133 /* optionally followed by a capture and a predicate. */
3134 if (token
->type
== CPP_COLON
)
3135 fatal_at (token
, "not implemented: predicate on leaf operand");
3136 if (token
->type
== CPP_ATSIGN
)
3137 op
= parse_capture (op
);
3143 /* Create a new simplify from the current parsing state and MATCH,
3144 MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
3147 parser::push_simplify (vec
<simplify
*>& simplifiers
,
3148 operand
*match
, source_location match_loc
,
3149 operand
*result
, source_location result_loc
)
3151 /* Build and push a temporary for for operator list uses in expressions. */
3152 if (!oper_lists
.is_empty ())
3153 active_fors
.safe_push (oper_lists
);
3155 simplifiers
.safe_push
3156 (new simplify (match
, match_loc
, result
, result_loc
,
3157 active_ifs
.copy (), active_fors
.copy (), capture_ids
));
3159 if (!oper_lists
.is_empty ())
3164 simplify = 'simplify' <expr> <result-op>
3166 match = 'match' <ident> <expr> [<result-op>]
3168 <result-op> = <op> | <if> | <with>
3169 <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
3170 <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
3171 and fill SIMPLIFIERS with the results. */
3174 parser::parse_simplify (source_location match_location
,
3175 vec
<simplify
*>& simplifiers
, predicate_id
*matcher
,
3178 /* Reset the capture map. */
3180 capture_ids
= new cid_map_t
;
3181 /* Reset oper_lists and set. */
3182 hash_set
<user_id
*> olist
;
3183 oper_lists_set
= &olist
;
3186 const cpp_token
*loc
= peek ();
3187 parsing_match_operand
= true;
3188 struct operand
*match
= parse_op ();
3189 parsing_match_operand
= false;
3190 if (match
->type
== operand::OP_CAPTURE
&& !matcher
)
3191 fatal_at (loc
, "outermost expression cannot be captured");
3192 if (match
->type
== operand::OP_EXPR
3193 && is_a
<predicate_id
*> (as_a
<expr
*> (match
)->operation
))
3194 fatal_at (loc
, "outermost expression cannot be a predicate");
3196 const cpp_token
*token
= peek ();
3198 /* If this if is immediately closed then it is part of a predicate
3199 definition. Push it. */
3200 if (token
->type
== CPP_CLOSE_PAREN
)
3203 fatal_at (token
, "expected transform expression");
3204 push_simplify (simplifiers
, match
, match_location
,
3205 result
, token
->src_loc
);
3209 unsigned active_ifs_len
= active_ifs
.length ();
3212 if (token
->type
== CPP_OPEN_PAREN
)
3214 source_location paren_loc
= token
->src_loc
;
3215 eat_token (CPP_OPEN_PAREN
);
3216 if (peek_ident ("if"))
3219 active_ifs
.safe_push (if_or_with (parse_c_expr (CPP_OPEN_PAREN
),
3220 token
->src_loc
, false));
3221 /* If this if is immediately closed then it contains a
3222 manual matcher or is part of a predicate definition.
3224 if (peek ()->type
== CPP_CLOSE_PAREN
)
3227 fatal_at (token
, "manual transform not implemented");
3228 push_simplify (simplifiers
, match
, match_location
,
3232 else if (peek_ident ("with"))
3235 /* Parse (with c-expr expr) as (if-with (true) expr). */
3236 c_expr
*e
= parse_c_expr (CPP_OPEN_BRACE
);
3238 active_ifs
.safe_push (if_or_with (e
, token
->src_loc
, true));
3242 operand
*op
= result
;
3245 push_simplify (simplifiers
, match
, match_location
,
3246 op
, token
->src_loc
);
3247 eat_token (CPP_CLOSE_PAREN
);
3248 /* A "default" result closes the enclosing scope. */
3249 if (active_ifs
.length () > active_ifs_len
)
3251 eat_token (CPP_CLOSE_PAREN
);
3258 else if (token
->type
== CPP_CLOSE_PAREN
)
3260 /* Close a scope if requested. */
3261 if (active_ifs
.length () > active_ifs_len
)
3263 eat_token (CPP_CLOSE_PAREN
);
3273 fatal_at (token
, "expected match operand expression");
3274 push_simplify (simplifiers
, match
, match_location
,
3275 matcher
? result
: parse_op (), token
->src_loc
);
3276 /* A "default" result closes the enclosing scope. */
3277 if (active_ifs
.length () > active_ifs_len
)
3279 eat_token (CPP_CLOSE_PAREN
);
3289 /* Parsing of the outer control structures. */
3291 /* Parse a for expression
3292 for = '(' 'for' <subst>... <pattern> ')'
3293 subst = <ident> '(' <ident>... ')' */
3296 parser::parse_for (source_location
)
3298 auto_vec
<const cpp_token
*> user_id_tokens
;
3299 vec
<user_id
*> user_ids
= vNULL
;
3300 const cpp_token
*token
;
3301 unsigned min_n_opers
= 0, max_n_opers
= 0;
3306 if (token
->type
!= CPP_NAME
)
3309 /* Insert the user defined operators into the operator hash. */
3310 const char *id
= get_ident ();
3311 if (get_operator (id
) != NULL
)
3312 fatal_at (token
, "operator already defined");
3313 user_id
*op
= new user_id (id
);
3314 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
3316 user_ids
.safe_push (op
);
3317 user_id_tokens
.safe_push (token
);
3319 eat_token (CPP_OPEN_PAREN
);
3322 while ((token
= peek_ident ()) != 0)
3324 const char *oper
= get_ident ();
3325 id_base
*idb
= get_operator (oper
);
3327 fatal_at (token
, "no such operator '%s'", oper
);
3328 if (*idb
== CONVERT0
|| *idb
== CONVERT1
|| *idb
== CONVERT2
)
3329 fatal_at (token
, "conditional operators cannot be used inside for");
3333 else if (idb
->nargs
== -1)
3335 else if (idb
->nargs
!= arity
)
3336 fatal_at (token
, "operator '%s' with arity %d does not match "
3337 "others with arity %d", oper
, idb
->nargs
, arity
);
3339 user_id
*p
= dyn_cast
<user_id
*> (idb
);
3342 if (p
->is_oper_list
)
3343 op
->substitutes
.safe_splice (p
->substitutes
);
3345 fatal_at (token
, "iterator cannot be used as operator-list");
3348 op
->substitutes
.safe_push (idb
);
3351 token
= expect (CPP_CLOSE_PAREN
);
3353 unsigned nsubstitutes
= op
->substitutes
.length ();
3354 if (nsubstitutes
== 0)
3355 fatal_at (token
, "A user-defined operator must have at least "
3356 "one substitution");
3357 if (max_n_opers
== 0)
3359 min_n_opers
= nsubstitutes
;
3360 max_n_opers
= nsubstitutes
;
3364 if (nsubstitutes
% min_n_opers
!= 0
3365 && min_n_opers
% nsubstitutes
!= 0)
3366 fatal_at (token
, "All user-defined identifiers must have a "
3367 "multiple number of operator substitutions of the "
3368 "smallest number of substitutions");
3369 if (nsubstitutes
< min_n_opers
)
3370 min_n_opers
= nsubstitutes
;
3371 else if (nsubstitutes
> max_n_opers
)
3372 max_n_opers
= nsubstitutes
;
3376 unsigned n_ids
= user_ids
.length ();
3378 fatal_at (token
, "for requires at least one user-defined identifier");
3381 if (token
->type
== CPP_CLOSE_PAREN
)
3382 fatal_at (token
, "no pattern defined in for");
3384 active_fors
.safe_push (user_ids
);
3388 if (token
->type
== CPP_CLOSE_PAREN
)
3394 /* Remove user-defined operators from the hash again. */
3395 for (unsigned i
= 0; i
< user_ids
.length (); ++i
)
3397 if (!user_ids
[i
]->used
)
3398 warning_at (user_id_tokens
[i
],
3399 "operator %s defined but not used", user_ids
[i
]->id
);
3400 operators
->remove_elt (user_ids
[i
]);
3404 /* Parse an identifier associated with a list of operators.
3405 oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
3408 parser::parse_operator_list (source_location
)
3410 const cpp_token
*token
= peek ();
3411 const char *id
= get_ident ();
3413 if (get_operator (id
) != 0)
3414 fatal_at (token
, "operator %s already defined", id
);
3416 user_id
*op
= new user_id (id
, true);
3419 while ((token
= peek_ident ()) != 0)
3422 const char *oper
= get_ident ();
3423 id_base
*idb
= get_operator (oper
);
3426 fatal_at (token
, "no such operator '%s'", oper
);
3430 else if (idb
->nargs
== -1)
3432 else if (arity
!= idb
->nargs
)
3433 fatal_at (token
, "operator '%s' with arity %d does not match "
3434 "others with arity %d", oper
, idb
->nargs
, arity
);
3436 /* We allow composition of multiple operator lists. */
3437 if (user_id
*p
= dyn_cast
<user_id
*> (idb
))
3438 op
->substitutes
.safe_splice (p
->substitutes
);
3440 op
->substitutes
.safe_push (idb
);
3443 // Check that there is no junk after id-list
3445 if (token
->type
!= CPP_CLOSE_PAREN
)
3446 fatal_at (token
, "expected identifier got %s", cpp_type2name (token
->type
, 0));
3448 if (op
->substitutes
.length () == 0)
3449 fatal_at (token
, "operator-list cannot be empty");
3452 id_base
**slot
= operators
->find_slot_with_hash (op
, op
->hashval
, INSERT
);
3456 /* Parse an outer if expression.
3457 if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
3460 parser::parse_if (source_location loc
)
3462 operand
*ifexpr
= parse_c_expr (CPP_OPEN_PAREN
);
3464 const cpp_token
*token
= peek ();
3465 if (token
->type
== CPP_CLOSE_PAREN
)
3466 fatal_at (token
, "no pattern defined in if");
3468 active_ifs
.safe_push (if_or_with (ifexpr
, loc
, false));
3471 const cpp_token
*token
= peek ();
3472 if (token
->type
== CPP_CLOSE_PAREN
)
3480 /* Parse a list of predefined predicate identifiers.
3481 preds = '(' 'define_predicates' <ident>... ')' */
3484 parser::parse_predicates (source_location
)
3488 const cpp_token
*token
= peek ();
3489 if (token
->type
!= CPP_NAME
)
3492 add_predicate (get_ident ());
3497 /* Parse outer control structures.
3498 pattern = <preds>|<for>|<if>|<simplify>|<match> */
3501 parser::parse_pattern ()
3503 /* All clauses start with '('. */
3504 eat_token (CPP_OPEN_PAREN
);
3505 const cpp_token
*token
= peek ();
3506 const char *id
= get_ident ();
3507 if (strcmp (id
, "simplify") == 0)
3509 parse_simplify (token
->src_loc
, simplifiers
, NULL
, NULL
);
3512 else if (strcmp (id
, "match") == 0)
3514 bool with_args
= false;
3515 if (peek ()->type
== CPP_OPEN_PAREN
)
3517 eat_token (CPP_OPEN_PAREN
);
3520 const char *name
= get_ident ();
3521 id_base
*id
= get_operator (name
);
3525 p
= add_predicate (name
);
3526 user_predicates
.safe_push (p
);
3528 else if ((p
= dyn_cast
<predicate_id
*> (id
)))
3531 fatal_at (token
, "cannot add a match to a non-predicate ID");
3532 /* Parse (match <id> <arg>... (match-expr)) here. */
3536 capture_ids
= new cid_map_t
;
3538 while (peek ()->type
== CPP_ATSIGN
)
3539 e
->append_op (parse_capture (NULL
));
3540 eat_token (CPP_CLOSE_PAREN
);
3543 && ((e
&& e
->ops
.length () != (unsigned)p
->nargs
)
3544 || (!e
&& p
->nargs
!= 0)))
3545 fatal_at (token
, "non-matching number of match operands");
3546 p
->nargs
= e
? e
->ops
.length () : 0;
3547 parse_simplify (token
->src_loc
, p
->matchers
, p
, e
);
3550 else if (strcmp (id
, "for") == 0)
3551 parse_for (token
->src_loc
);
3552 else if (strcmp (id
, "if") == 0)
3553 parse_if (token
->src_loc
);
3554 else if (strcmp (id
, "define_predicates") == 0)
3556 if (active_ifs
.length () > 0
3557 || active_fors
.length () > 0)
3558 fatal_at (token
, "define_predicates inside if or for is not supported");
3559 parse_predicates (token
->src_loc
);
3561 else if (strcmp (id
, "define_operator_list") == 0)
3563 if (active_ifs
.length () > 0
3564 || active_fors
.length () > 0)
3565 fatal_at (token
, "operator-list inside if or for is not supported");
3566 parse_operator_list (token
->src_loc
);
3569 fatal_at (token
, "expected %s'simplify', 'match', 'for' or 'if'",
3570 active_ifs
.length () == 0 && active_fors
.length () == 0
3571 ? "'define_predicates', " : "");
3573 eat_token (CPP_CLOSE_PAREN
);
3576 /* Main entry of the parser. Repeatedly parse outer control structures. */
3578 parser::parser (cpp_reader
*r_
)
3582 active_fors
= vNULL
;
3583 simplifiers
= vNULL
;
3584 oper_lists_set
= NULL
;
3587 user_predicates
= vNULL
;
3588 parsing_match_operand
= false;
3590 const cpp_token
*token
= next ();
3591 while (token
->type
!= CPP_EOF
)
3593 _cpp_backup_tokens (r
, 1);
3600 /* Helper for the linemap code. */
3603 round_alloc_size (size_t s
)
3609 /* The genmatch generator progam. It reads from a pattern description
3610 and outputs GIMPLE or GENERIC IL matching and simplification routines. */
3613 main (int argc
, char **argv
)
3617 progname
= "genmatch";
3623 bool verbose
= false;
3624 char *input
= argv
[argc
-1];
3625 for (int i
= 1; i
< argc
- 1; ++i
)
3627 if (strcmp (argv
[i
], "--gimple") == 0)
3629 else if (strcmp (argv
[i
], "--generic") == 0)
3631 else if (strcmp (argv
[i
], "-v") == 0)
3635 fprintf (stderr
, "Usage: genmatch "
3636 "[--gimple] [--generic] [-v] input\n");
3641 line_table
= XCNEW (struct line_maps
);
3642 linemap_init (line_table
, 0);
3643 line_table
->reallocator
= xrealloc
;
3644 line_table
->round_alloc_size
= round_alloc_size
;
3646 r
= cpp_create_reader (CLK_GNUC99
, NULL
, line_table
);
3647 cpp_callbacks
*cb
= cpp_get_callbacks (r
);
3648 cb
->error
= error_cb
;
3650 if (!cpp_read_main_file (r
, input
))
3652 cpp_define (r
, gimple
? "GIMPLE=1": "GENERIC=1");
3653 cpp_define (r
, gimple
? "GENERIC=0": "GIMPLE=0");
3655 /* Pre-seed operators. */
3656 operators
= new hash_table
<id_base
> (1024);
3657 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
3658 add_operator (SYM, # SYM, # TYPE, NARGS);
3659 #define END_OF_BASE_TREE_CODES
3661 add_operator (CONVERT0
, "CONVERT0", "tcc_unary", 1);
3662 add_operator (CONVERT1
, "CONVERT1", "tcc_unary", 1);
3663 add_operator (CONVERT2
, "CONVERT2", "tcc_unary", 1);
3664 #undef END_OF_BASE_TREE_CODES
3667 /* Pre-seed builtin functions.
3668 ??? Cannot use N (name) as that is targetm.emultls.get_address
3669 for BUILT_IN_EMUTLS_GET_ADDRESS ... */
3670 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
3671 add_builtin (ENUM, # ENUM);
3672 #include "builtins.def"
3679 write_header (stdout
, "gimple-match-head.c");
3681 write_header (stdout
, "generic-match-head.c");
3683 /* Go over all predicates defined with patterns and perform
3684 lowering and code generation. */
3685 for (unsigned i
= 0; i
< p
.user_predicates
.length (); ++i
)
3687 predicate_id
*pred
= p
.user_predicates
[i
];
3688 lower (pred
->matchers
, gimple
);
3691 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
3692 print_matches (pred
->matchers
[i
]);
3695 for (unsigned i
= 0; i
< pred
->matchers
.length (); ++i
)
3696 dt
.insert (pred
->matchers
[i
], i
);
3701 write_predicate (stdout
, pred
, dt
, gimple
);
3704 /* Lower the main simplifiers and generate code for them. */
3705 lower (p
.simplifiers
, gimple
);
3708 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
3709 print_matches (p
.simplifiers
[i
]);
3712 for (unsigned i
= 0; i
< p
.simplifiers
.length (); ++i
)
3713 dt
.insert (p
.simplifiers
[i
], i
);
3719 dt
.gen_gimple (stdout
);
3721 dt
.gen_generic (stdout
);
3724 cpp_finish (r
, NULL
);