From patchwork Mon Sep 13 10:42:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 44942 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 8FC9D3857422 for ; Mon, 13 Sep 2021 10:42:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 2F8303858002 for ; Mon, 13 Sep 2021 10:42:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2F8303858002 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=YJzwL3R1/uL/n82wwCsm98y6ER1Z7k4wFRAYpd+SR5A=; b=Yn5HPkCVtiitD3QCBonAMuXUah 9d17OB2unm4yGC02Ay0Zuycs56xxGhLXc3tfD1Ag+ZzNfx+ylmNxPK5LGUqg73rD6M6mudQR6eIF4 NRHJQXXQYSFpfaPB6HZQtpo7AI+HW737URC96Z4CcVdCTLFOoq/7i63iZ5cbxTx2VpJv+MoGMtcsl 5eJ1xTLlmzUfd8DrFILGIE4r9HTS7d+hWtkjn9t9SBFeW8xKxqxp8B+kJaKmYex+jxSYOGa6OPhOY SxetgoKfW0KiBi7RRE9EiwCs0nqI/ZKV01AJS/gqvOeRJ7slhNAMSmsrqccCFmTxN5SdrSplQvueg WpVSn7Mw==; Received: from host86-168-251-41.range86-168.btcentralplus.com ([86.168.251.41]:63410 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mPjPf-0007Br-E9 for gcc-patches@gcc.gnu.org; Mon, 13 Sep 2021 06:42:11 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] PR c/102245: Don't warn that ((_Bool)x<<0) isn't a truthvalue. Date: Mon, 13 Sep 2021 11:42:08 +0100 Message-ID: <020801d7a88c$0133aa90$039affb0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adeoio/jmuVbNhZDSfqrJBjLV5jpuw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" If the tree expression X is a truthvalue, then X << 0 is a truthvalue. In fact, because _Bool (truthvalue_type) has 1 bit precision, and shifts are only well defined for bit counts less than the precision, the only reasonable(?) left shift of a _Bool is by zero [where this reasonable overlooks that shifts by zero should be optimized away as no-ops]. Now consider a language front-end that doesn't fold binary expressions, hence retains (x<<0), but does fold type conversions, and can therefore see that ((_Bool)x<<0) can be shortened to _Bool, but then warns that any LSHIFT_EXPR in a boolean context is suspicious. The answer is that shifts by zero are special, and that all other shifts are indeed suspicious. The most suspicious thing about a (BImode) shift by zero, is why it hasn't already been optimized away. Indeed, in Bernd Edlinger's original 2016 patch submission to warn of LSHIFT_EXPR with -Wint-in-bool-context he included exceptions for shifts (of truthvalues) by zero, https://gcc.gnu.org/pipermail/gcc-patches/2016-September/457716.html but was talked out of this during the review process, and unconditionally warned of all LSHIFT_EXPRs by https://gcc.gnu.org/pipermail/gcc-patches/2016-September/458263.html This patch teaches c_common_truthvalue_conversion that a left shift by zero is special/a no-op, and to apply the conversion to the first operand, which both fixes the bogus warning and generates more sensible expression trees. [Some part of me thinks increasing the amount of folding in the front-ends is bad, but another part thinks that calling fold on trees that haven't had their operands folded/canonicalized (then complaining about suspicious looking but perfectly valid results) is sometimes worse]. This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap" and "make -k check" with no new failures. Ok for mainline? 2021-09-13 Roger Sayle gcc/c-family/ChangeLog PR c/102245 * c-common.c (c_common_truthvalue_conversion) [LSHIFT_EXPR]: Special case (optimize) shifts by zero. gcc/testsuite/ChangeLog PR c/102245 * gcc.dg/Wint-in-bool-context-4.c: New test case. Roger --- /* PR c/102245 */ /* { dg-options "-Wint-in-bool-context" } */ /* { dg-do compile } */ _Bool test1(_Bool x) { return !(x << 0); /* { dg-bogus "boolean context" } */ } _Bool test2(_Bool x) { return !(x << 1); /* { dg-warning "boolean context" } */ } _Bool test3(_Bool x, int y) { return !(x << y); /* { dg-warning "boolean context" } */ } _Bool test4(int x, int y) { return !(x << y); /* { dg-warning "boolean context" } */ } _Bool test5(int x, int y) { return !((x << y) << 0); /* { dg-warning "boolean context" } */ } int test6(_Bool x) { int v = 0; return (v & ~1L) | (1L & (x << 0)); /* { dg-bogus "boolean context" } */ } diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 017e415..44b5fcc 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -3541,6 +3541,10 @@ c_common_truthvalue_conversion (location_t location, tree expr) break; case LSHIFT_EXPR: + /* Treat shifts by zero as a special case. */ + if (integer_zerop (TREE_OPERAND (expr, 1))) + return c_common_truthvalue_conversion (location, + TREE_OPERAND (expr, 0)); /* We will only warn on signed shifts here, because the majority of false positive warnings happen in code where unsigned arithmetic was used in anticipation of a possible overflow.