From patchwork Thu May 14 16:33:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 472408 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F773140284 for ; Fri, 15 May 2015 02:33:35 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=iyrsVWyc; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=kP6yEGc7XnsLwVI+8hesWEXHnZRvU0iQZaT5qlac6THApCzIUXMSv 8WdIwl3j9Z3tRfr3NiggTjUfF4Qn3wjRWSt1RWu860tJ6RsAMJnnyPFEsYOe7Taj DV/lrHAr+XvpwCeOjPzDnCqx2SiIa3zVCTvgmwvYlFR1Ysexjfl00s= 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:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=EnxggSJpQTjGrfQu5h5Dgdyb/lE=; b=iyrsVWyc81XSYYais3LU UarT3PTgUrc1FcWzX4zcDdpvGgFaaVKQNarxEkrg+anDntdzFWL7tpA+qUKSfLL4 Zpo7uNvckDj+NQarGqwTA5i8yQQa5QsNfOwapYpb8hJe0TO4Vu/q+MKfDrYauplx Em0f2oS2EcA0DQ0g7eDi1dY= Received: (qmail 70904 invoked by alias); 14 May 2015 16:33:25 -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 70894 invoked by uid 89); 14 May 2015 16:33:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.0 required=5.0 tests=AWL, BAYES_40, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mail3-relais-sop.national.inria.fr Received: from mail3-relais-sop.national.inria.fr (HELO mail3-relais-sop.national.inria.fr) (192.134.164.104) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Thu, 14 May 2015 16:33:15 +0000 Received: from stedding.saclay.inria.fr (HELO stedding) ([193.55.250.194]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/AES128-SHA; 14 May 2015 18:33:11 +0200 Received: from glisse (helo=localhost) by stedding with local-esmtp (Exim 4.85) (envelope-from ) id 1Ysw4N-0001W2-Av for gcc-patches@gcc.gnu.org; Thu, 14 May 2015 18:33:11 +0200 Date: Thu, 14 May 2015 18:33:11 +0200 (CEST) From: Marc Glisse To: gcc-patches@gcc.gnu.org Subject: PR64454: (x % y) % y Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Hello, after this patch I think I'll close the PR. This was regtested on ppc64le-redhat-linux. Apparently I wrote this patch in a file that already had a trivial hunk: -1-A -> ~A is rejected for complex while -A-1 isn't, there is no reason for this difference (maybe there was before integer_all_onesp / integer_minus_onep was introduced), I hope you don't mind. I am wondering if we want some helper (like :c for commutative operations) to avoid duplicating patterns for xx. We could also, when a comparison x<=y doesn't simplify, see if !!(x<=y) simplifies better, but that's becoming a bit complicated. 2015-05-15 Marc Glisse PR tree-optimization/64454 gcc/ * match.pd ((X % Y) % Y, (X % Y) < Y): New patterns. (-1 - A -> ~A): Remove unnecessary condition. gcc/testsuite/ * gcc.dg/modmod.c: New testcase. Index: gcc/match.pd =================================================================== --- gcc/match.pd (revision 223199) +++ gcc/match.pd (working copy) @@ -204,33 +204,49 @@ along with GCC; see the file COPYING3. (if (!integer_zerop (@1)) @0)) /* X % 1 is always zero. */ (simplify (mod @0 integer_onep) { build_zero_cst (type); }) /* X % -1 is zero. */ (simplify (mod @0 integer_minus_onep@1) (if (!TYPE_UNSIGNED (type)) - { build_zero_cst (type); }))) + { build_zero_cst (type); })) + /* (X % Y) % Y is just X % Y. */ + (simplify + (mod (mod@2 @0 @1) @1) + @2)) /* X % -C is the same as X % C. */ (simplify (trunc_mod @0 INTEGER_CST@1) (if (TYPE_SIGN (type) == SIGNED && !TREE_OVERFLOW (@1) && wi::neg_p (@1) && !TYPE_OVERFLOW_TRAPS (type) /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ && !sign_bit_p (@1, @1)) (trunc_mod @0 (negate @1)))) +/* X % Y is smaller than Y. */ +(for cmp (lt ge) + (simplify + (cmp (trunc_mod @0 @1) @1) + (if (TYPE_UNSIGNED (TREE_TYPE (@0))) + { constant_boolean_node (cmp == LT_EXPR, type); }))) +(for cmp (gt le) + (simplify + (cmp @1 (trunc_mod @0 @1)) + (if (TYPE_UNSIGNED (TREE_TYPE (@0))) + { constant_boolean_node (cmp == GT_EXPR, type); }))) + /* x | ~0 -> ~0 */ (simplify (bit_ior @0 integer_all_onesp@1) @1) /* x & 0 -> 0 */ (simplify (bit_and @0 integer_zerop@1) @1) @@ -526,22 +542,21 @@ along with GCC; see the file COPYING3. /* -A - 1 -> ~A */ (simplify (minus (convert? (negate @0)) integer_each_onep) (if (!TYPE_OVERFLOW_TRAPS (type) && tree_nop_conversion_p (type, TREE_TYPE (@0))) (bit_not (convert @0)))) /* -1 - A -> ~A */ (simplify (minus integer_all_onesp @0) - (if (TREE_CODE (type) != COMPLEX_TYPE) - (bit_not @0))) + (bit_not @0)) /* (T)(P + A) - (T)P -> (T) A */ (for add (plus pointer_plus) (simplify (minus (convert (add @0 @1)) (convert @0)) (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) /* For integer types, if A has a smaller type than T the result depends on the possible overflow in P + A. Index: gcc/testsuite/gcc.dg/modmod.c =================================================================== --- gcc/testsuite/gcc.dg/modmod.c (revision 0) +++ gcc/testsuite/gcc.dg/modmod.c (working copy) @@ -0,0 +1,13 @@ +/* { dg-options "-O -fdump-tree-optimized-raw" } */ + +int f(int a, int b){ + a %= b; + return a % b; +} +int g(unsigned a, unsigned b){ + a %= b; + return a < b; +} + +/* { dg-final { scan-tree-dump-times "trunc_mod_expr" 1 "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */