From patchwork Thu May 4 16:00:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 98544 Delivered-To: patch@linaro.org Received: by 10.140.89.200 with SMTP id v66csp672311qgd; Thu, 4 May 2017 09:01:19 -0700 (PDT) X-Received: by 10.84.224.200 with SMTP id k8mr35378871pln.49.1493913679776; Thu, 04 May 2017 09:01:19 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id p20si2483252pgd.413.2017.05.04.09.01.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 May 2017 09:01:19 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-452810-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; spf=pass (google.com: domain of gcc-patches-return-452810-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-452810-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=lRzjjFdPlHS4wL4eqwERT/KfkyXrrFP3Ks44xZBPktUIYz 7Ez87uzGJZxjgMrt9GGm9mgK73HIMe3Qqz5Mx4RUOC2XemoGvBB1v6uXet6exvZN PobZMYWo0PEQZKp3UttkEN/EixgZ64uEO2ZeTugWs4G+lRvK/t02+ErKAsLWc= 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=Z/AMh5+GEtIPWeK6jngafeJLEJs=; b=JoTgqIbvqiPru101QR2M x+KnX4+HdTll11SfbVWK12UtPAjJ6wXWZEHW8DmuglbcI9yXkGLq+Tv1Y5IMymUS 4Axe93LIsVHZe9B8oBEDJzw1tQCDuNUNILN6/JKRGTNObLeu+23sDC+7x9bxcr8F smQ22jDqlsBQ/2gNKFyB2nU= Received: (qmail 48192 invoked by alias); 4 May 2017 16:01:00 -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 48039 invoked by uid 89); 4 May 2017 16:00:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-io0-f180.google.com Received: from mail-io0-f180.google.com (HELO mail-io0-f180.google.com) (209.85.223.180) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 04 May 2017 16:00:45 +0000 Received: by mail-io0-f180.google.com with SMTP id k91so27919327ioi.1 for ; Thu, 04 May 2017 09:00:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=DSpaeAKA2E/0YaqHobDDbvIpT2ulSCO3bOv9rzC7pV8=; b=L8lCLMMNGe45vYFDyLUHAM/66QGTPsnj1g281W20tRjgD0O72wMwyxaesRFDOaXzZJ hKzoKFU+oXgAp24wZTyLn/mCEIA+CkTTqGYd56M/OKPSJrfr0QqCSmnES/q9HRudIl0u wq6ZM0GNWOYJV2YTIGr2Ww6cH9eiVNIF7UGiQHR2yZiEkhmAZoOGCfgoyI8/Vok3IwQS hLjbdPgfJG7mHXR/dgbXdGGH2EecR8p0SrbiLvR92oNUzQ7QqyUzPNs/Cv6u8v2AMyw/ whmpsM+b1gyEhBV37+aHCcok2QAYFxy+tA4+rIi7qvmzKO32ropSBRW1Fu6ABOU+lkGW nyfA== X-Gm-Message-State: AN3rC/7BGZdNYiA+xW5szYZF6Tqv0NMJQMQBx1usaTdGILQVL9N/Pm0C ESRlEwXxFXc7/c8ntnH+RwlJLqzVz0rpv2s= X-Received: by 10.107.161.82 with SMTP id k79mr42673533ioe.204.1493913645630; Thu, 04 May 2017 09:00:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.25.67 with HTTP; Thu, 4 May 2017 09:00:44 -0700 (PDT) From: Prathamesh Kulkarni Date: Thu, 4 May 2017 21:30:44 +0530 Message-ID: Subject: PR80613 To: gcc Patches , Richard Biener X-IsSubscribed: yes Hi, As mentioned in PR, the issue is that cddce1 marks the call to __builtin_strdup as necessary: marking necessary through .MEM_6 stmt p_7 = __builtin_strdup (&d); and since p_7 doesn't get added to worklist in propagate_necessity() because it's used only within free(), it's treated as "dead" and wrongly gets released. The patch fixes that by adding strdup/strndup in corresponding condition in eliminate_unnecessary_stmts(). Another issue, was that my previous patch failed to remove multiple calls to strdup: char *f(char **tt) { char *t = *tt; char *p; p = __builtin_strdup (t); p = __builtin_strdup (t); return p; } That's fixed in patch by adding strdup/strndup to another corresponding condition in propagate_necessity() so that only one instance of strdup would be kept. Bootstrapped+tested on x86_64-unknown-linux-gnu. Cross-testing on arm*-*-* and aarch64*-*-* in progress. OK to commit if testing passes ? Thanks Prathamesh 2017-05-04 Prathamesh Kulkarni PR tree-optimization/80613 * tree-ssa-dce.c (propagate_necessity): Add cases for BUILT_IN_STRDUP and BUILT_IN_STRNDUP. * (eliminate_unnecessary_stmts): Likewise. testsuite/ * gcc.dg/tree-ssa/pr80613-1.c: New test-case. * gcc.dg/tree-ssa/pr80613-2.c: New test-case. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr80613-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr80613-1.c new file mode 100644 index 00000000000..56176427922 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr80613-1.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +char *a(int); +int b; + +void c() { + for (;;) { + char d = *a(b); + char *e = __builtin_strdup (&d); + __builtin_free(e); + } +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr80613-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr80613-2.c new file mode 100644 index 00000000000..c58cc08d6c5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr80613-2.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-cddce1" } */ + +/* There should only be one instance of __builtin_strdup after cddce1. */ + +char *f(char **tt) +{ + char *t = *tt; + char *p; + + p = __builtin_strdup (t); + p = __builtin_strdup (t); + return p; +} + +/* { dg-final { scan-tree-dump-times "__builtin_strdup" 1 "cddce1" } } */ diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c index e17659df91f..7c05f981307 100644 --- a/gcc/tree-ssa-dce.c +++ b/gcc/tree-ssa-dce.c @@ -852,7 +852,9 @@ propagate_necessity (bool aggressive) == BUILT_IN_ALLOCA_WITH_ALIGN) || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE - || DECL_FUNCTION_CODE (callee) == BUILT_IN_ASSUME_ALIGNED)) + || DECL_FUNCTION_CODE (callee) == BUILT_IN_ASSUME_ALIGNED + || DECL_FUNCTION_CODE (callee) == BUILT_IN_STRDUP + || DECL_FUNCTION_CODE (callee) == BUILT_IN_STRNDUP)) continue; /* Calls implicitly load from memory, their arguments @@ -1353,6 +1355,8 @@ eliminate_unnecessary_stmts (void) && DECL_FUNCTION_CODE (call) != BUILT_IN_MALLOC && DECL_FUNCTION_CODE (call) != BUILT_IN_CALLOC && DECL_FUNCTION_CODE (call) != BUILT_IN_ALLOCA + && DECL_FUNCTION_CODE (call) != BUILT_IN_STRDUP + && DECL_FUNCTION_CODE (call) != BUILT_IN_STRNDUP && (DECL_FUNCTION_CODE (call) != BUILT_IN_ALLOCA_WITH_ALIGN))) /* Avoid doing so for bndret calls for the same reason. */