From patchwork Tue Nov 8 14:24:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 17058 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2739532wru; Tue, 8 Nov 2022 06:25:54 -0800 (PST) X-Google-Smtp-Source: AMsMyM5UcNrRw7tE66Dlaf0H2cwC2/eLdrB26aj0wIV9ZiLWtwAhZEUN8MQP8CFaBDeQ1GlO12C4 X-Received: by 2002:aa7:d889:0:b0:460:62ef:2695 with SMTP id u9-20020aa7d889000000b0046062ef2695mr53876376edq.273.1667917554800; Tue, 08 Nov 2022 06:25:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667917554; cv=none; d=google.com; s=arc-20160816; b=hnjK/LjZpupaRbgEWG67Mk6QickENI9ajI88ER6MEW06Yxk7vJeiTdSxAn8WOZtts0 c1DUDkjNVoQ/2sCFJi82w1gnrvjLwSImKWfhB5ZQL5KCQPbSGlkkXDxk0bPMTO4mJ6M6 64rQJkK8jGnJA0j1rzNxvYxKAWzjYQqiB5CC1tq+JOCuf87xlCuLW+rLfuS+nSsKspZ7 NzBl7+5EMuprm8+jjYIRDDzkToIPo6X10DBdeg1ZHo0T8RovSAOuUqHQrBfJbT/bm7gO W2WKvefgURGmi0XsOdi10WrKlQuo6U26457G7oalQuH3AMXXNed1DHi3ZS0GJcV1QRaH hg9g== 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-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=FWMU3EqK1lFg6Y5us3APY+b3fLJ6tkmcjfo0e0pXHVc=; b=1EraQ6bFGdRzX71w8PA+oxgo8rN0yfBHe8LNVUgtxBQ3Ejwy7Hos919Yer8mJmUgtF oOYplF4aF0hvOzKzaei7r6q369BQil9OmPG3xpUjth7hpUuZSIWRhl24xyMpAzZjfACz QcmWEF61HZ0NsjbD9Wp349y+88Ogho4JJ62eTPKkUsG6AoTimWscvgixELVk2QeHssB4 UovJ71wNgvorOAAttKgVx3t4Afx44Cs3iVV3uoS85B/Bf7Evwh2S0xdbUtIClOsBeJzg 4AHW3HaE5R/NwRBInxOpEKjqGkLI89eN+QIMD5t5P7zahwP/yusQRdJa4bskcHlck8r2 eq7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ut9btFse; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s14-20020a508d0e000000b00462f85ee700si12464130eds.65.2022.11.08.06.25.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 06:25:54 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ut9btFse; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 D5BD8385840A for ; Tue, 8 Nov 2022 14:25:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5BD8385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667917553; bh=FWMU3EqK1lFg6Y5us3APY+b3fLJ6tkmcjfo0e0pXHVc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Ut9btFseC267UUVa4a0iY3rkklcQcLXJUp586hpeS1vZ76KwreGl4cWSmbuDnnRLC xi4XKhrfDP1wJSnGpbd47VisV2oUqKYrcVIf/GQ5LLYk/rtnxgYlDCrxCSVOvcRcDG DHFreAxkwA1FECtbg+QpQeyPTN+kdCYXZOglvnoA= 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 726593858C39 for ; Tue, 8 Nov 2022 14:25:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 726593858C39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-664-Dvi_E1WHNmC0gW_QlQxKOg-1; Tue, 08 Nov 2022 09:25:05 -0500 X-MC-Unique: Dvi_E1WHNmC0gW_QlQxKOg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A1DAF811E7A; Tue, 8 Nov 2022 14:25:05 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.204]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 42D00140EBF3; Tue, 8 Nov 2022 14:25:05 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 2A8EP2L2862727 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 8 Nov 2022 15:25:02 +0100 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 2A8EP2wf862724; Tue, 8 Nov 2022 15:25:02 +0100 To: GCC patches Cc: Richard Biener , Andrew MacLeod , Aldy Hernandez Subject: [PATCH] CCP: handle division by a power of 2 as a right shift. Date: Tue, 8 Nov 2022 15:24:58 +0100 Message-Id: <20221108142458.862678-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 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_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748938317977227649?= X-GMAIL-MSGID: =?utf-8?q?1748938317977227649?= We have some code in range-ops that sets better maybe nonzero bits for TRUNC_DIV_EXPR by a power of 2 than CCP does, by just shifting the mask. I'd like to offload this functionality into the CCP mask tracking code, which already does the right thing for right shifts. The testcase for this change is gcc.dg/tree-ssa/vrp123.c and gcc.dg/tree-ssa/pr107541.c. Tested on x86-64 Linux. OK? gcc/ChangeLog: * range-op.cc (operator_div::fold_range): Call update_known_bitmask. * tree-ssa-ccp.cc (bit_value_binop): Handle divisions by powers of 2 as a right shift. --- gcc/range-op.cc | 18 +----------------- gcc/tree-ssa-ccp.cc | 12 ++++++++++++ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 846931ddcae..8ff5d5b4c78 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -1995,23 +1995,7 @@ operator_div::fold_range (irange &r, tree type, if (!cross_product_operator::fold_range (r, type, lh, rh, trio)) return false; - if (lh.undefined_p ()) - return true; - - tree t; - if (code == TRUNC_DIV_EXPR - && rh.singleton_p (&t) - && !wi::neg_p (lh.lower_bound ())) - { - wide_int wi = wi::to_wide (t); - int shift = wi::exact_log2 (wi); - if (shift != -1) - { - wide_int nz = lh.get_nonzero_bits (); - nz = wi::rshift (nz, shift, TYPE_SIGN (type)); - r.set_nonzero_bits (nz); - } - } + update_known_bitmask (r, code, lh, rh); return true; } diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc index 3a4b6bc1118..2bcd90646f6 100644 --- a/gcc/tree-ssa-ccp.cc +++ b/gcc/tree-ssa-ccp.cc @@ -1934,6 +1934,18 @@ bit_value_binop (enum tree_code code, signop sgn, int width, { widest_int r1max = r1val | r1mask; widest_int r2max = r2val | r2mask; + if (r2mask == 0 && !wi::neg_p (r1max)) + { + widest_int shift = wi::exact_log2 (r2val); + if (shift != -1) + { + // Handle division by a power of 2 as an rshift. + bit_value_binop (RSHIFT_EXPR, sgn, width, val, mask, + r1type_sgn, r1type_precision, r1val, r1mask, + r2type_sgn, r2type_precision, shift, r2mask); + return; + } + } if (sgn == UNSIGNED || (!wi::neg_p (r1max) && !wi::neg_p (r2max))) {