From patchwork Tue May 17 18:39:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 54108 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A17483857823 for ; Tue, 17 May 2022 18:41:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A17483857823 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652812879; bh=p26Ru8+T01cAtR8DmgVdC1v1b+yKYG2JXMsQeIRN4e8=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KJUX2ABgW8ghgmjgPG048qlEqLQCo67psy7ppcpP0TDavhi6MwgPhe5Ndu8+9EQAz dUHwYEIl5FsEg6hI2Z+ygSXh2PWajQmuPYpY9P4actnTUvBjMv9Qi2nEffufQ02BpU oms2mc/HmZZlzBVQJ2EHUYXFYYBu+a+Il91joeE8= 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 717223857410 for ; Tue, 17 May 2022 18:39:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 717223857410 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-173-IqKFx20vNpyhzNNr7BPvlg-1; Tue, 17 May 2022 14:39:49 -0400 X-MC-Unique: IqKFx20vNpyhzNNr7BPvlg-1 Received: by mail-qk1-f200.google.com with SMTP id d67-20020a379b46000000b006a00045c8c9so14292353qke.22 for ; Tue, 17 May 2022 11:39:49 -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=N5C3fnqrrL4bKSYWRu6Gp3TbBbzKmwUBrtKm4jj93no=; b=wRtWu1jOMeE9zcAwZ6ZaRUhRAIXFPnQS5jHjRpXFmfE3o+cfAf8IolmZYJYQ7f1Nou KpYXOGApRfTAPtfv92uLoSIceql/FYDmgZjqFIvj/Rc0YmwixQHu9WA3/kPtTBUC38qL rDeVa65DkV7Qt20Da+cljs56e/quM4n1sF+4ZfSPkDjN9ZL8fxCqW/adTBgBKfTRiha1 YxfQibo+Qv27nTs2oXt3MU/v9d/aBHuc+MzxnBixpEA63VcLkKFXHUvs/CUbdCrblQnx Xj4oS3TLb63JeFEtJam+jv9i1+eBpL9cMoXnqYh1IorhtcVkv8erjo/TTTf0BA7Vwfzh Djyg== X-Gm-Message-State: AOAM533XEqVC+XQ8m15/TsRo/UlGjkyZSpV3Fyi3vqT4i8+kxIF/UzlB sur/Da3uGnQaqLfCDxpJguQVXGP6PfQJqsHghkohbYUQr4zBnwPj/G6mEPdmAAgOFQyO5SZ7MKn 2LspKZXeulSMHRv7+TPPZ5peu/wlJowqNmTIIqHEsrEwsXZQlM/+ZKxa3g68ON+k4TX8DQg== X-Received: by 2002:ad4:5ca9:0:b0:45f:b563:8af3 with SMTP id q9-20020ad45ca9000000b0045fb5638af3mr21453724qvh.82.1652812788670; Tue, 17 May 2022 11:39:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8iYxBm+CYLfgzbgVXLo0tP2STNPfZbrqtEuNisz8eyu2NOX2ygMu2epiDMt/8m2ye+xlkOg== X-Received: by 2002:ad4:5ca9:0:b0:45f:b563:8af3 with SMTP id q9-20020ad45ca9000000b0045fb5638af3mr21453705qvh.82.1652812788284; Tue, 17 May 2022 11:39:48 -0700 (PDT) Received: from ?IPV6:2607:fea8:a261:5e00::94b0? ([2607:fea8:a261:5e00::94b0]) by smtp.gmail.com with ESMTPSA id f84-20020a379c57000000b0069fd35d2abcsm8425858qke.112.2022.05.17.11.39.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 May 2022 11:39:47 -0700 (PDT) Message-ID: Date: Tue, 17 May 2022 14:39:46 -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] PR tree-optimization/31178 - Add rshift 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch implements side effects of the second operand of a shift operation. given A >> B or A << B, the range of B is restricted to [0, PRECISION_A). Fortran is currently more permissive than this, allowing the range to be [0, PRECISION_A], so this si the value we currently default to in this patch.  If the fortran front end were adjusted, we could adjust the end point. This currently bootstraps with no regressions on x86_64-pc-linux-gnu. Is this sufficient, or should I also be checking some other flags which may allow other values outside this range to be valid? Andrew PS. Note that in the testcase,  one of the tests is currently disabled as full recomputation of side-effects is not quite in place yet. WHen ti is, I will enable the test. commit e283395a570328874d3215893c7781fd2770d87f Author: Andrew MacLeod Date: Mon Apr 4 16:26:15 2022 -0400 Add rshift side effect. After a shift operation, we can make deductions about the bounds of the shift value based on the precision of the value being shifted. Fortran is currently more permissive than the other front ends, so we set the range of B in A >> B to [0, PRECISION_A] rather than [0, PRECISION_A) that the other front ends require. gcc/ PR tree-optimization/31178 * gimple-range-side-effect.cc (stmt_side_effects::stmt_side_effects): Add suport for LSHIFT_EXPR and RSHIFT_EXPR. gcc/testsuite/ * gcc.dg/tree-ssa/pr31178.c: New. diff --git a/gcc/gimple-range-side-effect.cc b/gcc/gimple-range-side-effect.cc index 548e4bea313..fdd5fdc296d 100644 --- a/gcc/gimple-range-side-effect.cc +++ b/gcc/gimple-range-side-effect.cc @@ -34,6 +34,7 @@ along with GCC; see the file COPYING3. If not see #include "gimple-iterator.h" #include "gimple-walk.h" #include "cfganal.h" +#include "stor-layout.h" // for element_precision() // Adapted from infer_nonnull_range_by_dereference and check_loadstore // to process nonnull ssa_name OP in S. DATA contains a pointer to a @@ -129,6 +130,22 @@ stmt_side_effects::stmt_side_effects (gimple *s) if (gimple_range_ssa_p (gimple_assign_rhs2 (s))) add_nonzero (gimple_assign_rhs2 (s)); break; + + case LSHIFT_EXPR: + case RSHIFT_EXPR: + if (gimple_range_ssa_p (gimple_assign_rhs2 (s))) + { + // A << B, A >>B implies [0, PRECISION_of_A) + tree op1_type = TREE_TYPE (gimple_assign_rhs1 (s)); + tree op2_type = TREE_TYPE (gimple_assign_rhs2 (s)); + tree l = build_int_cst (op2_type, 0); + // C is [0, N), but fortran is [0, N], so default to [0, N]. + tree u = build_int_cst (op2_type, element_precision (op1_type)); + int_range_max shift (l, u); + add_range (gimple_assign_rhs2 (s), shift); + } + break; + default: break; } diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31178.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31178.c new file mode 100644 index 00000000000..27c72fb7104 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31178.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp " } */ + +/* Side effects of divide are that the divisor cannot be 0. */ + +#include "limits.h" +void dead (int); + +void +f1 (int a, int c) { + int b = a; + if ((a << c) > 100) + b = c; + + // Fortran allows [0, sizeof(int)] , so that is GCC default for now. + if (c < 0 || c > sizeof(int) * CHAR_BIT) + dead (b); +} + +#if 0 +/* Until we get recomputation of a side effect value working, ... */ + +int +f2 (int a, int c) { + int nz = (c < 0 || c > sizeof(int) * CHAR_BIT); + int b = a >> c; + if (nz) + dead (0); + return b; +} +#endif + +/* { dg-final { scan-tree-dump-not "dead" "evrp" } } */