From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669802223; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=Qyi3uPUdujuARBAOZexqCk6YVUE27003DE9uIZGwu4s=; b=Fvru3ex2EPqfRkZvWrMLRF9XZfINKb6B5RUTJKt/iY2ZMEWL/z2TBKu0ypZThNA7wrb7+2 UWwRJ/0E/cnwQsSDZeT8nZu/wZCl4/Y2LbXi+0B0cDQ9zIfS7iqEPQletbPZwVlKf9ssTX 06Qn7NfvbunGSZ+pOkRbkTjvjjBB3fM= 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 From: Jakub Jelinek 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: Reply-To: Jakub Jelinek 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-Type: text/plain; charset=us-ascii 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 List-Id: 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. --- 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); +} Jakub