diff mbox series

[PR,tree-optimization/97750] Fix trap in pointer conversion in op1_range.

Message ID 69ba5674-4d3d-e84a-4de5-dbb100a419cb@redhat.com
State New
Headers show
Series [PR,tree-optimization/97750] Fix trap in pointer conversion in op1_range. | expand

Commit Message

Andrew MacLeod Dec. 17, 2020, 2:39 p.m. UTC
operator_cast::op1_range() does some masking and calculations when a 
higher precision value is cast to a lower precision one.
ie,  it fills in the ranges for the upper bits and trys to produce more 
usable results for the wind-back process.  Part of that calculation 
involved using PLUS_EXPR to the current range...  for which 
POINTER_TYPE_P does not have any support (It uses POINTER_PLUS which has 
difference semantics)./  Thus the trap.

in this particular case
     CopyFromUswc_src.2_1 = CopyFromUswc_src;
     CopyFromUswc_src.3_2 = (long int) CopyFromUswc_src.2_1;
     unaligned_12 = (unsigned int) CopyFromUswc_src.3_2;
     if (unaligned_12 != 0)
       goto <bb 4>; [INV]
     else
       goto <bb 5>; [INV]

3->4  (T) CopyFromUswc_src.2_1 :        char * [1B, +INF]
3->4  (T) CopyFromUswc_src.3_2 :        long int [-INF, 
-4294967297][-4294967295, -1][1, +INF]
3->4  (T) unaligned_12 :        unsigned int [1, +INF]
3->5  (F) CopyFromUswc_src.3_2 :        long int [-INF, -4294967296][0, 
0][4294967296, +INF]
3->5  (F) unaligned_12 :        unsigned int [0, 0]

THis is what we produce now.
before this patch, we were not doing anything different for pointers, so 
when processing:

CopyFromUswc_src.2_1 = CopyFromUswc_src;
we were calculating the RHS as a char * from
  long int [-INF, -4294967297][-4294967295, -1][1, +INF]

which is a lot of work for very little, since we really only care about 
zero, non-zero, and varying.  We do occasionally track a constant 
value.... and I'm leaving in that capability since it allows us to treat 
0/NULL as just another constant, but thats all we really need.

Bootstrapped on x86_64-pc-linux-gnu, no regressions.  Pushed.
diff mbox series

Patch

commit c25b504636fec7bf8f181a84af83a52757ba7e89
Author: Andrew MacLeod <amacleod@redhat.com>
Date:   Thu Dec 17 09:24:11 2020 -0500

    Fix trap in pointer conversion in op1_range.
    
    Processing op1_range for conversion between a non-pointer and pointer
    shouldnt do any fancy math.
    
            gcc/
            PR tree-optimization/97750
            * range-op.cc (operator_cast::op1_range): Handle pointers better.
            gcc/testsuite/
            * gcc.dg/pr97750.c: New.

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 36f9fd66cb3..a473f33169d 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -1850,6 +1850,31 @@  operator_cast::op1_range (irange &r, tree type,
   tree lhs_type = lhs.type ();
   gcc_checking_assert (types_compatible_p (op2.type(), type));
 
+  // If we are calculating a pointer, shortcut to what we really care about.
+  if (POINTER_TYPE_P (type))
+    {
+      // Conversion from other pointers or a constant (including 0/NULL)
+      // are straightforward.
+      if (POINTER_TYPE_P (lhs.type ())
+	  || (lhs.singleton_p ()
+	      && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
+	{
+	  r = lhs;
+	  range_cast (r, type);
+	}
+      else
+	{
+	  // If the LHS is not a pointer nor a singleton, then it is
+	  // either VARYING or non-zero.
+	  if (!lhs.contains_p (build_zero_cst (lhs.type ())))
+	    r.set_nonzero (type);
+	  else
+	    r.set_varying (type);
+	}
+      r.intersect (op2);
+      return true;
+    }
+
   if (truncating_cast_p (op2, lhs))
     {
       if (lhs.varying_p ())
diff --git a/gcc/testsuite/gcc.dg/pr97750.c b/gcc/testsuite/gcc.dg/pr97750.c
new file mode 100644
index 00000000000..822b53abcff
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr97750.c
@@ -0,0 +1,21 @@ 
+/* PR tree-optimization/97750 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wall -Wextra" } */
+
+char CopyPlane_src;
+long CopyPlane_copy_pitch;
+char *CopyFromUswc_src;
+int CopyFromUswc_height;
+void CopyPlane(char *dst) {
+  __builtin_memcpy(dst, &CopyPlane_src, CopyPlane_copy_pitch);
+}
+void CopyFromUswc(long src_pitch) {
+  char *dst;
+  for (; CopyFromUswc_height;) {
+    unsigned unaligned = (long)CopyFromUswc_src;
+    if (unaligned)
+      CopyPlane(&dst[unaligned]);  /* { dg-warning "may be used uninitialized" } */
+    CopyFromUswc_src += src_pitch;
+  }
+}
+