[PATCH] Fix recip pass not to 'optimize' x/x

Richard Guenther rguenther@suse.de
Thu Jan 17 13:41:00 GMT 2008


Which we were 1) mis-optimizing (replacing both uses of x in the
stmt by the reciprocal) and 2) we'd ICE because the immediate
uses were hosed after the first replacement (we don't generate
SSA_NAMEs for the recip tmps, but rely on the renamer).

Fixed by just skipping stmts like x / x.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to
mainline.

Richard.

2008-01-17  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/34825
	* tree-ssa-math-opts.c (is_division_by): Do not recognize
	x / x as division to handle.

	* gcc.dg/pr34825.c: New testcase.

Index: tree-ssa-math-opts.c
===================================================================
*** tree-ssa-math-opts.c	(revision 131592)
--- tree-ssa-math-opts.c	(working copy)
*************** is_division_by (tree use_stmt, tree def)
*** 275,281 ****
  {
    return TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
  	 && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == RDIV_EXPR
! 	 && TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 1) == def;
  }
  
  /* Walk the subset of the dominator tree rooted at OCC, setting the
--- 275,285 ----
  {
    return TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
  	 && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == RDIV_EXPR
! 	 && TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 1) == def
! 	 /* Do not recognize x / x as valid division, as we are getting
! 	    confused later by replacing all immediate uses x in such
! 	    a stmt.  */
! 	 && TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 0) != def;
  }
  
  /* Walk the subset of the dominator tree rooted at OCC, setting the
Index: testsuite/gcc.dg/pr34825.c
===================================================================
*** testsuite/gcc.dg/pr34825.c	(revision 0)
--- testsuite/gcc.dg/pr34825.c	(revision 0)
***************
*** 0 ****
--- 1,8 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O -funsafe-math-optimizations" } */
+ 
+ double foo(double x, double y)
+ {
+   return x == y ? x/y*x/y : 0;
+ }
+ 



More information about the Gcc-patches mailing list