From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 66A4338582BE for ; Mon, 8 Aug 2022 11:49:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 66A4338582BE Received: by mail-ej1-x630.google.com with SMTP id k26so16116987ejx.5 for ; Mon, 08 Aug 2022 04:49:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Je1ddVZybb/aUAix4n21HfwF3gaMeiOZq1mAy5a11Vw=; b=M89GOBqRMlmQ7dBr1Exif5dxK/eZCgcNoRw1HibTtyWiTnighOeD+EohMqUtw0ycDO 2RsTv2R/sF02vBl21aQjL2M8v73viO0Juw0goKMYHJGoEi1uSdL64Q8L7FBN9DphKvod yH0n3wkXFvF4jAiWzVUCW7GqnZPj4Nyah9UYkyrCh65WFRL2xI0aga9pJMnAJGudQG+h eRIr7c+jqbQkJRj+GyN5GljztBQfaWZHdPQ2ezi7CJZb8oDlfd3NS9c+fytUhNdb+hC2 D4mMw++N2KhXuykbJ/e5f5Li3pooRE4J0Np/YvyogMgPX/fBMYV7wMN+MSoa9HE3XIW5 0yaw== X-Gm-Message-State: ACgBeo3oz6/Nz/hgqBXLDhiXJhKHmrgHJpkPiXR+eS+NC97CKSlL93oG S+LfxtYKHR/j/tUOgK5k52jyhX4Gz1dBXuCqWZ8= X-Google-Smtp-Source: AA6agR51Eu4l/uyyWL7ZpM173xvw9ZhqvgvRjvrSECJnFxoLioF8te8Ypu9wUyVTdMu7snoWZOGEYVZdUyciZqu+pO8= X-Received: by 2002:a17:907:3e29:b0:730:9d82:5113 with SMTP id hp41-20020a1709073e2900b007309d825113mr14054462ejc.29.1659959373136; Mon, 08 Aug 2022 04:49:33 -0700 (PDT) MIME-Version: 1.0 References: <00ce01d8ab06$19113060$4b339120$@nextmovesoftware.com> In-Reply-To: <00ce01d8ab06$19113060$4b339120$@nextmovesoftware.com> From: Richard Biener Date: Mon, 8 Aug 2022 13:49:21 +0200 Message-ID: Subject: Re: [PATCH] PR tree-optimization/64992: (B << 2) != 0 is B when B is Boolean. To: Roger Sayle Cc: GCC Patches , Andrew Pinski Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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-List-Received-Date: Mon, 08 Aug 2022 11:49:36 -0000 On Mon, Aug 8, 2022 at 11:06 AM Roger Sayle wrote: > > > This patch resolves both PR tree-optimization/64992 and PR > tree-optimization/98956 which are missed optimization enhancement > request, for which Andrew Pinski already has a proposed solution > (related to a fix for PR tree-optimization/98954). Yesterday, > I proposed an alternate improved patch for PR98954, which although > superior in most respects, alas didn't address this case [which > doesn't include a BIT_AND_EXPR], hence this follow-up fix. > > For many functions, F(B), of a (zero-one) Boolean value B, the > expression F(B) != 0 can often be simplified to just B. Hence > "(B * 5) != 0" is B, "-B != 0" is B, "bswap(B) != 0" is B, > "(B >>r 3) != 0" is B. These are all currently optimized by GCC, > with the strange exception of left shifts by a constant (possibly > due to the undefined/implementation defined behaviour when the > shift constant is larger than the first operand's precision). > This patch adds support for this particular case, when the shift > constant is valid. > > This patch has been tested on x86_64-pc-linux-gnu with make bootstrap > and make -k check, both with and without --target_board=unix{-m32}, > with no new failures. Ok for mainline? +/* (X << C) != 0 can be simplified to X, when X is zero_one_valued_p. */ +(simplify + (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) + (if (tree_fits_shwi_p (@1) + && tree_to_shwi (@1) > 0 + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) + (convert @0))) while we deliberately do not fold int << 34 since the result is undefined there is IMHO no reason to not fold the above for any (even non-constant) shift value. We have guards with TYPE_OVERFLOW_SANITIZED in some cases but I think that's not appropriate here, there's one flag_sanitize check, maybe there's a special bit for SHIFT overflow we can use. Why is (X << 0) != 0 excempt in the condition? > > 2022-08-08 Roger Sayle > > gcc/ChangeLog > PR tree-optimization/64992 > PR tree-optimization/98956 > * match.pd (ne (lshift @0 @1) 0): Simplify (X << C) != 0 to X > when X is zero_one_valued_p and the shift constant C is valid. > (eq (lshift @0 @1) 0): Likewise, simplify (X << C) == 0 to !X > when X is zero_one_valued_p and the shift constant C is valid. > > gcc/testsuite/ChangeLog > PR tree-optimization/64992 > * gcc.dg/pr64992.c: New test case. > > > Thanks in advance, > Roger > -- >