From patchwork Fri Sep 29 09:50:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 146473 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3901061vqu; Fri, 29 Sep 2023 02:50:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlY7C7AHGhemk5bdyU334J/+HOcVxvpDJm/T+e6yY/IQy8cpW+7nMoeIoBUrqhzRwScmKY X-Received: by 2002:a17:906:7496:b0:9b2:74a1:6b30 with SMTP id e22-20020a170906749600b009b274a16b30mr3877785ejl.33.1695981031001; Fri, 29 Sep 2023 02:50:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695981030; cv=none; d=google.com; s=arc-20160816; b=fG9jn06tBkeW7p3fJ3CPhhfq7wh0fJe+hDCKxXOf7Q55nFqlORHat0qNcagE0nnzvS VoNJWaVU4ahgTNF4D3zwtjHxPJi6YwtKoZWrShsa6atuWJJ7n0OIZN4lFHb40H7Emvup wZhXp3IPMFJxbrSvxIGMjoWQpH9y1uhfVR88+D7PQXBqBZDaW/OAWCOyCYBmu4jJwcHQ XQRdo08iyiDIpFYOJ0pOJxCYL7VLxg3NTTrLlbIN5E/GUpY4y0KRT9dF6up1dlh/8RZC lThH0lTGojlj6reyF/k0gf5aMr7pqPcR1QX6bhGfGwYuS2JEzAMTfBHD7V5ke7cuhyIZ vAWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:mime-version:subject :to:from:date:dkim-signature:dkim-signature:dmarc-filter :delivered-to; bh=AOlTBUn6QSLt+8NRjUBAzAa2VVfBS6xSFKfcUOt8tBs=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=asmA8QUI29FlN0EPAJFfEMAuoWHWZn3kTTiS/Pn9e2UTWeMsnizedzfQQwtoPm4jbN fQL3gdfj6rlXAqy+MQqVD8TKGUuFsUOneF9bCnhFyvjcwrLGRzHhFfQjhwoApfMLYeCs qVx0Lye+fC4EWqklxP6UcxiDRCjLHbQRhvQxjK8Q5vkTvm4nWtBPznA1aHXeBy2isY3x 4aayNFyGMvIvl8ZPL/5XcfntdAmTjbXpbec3TUbI2J34NieRQgIX2TJQf/gJUGjA/26Q S4gSHG4zFBLb3WjHXV/A54I77XbDmgP68sppGYk/vTGiQAjTRVCydMGXNaSG1KxizQcU x1PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=WknPuUkC; dkim=neutral (no key) header.i=@suse.de header.b=fgsMZ8UR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ga38-20020a1709070c2600b009b2c6a3a028si1439549ejc.651.2023.09.29.02.50.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 02:50:30 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=WknPuUkC; dkim=neutral (no key) header.i=@suse.de header.b=fgsMZ8UR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 24EF9387543F for ; Fri, 29 Sep 2023 09:50:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 1742F3858C01 for ; Fri, 29 Sep 2023 09:50:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1742F3858C01 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 4E6381F390 for ; Fri, 29 Sep 2023 09:50:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1695981001; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=AOlTBUn6QSLt+8NRjUBAzAa2VVfBS6xSFKfcUOt8tBs=; b=WknPuUkCBX6Cvs4+GEZbA0giWyguy6Y4CRWy/ElLV3X0c/jiQ8mRQfv4ot0sEJU3PDOpKN t88m9WIEOQ1jUbWyLuextjLwjPyYmYbfKLkXf1pDDJa7Rr5yY9oXmMkFYot4l8yj9o9iZa 7h6yYfI4pr0zf8JoILjVtRKg6z2XMEQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1695981001; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=AOlTBUn6QSLt+8NRjUBAzAa2VVfBS6xSFKfcUOt8tBs=; b=fgsMZ8URd6LRLGKnN212yHYz3T7w3sv99hDPdipTicP4sA10LOQiG63pJEuXYrETOaHVHh az1rNTDK+VU5zaBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3ABB91390A for ; Fri, 29 Sep 2023 09:50:01 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 254LDcmdFmU1FQAAMHmgww (envelope-from ) for ; Fri, 29 Sep 2023 09:50:01 +0000 Date: Fri, 29 Sep 2023 11:50:00 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/111583 - loop distribution issue MIME-Version: 1.0 Message-Id: <20230929095001.3ABB91390A@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778365005977719208 X-GMAIL-MSGID: 1778365005977719208 The following conservatively fixes loop distribution to only recognize memset/memcpy and friends when at least one element is going to be processed. This avoids having an unconditional builtin call in the IL that might imply the source and destination pointers are non-NULL when originally pointers were not always dereferenced. With -Os loop header copying is less likely to ensure this. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/111583 * tree-loop-distribution.cc (find_single_drs): Ensure the load/store are always executed. * gcc.dg/tree-ssa/pr111583-1.c: New testcase. * gcc.dg/tree-ssa/pr111583-2.c: Likewise. --- gcc/testsuite/gcc.dg/tree-ssa/pr111583-1.c | 30 ++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr111583-2.c | 36 ++++++++++++++++++++++ gcc/tree-loop-distribution.cc | 15 +++++++++ 3 files changed, 81 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111583-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111583-2.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111583-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111583-1.c new file mode 100644 index 00000000000..1dd8dbcf1d8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111583-1.c @@ -0,0 +1,30 @@ +/* { dg-do run } */ +/* { dg-options "-Os" } */ + +short a, f, i; +static const int *e; +short *g; +long h; +int main() +{ + { + unsigned j = i; + a = 1; + for (; a; a++) { + { + long b = j, d = h; + int c = 0; + while (d--) + *(char *)b++ = c; + } + if (e) + break; + } + j && (*g)--; + const int **k = &e; + *k = 0; + } + if (f != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111583-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111583-2.c new file mode 100644 index 00000000000..0ee21854552 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111583-2.c @@ -0,0 +1,36 @@ +/* { dg-do run } */ +/* { dg-options "-Os" } */ + +int b, c, d; +char e; +short f; +const unsigned short **g; +char h(char k) { + if (k) + return '0'; + return 0; +} +int l() { + b = 0; + return 1; +} +static short m(unsigned k) { + const unsigned short *n[65]; + g = &n[4]; + k || l(); + long a = k; + char i = 0; + unsigned long j = k; + while (j--) + *(char *)a++ = i; + c = h(d); + f = k; + return 0; +} +int main() { + long o = (e < 0) << 5; + m(o); + if (f != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-loop-distribution.cc b/gcc/tree-loop-distribution.cc index a28470b66ea..39fd4402d25 100644 --- a/gcc/tree-loop-distribution.cc +++ b/gcc/tree-loop-distribution.cc @@ -1574,6 +1574,7 @@ find_single_drs (class loop *loop, struct graph *rdg, const bitmap &partition_st basic_block bb_ld = NULL; basic_block bb_st = NULL; + edge exit = single_exit (loop); if (single_ld) { @@ -1589,6 +1590,14 @@ find_single_drs (class loop *loop, struct graph *rdg, const bitmap &partition_st bb_ld = gimple_bb (DR_STMT (single_ld)); if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb_ld)) return false; + + /* The data reference must also be executed before possibly exiting + the loop as otherwise we'd for example unconditionally execute + memset (ptr, 0, n) which even with n == 0 implies ptr is non-NULL. */ + if (bb_ld != loop->header + && (!exit + || !dominated_by_p (CDI_DOMINATORS, exit->src, bb_ld))) + return false; } if (single_st) @@ -1604,6 +1613,12 @@ find_single_drs (class loop *loop, struct graph *rdg, const bitmap &partition_st bb_st = gimple_bb (DR_STMT (single_st)); if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb_st)) return false; + + /* And before exiting the loop. */ + if (bb_st != loop->header + && (!exit + || !dominated_by_p (CDI_DOMINATORS, exit->src, bb_st))) + return false; } if (single_ld && single_st)