This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[match-and-simplify] adjust for pattern to allow multiple ids
- From: Prathamesh Kulkarni <bilbotheelffriend at gmail dot com>
- To: Richard Biener <richard dot guenther at gmail dot com>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 8 Sep 2014 03:23:49 +0530
- Subject: [match-and-simplify] adjust for pattern to allow multiple ids
- Authentication-results: sourceware.org; auth=none
Changes syntax of for pattern to:
(for op (list1...) op2 (list2...) opN (listN...)
patterns)
Number of operator substitutions must be same.
* genmatch.c (peek_ident): Change id to default argument with value 0.
(parse_for): Adjust.
* match-builtin.pd: Rewrite for-patterns to match new syntax.
* match-comparison.pd: Likewise.
* match-constant-folding.pd: Likewise.
* match-conversions.pd: Likewise.
* match-rotate.pd: Likewise.
Index: gcc/genmatch.c
===================================================================
--- gcc/genmatch.c (revision 215002)
+++ gcc/genmatch.c (working copy)
@@ -832,7 +832,7 @@
return ne;
}
-
+
void
check_no_user_id (operand *o)
{
@@ -2023,12 +2023,15 @@
}
static const cpp_token *
-peek_ident (cpp_reader *r, const char *id)
+peek_ident (cpp_reader *r, const char *id = 0)
{
const cpp_token *token = peek (r);
if (token->type != CPP_NAME)
return 0;
+ if (id == 0)
+ return token;
+
const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
if (strcmp (id, t) == 0)
return token;
@@ -2398,62 +2401,101 @@
void parse_pattern (cpp_reader *, vec<simplify *>&);
void
-parse_for (cpp_reader *r, source_location, vec<simplify *>& simplifiers)
+parse_for (cpp_reader *r, source_location, vec<simplify *>& simplifiers)
{
- const char *user_id = get_ident (r);
- eat_ident (r, "in");
+ vec<const char *> user_ids = vNULL;
+ vec< vec<const char *> > opers_vec = vNULL;
+ const cpp_token *token;
+ unsigned n_opers = 0;
- if (get_operator (user_id) != 0)
- fatal_at (peek (r), "%s is an operator, cannot be used as variable in for", user_id);
-
- vec<const char *> opers = vNULL;
-
- const cpp_token *token;
while (1)
{
- token = peek (r);
- if (token->type != CPP_NAME)
+ token = peek_ident (r);
+ if (token == 0)
break;
- const char *id = get_ident (r);
- id_base *idb = get_operator (id);
- if (*idb == CONVERT0 || *idb == CONVERT1 || *idb == CONVERT2)
- fatal_at (token, "conditional operators cannot be used inside for");
- opers.safe_push (id);
- }
- if (token->type == CPP_CLOSE_PAREN)
- fatal_at (token, "no pattern defined in for");
+ const char *id = (const char *) NODE_NAME (token->val.node.node);
+ if (get_operator (id))
+ fatal ("built-in operators cannot be user defined identifiers in for");
+ user_ids.safe_push (id);
+ eat_token (r, CPP_NAME);
+
+ eat_token (r, CPP_OPEN_PAREN);
+
+ vec<const char *> opers = vNULL;
+
+ while ((token = peek_ident (r)) != 0)
+ {
+ eat_token (r, CPP_NAME);
+ const char *oper = (const char *) NODE_NAME (token->val.node.node);
+ id_base *idb = get_operator (oper);
+ if (idb == 0)
+ fatal_at (token, "no such operator %s", oper);
+ if (*idb == CONVERT0 || *idb == CONVERT1 || *idb == CONVERT2)
+ fatal_at (token, "conditional operators cannot be used inside for");
+
+ opers.safe_push (oper);
+ }
+ opers_vec.safe_push (opers);
+ if (n_opers == 0)
+ n_opers = opers.length ();
+ else if (n_opers != opers.length ())
+ fatal ("All user-defined identifiers must have same number of operator substitutions");
+ eat_token (r, CPP_CLOSE_PAREN);
+ }
+
+ if (user_ids.length () == 0)
+ fatal ("for requires at least one user-defined identifier");
+
+ fprintf (stderr, "Supriya\n");
+
+ vec<simplify *> for_simplifiers = vNULL;
while (1)
{
- const cpp_token *token = peek (r);
+ token = peek (r);
if (token->type == CPP_CLOSE_PAREN)
- break;
-
- vec<simplify *> for_simplifiers = vNULL;
+ break;
parse_pattern (r, for_simplifiers);
+ }
- for (unsigned i = 0; i < opers.length (); ++i)
+ if (for_simplifiers.length () == 0)
+ fatal ("no pattern defined in for");
+
+ unsigned n_ids = user_ids.length ();
+
+ for (unsigned ix = 0; ix < for_simplifiers.length (); ++ix)
+ {
+ simplify *s = for_simplifiers[ix];
+
+ for (unsigned j = 0; j < n_opers; ++j)
{
- id_base *idb = get_operator (opers[i]);
- gcc_assert (idb);
+ vec<const char *> opers = vNULL;
+ for (unsigned i = 0; i < opers_vec.length (); ++i)
+ opers.safe_push (opers_vec[i][j]);
+
+ operand *match_op = s->match;
+ operand *result_op = s->result;
+ vec<if_or_with> ifexpr_vec = vNULL;
- for (unsigned j = 0; j < for_simplifiers.length (); ++j)
+ for (unsigned i = 0; i < s->ifexpr_vec.length (); ++i)
+ ifexpr_vec.safe_push (if_or_with (s->ifexpr_vec[i].cexpr, s->ifexpr_vec[i].location, s->ifexpr_vec[i].is_with));
+
+ for (unsigned i = 0; i < n_ids; ++i)
{
- simplify *s = for_simplifiers[j];
- operand *match_op = replace_id (s->match, user_id, opers[i]);
- operand *result_op = replace_id (s->result, user_id, opers[i]);
- vec<if_or_with> ifexpr_vec = vNULL;
- for (unsigned j = 0; j < s->ifexpr_vec.length (); ++j)
- ifexpr_vec.safe_push (if_or_with (replace_id (s->ifexpr_vec[j].cexpr, user_id, opers[i]), s->ifexpr_vec[j].location, s->ifexpr_vec[j].is_with));
- simplify *ns = new simplify (s->name, match_op, s->match_location,
- result_op, s->result_location, ifexpr_vec);
+ match_op = replace_id (match_op, user_ids[i], opers[i]);
+ result_op = replace_id (result_op, user_ids[i], opers[i]);
- simplifiers.safe_push (ns);
+ for (unsigned k = 0; k < s->ifexpr_vec.length (); ++k)
+ ifexpr_vec[k].cexpr = replace_id (ifexpr_vec[k].cexpr, user_ids[i], opers[i]);
+
}
+ simplify *ns = new simplify (s->name, match_op, s->match_location,
+ result_op, s->result_location, ifexpr_vec);
+ simplifiers.safe_push (ns);
}
}
-}
+}
void
parse_if (cpp_reader *r, source_location loc, vec<simplify *>& simplifiers)
@@ -2616,3 +2658,4 @@
return 0;
}
+
Index: gcc/match-builtin.pd
===================================================================
--- gcc/match-builtin.pd (revision 215002)
+++ gcc/match-builtin.pd (working copy)
@@ -44,7 +44,7 @@
/* From fold_builtin_fabs and fold_builtin_abs. */
/* Fold a call to fabs, fabsf or fabsl, to abs, labs, llabs or imaxabs. */
-(for fn in BUILT_IN_FABS BUILT_IN_FABSF BUILT_IN_FABSL BUILT_IN_ABS BUILT_IN_LABS BUILT_IN_LLABS BUILT_IN_IMAXABS
+(for fn (BUILT_IN_FABS BUILT_IN_FABSF BUILT_IN_FABSL BUILT_IN_ABS BUILT_IN_LABS BUILT_IN_LLABS BUILT_IN_IMAXABS)
(simplify
(fn @0)
(abs @0)))
@@ -80,7 +80,7 @@
/* Strip sign ops from even integer powers.
??? The code in builtins.c manages to perform this recursively
through the whole expression in arg0 of pow. */
-(for sgnop in abs negate
+(for sgnop (abs negate)
(simplify
(BUILT_IN_POW (sgnop @0) REAL_CST@1)
(with
@@ -98,12 +98,12 @@
/* From fold_builtin_sqrt. */
(if (flag_unsafe_math_optimizations)
/* Optimize sqrt(expN(x)) = expN(x*0.5). */
- (for expfn in BUILT_IN_EXP10 BUILT_IN_POW10 BUILT_IN_EXP BUILT_IN_EXP2
+ (for expfn (BUILT_IN_EXP10 BUILT_IN_POW10 BUILT_IN_EXP BUILT_IN_EXP2)
(simplify
(BUILT_IN_SQRT (expfn @0))
(expfn (mult @0 { build_real (type, dconsthalf); }))))
/* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
- (for rootfn in BUILT_IN_SQRT BUILT_IN_CBRT
+ (for rootfn (BUILT_IN_SQRT BUILT_IN_CBRT)
(simplify
(BUILT_IN_SQRT (rootfn @0))
(with
Index: gcc/match-comparison.pd
===================================================================
--- gcc/match-comparison.pd (revision 215002)
+++ gcc/match-comparison.pd (working copy)
@@ -7,7 +7,7 @@
(convert @0)))
/* Distribute operations in equality compares. */
-(for op in eq ne
+(for op (eq ne)
/* -exp op CST is exp op -CST. */
(simplify
(op (negate @0) INTEGER_CST@1)
@@ -22,8 +22,8 @@
/* From fold_comparison, in the order of transforms in there. */
/* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
-(for cmp in lt le eq ge gt ne
- (for op in plus minus
+(for cmp (lt le eq ge gt ne)
+ (for op (plus minus)
(simplify
(cmp (op @0 INTEGER_CST@1) INTEGER_CST@2)
(if ((cmp == NE_EXPR || cmp == EQ_EXPR
@@ -40,7 +40,7 @@
is undefined for the type, but performing it here badly interacts
with the transformation in fold_cond_expr_with_comparison which
attempts to synthetize ABS_EXPR. */
-(for cmp in eq ne
+(for cmp (eq ne)
(simplify
(cmp (minus @0 @1) integer_zerop)
(cmp @0 @1)))
@@ -58,7 +58,7 @@
#endif
/* Simplify X * C1 CMP 0 to X CMP 0 if C1 is not zero. */
-(for op in lt le eq ne ge gt
+(for op (lt le eq ne ge gt)
(simplify
(op (mult @0 INTEGER_CST@1) integer_zerop@2)
/* In fold-const.c we have this and the following pattern
@@ -89,7 +89,7 @@
/* Simplify comparison of something with itself. For IEEE
floating-point, we can only do some of these simplifications. */
-(for cmp in ge le
+(for cmp (ge le)
(simplify
(cmp @0 @0)
(eq @0 @0)))
@@ -98,7 +98,7 @@
(if (! FLOAT_TYPE_P (TREE_TYPE (@0))
|| ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
{ constant_boolean_node (true, type); }))
-(for cmp in ne gt lt
+(for cmp (ne gt lt)
(simplify
(cmp @0 @0)
(if (cmp != NE_EXPR
@@ -124,13 +124,13 @@
#endif
/* Fold ~X op ~Y as Y op X. */
-(for cmp in lt le eq ge gt ne
+(for cmp (lt le eq ge gt ne)
(simplify
(cmp (bit_not @0) (bit_not @1))
(cmp @1 @0)))
/* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
-(for cmp in lt le eq ge gt ne
+(for cmp (lt le eq ge gt ne)
(simplify
(cmp (bit_not @0) @1)
/* ??? (for cst in INTEGER_CST VECTOR_CST) is not supported yet. */
Index: gcc/match-constant-folding.pd
===================================================================
--- gcc/match-constant-folding.pd (revision 215002)
+++ gcc/match-constant-folding.pd (working copy)
@@ -17,7 +17,7 @@
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
-(for op in plus pointer_plus minus bit_ior bit_xor
+(for op (plus pointer_plus minus bit_ior bit_xor)
(simplify
(op @0 integer_zerop)
(if (GENERIC && !in_gimple_form)
@@ -40,7 +40,7 @@
/* Make sure to preserve divisions by zero. This is the reason why
we don't simplify x / x to 1 or 0 / x to 0. */
-(for op in mult trunc_div ceil_div floor_div round_div
+(for op (mult trunc_div ceil_div floor_div round_div)
(simplify
(op @0 integer_onep)
@0))
Index: gcc/match-conversions.pd
===================================================================
--- gcc/match-conversions.pd (revision 215002)
+++ gcc/match-conversions.pd (working copy)
@@ -10,7 +10,7 @@
(paren @0))
/* Basic strip-useless-type-conversions / strip_nops. */
-(for cvt in convert view_convert
+(for cvt (convert view_convert)
(simplify
(cvt @0)
(if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
@@ -25,7 +25,7 @@
folding again results in recursions. */
/* ??? Eh, do we want sth like (define-ops cmp lt le eq ...) to not
repeat this too many times? */
-(for cmp in lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt
+(for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
(simplify
(convert (cmp@2 @0 @1))
(if (TREE_CODE (type) == BOOLEAN_TYPE)
@@ -90,8 +90,8 @@
/* From tree-ssa-forwprop.c:combine_conversions. */
/* Combine two conversions in a row. */
-(for ocvt in convert float fix_trunc
- (for icvt in convert float
+(for ocvt (convert float fix_trunc)
+ (for icvt (convert float)
(simplify
(ocvt (icvt@1 @0))
(with
Index: gcc/match-rotate.pd
===================================================================
--- gcc/match-rotate.pd (revision 215002)
+++ gcc/match-rotate.pd (working copy)
@@ -19,7 +19,7 @@
/* (x << CNT1) OP (x >> CNT2) -> x r<< CNT1 OP being +, |, ^ */
-(for op in plus bit_ior bit_xor
+(for op (plus bit_ior bit_xor)
(simplify
(op:c (lshift @0 INTEGER_CST@1) (rshift @0 INTEGER_CST@2))
(if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)