This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[GSoC][match-and-simplify] change syntax of inner-if
- From: Prathamesh Kulkarni <bilbotheelffriend at gmail dot com>
- To: Richard Biener <richard dot guenther at gmail dot com>, Diego Novillo <dnovillo at google dot com>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>, Maxim Kuvyrkov <maxim dot kuvyrkov at linaro dot org>
- Date: Wed, 23 Jul 2014 01:09:22 +0100
- Subject: [GSoC][match-and-simplify] change syntax of inner-if
- Authentication-results: sourceware.org; auth=none
This patch changes syntax of inner-if to be parenthesized - (if (cond))
* genmatch.c (parse_match_and_simplify): Adjust to parse parenthesized if.
(peek_ident): New function.
* match.pd: Adjust patterns having if-expr to new syntax.
Thanks and Regards,
Prathamesh
Index: gcc/genmatch.c
===================================================================
--- gcc/genmatch.c (revision 212916)
+++ gcc/genmatch.c (working copy)
@@ -1769,6 +1769,20 @@ peek (cpp_reader *r)
return token;
}
+static const cpp_token *
+peek_ident (cpp_reader *r, const char *id)
+{
+ const cpp_token *token = peek (r);
+ if (token->type != CPP_NAME)
+ return 0;
+
+ const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
+ if (strcmp (id, t) == 0)
+ return token;
+
+ return 0;
+}
+
/* Read the next token from R and assert it is of type TK. */
static const cpp_token *
@@ -1985,7 +1999,6 @@ parse_op (cpp_reader *r)
return op;
}
-
/* Parse
(define_match_and_simplify "<ident>"
<op> <op>) */
@@ -2008,21 +2021,33 @@ parse_match_and_simplify (cpp_reader *r,
struct operand *match = parse_op (r);
if (match->type != operand::OP_EXPR)
fatal_at (loc, "expected uncaptured expression");
+
token = peek (r);
- /* Conditional if (....) */
- struct operand *ifexpr = NULL;
- source_location ifexpr_location = 0;
- if (token->type == CPP_NAME)
+
+ if (token->type != CPP_OPEN_PAREN)
+ return new simplify (id, match, match_location, 0, 0, parse_op (r), token->src_loc);
+
+ eat_token (r, CPP_OPEN_PAREN);
+
+ token = peek (r);
+ source_location result_loc = token->src_loc;
+
+ // ( expr )
+ if (peek_ident (r, "if") == 0)
{
- const char *tem = get_ident (r);
- if (strcmp (tem, "if") != 0)
- fatal_at (token, "expected 'if' or expression");
- ifexpr_location = token->src_loc;
- ifexpr = parse_c_expr (r, CPP_OPEN_PAREN);
+ operand *result = parse_expr (r);
+ eat_token (r, CPP_CLOSE_PAREN);
+ return new simplify (id, match, match_location, 0, 0, result, result_loc);
}
- token = peek (r);
- return new simplify (id, match, match_location,
- ifexpr, ifexpr_location, parse_op (r), token->src_loc);
+
+ // (if c-expr)
+ source_location ifexpr_loc = token->src_loc;
+ eat_ident (r, "if");
+ operand *ifexpr = parse_c_expr (r, CPP_OPEN_PAREN);
+ eat_token (r, CPP_CLOSE_PAREN);
+
+ result_loc = peek (r)->src_loc;
+ return new simplify (id, match, match_location, ifexpr, ifexpr_loc, parse_op (r), result_loc);
}
void
Index: gcc/match.pd
===================================================================
--- gcc/match.pd (revision 212916)
+++ gcc/match.pd (working copy)
@@ -49,7 +49,7 @@ along with GCC; see the file COPYING3.
and simplifies 0 % x to 0. */
(match_and_simplify
(trunc_mod integer_zerop@0 @1)
- if (!integer_zerop (@1))
+ (if (!integer_zerop (@1)))
@0)
(match_and_simplify
(bit_ior @0 integer_all_onesp@1)
@@ -83,7 +83,7 @@ along with GCC; see the file COPYING3.
#if GIMPLE
(match_and_simplify
(pointer_plus (addr@2 @0) INTEGER_CST_P@1)
- if (is_gimple_min_invariant (@2))
+ (if (is_gimple_min_invariant (@2)))
{
HOST_WIDE_INT off;
tree base = get_addr_base_and_unit_offset (@0, &off);
@@ -106,11 +106,11 @@ along with GCC; see the file COPYING3.
/* Contract negates. */
(match_and_simplify
(plus:c @0 (negate @1))
- if (!TYPE_SATURATING (type))
+ (if (!TYPE_SATURATING (type)))
(minus @0 @1))
(match_and_simplify
(minus @0 (negate @1))
- if (!TYPE_SATURATING (type))
+ (if (!TYPE_SATURATING (type)))
(plus @0 @1))
@@ -123,24 +123,24 @@ along with GCC; see the file COPYING3.
/* (A +- B) - A -> +-B. */
(match_and_simplify
(minus (plus @0 @1) @0)
- if (!TYPE_SATURATING (type)
- && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
+ (if (!TYPE_SATURATING (type)
+ && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type)))
@1)
(match_and_simplify
(minus (minus @0 @1) @0)
- if (!TYPE_SATURATING (type)
- && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
+ (if (!TYPE_SATURATING (type)
+ && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type)))
(negate @1))
/* (A +- B) -+ B -> A. */
(match_and_simplify
(minus (plus @0 @1) @1)
- if (!TYPE_SATURATING (type)
- && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
+ (if (!TYPE_SATURATING (type)
+ && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type)))
@0)
(match_and_simplify
(plus:c (minus @0 @1) @1)
- if (!TYPE_SATURATING (type)
- && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
+ (if (!TYPE_SATURATING (type)
+ && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type)))
@0)
/* (CST +- A) +- CST -> CST' +- A. */
/* match_and_simplify handles constant folding for us so we can
@@ -152,7 +152,7 @@ along with GCC; see the file COPYING3.
/* If the constant operation overflows we cannot do the transform
as we would introduce undefined overflow, for example
with (a - 1) + INT_MIN. */
- if (!TREE_OVERFLOW (@1 = int_const_binop (PLUS_EXPR, @1, @2)))
+ (if (!TREE_OVERFLOW (@1 = int_const_binop (PLUS_EXPR, @1, @2))))
(plus @0 @1))
(match_and_simplify
(plus (minus INTEGER_CST_P@0 @1) INTEGER_CST_P@2)
@@ -202,7 +202,7 @@ along with GCC; see the file COPYING3.
if the new mask might be further optimized. */
(match_and_simplify
(bit_and (rshift@0 @1 INTEGER_CST_P@2) integer_onep)
- if (compare_tree_int (@2, TYPE_PRECISION (TREE_TYPE (@1)) - 1) == 0)
+ (if (compare_tree_int (@2, TYPE_PRECISION (TREE_TYPE (@1)) - 1) == 0))
@0)
/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
@@ -258,7 +258,7 @@ along with GCC; see the file COPYING3.
/* This needs to be conditionalized on flag_unsafe_math_optimizations,
but we keep it for now to exercise function re-optimization.
It makes gcc.dg/pr43419.c FAIL execution though. */
- if (REAL_VALUES_EQUAL (TREE_REAL_CST (@1), dconsthalf))
+ (if (REAL_VALUES_EQUAL (TREE_REAL_CST (@1), dconsthalf)))
(BUILT_IN_SQRT @0))
/* TODO bitwise patterns:
@@ -349,10 +349,10 @@ along with GCC; see the file COPYING3.
(for op in plus bit_ior bit_xor
(match_and_simplify
(op:c (lshift @0 INTEGER_CST_P@1) (rshift @0 INTEGER_CST_P@2))
- if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
+ (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
&& TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
&& tree_fits_uhwi_p (@1) && tree_fits_uhwi_p (@2)
- && tree_to_uhwi (@1) + tree_to_uhwi (@2) == TYPE_PRECISION (type))
+ && tree_to_uhwi (@1) + tree_to_uhwi (@2) == TYPE_PRECISION (type)))
(lrotate @0 @1)))