From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4617 invoked by alias); 21 Jan 2015 10:50:23 -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 4441 invoked by uid 89); 21 Jan 2015 10:50:22 -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-la0-f50.google.com Received: from mail-la0-f50.google.com (HELO mail-la0-f50.google.com) (209.85.215.50) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 21 Jan 2015 10:50:19 +0000 Received: by mail-la0-f50.google.com with SMTP id pn19so39368997lab.9 for ; Wed, 21 Jan 2015 02:50:16 -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=fzxcVsSRgdyNdQ+FBDCD3Oj00Q5svYTJgiJ5P5RxrTY=; b=XvOi4lzMncXwOkYaAIoy12xJn0G5EkoRtPJ81/KBrZEh4WNK7E6+Kdioy/okozj/us qV+57kw2rEiJNNQsU1tayymFKu5g8or96veNxcAc3WNsJ7u7s1NLch3Y7OAVXyFNnRPD dMq0406gmBS+oZF4U0VtNKR9uB2lWbS4qtTKiEBF2aNb3zT++x3+Rosd3oU2iQI2bDZh K/r4XjYjpCPnuLPnCzmq1yNZEURKuOnp9t9s8LHGff7Oba4CYotYpiNwZA0S6kUE/TY8 vLGme2Tz+j7kRwv4Mt7aCHRNZxVwb/xiuxvQ9zhbeQM3KwuB5JbtiBo5vfVi02Fo5Kja dj2Q== X-Gm-Message-State: ALoCoQm/SrK4yTj5kI43YOpYnDT2wOhk3rIYq0t4WKbSJIxPEYvnwwqIewYVLNCJJ5mXfALVam1q X-Received: by 10.112.30.42 with SMTP id p10mr36843276lbh.70.1421837416208; Wed, 21 Jan 2015 02:50:16 -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.14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Jan 2015 02:50:15 -0800 (PST) From: Rasmus Villemoes To: GCC Patches Cc: Richard Biener , Andrew Pinski , Rasmus Villemoes Subject: [PATCH 1/4] match.pd: Add x + (x & 1) -> (x + 1) & ~1 pattern Date: Wed, 21 Jan 2015 10:50:00 -0000 Message-Id: <1421837394-7619-2-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/msg01898.txt.bz2 gcc.dg/20150120-1.c: New test Rounding an integer to the next even integer is sometimes written x += x & 1. The equivalent x = (x+1)&~1 usually uses one less register, and in practical cases only the new value of x will be used (making it unlikely that the subexpression x&1 has any uses). Signed-off-by: Rasmus Villemoes --- gcc/match.pd | 6 +++++ gcc/testsuite/gcc.dg/20150120-1.c | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/20150120-1.c diff --git gcc/match.pd gcc/match.pd index 81c4ee6..ecefcfb 100644 --- gcc/match.pd +++ gcc/match.pd @@ -255,6 +255,12 @@ along with GCC; see the file COPYING3. If not see (bitop @0 @0) (non_lvalue @0))) +/* x + (x & 1) -> (x + 1) & ~1 */ +(simplify + (plus:c @0 (bit_and@2 @0 integer_onep@1)) + (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2)) + (bit_and (plus @0 @1) (bit_not @1)))) + (simplify (abs (negate @0)) (abs @0)) diff --git gcc/testsuite/gcc.dg/20150120-1.c gcc/testsuite/gcc.dg/20150120-1.c new file mode 100644 index 0000000..18906c4 --- /dev/null +++ gcc/testsuite/gcc.dg/20150120-1.c @@ -0,0 +1,51 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-original" } */ + +/* x + (x & 1) -> (x + 1) & ~1 */ +int +fn1 (int x) +{ + return x + (x & 1); +} +int +fn2 (int x) +{ + return (x & 1) + x; +} +int +fn3 (int x) +{ + return x + (1 & x); +} +int +fn4 (int x) +{ + return (1 & x) + x; +} +unsigned int +fn5 (unsigned int x) +{ + return x + (x & 1); +} +unsigned int +fn6 (unsigned int x) +{ + return (x & 1) + x; +} +unsigned int +fn7 (unsigned int x) +{ + return x + (x % 2); +} +unsigned int +fn8 (unsigned int x) +{ + return (x % 2) + x; +} +unsigned int +fn9 (unsigned int x) +{ + return (1LL & x) + x; +} + +/* { dg-final { scan-tree-dump-times "x \\+ 1" 9 "original" } } */ -- 2.1.3