From patchwork Thu Oct 5 20:34:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 822041 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-463574-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="kILk8G9p"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3y7Pdl3jWMz9t48 for ; Fri, 6 Oct 2017 07:34:50 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=OSgeb5xw8DokPngpnEX9hYT2VPHwp DYVw0TfC8llNTghQv9bYGOGM5RDdIo8nXhiNOaOXtlUq0wyxPg/DrqTGiZBNHtTM f0z95K90xm4kE01EtuAoOa3GNkkxKQXbtlJ+W+4+bxN1pk0iODbUUflY2AkZnKiG 5xDXv3ASInv7hE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=ecceUTdwfXtnGElhlX8gSrQHBLk=; b=kIL k8G9p1j7xSxZcIXNR+7ZInYrnct5LI4eAT1CypZ3uYL2X6iHHGaYA6wv/RoUPKEm 52nvzfxMF/yAKoHnwMLsNeLfSYFxHjdDYtkiZ0PI0SdXTgFVrH+/XtmjJVm6cboL VRVGHhJJBfEbredI6aPDdlbRzRXOlacFlhrzeq7A= Received: (qmail 67041 invoked by alias); 5 Oct 2017 20:34:42 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 66975 invoked by uid 89); 5 Oct 2017 20:34:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=warned X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 05 Oct 2017 20:34:40 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3EDB772D73; Thu, 5 Oct 2017 20:34:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3EDB772D73 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jakub@redhat.com Received: from tucnak.zalov.cz (ovpn-116-223.ams2.redhat.com [10.36.116.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2FDEA600C0; Thu, 5 Oct 2017 20:34:35 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id v95KYWIB007174; Thu, 5 Oct 2017 22:34:33 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id v95KYQoA007173; Thu, 5 Oct 2017 22:34:26 +0200 Date: Thu, 5 Oct 2017 22:34:26 +0200 From: Jakub Jelinek To: "Joseph S. Myers" , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [C PATCH] Fix -Wtautological-compare (PR c/82437) Message-ID: <20171005203426.GJ18588@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes Hi! In warn_tautological_bitwise_comparison, there is even a comment mentioning the fact that the types of the two constants might not be the same (it is called with the original arguments before they are promoted to common type for the comparison). On the following testcase, one of the constants was unsigned (because it has been converted to that when anded with unsigned variable), while the other was signed and wi::to_widest (bitopcst) & wi::to_widest (cst) wasn't sign-extended from 32-bit (because bitopcst was unsigned), while wi::to_widest (cst) was (because cst was int), so we warned even when we shouldn't. The following patch instead uses the precision of the larger type and zero extends from that (because we really don't need to care about bits beyond that precision). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2017-10-05 Jakub Jelinek PR c/82437 * c-warn.c (warn_tautological_bitwise_comparison): Instead of using to_widest use wide_int with the larger of the two precisions. * c-c++-common/Wtautological-compare-6.c: New test. Jakub --- gcc/c-family/c-warn.c.jj 2017-10-02 13:46:57.000000000 +0200 +++ gcc/c-family/c-warn.c 2017-10-05 16:18:20.050207720 +0200 @@ -356,16 +356,24 @@ warn_tautological_bitwise_comparison (lo return; /* Note that the two operands are from before the usual integer - conversions, so their types might not be the same. */ - widest_int res; + conversions, so their types might not be the same. + Use the larger of the two precisions and ignore bits outside + of that. */ + int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)), + TYPE_PRECISION (TREE_TYPE (bitopcst))); + + wide_int bitopcstw = wide_int::from (bitopcst, prec, UNSIGNED); + wide_int cstw = wide_int::from (cst, prec, UNSIGNED); + + wide_int res; if (TREE_CODE (bitop) == BIT_AND_EXPR) - res = wi::to_widest (bitopcst) & wi::to_widest (cst); + res = bitopcstw & cstw; else - res = wi::to_widest (bitopcst) | wi::to_widest (cst); + res = bitopcstw | cstw; /* For BIT_AND only warn if (CST2 & CST1) != CST1, and for BIT_OR only if (CST2 | CST1) != CST1. */ - if (res == wi::to_widest (cst)) + if (res == cstw) return; if (code == EQ_EXPR) --- gcc/testsuite/c-c++-common/Wtautological-compare-6.c.jj 2017-10-05 16:21:41.799800708 +0200 +++ gcc/testsuite/c-c++-common/Wtautological-compare-6.c 2017-10-05 16:23:36.727429542 +0200 @@ -0,0 +1,11 @@ +/* PR c/82437 */ +/* { dg-do compile { target int32plus } } */ +/* { dg-options "-Wtautological-compare" } */ + +int +foo (unsigned int x) +{ + if ((x & -1879048192) != -1879048192) /* { dg-bogus "bitwise comparison always evaluates to" } */ + return 0; + return 1; +}