From patchwork Tue May 17 18:39:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 1632499 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=pUONoDXP; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4L2lKq3B84z9s09 for ; Wed, 18 May 2022 04:40:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 22EAA3857831 for ; Tue, 17 May 2022 18:40:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22EAA3857831 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652812813; bh=LKDv5gbOMzNVJeIipm+cYQ1ONhPMij2EBzDem89VMhM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pUONoDXPgAS6v0gY7/CvLSLaS4J6Y6u1TIfRksFZNBdCy4uPqvuog0UM6p/AK52f7 OmwVjqTMFb6/9RsnMHUHDflCFqyJr16Yj1Gv91UJudu3MWBWUygg45sSsHoSt2qgp1 5PdcQWEwlJE3/Y0fcq6De4QVKlkRyROkSPxmC3K4= 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 2BA9D3858407 for ; Tue, 17 May 2022 18:39:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2BA9D3858407 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-317-rN8aFC20Mb-6vs2P7n25Pg-1; Tue, 17 May 2022 14:39:14 -0400 X-MC-Unique: rN8aFC20Mb-6vs2P7n25Pg-1 Received: by mail-qt1-f198.google.com with SMTP id h5-20020ac81385000000b002f3f91834ffso11003qtj.10 for ; Tue, 17 May 2022 11:39:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:from:subject; bh=krBh/r+tTjgqdPT7c4OcOAdJXNVL62/rzcSeE2UpxoM=; b=4T2N7GiEyyDK8izUF048CV1k9oz/GDY+JiAh9uRbyg2fDGVcUOg0yPzhZHDBy1RAHH 3cmIUjw0CpQesFWGeZwv1fq6OSlwcyS8GyiV5lzy8KGJtDwruFh2f2QBh8kGsZ9QvyqG JUmFfZTtXiJtpKThbajvQ38Vm0E9oOGPTLxWA5+FHki5QbNi/3PpQw0niZ1jdybyQjMB ZqvfnUGud53WMCpW9Pec3AE2PAkfYZ+WuiGajygZ5Y4WkmPmw/D+0hdfcCLLsDZB9dv0 dDczhIQ9DqyS9mnSE16WBz7uy979HV15Yj37/wE/9RZWmhqzmKbQbtcnSWne9LuyXNvA rN8A== X-Gm-Message-State: AOAM5308CBAXImB80Y1/ZHcJLpRuv2dDIn+0NXLzYk+YsmAiFzqVmuQy YwQ8Cx0bJtqeAB/AwEtrrgZjtJCxya/bcZpPYXCE0XG6GlyqC1uPE9l+zl/vwbf6gOfhPld2JBT XNLnYBdMcVjzohPnbZLaTTRj9ZmOl/E6mEu330vhD/q/nqKT0mE4uVXFW3r74Kk88FwvaSg== X-Received: by 2002:a05:622a:1792:b0:2f3:cc23:d526 with SMTP id s18-20020a05622a179200b002f3cc23d526mr21329346qtk.205.1652812753448; Tue, 17 May 2022 11:39:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzK25FUBZAwo06gNqyp0V+CRJaHXwmYE9h96rXKuieezivzaxNq/Jwrw6WDwRzHZRjJMytvHw== X-Received: by 2002:a05:622a:1792:b0:2f3:cc23:d526 with SMTP id s18-20020a05622a179200b002f3cc23d526mr21329325qtk.205.1652812753115; Tue, 17 May 2022 11:39:13 -0700 (PDT) Received: from ?IPV6:2607:fea8:a261:5e00::94b0? ([2607:fea8:a261:5e00::94b0]) by smtp.gmail.com with ESMTPSA id y15-20020ac8524f000000b002f39b99f67fsm7927953qtn.25.2022.05.17.11.39.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 May 2022 11:39:12 -0700 (PDT) Message-ID: Date: Tue, 17 May 2022 14:39:11 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 To: gcc-patches Subject: [PATCH] Add divide by zero side effect. X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-12.8 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.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: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" I haven't checked this patch in yet.  This implements a side effect that the divisor cannot be 0 after a divide executes. This allows us to fold the divide away: a = b / c; if (c == 0)   dead(); This bootstraps on x86_64-pc-linux-gnu with no regressions, but I first wanted to check to see if there are some flags or conditions that should e checked in order NOT to do this optimization.  I am guessing there is probably something :-)    Anyway, this is how we straightforwardly add side effects now. Does the patch conditions need tweaking to apply the side effect? Andrew commit 3bbcccf2ddd4d50cc5febf630bd8b55a45688352 Author: Andrew MacLeod Date: Mon Apr 4 16:13:57 2022 -0400 Add divide by zero side effect. After a divide, we know the divisor is not zero. gcc/ * gimple-range-side-effect.cc (stmt_side_effects::stmt_side_effects): Add support for all divides. gcc/testsuite/ * gcc.dg/tree-ssa/evrp-zero.c: New. diff --git a/gcc/gimple-range-side-effect.cc b/gcc/gimple-range-side-effect.cc index 2c8c77dc569..548e4bea313 100644 --- a/gcc/gimple-range-side-effect.cc +++ b/gcc/gimple-range-side-effect.cc @@ -116,6 +116,23 @@ stmt_side_effects::stmt_side_effects (gimple *s) walk_stmt_load_store_ops (s, (void *)this, non_null_loadstore, non_null_loadstore); + if (is_a (s)) + { + switch (gimple_assign_rhs_code (s)) + { + case TRUNC_DIV_EXPR: + case CEIL_DIV_EXPR: + case FLOOR_DIV_EXPR: + case ROUND_DIV_EXPR: + case EXACT_DIV_EXPR: + // Divide means operand 2 is not zero after this stmt. + if (gimple_range_ssa_p (gimple_assign_rhs2 (s))) + add_nonzero (gimple_assign_rhs2 (s)); + break; + default: + break; + } + } } // ------------------------------------------------------------------------- diff --git a/gcc/testsuite/gcc.dg/tree-ssa/evrp-zero.c b/gcc/testsuite/gcc.dg/tree-ssa/evrp-zero.c new file mode 100644 index 00000000000..2b76e449c9b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/evrp-zero.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp " } */ + +/* Side effects of divide are that the divisor cannot be 0. */ + +void dead (int); + +void +f1 (int a, int c) { + int b = a; + if (a / c > 10) + b = c; + + if (c == 0) + dead (b); +} + + +void +f2 (int a, int c) { + int nz = c == 0; + int b = a / c; + if (nz) + dead (0); +} + + +/* { dg-final { scan-tree-dump-not "dead" "evrp" } } */