From patchwork Sat Nov 4 09:58:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 834184 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-465933-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="wEKz4Aso"; dkim-atps=neutral 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 3yTZ5b5zt5z9sNr for ; Sat, 4 Nov 2017 20:58:24 +1100 (AEDT) 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=x4qcHC3doXNzpK4/EK+BrNddVNqhu6fO3SILhBUETUOeBMxNDFe4D AlZU+c6px2Vw6Bxr00fMWofEnHW/kUerV352QTw4IJ0Zmu+hLJ1S7dpSVMDuY62b jUNknLzCx8eUuC9c/iZGr8PnJ/Sndr26WXsQEiRhRzxGYI0DOxqFFE= 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=ECpgQl0xuT5AYvqSNsDsv1cZa/o=; b=wEKz4AsoK4P3TBLOYf8I qLX4Q44TOuDrphsAp/9u3wnSmWAr9d6W18ZeAT2FJolKmN/ejegUVmdxKyypLx8Q 5P0LXdnOm+A91oE/lm50p1OarJEvhJ6Ik7ArHkw91FF+AleZRkamK3Za6MGHjXmE Ao/PjXbkOAooo7ejTp5rp6w= Received: (qmail 22150 invoked by alias); 4 Nov 2017 09:58:17 -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 22139 invoked by uid 89); 4 Nov 2017 09:58:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-9.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=ba, BA, CST, 254399 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 ESMTP; Sat, 04 Nov 2017 09:58:14 +0000 Received: from ip-195.net-89-2-234.rev.numericable.fr (HELO stedding) ([89.2.234.195]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 04 Nov 2017 10:58:11 +0100 Date: Sat, 4 Nov 2017 10:58:07 +0100 (CET) From: Marc Glisse To: gcc-patches@gcc.gnu.org Subject: More fold_negate in match.pd Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Hello, this copies some more transformations from fold_negate_expr to match.pd. Mostly, I wanted to add (negate (pointer_diff @0 @1)), and couldn't find the corresponding transformation with minus_expr... We can see about generalizing a bit with conversions later, I wanted to have something at least. Bootstrap+regtest on powerpc64le-unknown-linux-gnu. gcc/ChangeLog: 2017-11-04 Marc Glisse * fold-const.c (negate_expr_p) [PLUS_EXPR, MINUS_EXPR]: Handle non-scalar integral types. * match.pd (negate_expr_p): Handle MINUS_EXPR. (-(A-B), -(~A)): New transformations. gcc/testsuite/ChangeLog: 2017-11-04 Marc Glisse * gcc.dg/tree-ssa/negminus.c: New test. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 254399) +++ gcc/fold-const.c (working copy) @@ -421,34 +421,34 @@ negate_expr_p (tree t) case COMPLEX_EXPR: return negate_expr_p (TREE_OPERAND (t, 0)) && negate_expr_p (TREE_OPERAND (t, 1)); case CONJ_EXPR: return negate_expr_p (TREE_OPERAND (t, 0)); case PLUS_EXPR: if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) || HONOR_SIGNED_ZEROS (element_mode (type)) - || (INTEGRAL_TYPE_P (type) + || (ANY_INTEGRAL_TYPE_P (type) && ! TYPE_OVERFLOW_WRAPS (type))) return false; /* -(A + B) -> (-B) - A. */ if (negate_expr_p (TREE_OPERAND (t, 1))) return true; /* -(A + B) -> (-A) - B. */ return negate_expr_p (TREE_OPERAND (t, 0)); case MINUS_EXPR: /* We can't turn -(A-B) into B-A when we honor signed zeros. */ return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) && !HONOR_SIGNED_ZEROS (element_mode (type)) - && (! INTEGRAL_TYPE_P (type) + && (! ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)); case MULT_EXPR: if (TYPE_UNSIGNED (type)) break; /* INT_MIN/n * n doesn't overflow while negating one operand it does if n is a (negative) power of two. */ if (INTEGRAL_TYPE_P (TREE_TYPE (t)) && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST Index: gcc/match.pd =================================================================== --- gcc/match.pd (revision 254399) +++ gcc/match.pd (working copy) @@ -951,35 +951,50 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (negate @0) (if (!TYPE_OVERFLOW_SANITIZED (type)))) (match negate_expr_p REAL_CST (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) /* VECTOR_CST handling of non-wrapping types would recurse in unsupported ways. */ (match negate_expr_p VECTOR_CST (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) +(match negate_expr_p + (minus @0 @1) + (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) + || (FLOAT_TYPE_P (type) + && !HONOR_SIGN_DEPENDENT_ROUNDING (type) + && !HONOR_SIGNED_ZEROS (type))))) /* (-A) * (-B) -> A * B */ (simplify (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1)) (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) && tree_nop_conversion_p (type, TREE_TYPE (@1))) (mult (convert @0) (convert (negate @1))))) /* -(A + B) -> (-B) - A. */ (simplify (negate (plus:c @0 negate_expr_p@1)) (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) && !HONOR_SIGNED_ZEROS (element_mode (type))) (minus (negate @1) @0))) +/* -(A - B) -> B - A. */ +(simplify + (negate (minus @0 @1)) + (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type)) + || (FLOAT_TYPE_P (type) + && !HONOR_SIGN_DEPENDENT_ROUNDING (type) + && !HONOR_SIGNED_ZEROS (type))) + (minus @1 @0))) + /* A - B -> A + (-B) if B is easily negatable. */ (simplify (minus @0 negate_expr_p@1) (if (!FIXED_POINT_TYPE_P (type)) (plus @0 (negate @1)))) /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) when profitable. For bitwise binary operations apply operand conversions to the binary operation result instead of to the operands. This allows @@ -1075,20 +1090,25 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_not (bit_not @0)) @0) /* Convert ~ (-A) to A - 1. */ (simplify (bit_not (convert? (negate @0))) (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@0))) (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) +/* Convert - (~A) to A + 1. */ +(simplify + (negate (nop_convert (bit_not @0))) + (plus (view_convert @0) { build_each_one_cst (type); })) + /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ (simplify (bit_not (convert? (minus @0 integer_each_onep))) (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@0))) (convert (negate @0)))) (simplify (bit_not (convert? (plus @0 integer_all_onesp))) (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@0))) Index: gcc/testsuite/gcc.dg/tree-ssa/negminus.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/negminus.c (nonexistent) +++ gcc/testsuite/gcc.dg/tree-ssa/negminus.c (working copy) @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fno-rounding-math -fno-signed-zeros -fdump-tree-optimized-raw" } */ + +double f(double a, double b){ + double c = a - b; + return -c; +} + +int g(unsigned x){ + unsigned y = ~x; + int z = (int) y; + return -z; +} + +unsigned h(unsigned a, unsigned b, unsigned c){ + unsigned d = b - c; + unsigned e = a + d; + return -e; +} + +/* { dg-final { scan-tree-dump-not "negate_expr" "optimized"} } */