From patchwork Thu Oct 14 18:02:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 46245 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 6CB193858026 for ; Thu, 14 Oct 2021 18:06:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6CB193858026 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634234769; bh=NyvBlupDTPZh06pA/PBSQWmTSmuKX9I/VhIq9Vnz/cA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=slJ5ByRiFtr4g+TwRuR2Dbs6bQea/k2hIQ9sPzSzkUV54w/X9ie9PSYuGm3l8eTqj wXJ52ZwD97F08vMB8P8V3Cd5OVYBsHSM67XvxY2e61e5TFuI60UbdTO0sim1KuaXI3 Ozg2cbtF24zBp/Prf0iYIvx5YkC7wv4Lq3wwwx7Q= 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.133.124]) by sourceware.org (Postfix) with ESMTP id D3B75385843D for ; Thu, 14 Oct 2021 18:02:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D3B75385843D Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-298-qc8WF7mKOWSvof4Wwqo1kw-1; Thu, 14 Oct 2021 14:02:43 -0400 X-MC-Unique: qc8WF7mKOWSvof4Wwqo1kw-1 Received: by mail-qk1-f200.google.com with SMTP id y5-20020ae9f405000000b0045fa3120bebso5019101qkl.17 for ; Thu, 14 Oct 2021 11:02:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=NyvBlupDTPZh06pA/PBSQWmTSmuKX9I/VhIq9Vnz/cA=; b=xejGoRlrDpYh0lBXFBZT730Wh+Zh2GuZF+z5QxZrkq0KfMg/PzQXOiNTK4HRD4nN9A ux5kmhroXCxTkAWP3vTXD0YFiCT+cV7XKqOZ5gwna53OIUfu6HBX32dwKrs52951VR1C Ek0/dek3AnR5cLJkNsCbRpolxcxmFNFJ89NgZzgclPFdYEXlVFAYyWneT7BPFfvt94Hy 9X48i/Q7m7qN9lY0kDgICZ44Wp02RRdLBPdyyyRreNGBesz/27HaY9hSt3m/ZkviO8Ex VU9uQs+paF+SMHQl/3Eh5g01fQGmsi/LkttDLYqfCdyYv7yH3RaOHHBsmH/pQ4PYxiVN G+Zw== X-Gm-Message-State: AOAM532MUxrOqcmh+a8r2EGWqt+RY1TRPXastfjZtYWvk6a019BGKZLr TJhHWHWgzCIlvIMY4P0zpdc14RbfDC8kN2dUawPXqyfBLgzemsM1wMCXyq5CI1Vm1JrFqL49rNK qOX8eJSqENcxNPUFkKoHphswHOSP3dPdRw08UMI7u9+uJLj8b9d9hX1cwkO2Rk1rWKXeVYA== X-Received: by 2002:ac8:4995:: with SMTP id f21mr8030283qtq.417.1634234561880; Thu, 14 Oct 2021 11:02:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+rRCZPqmMf7wMjDfDzqu01oEkQf/O9bVeukZt5ZIX6cm8m6JZjZXWZ7AzJf9ZG8bNLjWB2Q== X-Received: by 2002:ac8:4995:: with SMTP id f21mr8030243qtq.417.1634234561582; Thu, 14 Oct 2021 11:02:41 -0700 (PDT) Received: from [192.168.0.102] ([192.24.49.122]) by smtp.gmail.com with ESMTPSA id i13sm1983163qko.70.2021.10.14.11.02.40 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Oct 2021 11:02:41 -0700 (PDT) To: gcc-patches Subject: [COMMITTED] tree-optimization/102738 - Simplification for right shift. Message-ID: <3182839e-32e7-dde6-c896-1b4400afda09@redhat.com> Date: Thu, 14 Oct 2021 14:02:35 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-11.2 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, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" As the PR observes, if the first operand of a right shift is 0 or -1, operand 2 doesn't matter and the result will be the same as op1, so it can be turned into a copy. This patch checks for that condition and performs the operation in EVRP. Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed. Andrew commit f0b7d4cc49ddb1c2c7474cc3f61e260aa93a96c0 Author: Andrew MacLeod Date: Thu Oct 14 10:43:58 2021 -0400 Simplification for right shift. When the first operand of a signed right shift is zero or negative one, the RHS doesn't matter and the shift can be converted to a copy. PR tree-optimization/102738 gcc/ * vr-values.c (simplify_using_ranges::simplify): Handle RSHIFT_EXPR. gcc/testsuite * gcc.dg/pr102738.c: New. diff --git a/gcc/testsuite/gcc.dg/pr102738.c b/gcc/testsuite/gcc.dg/pr102738.c new file mode 100644 index 00000000000..776fcecb27a --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr102738.c @@ -0,0 +1,48 @@ +/* PR tree-optimization/102738 */ +/* { dg-options "-O2 -fdump-tree-evrp" } */ + +/* Remove arithmetic shift right when the LHS is known to be 0 or -1. */ + +int a1(__int128 f, int g) +{ + /* Leaves f >> 127. */ + return (f >> 127) >> g; +} + +int a2(int f, int g) +{ + /* Leaves f >> 31. */ + return (f >> 31) >> g; +} + +int a3(int f, int g) +{ + if (f == 0 || f == -1) + return f >> g; + __builtin_unreachable(); +} + +int a4(int f, int g) +{ + if (f == 0 || f == 1) + return (-f) >> g; + __builtin_unreachable(); +} + +int a5(int f, int g) +{ + if (f == 0 || f == 1) + return (f-1) >> g; + return 0; +} + +int a6(int f, int g) +{ + if (f == 6 || f == 7) + return (f-7) >> g; + __builtin_unreachable(); +} + +/* { dg-final { scan-tree-dump-times " >> 127" 1 "evrp" } } */ +/* { dg-final { scan-tree-dump-times " >> 31" 1 "evrp" } } */ +/* { dg-final { scan-tree-dump-times " >> " 2 "evrp" } } */ diff --git a/gcc/vr-values.c b/gcc/vr-values.c index 9bf58f416f2..d0f7cb41bc8 100644 --- a/gcc/vr-values.c +++ b/gcc/vr-values.c @@ -4281,6 +4281,28 @@ simplify_using_ranges::simplify (gimple_stmt_iterator *gsi) case MAX_EXPR: return simplify_min_or_max_using_ranges (gsi, stmt); + case RSHIFT_EXPR: + { + tree op0 = gimple_assign_rhs1 (stmt); + tree type = TREE_TYPE (op0); + int_range_max range; + if (TYPE_SIGN (type) == SIGNED + && query->range_of_expr (range, op0, stmt)) + { + unsigned prec = TYPE_PRECISION (TREE_TYPE (op0)); + int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec), + VR_ANTI_RANGE); + range.intersect (nzm1); + // If there are no ranges other than [-1, 0] remove the shift. + if (range.undefined_p ()) + { + gimple_assign_set_rhs_from_tree (gsi, op0); + return true; + } + return false; + } + break; + } default: break; }