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: 61253 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 7CED3385842E for ; Wed, 30 Nov 2022 09:57:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CED3385842E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669802252; bh=Qyi3uPUdujuARBAOZexqCk6YVUE27003DE9uIZGwu4s=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=A6s7pv91w+X7+lVE0La6ddtdc8hSYnwMeVP8MF+f8RGCeJak1coxvnR6AEuRXAXd/ cTJQh/Fe6i3TBQHwy4XLKRcYEkArwq/N+//sJbVuy4CpKkgP1QMBQQGZJF0rcLjoMN N7QodtL06SqA69T/AJtOrg0KPToZBNzLwkXfG77I= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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); +}