[Committed] PR middle-end/102029: Stricter typing in LSHIFT_EXPR sign folding.
authorRoger Sayle <roger@nextmovesoftware.com>
Tue, 24 Aug 2021 01:59:02 +0000 (02:59 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Tue, 24 Aug 2021 01:59:02 +0000 (02:59 +0100)
My sincere apologies to everyone (again).  As diagnosed by
Jakub Jelinek, my recent patch to fold the signedness of LSHIFT_EXPR
needs to be careful not to attempt transforming a left shift in an
integer type into an invalid left shift of a pointer type.

2021-08-24  Roger Sayle  <roger@nextmovesoftware.com>
    Jakub Jelinek  <jakub@redhat.com>

gcc/ChangeLog
PR middle-end/102029
* match.pd (shift transformations): Add an additional check for
!POINTER_TYPE_P in the recently added left shift transformation.

gcc/testsuite/ChangeLog
PR middle-end/102029
* gcc.dg/fold-convlshift-3.c: New test case.

gcc/match.pd
gcc/testsuite/gcc.dg/fold-convlshift-3.c [new file with mode: 0644]

index 978a1b0..e5bbb12 100644 (file)
@@ -3389,7 +3389,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    the form that minimizes the number of conversions.  */
 (simplify
  (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
- (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
+ (if (INTEGRAL_TYPE_P (type)
+      && !POINTER_TYPE_P (type)
+      && tree_nop_conversion_p (type, TREE_TYPE (@0))
       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
       && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type))
   (lshift (convert @2) @3)))
diff --git a/gcc/testsuite/gcc.dg/fold-convlshift-3.c b/gcc/testsuite/gcc.dg/fold-convlshift-3.c
new file mode 100644 (file)
index 0000000..8d01191
--- /dev/null
@@ -0,0 +1,8 @@
+/* PR middle-end/102029 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+int *
+foo (const __PTRDIFF_TYPE__ l)
+{
+  return (int *) (l << 2);
+}