From patchwork Sat Oct 15 19:28:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 77689 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp817489qge; Sat, 15 Oct 2016 12:28:43 -0700 (PDT) X-Received: by 10.98.150.79 with SMTP id c76mr26885668pfe.154.1476559723530; Sat, 15 Oct 2016 12:28:43 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id w1si19755859par.346.2016.10.15.12.28.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Oct 2016 12:28:43 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-438713-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-438713-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-438713-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=MMjNL4NRyjbTxDa9ZN0t3K4XZl2dXZ+YPL4EyeuGpPAWfj qTsy+zvUL3/wW4tAMQjGmEkrQQUwsoOoE7Gjr1QgyONl665ti0VYDd1NO936cip8 xzYYbJQuhRI1u92VljIfZE3Eu6+qW1TzAo0EXBtrUsj7mnd8zkzbbVmAh7I8Y= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=OzMRE6Z2EquAxNgremU7mpJ/n1c=; b=fl0AlC4mlcAWig2bmsga DxgLe4RjLkIXU/kv7Dxm7t6B0pHNa4doEzN/8+GnQO3+2t/HomKmNbUTAVSfsDhw AwY1LJQmoIzUTTw8pGpGbBh+nDln0pe0zjiAxxsiZvyl+Rr0a8u6buYYJD+zT3Ec 0TtWaGJquPMmrQkjJgluXsI= Received: (qmail 65334 invoked by alias); 15 Oct 2016 19:28:30 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 64600 invoked by uid 89); 15 Oct 2016 19:28:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=20161016, 2016-10-16, peek, composition X-HELO: mail-it0-f54.google.com Received: from mail-it0-f54.google.com (HELO mail-it0-f54.google.com) (209.85.214.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 15 Oct 2016 19:28:18 +0000 Received: by mail-it0-f54.google.com with SMTP id o19so18863307ito.1 for ; Sat, 15 Oct 2016 12:28:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=2D6a3htE4Z3GUToIbScs11rL619JJu523R5DPk8EQe0=; b=diZ+713mCrK1gh8F036vVyRGIg5h6YPhvf1QSlNJK8Mv02foYDydgAXbq/MD9ug9aq RbSR4OwMJxizmruyoXvxJAyVRUzo8Jc0uYWZWAg1tV1itBFkB0s19x8HjaSKn7MxTHcS sEinAi8yNJj3Ai09es+P/FY9g90qYE6Zj5OnEZfiXqHfu4HSnLrcHxJQ88t+sygs+o1+ GMQcv3twLz4iPkgFlbW0lr/5BA5MPOWhUvGfxd7JFBdcR9LfsJnZRwR0HmUE1Z65cFJV NrGJqsWpW7hLyAUPW7d5ebXC5nQiwqefm1Uk6/E3amG8mcCe0v6aBGbRTndWVp9fpxAc LT0w== X-Gm-Message-State: AA6/9RkNO3K04H371hK/Jw2ArmDpo0fuS50+QoakoXdUVUuFySlmvdYlnO56DhSD4n/zFNonqhF5DLhaKFe3fziB X-Received: by 10.36.210.68 with SMTP id z65mr2828823itf.32.1476559697285; Sat, 15 Oct 2016 12:28:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.112.78 with HTTP; Sat, 15 Oct 2016 12:28:16 -0700 (PDT) From: Prathamesh Kulkarni Date: Sun, 16 Oct 2016 00:58:16 +0530 Message-ID: Subject: [genmatch] Introduce reverse keyword To: Richard Biener , gcc Patches X-IsSubscribed: yes Hi Richard, This patch makes a minor change to genmatch to add reverse keyword for iterating over user-defined operators in reverse in for-stmt and in define_operator_list. eg: (for op (bit_and bit_ior) rop (reverse (op)) ...) Verified gimple-match.c and generic-match.c remain unchanged with patch. Bootstrap+test in progress on x86_64-unknown-linux-gnu. OK to commit if passes ? Thanks, Prathamesh 2016-10-16 Prathamesh Kulkarni * genmatch.c (parser::parse_for): Adjust for parsing reverse. (parser::parse_operator_list): Likewise. * match.pd: Use reverse in few patterns. * doc/match-and-simplify.texi: Add documentation for reverse. diff --git a/gcc/doc/match-and-simplify.texi b/gcc/doc/match-and-simplify.texi index aaa367b..9530224 100644 --- a/gcc/doc/match-and-simplify.texi +++ b/gcc/doc/match-and-simplify.texi @@ -420,3 +420,13 @@ You can use the above predicate like Which will match a bitwise and of an operand with its logical inverted value. +@code{reverse} can be used in for and define_operator_list for iterating +in reverse. + +@smallexample +(for opo (bit_and bit_xor) + opi (reverse(opo)) + (simplify + (opo:c (opi:c @@0 @@1) @@1) + (bit_and (bit_not @@0) @@1))) +@end smallexample diff --git a/gcc/genmatch.c b/gcc/genmatch.c index fc4f598..0a96784 100644 --- a/gcc/genmatch.c +++ b/gcc/genmatch.c @@ -4506,7 +4506,15 @@ parser::parse_for (source_location) int arity = -1; while ((token = peek_ident ()) != 0) { + bool reverse = false; const char *oper = get_ident (); + if (!strcmp (oper, "reverse")) + { + reverse = true; + eat_token (CPP_OPEN_PAREN); + oper = get_ident (); + eat_token (CPP_CLOSE_PAREN); + } id_base *idb = get_operator (oper, true); if (idb == NULL) fatal_at (token, "no such operator '%s'", oper); @@ -4527,10 +4535,29 @@ parser::parse_for (source_location) if (p) { if (p->is_oper_list) - op->substitutes.safe_splice (p->substitutes); + { + if (reverse) + { + auto_vec substitutes; + for (unsigned i = p->substitutes.length (); i > 0; --i) + substitutes.safe_push (p->substitutes[i - 1]); + op->substitutes.safe_splice (substitutes); + } + else + op->substitutes.safe_splice (p->substitutes); + } + else if (reverse) + { + auto_vec substitutes; + for (unsigned i = p->substitutes.length (); i > 0; --i) + substitutes.safe_push (p->substitutes[i - 1]); + op->substitutes.safe_splice (substitutes); + } else fatal_at (token, "iterator cannot be used as operator-list"); } + else if (reverse) + fatal_at (token, "reverse can only be used on user-defined operators"); else op->substitutes.safe_push (idb); } @@ -4607,6 +4634,16 @@ parser::parse_operator_list (source_location) { token = peek (); const char *oper = get_ident (); + + bool reverse = false; + if (!strcmp (oper, "reverse")) + { + reverse = true; + eat_token (CPP_OPEN_PAREN); + oper = get_ident (); + eat_token (CPP_CLOSE_PAREN); + } + id_base *idb = get_operator (oper, true); if (idb == 0) @@ -4622,7 +4659,19 @@ parser::parse_operator_list (source_location) /* We allow composition of multiple operator lists. */ if (user_id *p = dyn_cast (idb)) - op->substitutes.safe_splice (p->substitutes); + { + if (reverse) + { + auto_vec substitutes; + for (unsigned i = p->substitutes.length (); i > 0; --i) + substitutes.safe_push (p->substitutes[i - 1]); + op->substitutes.safe_splice (substitutes); + } + else + op->substitutes.safe_splice (p->substitutes); + } + else if (reverse) + fatal_at (token, "reverse can only be applied on user-defined operators"); else op->substitutes.safe_push (idb); } diff --git a/gcc/match.pd b/gcc/match.pd index e4ff0e7..ac0ebc8 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -689,7 +689,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* ~x & ~y -> ~(x | y) ~x | ~y -> ~(x & y) */ (for op (bit_and bit_ior) - rop (bit_ior bit_and) + rop (reverse(op)) (simplify (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) @@ -730,7 +730,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */ (for opo (bit_and bit_xor) - opi (bit_xor bit_and) + opi (reverse(opo)) (simplify (opo:c (opi:c @0 @1) @1) (bit_and (bit_not @0) @1))) @@ -860,7 +860,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (bitop @0 (convert @1)))))) (for bitop (bit_and bit_ior) - rbitop (bit_ior bit_and) + rbitop (reverse(bitop)) /* (x | y) & x -> x */ /* (x & y) | x -> x */ (simplify @@ -3336,7 +3336,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */ (for cmp (eq ne) (for op (plus minus) - rop (minus plus) + rop (reverse(op)) (simplify (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) @@ -3350,7 +3350,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp @0 { res; })))))))) (for cmp (lt le gt ge) (for op (plus minus) - rop (minus plus) + rop (reverse(op)) (simplify (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)