From patchwork Fri Jun 21 22:25:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1950995 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cvfVRpeD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W5X530wqqz20Wk for ; Sat, 22 Jun 2024 08:26:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7BA13830B69 for ; Fri, 21 Jun 2024 22:26:12 +0000 (GMT) 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 D191B38313A7 for ; Fri, 21 Jun 2024 22:25:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D191B38313A7 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D191B38313A7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719008751; cv=none; b=nQn6yhIP2UvaUtekaw8DwQC/BEefl5bts9WxEbT+XXe8mdd85kj2pGIQRhlPD7lSj+ZnUEBPBiRlq+TLAsK7yLHtweIGBgUP9o6sPRs7Z3e4pgNNQ4SExxw9OrbGBeev3SH1P+njAKSMsprTgev9gmYEEEfGvTmfnw0IeAAYOhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719008751; c=relaxed/simple; bh=YZJFTwdbSiGcpFkgPVq2W5Ubisgmx6y3XO2GvdoFvCg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=HIJkkYiO4efyr7GdM3oYyxGlU/zCjT8MbX2PXoB5s2TyXoyt/7jIOs1VfSm9O+5gB2+8qDz9MI6wivCSbys11sQX5zuSef2kTESDOVLUBd1lFiI881chHO7mUQYVbf/42xDxfBWZLqBb7lOZjWKb2R6pff7tsb1DCayCYXlOJJ0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719008749; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YkTcNKGsdw/y536nj4qptYoSuFIKJupAgZ/0SrT/n/c=; b=cvfVRpeDrzpmFXA6V6VyBPe4PWpcHCxysxPK1dtlm1mf/iMpzYttSRbRD0UpnqnHMXt/lX Nm171VifEEwWaTdha3GckiO9pDpLFlspiQiI9aIziQt9iog8xC4Pi4cJhQCL/9/4nRi/o7 ZM8/MqOwzt/KyOdofQ2m8CxZwqQDuFY= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-159-QAs7NrFlNVeOXuSQKVGkMQ-1; Fri, 21 Jun 2024 18:25:47 -0400 X-MC-Unique: QAs7NrFlNVeOXuSQKVGkMQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3DA4119560B2 for ; Fri, 21 Jun 2024 22:25:46 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.10.107]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 545673000218; Fri, 21 Jun 2024 22:25:44 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH] cfg: propagate source location in gimple_split_edge [PR115564] Date: Fri, 21 Jun 2024 18:25:41 -0400 Message-Id: <20240621222541.1166986-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.4 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org PR analyzer/115564 reports a missing warning from the analyzer on this infinite loop at -O2 and above: void test (unsigned b) { for (unsigned i = b; i >= 0; --i) {} } The issue is that there are no useful location_t values in the CFG by the time the analyzer sees it: two basic blocks with no statements, connected by edges with UNKNOWN_LOCATION for their "goto_locus" values. The analyzer's attempts to get a location for the loop fail with "UNKNOWN_LOCATION", and so it gives up on the warning. Root cause is that the edge in question is created by gimple_split_edge within the loop optimizer, and gimple_split_edge creates the new edge with UNKNOWN_LOCATION. This patch tweaks gimple_split_edge to copy edge_in->goto_locus's to the new edge, so that the edge seen by the analyzer has a useful goto_locus value, fixing the issue. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu, which shows 8 new true positives from -Wanalyzer-infinite-loop with the patch. OK for trunk? gcc/testsuite/ChangeLog: PR analyzer/115564 * c-c++-common/analyzer/infinite-loop-pr115564.c: New test. gcc/ChangeLog: PR analyzer/115564 * tree-cfg.cc (gimple_split_edge): Propagate any source location from EDGE_IN to the new edge. Signed-off-by: David Malcolm --- .../c-c++-common/analyzer/infinite-loop-pr115564.c | 8 ++++++++ gcc/tree-cfg.cc | 3 +++ 2 files changed, 11 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c diff --git a/gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c b/gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c new file mode 100644 index 000000000000..950d92dd1254 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/infinite-loop-pr115564.c @@ -0,0 +1,8 @@ +/* Verify that we detect the infinite loop below even at -O2. */ + +/* { dg-additional-options "-O2" } */ + +void test (unsigned b) +{ + for (unsigned i = b; i >= 0; --i) {} /* { dg-warning "infinite loop" } */ +} diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc index 7fb7b92966be..45c0eef6c095 100644 --- a/gcc/tree-cfg.cc +++ b/gcc/tree-cfg.cc @@ -3061,6 +3061,9 @@ gimple_split_edge (edge edge_in) /* set_phi_nodes sets the BB of the PHI nodes, so do it manually here. */ dest->il.gimple.phi_nodes = saved_phis; + /* Propagate any source location from EDGE_IN to the new edge. */ + new_edge->goto_locus = edge_in->goto_locus; + return new_bb; }