From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5138 invoked by alias); 21 Jan 2015 10:50:28 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 5035 invoked by uid 89); 21 Jan 2015 10:50:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-lb0-f172.google.com Received: from mail-lb0-f172.google.com (HELO mail-lb0-f172.google.com) (209.85.217.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 21 Jan 2015 10:50:24 +0000 Received: by mail-lb0-f172.google.com with SMTP id l4so25750695lbv.3 for ; Wed, 21 Jan 2015 02:50:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=D+wmAtCHoOBnDFU4miPuJjcOOONp/qusuRYgFfK9Pns=; b=cFpTYO+ePLeVml2YnlowFtZhzOEekh8CzW97eLxfrgSXhVAt4/Q9ORp1FBkukVP/r1 rrS8trjZ36ZBpsKFlHxXne2WafiO/mg/SboTpQGtsX/lrtvpSTWBweCisQWrLZbY7aOu +XSWCs6ls8I9E8E4rg3HLILtkrcCPAiGPyebOeheabzXE/DfseazvDUw5X6WdxgqwZbh 28F+hat51XNRn9ZwSnhRbEe27CA45HcpqP/76/728kNQSKd2A7ZXQiG8k7OOfbxQE1Si s1cv2hxEzWgYLi3gG1VYaCwdbDHUZ2JRxggyHb8bbvAOs812rsEDpaBeMFfDtmwzOV/4 c3+w== X-Gm-Message-State: ALoCoQmWNv95BLdVKMPqJyeANhdNInWwzNpmH5mi/QEB+10lBbUcz2UybVF66hrM1N9QuRX/N5mg X-Received: by 10.152.36.100 with SMTP id p4mr40136208laj.11.1421837420561; Wed, 21 Jan 2015 02:50:20 -0800 (PST) Received: from spencer.imf.au.dk ([130.225.20.51]) by mx.google.com with ESMTPSA id aq4sm4792792lbc.46.2015.01.21.02.50.19 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Jan 2015 02:50:19 -0800 (PST) From: Rasmus Villemoes To: GCC Patches Cc: Richard Biener , Andrew Pinski , Rasmus Villemoes Subject: [PATCH 4/4] match.pd: Add x + ((-x) & m) -> (x + m) & ~m pattern Date: Wed, 21 Jan 2015 11:17:00 -0000 Message-Id: <1421837394-7619-5-git-send-email-rv@rasmusvillemoes.dk> In-Reply-To: <1421837394-7619-1-git-send-email-rv@rasmusvillemoes.dk> References: <1421837394-7619-1-git-send-email-rv@rasmusvillemoes.dk> X-SW-Source: 2015-01/txt/msg01901.txt.bz2 Generalizing the x+(x&1) pattern, one can round up x to a multiple of a 2^k by adding the negative of x modulo 2^k. But it is fewer instructions, and presumably requires fewer registers, to do the more common (x+m)&~m where m=2^k-1. Signed-off-by: Rasmus Villemoes --- gcc/match.pd | 9 ++++++ gcc/testsuite/gcc.dg/20150120-4.c | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/20150120-4.c diff --git gcc/match.pd gcc/match.pd index 47865f1..93c2298 100644 --- gcc/match.pd +++ gcc/match.pd @@ -273,6 +273,15 @@ along with GCC; see the file COPYING3. If not see (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2)) (bit_ior @0 (bit_not @1)))) +/* x + ((-x) & m) -> (x + m) & ~m when m == 2^k-1. */ +(simplify + (plus:c @0 (bit_and@2 (negate @0) CONSTANT_CLASS_P@1)) + (with { tree cst = fold_binary (PLUS_EXPR, TREE_TYPE (@1), + @1, build_one_cst (TREE_TYPE (@1))); } + (if ((TREE_CODE (@2) != SSA_NAME || has_single_use (@2)) + && cst && integer_pow2p (cst)) + (bit_and (plus @0 @1) (bit_not @1))))) + (simplify (abs (negate @0)) (abs @0)) diff --git gcc/testsuite/gcc.dg/20150120-4.c gcc/testsuite/gcc.dg/20150120-4.c new file mode 100644 index 0000000..c3552bf --- /dev/null +++ gcc/testsuite/gcc.dg/20150120-4.c @@ -0,0 +1,59 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-original" } */ + +/* x + ((-x) & m) -> (x + m) & ~m for m one less than a pow2. */ +int +fn1 (int x) +{ + return x + ((-x) & 7); +} +int +fn2 (int x) +{ + return ((-x) & 7) + x; +} +unsigned int +fn3 (unsigned int x) +{ + return x + ((-x) & 7); +} +unsigned int +fn4 (unsigned int x) +{ + return ((-x) & 7) + x; +} +unsigned int +fn5 (unsigned int x) +{ + return x + ((-x) % 8); +} +unsigned int +fn6 (unsigned int x) +{ + return ((-x) % 8) + x; +} +int +fn7 (int x) +{ + return x + ((-x) & 9); +} +int +fn8 (int x) +{ + return ((-x) & 9) + x; +} +unsigned int +fn9 (unsigned int x) +{ + return x + ((-x) & ~0U); +} +unsigned int +fn10 (unsigned int x) +{ + return ((-x) & ~0U) + x; +} + + +/* { dg-final { scan-tree-dump-times "x \\+ 7" 6 "original" } } */ +/* { dg-final { scan-tree-dump-times "-x & 9" 2 "original" } } */ +/* { dg-final { scan-tree-dump-times "return 0" 2 "original" } } */ -- 2.1.3