From patchwork Thu Sep 22 19:05: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: 1393 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp345472wrt; Thu, 22 Sep 2022 12:07:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM535IEL5SoCdefVvbNMctsRanbEDdl/QLw0wVCmOM6nPjSREMIbb7fr/5G9pAWdfwQBBOXd X-Received: by 2002:a17:907:1c0b:b0:782:5250:53ad with SMTP id nc11-20020a1709071c0b00b00782525053admr3607294ejc.50.1663873670643; Thu, 22 Sep 2022 12:07:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663873670; cv=none; d=google.com; s=arc-20160816; b=XEH8cHsEWWaY6LXfCAl8KiMc+R/UjeDCccWQXYuXzQV0wny9NtFjdQ+8Isb8639q6A S5Mz+oqrYgeCw1XHWdK60dkShbS8snWkvl6NP5MFp4Brb13uq9G2tcGPNN705619LxUL iejolHfNmedWqwXw3ZtL/9eYocw/ZNt9JrL1jcj7rXjtV0wDDd9m6Uc1Ov/U45sqSjuY IOitZWdkU4oQDb3trVdvMtN4uP8TcCNSlaOHw7euoLWhrhoCUlq2Ox7qsfVVe6xIJugZ z5PPcwjZAorINX382urDeyrtD8LhFecJnXBC6DMyaUCCDsl8XtvKzw6YVfLOkhcZ1cXo GxpA== 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-language :in-reply-to:references:to:subject:user-agent:mime-version:date :message-id:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=qImNkXHpd61aFKiZ9zVfuX5dNognbcw3t825oM7utwA=; b=dy61auJEy1Z2MW5CWThie/t9RVlZvi7sr/K0bnDrZFzDVZik/0xp/jkfNqRPgDHSKF dzDHtuUkA5rvz6XYm5Jzer78Q4FA6WTCvpBxvdVEPG3IHTblarXpOHea5DCK7YbGyN1n 9CRmJ65CVmzwqnA4u9tuQOviEIRJ2dbRgceQMbU0eUQE8RSF7Z74sLFHjVCos8KjUNr2 GOnTgM8Q5MtAAxTiQBRkq6WgK/Y7je1jIUZN602qGhHio8n81F8J3AjbAIIacrZWJbhR 5UFSXEnXMBiBZnKid31B9gTxbLf7UlB7uI2/rTRaPn+QBcdNqwvLQt4VYC5hQ+1RJd+P tV7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DQmurGI4; 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 gt15-20020a1709072d8f00b00781c3c51284si5970126ejc.288.2022.09.22.12.07.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 12:07:50 -0700 (PDT) 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=DQmurGI4; 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 BD7B7385703C for ; Thu, 22 Sep 2022 19:07:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD7B7385703C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663873643; bh=qImNkXHpd61aFKiZ9zVfuX5dNognbcw3t825oM7utwA=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=DQmurGI4I7QMEMzZxePCK54zufQrbGamHDI+15w0HpbYDJzvfcZ/YZf6NMu0yifRO EIAofpTllyk3TgM06b+L+k+pGPQeOIQ9gwZT2swZYP3gU9tJOPByTSgBKLLg0y4wx+ /8e4P40RDF7pa4mdy2oT5DigHvUkyZq9qGKVGS9s= 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 A1582385702F for ; Thu, 22 Sep 2022 19:05:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A1582385702F Received: from mail-il1-f198.google.com (mail-il1-f198.google.com [209.85.166.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-635-xKf5N_B3N3Ca19tZpWqmZA-1; Thu, 22 Sep 2022 15:05:41 -0400 X-MC-Unique: xKf5N_B3N3Ca19tZpWqmZA-1 Received: by mail-il1-f198.google.com with SMTP id a8-20020a92c548000000b002f6440ff96bso5546242ilj.22 for ; Thu, 22 Sep 2022 12:05:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:references:cc:to:from:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date; bh=fvPMfN3o24v+rP/BXLY4oDtwFUmZ0q4shuyhcSPIm3o=; b=KRqZaA+ktXJ6J5bCrxMmJfmhZaiXxqHIzHgPGEgHhvppSBy1mnxH0lTfPUVLfmy+6U 7y1YdxgmRWvExj7jh+xxQLuPPzHjTFMGshXx6ZAAl7wzCucEOje1Zx2kZDCFqciCZves W9MCFUaXAAThnx1jdh1ngvUlgawHT5Aa8/L/d0eAa/j8bz33sLn2oyw264Dk3RzFJ7I3 faIzx+H9l/HlB1bXMfiRAxQHDT0XqyQW9rOPNiUDJ0naQk4gPEC/lKcr670rQxcYj7a8 AJL3P7EZ977MXuuq/ZSi+0V5+aL8PGsrMOSWEz67PioJSRQK+/PL6uwmRSGcfzOrdG0z jQXA== X-Gm-Message-State: ACrzQf0H5brCNUderuuKdNogJJjNYSWppXfDN/+dPIZdy/UMSvYflVq1 ysGSdyM8kMXT3/55b9Rv8CQT1GBbAkE3AD60EK4EefSNTAbhWttFPxJGc/Cxe0aS3mWAeWs6SpD k/DdxDsakRZ0/s6lvs3yhl36sZ51/8vEejvXhgPclUVQhxxksV5yRXOEYCFPpJMZh2eHkfw== X-Received: by 2002:a05:6638:16d6:b0:358:31d8:65d8 with SMTP id g22-20020a05663816d600b0035831d865d8mr3000617jat.137.1663873538285; Thu, 22 Sep 2022 12:05:38 -0700 (PDT) X-Received: by 2002:a05:6638:16d6:b0:358:31d8:65d8 with SMTP id g22-20020a05663816d600b0035831d865d8mr3000607jat.137.1663873538070; Thu, 22 Sep 2022 12:05:38 -0700 (PDT) Received: from ?IPV6:2607:fea8:a263:f600::3dbe? ([2607:fea8:a263:f600::3dbe]) by smtp.gmail.com with ESMTPSA id b88-20020a0295e1000000b00349c8fe9109sm2507720jai.174.2022.09.22.12.05.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 22 Sep 2022 12:05:37 -0700 (PDT) Message-ID: <622e4a48-eae4-600f-db3c-c478f537caa7@redhat.com> Date: Thu, 22 Sep 2022 15:05:36 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.1 Subject: [PATCH 12/17] Convert CFN_CTZ builtins to range-ops. To: gcc-patches References: <571782f9-72e6-5c30-da55-b8d62d3a153e@redhat.com> In-Reply-To: <571782f9-72e6-5c30-da55-b8d62d3a153e@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-12.7 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 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1744697997741077544?= X-GMAIL-MSGID: =?utf-8?q?1744697997741077544?= Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed. Andrew From 55738d8d96bb4f39a72cf5e3739d35b39fc2146a Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Tue, 20 Sep 2022 18:19:30 -0400 Subject: [PATCH 12/17] Convert CFN_CTZ builtins to range-ops. * gimple-range-fold.cc (range_of_builtin_int_call): Remove case for CFN_CTZ. * gimple-range-op.cc (class cfn_ctz): New. (gimple_range_op_handler::maybe_builtin_call): Set arguments. --- gcc/gimple-range-fold.cc | 61 +------------------------------ gcc/gimple-range-op.cc | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 60 deletions(-) diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc index 63eaa90be96..96a138a7a02 100644 --- a/gcc/gimple-range-fold.cc +++ b/gcc/gimple-range-fold.cc @@ -917,7 +917,7 @@ fold_using_range::range_of_builtin_int_call (irange &r, gcall *call, tree type = gimple_range_type (call); tree arg; - int mini, maxi, zerov = 0, prec; + int prec; scalar_int_mode mode; switch (func) @@ -926,65 +926,6 @@ fold_using_range::range_of_builtin_int_call (irange &r, gcall *call, r.set (build_zero_cst (type), build_one_cst (type)); return true; - CASE_CFN_CTZ: - // __builtin_ctz* return [0, prec-1], except for when the - // argument is 0, but that is undefined behavior. - // - // For __builtin_ctz* consider argument of 0 always undefined - // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO. - arg = gimple_call_arg (call, 0); - prec = TYPE_PRECISION (TREE_TYPE (arg)); - mini = 0; - maxi = prec - 1; - mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg)); - if (gimple_call_internal_p (call)) - { - if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing - && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2) - { - // Handle only the two common values. - if (zerov == -1) - mini = -1; - else if (zerov == prec) - maxi = prec; - else - // Magic value to give up, unless we can prove arg is non-zero. - mini = -2; - } - } - src.get_operand (r, arg); - if (!r.undefined_p ()) - { - // If arg is non-zero, then use [0, prec - 1]. - if (!range_includes_zero_p (&r)) - { - mini = 0; - maxi = prec - 1; - } - // If some high bits are known to be zero, we can decrease - // the maximum. - wide_int max = r.upper_bound (); - if (max == 0) - { - // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO - // is 2 with value -1 or prec, return [-1, -1] or [prec, prec]. - // Otherwise ignore the range. - if (mini == -1) - maxi = -1; - else if (maxi == prec) - mini = prec; - } - // If value at zero is prec and 0 is in the range, we can't lower - // the upper bound. We could create two separate ranges though, - // [0,floor_log2(max)][prec,prec] though. - else if (maxi != prec) - maxi = wi::floor_log2 (max); - } - if (mini == -2) - break; - r.set (build_int_cst (type, mini), build_int_cst (type, maxi)); - return true; - CASE_CFN_CLRSB: arg = gimple_call_arg (call, 0); prec = TYPE_PRECISION (TREE_TYPE (arg)); diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index caba49309f9..801c2bb235e 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -489,6 +489,76 @@ cfn_clz::fold_range (irange &r, tree type, const irange &lh, return true; } +// Implement range operator for CFN_BUILT_IN_CTZ +class cfn_ctz : public range_operator +{ +public: + cfn_ctz (bool internal) { m_gimple_call_internal_p = internal; } + using range_operator::fold_range; + virtual bool fold_range (irange &r, tree type, const irange &lh, + const irange &, relation_kind) const; +private: + bool m_gimple_call_internal_p; +} op_cfn_ctz (false), op_cfn_ctz_internal (true); + +bool +cfn_ctz::fold_range (irange &r, tree type, const irange &lh, + const irange &, relation_kind) const +{ + if (lh.undefined_p ()) + return false; + int prec = TYPE_PRECISION (lh.type ()); + int mini = 0; + int maxi = prec - 1; + int zerov = 0; + scalar_int_mode mode = SCALAR_INT_TYPE_MODE (lh.type ()); + + if (m_gimple_call_internal_p) + { + if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing + && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2) + { + // Handle only the two common values. + if (zerov == -1) + mini = -1; + else if (zerov == prec) + maxi = prec; + else + // Magic value to give up, unless we can prove arg is non-zero. + mini = -2; + } + } + // If arg is non-zero, then use [0, prec - 1]. + if (!range_includes_zero_p (&lh)) + { + mini = 0; + maxi = prec - 1; + } + // If some high bits are known to be zero, we can decrease + // the maximum. + wide_int max = lh.upper_bound (); + if (max == 0) + { + // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO + // is 2 with value -1 or prec, return [-1, -1] or [prec, prec]. + // Otherwise ignore the range. + if (mini == -1) + maxi = -1; + else if (maxi == prec) + mini = prec; + } + // If value at zero is prec and 0 is in the range, we can't lower + // the upper bound. We could create two separate ranges though, + // [0,floor_log2(max)][prec,prec] though. + else if (maxi != prec) + maxi = wi::floor_log2 (max); + + if (mini == -2) + return false; + r.set (build_int_cst (type, mini), build_int_cst (type, maxi)); + return true; +} + // Set up a gimple_range_op_handler for any built in function which can be // supported via range-ops. @@ -553,6 +623,15 @@ gimple_range_op_handler::maybe_builtin_call () m_int = &op_cfn_clz; break; + CASE_CFN_CTZ: + m_op1 = gimple_call_arg (call, 0); + m_valid = true; + if (gimple_call_internal_p (call)) + m_int = &op_cfn_ctz_internal; + else + m_int = &op_cfn_ctz; + break; + default: break; } -- 2.37.3