public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>,
	"Joseph S. Myers" <joseph@codesourcery.com>,
	Marek Polacek <polacek@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] c-family: Account for integral promotions of left shifts for -Wshift-overflow warning [PR107846]
Date: Wed, 30 Nov 2022 10:56:54 +0100	[thread overview]
Message-ID: <Y4co5oszzPoXjjkU@tucnak> (raw)

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  <jakub@redhat.com>

	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


                 reply	other threads:[~2022-11-30  9:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y4co5oszzPoXjjkU@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).