From patchwork Fri Nov 2 09:36:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 150019 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp1854804ljp; Fri, 2 Nov 2018 02:37:17 -0700 (PDT) X-Google-Smtp-Source: AJdET5dJ2TZxScOrH38ElL8J0YvX4oD3S9aXPFnGA3y8B/EWwwP5yF6PLRqnE1EnM2s7QhXGWRHu X-Received: by 2002:a63:ec12:: with SMTP id j18mr8548981pgh.200.1541151437029; Fri, 02 Nov 2018 02:37:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541151437; cv=none; d=google.com; s=arc-20160816; b=Cd8Wr8kZA28f5f0Daz+f5/8hsh0ZdD8ni57tf1eGKkDuj894qw18HeRlnYZ8+j4M9p ABEPdmLMmrSvDzcDteqregy1Cn3vFYWakYRnjJ0RjHkwA6KuJzqFLEzHi4c1tI2Vdknz 4HvNjooAXSuvscJ2cSDeafZ7kJzkHWicIfKabAucInZLMkQA7qnxa1sBvS8jwnZ475In 4D9cKxPpMoplT0Rf/USlKyYe3J4mFHabl41cRYw5fMalm49KABar5zsTNM6Rq3A/V/y7 vGp+Yo1x9AAJpvUzK5otb/Kcd4Uo8hktARUAAQRPkGn6A8lKGrup7bowGeYPuK72B2Dd ICUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:mime-version:dkim-signature :delivered-to:sender:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature; bh=FYvJlN+yBHLm2HPHMoK7oP0OILZAf/BXHsV1b19kZM4=; b=HRlVqEcK7gCPqUWdnnUfNjWWyt6WAXVehd2iykHL98SfvIF+VLYNYOnWTtJ4eejM7K jfpaMW407ib9CJU3h5nXl29y4LN6iWrIyX5sx7tlLbZW03ggn+MqNrHSXXpxI4J3d8cZ 7Ioi+LsQAM08SxpUqnWdjtce3K1nGzzLNSIyy9LF4GZ8bSFk/8QWM2IZib0Z8iNnY6Ro 8ke4JP0sSNGCrvk6IKULxts6xk02r/s/0I0GxjvVHrLwQtzmlAoz7nKTJJDDajl2aSQv +VSihz0OW0SQKxiLV34xQ8MCVdo/jLuFsx9/Fl7gDT75nIp62MBCBsplsF14NQq0Ipo3 RWwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iWCFb7qJ; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=KE0b9oZ9; spf=pass (google.com: domain of gcc-patches-return-488874-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gcc-patches-return-488874-patch=linaro.org@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id x32-v6si2490326pld.70.2018.11.02.02.37.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Nov 2018 02:37:17 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-488874-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 header.s=default header.b=iWCFb7qJ; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=KE0b9oZ9; spf=pass (google.com: domain of gcc-patches-return-488874-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gcc-patches-return-488874-patch=linaro.org@gcc.gnu.org"; dmarc=fail (p=NONE sp=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=vx7Ts2Sw0tuMzvjJNojD5P+WY+IO1mef+opGXR/8cNH4T0 kKuHIVAqej7nn/9Ecz62qHX/iNme1zLCDA123y6OvKsZWuzp+0NJeph4o5yWOjev jAnVWnQY3e8dp7eV0CTBuegW+GAPMVarX5kZ2WsnjfxAol6QWByZVUcDxEbgo= 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=KvTipGSbdaqJeeeIZoXf5x87jTw=; b=iWCFb7qJ7fPaBaQYsxP2 iSaiZTvd83tDKdpsYzwtLZBx54N5Ov1A4LDhqSDGcWX8Rdu/Wdkc8os9KISAvHNG wUs9kvOzMkoPvPWNOUqISBrI85vc7x0bLHlBB+Ta5CYD/M8wZSbO+++NhmOcy0lj pkp9/9SfX1rLIyUgBE0v4Co= Received: (qmail 61271 invoked by alias); 2 Nov 2018 09:37:06 -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 61259 invoked by uid 89); 2 Nov 2018 09:37:05 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=resolves, D*1, build_one_cst, inv X-HELO: mail-lf1-f46.google.com Received: from mail-lf1-f46.google.com (HELO mail-lf1-f46.google.com) (209.85.167.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 02 Nov 2018 09:37:03 +0000 Received: by mail-lf1-f46.google.com with SMTP id p86so888304lfg.5 for ; Fri, 02 Nov 2018 02:37:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:from:date:message-id:subject:to; bh=hFo4be4Rpo//6UEPziOrWiE+8GDSomXY9i8sEGRa3J8=; b=KE0b9oZ9RmQr1Qs8ciEbiWi1mkwryfdGhXDFF2CtmYKMby8ZwIn7U4h/nvhoR9qR4m 2G5TcSA6+tp3SWTvqidzoh6+l+6Fa6ylpX/laXPKLs0BRMCWHmcn2H5dVXnDVvTJmbqF kPO0rRjk8sCeB43JHoQvh/x1g8WY5LEo6cWZA= MIME-Version: 1.0 From: Prathamesh Kulkarni Date: Fri, 2 Nov 2018 15:06:48 +0530 Message-ID: Subject: PR83750: CSE erf/erfc pair To: gcc Patches X-IsSubscribed: yes Hi, This patch adds two transforms to match.pd to CSE erf/erfc pair. erfc(x) is canonicalized to 1 - erf(x) and is then reversed to 1 - erf(x) when canonicalization is disabled and result of erf(x) has single use within 1 - erf(x). The patch regressed builtin-nonneg-1.c. The following test-case reproduces the issue with patch: void test(double d1) { if (signbit(erfc(d1))) link_failure_erfc(); } ssa dump: : _5 = __builtin_erf (d1_4(D)); _1 = 1.0e+0 - _5; _6 = _1 < 0.0; _2 = (int) _6; if (_2 != 0) goto ; [INV] else goto ; [INV] : link_failure_erfc (); : return; As can be seen, erfc(d1) is folded to 1 - erf(d1). forwprop then transforms the if condition from _2 != 0 to _5 > 1.0e+0 and that defeats DCE thus resulting in link failure in undefined reference to link_failure_erfc(). So, the patch adds another transform erf(x) > 1 -> 0 which resolves the regression. Bootstrapped+tested on x86_64-unknown-linux-gnu. Cross-testing on arm and aarch64 variants in progress. OK for trunk if passes ? Thanks, Prathamesh 2018-11-02 Prathamesh Kulkarni * match.pd (erfc(x) -> 1 - erf(x)): New pattern. (1 - erf(x) -> erfc(x)): Likewise. (erf(x) > 1 -> 0): Likewise. testsuite/ * gcc.dg/tree-ssa/pr83750-1.c: New test * gcc.dg/tree-ssa/pr83750-2.c: Likewise. diff --git a/gcc/match.pd b/gcc/match.pd index d07ceb7d087..03e9230a579 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4490,7 +4490,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (targetm.libc_has_function (function_c99_math_complex)) (complex (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0)))) - (mult @1 (imagpart @2))))))) + (mult @1 (imagpart @2)))))) + + + /* Canonicalize erfc(x) -> 1 - erf(x) */ + (simplify + (ERFC @0) + (minus { build_one_cst (TREE_TYPE (@0)); } (ERF @0)))) + +(if (flag_unsafe_math_optimizations + && !canonicalize_math_p()) + + /* 1 - erf(x) -> erfc(x) + This is only done if result of erf() has single use in 1 - erf(x). */ + (simplify + (minus real_onep (ERF@1 @0)) + (if (single_use (@1)) + (ERFC @0))) + + /* erf(x) > 1 -> 0 */ + (simplify + (gt (ERF @0) real_onep) + { integer_zero_node; })) (if (canonicalize_math_p ()) /* floor(x) -> trunc(x) if x is nonnegative. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83750-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-1.c new file mode 100644 index 00000000000..c4d3e428f15 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-1.c @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target c99_runtime } */ +/* { dg-options "-O2 -ffast-math -fdump-tree-optimized" } */ + +float f1(float x) +{ + float g1(float, float); + + float r = __builtin_erff (x); + float t = __builtin_erfcf (x); + return g1 (r, t); +} + +double f2(double x) +{ + double g2(double, double); + + double r = __builtin_erf (x); + double t = __builtin_erfc (x); + return g2 (r, t); +} + +long double f3(long double x) +{ + long double g3(long double, long double); + + long double r = __builtin_erfl (x); + long double t = __builtin_erfcl (x); + return g3(r, t); +} + +/* { dg-final { scan-tree-dump-not "erfc" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83750-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-2.c new file mode 100644 index 00000000000..60417b38681 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-2.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target c99_runtime } */ +/* { dg-options "-O2 -ffast-math -fdump-tree-optimized" } */ + +/* Check that the canonicalized form 1 - erf(x) is folded to erfc(x). */ + +float f1(float x) +{ + return __builtin_erfcf (x); +} + +double f2(double x) +{ + return __builtin_erfc (x); +} + +long double f3(long double x) +{ + return __builtin_erfcl (x); +} + +/* { dg-final { scan-tree-dump-times "erfc" 3 "optimized" } } */