From patchwork Wed Nov 30 09:56:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 27708 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp829728wrr; Wed, 30 Nov 2022 01:57:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf5zns6zsqCiS/m/wGNQXBrNr+Xf0+hx8iRpWPFGtSRt1UdlcODIQdtMItIbGQziomxVGAxg X-Received: by 2002:a05:6402:3c1:b0:46b:2d74:d970 with SMTP id t1-20020a05640203c100b0046b2d74d970mr12682748edw.138.1669802267952; Wed, 30 Nov 2022 01:57:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669802267; cv=none; d=google.com; s=arc-20160816; b=cfbCuWAO3jlH7YQNGVR9x5d8bPTpFyjfjVGkShlZ8n9ez/CGinXZSotzzHzI8KXB4A 9STNip2Fb6a/U3X2t2rVWINpgeQpGK7vR2THlmNw+6ItvpVkyoHlStk2swckFy22pODr 68zRyS3MPOoprCnpBcd/jSofMmOfjw5fTijiSpd2b60XYzFkwe8qM0+HQSFT0TmrxicB rAlEB/0aiDggTGpQzNLHE+apCr5tz0I3NVeyRFD97J+vsHoDtmX9njWjX9viYLIK1ZIY 7ZsXaPyI0rzL81ChHAKVYOQuz8MwYoXerdya6ipkpfXx+vzp/0jRT5ztALfCf+Jyl6Jz Xicw== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Qyi3uPUdujuARBAOZexqCk6YVUE27003DE9uIZGwu4s=; b=L90V1L9xgt3sFjinRCArleGVlUdMU6oDNkO2lOItpDDMh6zyX96CC9GgfI+j8j+hXS wEzq5LSFYsM1WaKCHZl3SqcPSXvKoz9Kswg2xNS3ME/i1x/UaOh5IfJpsl4TCJ/pus4q GIzneoS2zEd8g6TDMBegbsiAfVL7xaSMGGCQ/Z0I7l6F1AuRBBCOgKcExFC46+KMtKXl x1rlhe1CT1PvU47JmxmV3rbuUiXtfugI3CemiwFbEznb6fjS6+1G7EifhFqEZgOlWoF+ ILdCnhvXv9Jv5IoAt9AJRoCpnwRrB5ERd62ZY4Eqa6vLOkTiwRXFknREWxAX3KPtuR7b /hYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qSEmSh6J; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ho44-20020a1709070eac00b007a1084820f6si1089170ejc.230.2022.11.30.01.57.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Nov 2022 01:57:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qSEmSh6J; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 0A0A33858D1E for ; Wed, 30 Nov 2022 09:57:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A0A33858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669802267; bh=Qyi3uPUdujuARBAOZexqCk6YVUE27003DE9uIZGwu4s=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=qSEmSh6J2J/86srMH1kVAfgPOLOxuP2/EmV2KPnjStDPB9rGeASlQ2ljlogqSCmGK 05QN528MGD5kDY988F8HwIDmQNM8tq3VfQHzgYhvE+/7shz1GiEnZXvHGwdATfZQ2s c597jQrRHdFR7ZjUJPUCcOkcZPpUdyFC6vRZF3jU= 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 9294F3858D28 for ; Wed, 30 Nov 2022 09:57:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9294F3858D28 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-390-KKMW5Px0MxSzjokXvtuv1A-1; Wed, 30 Nov 2022 04:57:00 -0500 X-MC-Unique: KKMW5Px0MxSzjokXvtuv1A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5427129AB3F1; Wed, 30 Nov 2022 09:57:00 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E59FE2024CBE; Wed, 30 Nov 2022 09:56:59 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2AU9utMt1863058 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 30 Nov 2022 10:56:55 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AU9us411863057; Wed, 30 Nov 2022 10:56:54 +0100 Date: Wed, 30 Nov 2022 10:56:54 +0100 To: Jason Merrill , "Joseph S. Myers" , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c-family: Account for integral promotions of left shifts for -Wshift-overflow warning [PR107846] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1750914582777536294?= X-GMAIL-MSGID: =?utf-8?q?1750914582777536294?= Hi! The r13-1100-gacb1e6f43dc2bbedd124 change added match.pd narrowing of left shifts, and while I believe the C++ FE calls the warning on unfolded trees, the C FE folds them and so left shifts where integral promotion happened and so were done in int type will be usually narrowed back to char/signed char/unsigned char/short/unsigned short left shifts if the shift count is constant and fits into the precision of the var being shifted. One possibility would be to restrict the match.pd optimization to GIMPLE only, another don't fold in C FE before this warning (well, we need to fold the shift count operand to constant if possible), the following patch just takes integral promotion into account in the warning code. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk, or do you prefer some other way to resolve this? 2022-11-30 Jakub Jelinek PR c/107846 * c-warn.cc: Include langhooks.h. (maybe_warn_shift_overflow): Set type0 to what TREE_TYPE (op0) promotes to rather than TREE_TYPE (op0) itself, if TREE_TYPE (op0) is narrower than type0 and unsigned, use wi::min_precision with UNSIGNED and fold_convert op0 to type0 before emitting the warning. * gcc.dg/pr107846.c: New test. Jakub --- gcc/c-family/c-warn.cc.jj 2022-11-23 11:50:06.000000000 +0100 +++ gcc/c-family/c-warn.cc 2022-11-29 16:13:15.140713040 +0100 @@ -39,6 +39,7 @@ along with GCC; see the file COPYING3. #include "calls.h" #include "stor-layout.h" #include "tree-pretty-print.h" +#include "langhooks.h" /* Print a warning if a constant expression had overflow in folding. Invoke this function on every expression that the language @@ -2615,14 +2616,19 @@ maybe_warn_shift_overflow (location_t lo || TREE_CODE (op1) != INTEGER_CST) return false; - tree type0 = TREE_TYPE (op0); + /* match.pd could have narrowed the left shift already, + take type promotion into account. */ + tree type0 = lang_hooks.types.type_promotes_to (TREE_TYPE (op0)); unsigned int prec0 = TYPE_PRECISION (type0); /* Left-hand operand must be signed. */ if (TYPE_OVERFLOW_WRAPS (type0) || cxx_dialect >= cxx20) return false; - unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), SIGNED) + signop sign = SIGNED; + if (TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (type0)) + sign = TYPE_SIGN (TREE_TYPE (op0)); + unsigned int min_prec = (wi::min_precision (wi::to_wide (op0), sign) + TREE_INT_CST_LOW (op1)); /* Handle the case of left-shifting 1 into the sign bit. * However, shifting 1 _out_ of the sign bit, as in @@ -2645,7 +2651,8 @@ maybe_warn_shift_overflow (location_t lo warning_at (loc, OPT_Wshift_overflow_, "result of %qE requires %u bits to represent, " "but %qT only has %u bits", - build2_loc (loc, LSHIFT_EXPR, type0, op0, op1), + build2_loc (loc, LSHIFT_EXPR, type0, + fold_convert (type0, op0), op1), min_prec, type0, prec0); return overflowed; --- gcc/testsuite/gcc.dg/pr107846.c.jj 2022-11-29 16:18:34.427033919 +0100 +++ gcc/testsuite/gcc.dg/pr107846.c 2022-11-29 16:16:32.464821272 +0100 @@ -0,0 +1,14 @@ +/* PR c/107846 */ +/* { dg-do compile } */ +/* { dg-options "-Wall" } */ + +#define foo(x, b, n, m) ((unsigned short) (x) << (b - (n + 1) * 8) >> (b - 8) << (m * 8)) +#define bar(x) ((unsigned short) (foo (x, 16, 0, 1) | foo (x, 16, 1, 0))) +#define baz(x) bar (x) +static const int v = 8000; + +unsigned short +qux (int t) +{ + return t != baz (v); +}