From patchwork Mon Jul 25 23:35:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 72750 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1397083qga; Mon, 25 Jul 2016 16:35:33 -0700 (PDT) X-Received: by 10.98.32.138 with SMTP id m10mr33843883pfj.146.1469489733729; Mon, 25 Jul 2016 16:35:33 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id tg15si35938457pac.91.2016.07.25.16.35.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jul 2016 16:35:33 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-432497-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-432497-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-432497-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=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=u79epfIgB1qSBXXCg/mgA3s81nM9QPMW6eGLjy4fol7Khc hfR0PI/M3GJNLEilEpl0p6hhLSOsBfuKs6rT8eAwOTkiNjXR64E4KZ0H+jKttRVw bVJ03s1v/+xdOwNj+8Tzldn6VYggZ3TIrxfTPPr7RY3ZDYJSbcekRHVyQk+yM= 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=tlMyb/vCzqL9p5Bbmo0f4qlNW2E=; b=hmpop3Ytkzx0JoZMrkni LcHLbC4UIdD0li9XciFkO5GPBGEOtZFMzerZCmVEIUijEdFVSxKj3Q4Oh3+oPwC8 WEoo4aiHbjjUs6fzlcXsAbQCvv6HDTG3FcUJPoghzQcQruG4p1UjqfzV5IkT4EOG P+e9ByBlr4sdiq08qO/GV0M= Received: (qmail 75281 invoked by alias); 25 Jul 2016 23:35:19 -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 75271 invoked by uid 89); 25 Jul 2016 23:35:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:pass_lo, looping, call_stmt, cgraph_node X-HELO: mail-io0-f181.google.com Received: from mail-io0-f181.google.com (HELO mail-io0-f181.google.com) (209.85.223.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 25 Jul 2016 23:35:07 +0000 Received: by mail-io0-f181.google.com with SMTP id b62so182471751iod.3 for ; Mon, 25 Jul 2016 16:35:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=CRq0onPlCBSG5WESN1Jpj9itqGtqYjKMn7PeBhXv0TI=; b=doB5SnvO92xvZs0YuxWsu546NDaUhhJiaSYz9FW2SZH9/W/nUouSErE3ghs2UeGWxn ELI2m+KSIWiWPBKAeTa7UDxoSNjwKAsRmjJGl023Lw6Fb2VZmy468xYTcf3ZhPCvNqCw OovgZoOfK/sMdPRY0+mJ5cOTZwegNhNGSsnkRsg1DqYj5gpgtSjWpXaK1FqeDedJBhCd 3n3PKIiiJWYPS6n8T/vV2wDZMzfbqIe+yRUaLaJvzNI+GYGszFp7W4LaZGLLZ0AgW9xE LzN3cz25saaUPRv8imrBQnYhjY0RsvqrQlhE2IPs4EJiKpp857NOEx0cHgrPagMYcs25 Y2NA== X-Gm-Message-State: AEkoouvL4L9xMFgEqlIv2Nnid2FFyHkJm16IabC6nzPgSGMQLWSuUUUW98JVu1eoeKDFVMRgTmrgwzWXeealXM9K X-Received: by 10.107.175.27 with SMTP id y27mr25435643ioe.137.1469489700933; Mon, 25 Jul 2016 16:35:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.48.197 with HTTP; Mon, 25 Jul 2016 16:35:00 -0700 (PDT) From: Prathamesh Kulkarni Date: Tue, 26 Jul 2016 05:05:00 +0530 Message-ID: Subject: [RFC] warn on dead function calls in ipa-pure-const [1/4] To: Richard Biener , Jan Hubicka , gcc Patches X-IsSubscribed: yes Hi, The attached patch emits warnings for functions found to be pure or const by the ipa-pure-const pass. It does not warn for functions with unused return values that have been declared as pure or const by the user since this is already handled in C and C++ FE's. I have split it into parts to individually address fallouts observed during bootstrap+test. I still have to add more test-cases. Apart from that does the patch look OK ? Thanks, Prathamesh diff --git a/gcc/ipa-pure-const.c b/gcc/ipa-pure-const.c index a9570e4..3b8e774 100644 --- a/gcc/ipa-pure-const.c +++ b/gcc/ipa-pure-const.c @@ -226,6 +226,34 @@ warn_function_noreturn (tree decl) true, warned_about, "noreturn"); } +/* Emit diagnostic if the callers don't use return value. + Only to be called for pure/const function */ + +static void +warn_function_unused_ret (struct cgraph_node *node) +{ + tree decl = node->decl; + int flags = flags_from_decl_or_type (decl); + + if (flags & ECF_NORETURN) + return; + + if (!(flags & (ECF_CONST | ECF_PURE)) + || (flags & ECF_LOOPING_CONST_OR_PURE)) + return; + + for (cgraph_edge *e = node->callers; e; e = e->next_caller) + { + gcall *g = e->call_stmt; + if (g + && !VOID_TYPE_P (gimple_call_return_type (g)) + && gimple_call_lhs (g) == NULL) + warning_at (gimple_location (g), OPT_Wunused_value, + "Call from %s to %s has no effect", + e->caller->name (), e->callee->name ()); + } +} + /* Return true if we have a function state for NODE. */ static inline bool @@ -1475,6 +1503,8 @@ propagate_pure_const (void) /* Inline clones share declaration with their offline copies; do not modify their declarations since the offline copy may be different. */ + bool warn_unused_ret = false; + if (!w->global.inlined_to) switch (this_state) { @@ -1482,6 +1512,7 @@ propagate_pure_const (void) if (!TREE_READONLY (w->decl)) { warn_function_const (w->decl, !this_looping); + warn_unused_ret = true; if (dump_file) fprintf (dump_file, "Function found to be %sconst: %s\n", this_looping ? "looping " : "", @@ -1496,6 +1527,8 @@ propagate_pure_const (void) NULL, true); if (w->set_const_flag (true, this_looping)) { + if (warn_unused_ret) + warn_function_unused_ret (w); if (dump_file) fprintf (dump_file, "Declaration updated to be %sconst: %s\n", @@ -1509,6 +1542,7 @@ propagate_pure_const (void) if (!DECL_PURE_P (w->decl)) { warn_function_pure (w->decl, !this_looping); + warn_unused_ret = true; if (dump_file) fprintf (dump_file, "Function found to be %spure: %s\n", this_looping ? "looping " : "", @@ -1521,6 +1555,8 @@ propagate_pure_const (void) NULL, true); if (w->set_pure_flag (true, this_looping)) { + if (warn_unused_ret) + warn_function_unused_ret (w); if (dump_file) fprintf (dump_file, "Declaration updated to be %spure: %s\n", @@ -1808,11 +1844,14 @@ pass_local_pure_const::execute (function *fun) changed = true; } + bool warn_unused_ret = false; + switch (l->pure_const_state) { case IPA_CONST: if (!TREE_READONLY (current_function_decl)) { + warn_unused_ret = true; warn_function_const (current_function_decl, !l->looping); if (dump_file) fprintf (dump_file, "Function found to be %sconst: %s\n", @@ -1828,6 +1867,8 @@ pass_local_pure_const::execute (function *fun) } if (!skip && node->set_const_flag (true, l->looping)) { + if (warn_unused_ret) + warn_function_unused_ret (cgraph_node::get_create (current_function_decl)); if (dump_file) fprintf (dump_file, "Declaration updated to be %sconst: %s\n", l->looping ? "looping " : "", @@ -1840,6 +1881,7 @@ pass_local_pure_const::execute (function *fun) if (!DECL_PURE_P (current_function_decl)) { warn_function_pure (current_function_decl, !l->looping); + warn_unused_ret = true; if (dump_file) fprintf (dump_file, "Function found to be %spure: %s\n", l->looping ? "looping " : "", @@ -1854,6 +1896,8 @@ pass_local_pure_const::execute (function *fun) } if (!skip && node->set_pure_flag (true, l->looping)) { + if (warn_unused_ret) + warn_function_unused_ret (cgraph_node::get_create (current_function_decl)); if (dump_file) fprintf (dump_file, "Declaration updated to be %spure: %s\n", l->looping ? "looping " : "", diff --git a/gcc/testsuite/gcc.dg/Wunused-value-4.c b/gcc/testsuite/gcc.dg/Wunused-value-4.c new file mode 100644 index 0000000..1f5463c --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunused-value-4.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wunused-value" } */ + +__attribute__((noinline, no_icf)) +int f() +{ + return 1; +} + +__attribute__((noinline, no_icf, const)) +int g() +{ + return 1; +} + +int foo (void) +{ + f (); /* { dg-warning "Call from foo to f has no effect" } */ + int k = f (); /* { dg-bogus "Call from foo to f has no effect" } */ + g (); /* { dg-warning "statement with no effect" } */ + return k; +} +