From patchwork Thu Aug 10 22:24:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Hubicka X-Patchwork-Id: 134240 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp723137vqi; Thu, 10 Aug 2023 15:25:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpc669abbrOBFGD9A9rwtr4XcbsWydtCX4UIYy7JieHiIw5zATLDabZCG1o93Pe/SlwNH+ X-Received: by 2002:a17:906:5395:b0:99d:7336:728c with SMTP id g21-20020a170906539500b0099d7336728cmr181044ejo.35.1691706341898; Thu, 10 Aug 2023 15:25:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691706341; cv=none; d=google.com; s=arc-20160816; b=xPKrJR6RFxW1of6JnUUSpKfOK1HI6Ot05ccgb0VemWUym50DxjhHtPgW7/0QvpE6jz DX3BWid+p4OiRr52TUnLpTvY5Q3p28FCyiAIVbQYHmgO6r8WVOWopnjG6etLoModjLVc 1/xLl4/mi3wEQaJsiUwYodNRpuQ57xL+0SLs25R5KPpUfaZJ0qyq4UF9oBkKQNUVFQeP eDzyVGgwXLjSOcXkOs+80i05WUtupsZTlQ2an4RBHuAEYzwgMcDyseNLvr1rppKe7JIE /pNQh+Iucc6s87eLjl9pc8ZrmsMKbt4rXnD19CwmcNYhD7JE2YDXYxzhLUrXpNO2Rsof /nnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Hj/SPF9e4mS8JgbMW9U0QcBKoVqRgisabKKvZ4Rd7lU=; fh=KxPrluukuxmHUzYWntxWAHNXePa4vWvGreVeug6Wy5k=; b=IbyDl2TFjQ2IlWxab60zrvtd5WuQiP0kaczKLFkRNgEaCtT0+Kk0fTE/0pL2+bwWAY oS3Oir2Nc3+IkNlUCLReLhjrlqXCZeLmF/VS88qyf+/yEbs5cjg+yhvdWMaUaLpRg7Qq lVn2BRefhEH349DOmLlyEq85Cc5DwhNqoJAawZnCvQ3SDWMvhFR2uXcIBnkRpl60TRxg UKcqWTuM12XcGctmRn+5Z5zwfRHF/l7adiJmWhqHpxhaXd6QwLIn0qrAoN7mqDHRL9E2 3UYBfQaLXcnGp0OO5XGST4EQLgmuv5wi1fEpoqR5sL/8xuNsmrmyfnoNBT4t1VVdudiH G3tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jNo5Nydw; 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=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gl5-20020a170906e0c500b00997c7157a03si2353201ejb.642.2023.08.10.15.25.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 15:25:41 -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=@gcc.gnu.org header.s=default header.b=jNo5Nydw; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6232D3857BA4 for ; Thu, 10 Aug 2023 22:25:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6232D3857BA4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691706339; bh=Hj/SPF9e4mS8JgbMW9U0QcBKoVqRgisabKKvZ4Rd7lU=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jNo5NydwAoBdAz5D/zSxmHPTwkLhdWcasMNJrLUHFW/gn3NyufpvlavlvqXzoSE9M pMRJVLVvnK/752VRsT023FdOWBgWNx8T73aUQxRFZHZ1mPbm+WooSrek33CQZJre1R wsBg/kv5c43DbPwXYAwQS5YaQsk1tmViPk4NwSqA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id DCD4C3857835 for ; Thu, 10 Aug 2023 22:24:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DCD4C3857835 Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id A6F47281318; Fri, 11 Aug 2023 00:24:54 +0200 (CEST) Date: Fri, 11 Aug 2023 00:24:54 +0200 To: gcc-patches@gcc.gnu.org Subject: Fix division by zero in tree-ssa-loop-split Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, JMQ_SPF_NEUTRAL, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jan Hubicka via Gcc-patches From: Jan Hubicka Reply-To: Jan Hubicka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773882668667679564 X-GMAIL-MSGID: 1773882668667679564 Hi, Profile update I added to tree-ssa-loop-split can divide by zero in situation that the conditional is predicted with 0 probability which is triggered by jump threading update in the testcase. gcc/ChangeLog: PR middle-end/110923 * tree-ssa-loop-split.cc (split_loop): Watch for division by zero. gcc/testsuite/ChangeLog: PR middle-end/110923 * gcc.dg/tree-ssa/pr110923.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110923.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110923.c new file mode 100644 index 00000000000..8f5720a5e9e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110923.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-lsplit-details-blocks" } */ +int a, b, c, d; +int main() { + for (a = 0; a < 2; a++) { + if (b > 2) + c = 0; + if (b > a) + d = 0; + } + return 0; +} +/* { dg-final { scan-tree-dump-times "loop split" 1 "lsplit" } } */ +/* { dg-final { scan-tree-dump-not "Invalid sum" "lsplit" } } */ diff --git a/gcc/tree-ssa-loop-split.cc b/gcc/tree-ssa-loop-split.cc index 2f7918c6e65..64464802c1e 100644 --- a/gcc/tree-ssa-loop-split.cc +++ b/gcc/tree-ssa-loop-split.cc @@ -703,7 +703,7 @@ split_loop (class loop *loop1) split between of the two new loops. Keep orignal estimate since it is likely better then completely dropping it. - TODO: If we know that onle of the new loops has constant + TODO: If we know that one of the new loops has constant number of iterations, we can do better. We could also update upper bounds. */ if (loop1->any_estimate @@ -713,11 +713,15 @@ split_loop (class loop *loop1) ? true_edge->probability.to_sreal () : (sreal)1; sreal scale2 = false_edge->probability.reliable_p () ? false_edge->probability.to_sreal () : (sreal)1; + sreal div1 = loop1_prob.to_sreal (); /* +1 to get header interations rather than latch iterations and then -1 to convert back. */ - loop1->nb_iterations_estimate - = MAX ((((sreal)loop1->nb_iterations_estimate.to_shwi () + 1) * scale - / loop1_prob.to_sreal ()).to_nearest_int () - 1, 0); + if (div1 != 0) + loop1->nb_iterations_estimate + = MAX ((((sreal)loop1->nb_iterations_estimate.to_shwi () + 1) + * scale / div1).to_nearest_int () - 1, 0); + else + loop1->any_estimate = false; loop2->nb_iterations_estimate = MAX ((((sreal)loop2->nb_iterations_estimate.to_shwi () + 1) * scale2 / profile_probability::very_likely ().to_sreal ())