From patchwork Wed Jan 3 06:03:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 123282 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp9799650qgn; Tue, 2 Jan 2018 22:03:47 -0800 (PST) X-Google-Smtp-Source: ACJfBovy/+rR49kULCyBuP6GDecwF3Hp6tuvLQQ5l6ZvYeuJOHZ+9c5q65t3iypTKlfjGhxdGcH5 X-Received: by 10.159.218.152 with SMTP id w24mr443678plp.43.1514959427910; Tue, 02 Jan 2018 22:03:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514959427; cv=none; d=google.com; s=arc-20160816; b=XsooWp7KoWOZzVvB3VYzoEWK0SErMU8ALwWjStZDWJoyz4asjOL/YO8g0AVG5xeJFT aiu49FQkGOFWmKZYph6nfqf7lFCgVj77CaOwBLmDaTVnwML+nLAj3PyCAxloRoD3XJXY VC2CvH15cFjQKAn4A5AWFNJFT5EFBNSYmkE/OkQsZvHvjp200mni8fVgielnta1kxUbl EMl1/cw1inB7ZstrFR2tTr35OnVBevt6peoIgcHGH3eT+oTkO6FdIwjOF5vRxrjk+mDQ /d3UqeetUXHq0NJGIcSgN5xk5OIXIkMWRjVUe3GBkohwVEMqiHr/hcl2+ALZbv+btovE 8zHw== 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:delivered-to:sender :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=7h0z8Vr18/3hKqfF3wh2xMMLEW/QDoUoELGm3rzYwFE=; b=wjpiEpNIjDYPC7Retu3u0+VNOYozyr+2KLtoKnx/cthLLHPxxvBuvBG3SxQcCYEJQ5 XA3cOVzyNd/RJoH3213tR1lTL0flN01CGiJn2amBR//sa4jSt8ZP3VwYEgU8tGS/2hl6 3vocgqIY9YgbrLFPhF9YzcVp42ZwCez2LLcopY0KG9Sw/bV+M3eSM390anaaNow06GFZ XpLpWynUScs0E4sdkua3Q1p2aUhzpqPE6jj4ghy14ZnEglBvyrfajVub8D3Tmlnf3Iew rv5HzzvYF80ftV7jn9AiISzS7jiX+RYILdcJvW7jSw1s3BRCZLIpUhgtHcPBQf8NJU73 YQPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VhVodbry; spf=pass (google.com: domain of gcc-patches-return-470024-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-470024-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 l12si256182plc.265.2018.01.02.22.03.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Jan 2018 22:03:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-470024-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=VhVodbry; spf=pass (google.com: domain of gcc-patches-return-470024-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-470024-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=vEh7cdtMdtJ3KGMjDXi6BLxDqMTbxV8N+9ZYi2z8ACYH9F j3Fz34jpbWJlqqZhwDygETBi57MUXHEm6XEheVvxtOVYXT2PcTsdbuQA6IiQg0Nm WV2cYJ56URjTHMgP4RZYJUtIPOJjHXp8ie1Hl/w9gFjYIqcGl2cUNuZPOZTsY= 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=rMmmTB+XFC0ib+KKYK4MMrU1XTQ=; b=VhVodbry2GshehjcFb8P UoLDOlMHXtDBwjjV9Z/6O5PheiOWGpE6lqnvE+PDaLh0tPsb7pwPQGfPIhRcE/9w 3szg9DpEu8P0tLHdp7ScrFKJgst4mj55K6ss5+G87whyaB9WSSuEDTIIvBqonVZ1 YKZHS9kb5rqUooBdTMpxfZE= Received: (qmail 8456 invoked by alias); 3 Jan 2018 06:03:32 -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 8443 invoked by uid 89); 3 Jan 2018 06:03:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No 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, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wr0-f177.google.com Received: from mail-wr0-f177.google.com (HELO mail-wr0-f177.google.com) (209.85.128.177) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 03 Jan 2018 06:03:29 +0000 Received: by mail-wr0-f177.google.com with SMTP id f8so517834wre.4 for ; Tue, 02 Jan 2018 22:03:28 -0800 (PST) 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=siur0eTf1EST5+jQsLJ33PJaMHbGoMfQgSczz1lGIy0=; b=ci6j+L5z8HWWWHsTC0l6dGvyj8/LcuFA5EGr40h+Cd4THr3B//ocpTrIWIjbe0vzhe kpHb8gS+Oq60tIdtOgpF9Pe/IS1ofOOyNk03eNjjriLfRhtZElLiDeZrJPc3i/XiY3gG v83lq+V3oRv98pRnVUXJNQET6jqLKYhrYMreZBp51S2jfh3Jn+UGZNg2kqyfGM6ginCd Uo/t4ibwhXqtpN73nZZZMuxfqs95wG2z2c2vQryuhsh3EmzqIdBRIUOsE8japdJmzq0T 0Qod7m0tAj8sq0fqw3W3Ey3YzuRcF1UJlauutXDE6kAnBfWUF6lxW3A5OPXSnHcz5EdA KINA== X-Gm-Message-State: AKGB3mLulX2MMxc5zoNYiEP9175JUHXu5mwtlbMc3ozbczz3QByvohFt H22ggdq5sHwPVw7oYzNluwF/JEdSNUFoDxtNbCHTgnmi X-Received: by 10.223.197.138 with SMTP id m10mr386094wrg.203.1514959406809; Tue, 02 Jan 2018 22:03:26 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.162.10 with HTTP; Tue, 2 Jan 2018 22:03:26 -0800 (PST) From: Prathamesh Kulkarni Date: Wed, 3 Jan 2018 11:33:26 +0530 Message-ID: Subject: PR83648 To: gcc Patches , Jan Hubicka , Richard Biener X-IsSubscribed: yes Hi, malloc_candidate_p() in ipa-pure-const misses detecting that a function is malloc-like if the return value is result of PHI and one of the arguments of PHI is 0. For example: void g(unsigned n) { return (n) ? __builtin_malloc (n) : 0; } The reason is that the following check: if (TREE_CODE (arg) != SSA_NAME) DUMP_AND_RETURN ("phi arg is not SSA_NAME.") fails for arg with constant value 0 and malloc_candidate_p returns false. The patch simply skips the arg if it equals null_pointer_node. Does it look OK ? One concern I have is that with the patch, malloc_candidate_p will return true if all the args to PHI are NULL: retval = PHI<0, 0> return retval However I expect that PHI with all 0 args would be constant folded to 0 earlier, so this case shouldn't occur in practice ? Bootstrapped+tested on x86_64-unknown-linux-gnu. Cross-testing on arm*-*-* and aarch64*-*-* in progress. Thanks, Prathamesh diff --git a/gcc/ipa-pure-const.c b/gcc/ipa-pure-const.c index 09ca3590039..0406d5588d2 100644 --- a/gcc/ipa-pure-const.c +++ b/gcc/ipa-pure-const.c @@ -910,7 +910,8 @@ malloc_candidate_p (function *fun, bool ipa) #define DUMP_AND_RETURN(reason) \ { \ if (dump_file && (dump_flags & TDF_DETAILS)) \ - fprintf (dump_file, "%s", (reason)); \ + fprintf (dump_file, "\n%s is not a malloc candidate, reason: %s\n", \ + (node->name()), (reason)); \ return false; \ } @@ -961,11 +962,14 @@ malloc_candidate_p (function *fun, bool ipa) for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i) { tree arg = gimple_phi_arg_def (phi, i); + if (arg == null_pointer_node) + continue; + if (TREE_CODE (arg) != SSA_NAME) - DUMP_AND_RETURN("phi arg is not SSA_NAME.") - if (!(arg == null_pointer_node || check_retval_uses (arg, phi))) - DUMP_AND_RETURN("phi arg has uses outside phi" - " and comparisons against 0.") + DUMP_AND_RETURN ("phi arg is not SSA_NAME."); + if (!check_retval_uses (arg, phi)) + DUMP_AND_RETURN ("phi arg has uses outside phi" + " and comparisons against 0.") gimple *arg_def = SSA_NAME_DEF_STMT (arg); gcall *call_stmt = dyn_cast (arg_def); diff --git a/gcc/testsuite/gcc.dg/ipa/pr83648.c b/gcc/testsuite/gcc.dg/ipa/pr83648.c new file mode 100644 index 00000000000..03b45de671b --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/pr83648.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-local-pure-const-details" } */ + +void *g(unsigned n) +{ + return n ? __builtin_malloc (n) : 0; +} + +/* { dg-final { scan-tree-dump "Function found to be malloc: g" "local-pure-const1" } } */