Apparently, my work on VRP will never finish. There's an infinity of things that can be tweaked ;-).

First, we shouldn't drop to null/non-null when we know what the actual pointer value is. For example, [1, 3] which we get when we store magic numbers in a pointer (p == (char *)1).

BTW, for this bit, I would much rather change range_int_cst_p to allow VR_ANTI_RANGE, instead of inlining as I've done. Is there a reason range_int_cst_p doesn't handle anti ranges?

Also, [&foo, &foo] is known to be non-null.  Don't drop to varying.

OK?
commit 7f42e101d5d26ea866b739692858289a8dff4396
Author: Aldy Hernandez <al...@redhat.com>
Date:   Fri Sep 14 00:11:34 2018 +0200

            * tree-vrp.c (extract_range_from_unary_expr): Handle pointers of
            known quantity.

diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c
index 622ccbc2df7..22e5ee3c729 100644
--- a/gcc/tree-vrp.c
+++ b/gcc/tree-vrp.c
@@ -1842,9 +1842,14 @@ extract_range_from_unary_expr (value_range *vr,
       tree inner_type = op0_type;
       tree outer_type = type;
 
-      /* If the expression evaluates to a pointer, we are only interested in
-	 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]).  */
-      if (POINTER_TYPE_P (type))
+      /* If the expression evaluates to a pointer of unknown quantity,
+	 we are only interested in determining if it evaluates to NULL
+	 [0, 0] or non-NULL (~[0, 0]).  */
+      if (POINTER_TYPE_P (type)
+	  && !((vr0.type == VR_RANGE
+		|| vr0.type == VR_ANTI_RANGE)
+	       && TREE_CODE (vr0.min) == INTEGER_CST
+	       && TREE_CODE (vr0.max) == INTEGER_CST))
 	{
 	  if (!range_includes_zero_p (&vr0))
 	    set_value_range_to_nonnull (vr, type);
@@ -1855,6 +1860,16 @@ extract_range_from_unary_expr (value_range *vr,
 	  return;
 	}
 
+      /* If we have a non-constant range that we know is non-zero (for
+	 example [&foo, &foo] or [&foo, +MAX]), make it known, so we
+	 don't drop to VR_VARYING later.  */
+      if (POINTER_TYPE_P (op0_type)
+	  && vr0.type == VR_RANGE
+	  && (TREE_CODE (vr0.min) != INTEGER_CST
+	      || TREE_CODE (vr0.max) != INTEGER_CST)
+	  && !range_includes_zero_p (&vr0))
+	set_value_range_to_nonnull (&vr0, op0_type);
+
       /* We normalize everything to a VR_RANGE, but for constant
 	 anti-ranges we must handle them by leaving the final result
 	 as an anti range.  This allows us to convert things like

Reply via email to