[PATCH] Fix PR33146, ICE in build_polynomial_chrec; NEGATE_EXPR of pointers

Richard Guenther rguenther@suse.de
Fri Sep 21 13:30:00 GMT 2007


This PR runs into an issue on whether we should allow (and thus deal with)
NEGATE_EXPRs of pointers.  SCEV folds (unsigned long)ptr * -1UL and
fold_binary transforms this to (unsigned long) -ptr, which further down
the path confuses SCEV.

I believe we shouldn't create NEGATE_EXPRs on pointers (but adding an
assert in negate_expr to force this falls over).   The problem is that
STRIP_SIGN_NOPs happily strips conversions to/from pointer types (as
they are TYPE_UNSIGNED).  Of course pointers do not really have signs,
so it makes sense to fix STRIP_SIGN_NOPs to preserve these changes in
type kind.

Now, the folding of A * -1 uses completely stripped operand for negation,
which we also need to fix here.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

Does this look like a sane approach?  (Of course I believe yes ;))

Thanks,
Richard.

2007-09-21  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/33146
	* fold-const.c (fold_binary): Use the original tree
	for negating.
	* tree.h (STRIP_SIGN_NOPS): Converting from or to pointer
	also changes "sign".

	* gcc.c-torture/compile/pr33146.c: New testcase.

Index: fold-const.c
===================================================================
--- fold-const.c	(revision 128644)
+++ fold-const.c	(working copy)
@@ -10242,7 +10242,7 @@ fold_binary (enum tree_code code, tree t
 	    return non_lvalue (fold_convert (type, arg0));
 	  /* Transform x * -1 into -x.  */
 	  if (integer_all_onesp (arg1))
-	    return fold_convert (type, negate_expr (arg0));
+	    return fold_convert (type, negate_expr (op0));
 	  /* Transform x * -C into -x * C if x is easily negatable.  */
 	  if (TREE_CODE (arg1) == INTEGER_CST
 	      && tree_int_cst_sgn (arg1) == -1
Index: tree.h
===================================================================
--- tree.h	(revision 128644)
+++ tree.h	(working copy)
@@ -1019,7 +1019,9 @@ extern void omp_clause_range_check_faile
 	 && (TYPE_MODE (TREE_TYPE (EXP))			\
 	     == TYPE_MODE (TREE_TYPE (TREE_OPERAND (EXP, 0))))	\
 	 && (TYPE_UNSIGNED (TREE_TYPE (EXP))			\
-	     == TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (EXP, 0))))) \
+	     == TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (EXP, 0)))) \
+	 && (POINTER_TYPE_P (TREE_TYPE (EXP))			\
+	     == POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (EXP, 0))))) \
     (EXP) = TREE_OPERAND (EXP, 0)
 
 /* Like STRIP_NOPS, but don't alter the TREE_TYPE either.  */
Index: testsuite/gcc.c-torture/compile/pr33146.c
===================================================================
--- testsuite/gcc.c-torture/compile/pr33146.c	(revision 0)
+++ testsuite/gcc.c-torture/compile/pr33146.c	(revision 0)
@@ -0,0 +1,19 @@
+typedef struct
+{
+  int end;
+  int term;
+}
+jpc_enc_pass_t;
+void foo(int numpasses, jpc_enc_pass_t *p)
+{
+  jpc_enc_pass_t *pass;
+  jpc_enc_pass_t *termpass;
+  for (pass = p; pass != termpass; ++pass)
+    if (!pass->term)
+    {
+      termpass = pass;
+      while (termpass - pass < numpasses && !termpass->term)
+        ++termpass;
+      pass->end = termpass->end;
+    }
+}



More information about the Gcc-patches mailing list