From patchwork Thu Jan 13 13:57:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 49965 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 671C43951C12 for ; Thu, 13 Jan 2022 13:58:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 671C43951C12 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642082289; bh=ZiWaAwGL0aEq8zI4HvRCwpkR1Ws1Ptdnho/+202AIww=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=uAd1RjrFRVnzH9tPaJUL2KllvsJNouLRa13EErlkrvFyg8TJtBXtaURCb2M5OBMzh CDkEbAtrYhiDZsuAXqCbKEZVqignpIoCgwvOyeUX3bm+gJet4HdSuL4FiWw3VplB5S tJVtyoM+AWtPw/QfQwP4VRZ6EEO7R0a8WjbW7aKU= 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 ESMTPS id ECE8139518AE for ; Thu, 13 Jan 2022 13:57:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ECE8139518AE 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-562-8t9_SA73P_Wr5bpCHmI3mg-1; Thu, 13 Jan 2022 08:57:39 -0500 X-MC-Unique: 8t9_SA73P_Wr5bpCHmI3mg-1 Received: by mail-qt1-f198.google.com with SMTP id d26-20020ac800da000000b002c43d2f6c7fso4969367qtg.14 for ; Thu, 13 Jan 2022 05:57:39 -0800 (PST) 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=GoBmeWpaMiZOTqgzpRdUubLmsd6NxGtnD5JgVT7Q1wI=; b=xkBMtOkMFanewgN63AxFTuIv68anHIRhkoe2hh0XXWciM6NEqa8DcrwtVaMcenTlgB sohyc1b/gyp3oI3lNqLuf78HoWXsgHAdvc7G+WEMuVSTXsVEbefpdJGyQvtIOduxeIjQ yMaXQAsmVnslgBrxd9PdwNW9DzgxMTCMiN/sQ2LLA9bFV1R+Ii/TkKgq6fihuKRgMosa 1stQcsXW2J7v3ZcJNivTgpjz8C9dTFS709C+x0+I02i7P8DzuFz6cH85QrZEugX9fTr0 xj//BvL+XHwZk/PKi9huu3zblbIMnIKkf7CyheN6LiRbMOv/W6nGXcrT0D1keHnX8CYs PBnA== X-Gm-Message-State: AOAM533FXv0jsP397l5xQzy6NvRdlCnBa+fn4s1Vqpvq/knkctUigPur ZUR7dmTRdyIGUGSA8bs8iSLIL25iAEN/Dumx4hZ3almQEK91eniO9f1BqwaVnyLRa2u3C4o6FpJ pVpEHs3rqwyuSLlwVZRFyX86V8oH3evi4g7qlg6Fm6wvK11XuxsYEgqkDAqkj6SQMsmMqYw== X-Received: by 2002:a05:622a:134e:: with SMTP id w14mr3467835qtk.394.1642082258756; Thu, 13 Jan 2022 05:57:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJy/0ZfeHYQr/HW2umLHJJUft3/lOakZPOeZe43xAaK99SAgEnhvzLk5f9dYSsc449XWal0jWA== X-Received: by 2002:a05:622a:134e:: with SMTP id w14mr3467821qtk.394.1642082258498; Thu, 13 Jan 2022 05:57:38 -0800 (PST) Received: from ?IPV6:2607:fea8:a262:5f00:e36d:32da:80f:d4e1? ([2607:fea8:a262:5f00:e36d:32da:80f:d4e1]) by smtp.gmail.com with ESMTPSA id s6sm1843717qki.23.2022.01.13.05.57.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Jan 2022 05:57:37 -0800 (PST) Message-ID: Date: Thu, 13 Jan 2022 08:57:36 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 To: gcc-patches Subject: [PATCH] tree-optimization/96707 - Add relation to unsigned right shift. X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-12.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_H3, RCVD_IN_MSPIKE_WL, 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" A quick addition to range ops for LHS = OP1 >> OP2 if OP1 and OP2 are both >= 0,   then we can register the relation  LHS <= OP1   and all the expected good things happen. Bootstrapped on x86_64-pc-linux-gnu with no regressions. OK for trunk? Andrew From c34dab537d6f54b66b430f5980cde278fa033904 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Wed, 12 Jan 2022 13:28:55 -0500 Subject: [PATCH 1/2] Add relation to unsigned right shift. If the first operand and the shift value of a right shift operation are both >= 0, then we know the LHS of the operation is <= the first operand. PR tree-optimization/96707 gcc/ * range-op.c (operator_rshift::lhs_op1_relation): New. gcc/testtsuite/ * g++.dg/pr96707.C: New. --- gcc/range-op.cc | 16 ++++++++++++++++ gcc/testsuite/g++.dg/pr96707.C | 10 ++++++++++ 2 files changed, 26 insertions(+) create mode 100644 gcc/testsuite/g++.dg/pr96707.C diff --git a/gcc/range-op.cc b/gcc/range-op.cc index a4f6e9eba29..19bdf30911a 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -1941,9 +1941,25 @@ public: const irange &lhs, const irange &op2, relation_kind rel = VREL_NONE) const; + virtual enum tree_code lhs_op1_relation (const irange &lhs, + const irange &op1, + const irange &op2) const; } op_rshift; +enum tree_code +operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED, + const irange &op1, + const irange &op2) const +{ + // If both operands range are >= 0, then the LHS <= op1. + if (!op1.undefined_p () && !op2.undefined_p () + && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ())) + && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ()))) + return LE_EXPR; + return VREL_NONE; +} + bool operator_lshift::fold_range (irange &r, tree type, const irange &op1, diff --git a/gcc/testsuite/g++.dg/pr96707.C b/gcc/testsuite/g++.dg/pr96707.C new file mode 100644 index 00000000000..2653fe3d043 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr96707.C @@ -0,0 +1,10 @@ +/* { dg-do compile} */ +/* { dg-options "-O2 -fdump-tree-evrp" } */ + +bool f(unsigned x, unsigned y) +{ + return (x >> y) <= x; +} + +/* { dg-final { scan-tree-dump "return 1" "evrp" } } */ + -- 2.17.2