From patchwork Thu Feb 17 02:43:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1593969 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=BpjspYNA; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JzfKl2T3Fz9sFk for ; Thu, 17 Feb 2022 13:44:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 07F7B385781A for ; Thu, 17 Feb 2022 02:44:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 07F7B385781A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1645065846; bh=tZP7Ma2/pFxZhKZSNWOBrEOKi9SfxTgtzJuPnR+IuIU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BpjspYNAi8VQ4KdPFkhdAAMb/vBOGH4s1zbFcBF48IHPdecm31tih7CarSMsAd27U i+wXeox54RpiOpObfEFpI0eYJm1YkgVJmcRpw37lwcgkRNBXMcqFaVJyUoGW6vlaJD egJPx23v/HEOdTvag24TaDaQ5Cj6kO+BTDfJccjY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 37ACB3858D20 for ; Thu, 17 Feb 2022 02:43:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 37ACB3858D20 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-369-srOC9ENMM1GN1l_koIWcjQ-1; Wed, 16 Feb 2022 21:43:40 -0500 X-MC-Unique: srOC9ENMM1GN1l_koIWcjQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C0D5E1006AA6 for ; Thu, 17 Feb 2022 02:43:39 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id 17B37101F6CA; Thu, 17 Feb 2022 02:43:34 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: const functions have no side effects [PR104576] Date: Wed, 16 Feb 2022 21:43:33 -0500 Message-Id: <20220217024333.1126644-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" PR analyzer/104576 tracks that we issue a false positive from -Wanalyzer-use-of-uninitialized-value for the reproducers of PR 63311 when optimization is disabled. The root cause is that the analyzer was considering that a call to __builtin_sinf could have side-effects. This patch fixes things by generalizing the handling for "pure" functions to also consider "const" functions. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r12-7270-g5fbcbcaff7248604e04b39464f4fbd64fbf6e43b. gcc/analyzer/ChangeLog: PR analyzer/104576 * region-model.cc: Include "calls.h". (region_model::on_call_pre): Use flags_from_decl_or_type to generalize check for DECL_PURE_P to also check for ECF_CONST. gcc/testsuite/ChangeLog: PR analyzer/104576 * gcc.dg/analyzer/torture/uninit-pr63311.c: New test. * gcc.dg/analyzer/uninit-pr104576.c: New test. * gfortran.dg/analyzer/uninit-pr63311.f90: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 6 +- .../gcc.dg/analyzer/torture/uninit-pr63311.c | 134 ++++++++++++++++++ .../gcc.dg/analyzer/uninit-pr104576.c | 16 +++ .../gfortran.dg/analyzer/uninit-pr63311.f90 | 39 +++++ 4 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/torture/uninit-pr63311.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/uninit-pr104576.c create mode 100644 gcc/testsuite/gfortran.dg/analyzer/uninit-pr63311.f90 diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 69e8fa7d1e3..d4d7816e0d5 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -72,6 +72,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-phinodes.h" #include "tree-ssa-operands.h" #include "ssa-iterators.h" +#include "calls.h" #if ENABLE_ANALYZER @@ -1271,13 +1272,14 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt, in region-model-impl-calls.cc. Having them split out into separate functions makes it easier to put breakpoints on the handling of specific functions. */ + int callee_fndecl_flags = flags_from_decl_or_type (callee_fndecl); if (fndecl_built_in_p (callee_fndecl, BUILT_IN_NORMAL) && gimple_builtin_call_types_compatible_p (call, callee_fndecl)) switch (DECL_UNCHECKED_FUNCTION_CODE (callee_fndecl)) { default: - if (!DECL_PURE_P (callee_fndecl)) + if (!(callee_fndecl_flags & (ECF_CONST | ECF_PURE))) unknown_side_effects = true; break; case BUILT_IN_ALLOCA: @@ -1433,7 +1435,7 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt, /* Handle in "on_call_post". */ } else if (!fndecl_has_gimple_body_p (callee_fndecl) - && !DECL_PURE_P (callee_fndecl) + && (!(callee_fndecl_flags & (ECF_CONST | ECF_PURE))) && !fndecl_built_in_p (callee_fndecl)) unknown_side_effects = true; } diff --git a/gcc/testsuite/gcc.dg/analyzer/torture/uninit-pr63311.c b/gcc/testsuite/gcc.dg/analyzer/torture/uninit-pr63311.c new file mode 100644 index 00000000000..a73289cb83f --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/torture/uninit-pr63311.c @@ -0,0 +1,134 @@ +/* { dg-additional-options "-Wno-analyzer-too-complex" } */ + +int foo () +{ + static volatile int v = 42; + int __result_foo; + + __result_foo = (int) v; + return __result_foo; +} + +void test (int * restrict n, int * restrict flag) +{ + int i; + int j; + int k; + double t; + int tt; + double v; + + if (*flag) + { + t = 4.2e+1; + tt = foo (); + } + L_1: ; + v = 0.0; + { + int D_3353; + + D_3353 = *n; + i = 1; + if (i <= D_3353) + { + while (1) + { + { + int D_3369; + + v = 0.0; + if (*flag) + { + if (tt == i) + { + { + double M_0; + + M_0 = v; + if (t > M_0 || (int) (M_0 != M_0)) + { + M_0 = t; + } + v = M_0; + } + } + L_5:; + } + L_4:; + { + int D_3359; + + D_3359 = *n; + j = 1; + if (j <= D_3359) + { + while (1) + { + { + int D_3368; + + { + int D_3362; + + D_3362 = *n; + k = 1; + if (k <= D_3362) + { + while (1) + { + { + int D_3367; + + { + double D_3366; + double M_1; + + M_1 = v; + D_3366 = (double) __builtin_sinf ((float) (j * k)); + if (D_3366 > M_1 || (int) (M_1 != M_1)) + { + M_1 = D_3366; + } + v = M_1; + } + L_8:; + D_3367 = k == D_3362; + k = k + 1; + if (D_3367) goto L_9; + } + } + } + L_9:; + } + L_6:; + D_3368 = j == D_3359; + j = j + 1; + if (D_3368) goto L_7; + } + } + } + L_7:; + } + L_2:; + D_3369 = i == D_3353; + i = i + 1; + if (D_3369) goto L_3; + } + } + } + L_3:; + } +} + + +int main () +{ + int flag; + int n; + + n = 4; + flag = 0; + test (&n, &flag); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/analyzer/uninit-pr104576.c b/gcc/testsuite/gcc.dg/analyzer/uninit-pr104576.c new file mode 100644 index 00000000000..0b59acdb6e7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/uninit-pr104576.c @@ -0,0 +1,16 @@ +float +test_1 (int *flag, float theta) +{ + float t; + float f; + + if (*flag) + t = 2.0f; + + f = __builtin_sinf (theta); + + if (*flag) + f *= t; + + return f; +} diff --git a/gcc/testsuite/gfortran.dg/analyzer/uninit-pr63311.f90 b/gcc/testsuite/gfortran.dg/analyzer/uninit-pr63311.f90 new file mode 100644 index 00000000000..34cc25da01d --- /dev/null +++ b/gcc/testsuite/gfortran.dg/analyzer/uninit-pr63311.f90 @@ -0,0 +1,39 @@ +! { dg-additional-options "-O0" } + +MODULE M1 + IMPLICIT NONE +CONTAINS + INTEGER FUNCTION foo() + INTEGER, VOLATILE :: v=42 + foo=v + END FUNCTION + SUBROUTINE test(n,flag) + INTEGER :: n,i,j,k,l,tt + LOGICAL :: flag + REAL(KIND=8) :: v,t + IF (flag) THEN + t=42 + tt=foo() + ENDIF + v=0 + DO i=1,n + v=0 + IF (flag) THEN + IF (tt==i) v=MAX(v,t) + ENDIF + DO j=1,n + DO k=1,n + v=MAX(v,sin(REAL(j*k))) + ENDDO + ENDDO + ENDDO + END SUBROUTINE +END MODULE M1 + +USE M1 +INTEGER :: n +LOGICAL :: flag +n=4 +flag=.FALSE. +CALL test(n,flag) +END