[PATCH] Fix multiply evaluated arguments of builtin math functions (PR middle-end/18951)

Jakub Jelinek jakub@redhat.com
Mon Dec 13 11:52:00 GMT 2004


Hi!

Although expand_builtin_mathfn etc. call save_expr, it is not using always
the returned tree:

      narg = save_expr (arg);
      if (narg != arg)
        {
          arglist = build_tree_list (NULL_TREE, arg);
          exp = build_function_call_expr (fndecl, arglist);
        }

      op0 = expand_expr (arg, subtarget, VOIDmode, 0);

The (potential) SAVE_EXPR is passed to expand_call in
expand_errno_check or if expand_unop fails expand_call below this, but
op0 = expand_expr uses the original argument, so it will be expanded
multiple times.

On HEAD I can't reproduce this, because with tree-SSA there aren't
side-effects when we reach this routine, still I think it should be fixed
there too.

Ok for HEAD/3.4?

	Jakub
-------------- next part --------------
2004-12-13  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/18951
	* builtins.c (expand_builtin_mathfn, expand_builtin_mathfn_2): Avoid
	using arguments passed to save_expr after that call.

	* gcc.c-torture/execute/20041213-1.c: New test.

--- gcc34/builtins.c.jj	2004-11-29 15:26:47.000000000 +0100
+++ gcc34/builtins.c	2004-12-13 11:47:14.648512396 +0100
@@ -1722,6 +1722,7 @@ expand_builtin_mathfn (tree exp, rtx tar
       narg = save_expr (arg);
       if (narg != arg)
 	{
+	  arg = narg;
 	  arglist = build_tree_list (NULL_TREE, arg);
 	  exp = build_function_call_expr (fndecl, arglist);
 	}
@@ -1854,6 +1855,7 @@ expand_builtin_mathfn_2 (tree exp, rtx t
   narg = save_expr (arg1);
   if (narg != arg1)
     {
+      arg1 = narg;
       temp = build_tree_list (NULL_TREE, narg);
       stable = false;
     }
@@ -1863,6 +1865,7 @@ expand_builtin_mathfn_2 (tree exp, rtx t
   narg = save_expr (arg0);
   if (narg != arg0)
     {
+      arg0 = narg;
       arglist = tree_cons (NULL_TREE, narg, temp);
       stable = false;
     }
--- gcc34/testsuite/gcc.c-torture/execute/20041213-1.c.jj	2004-12-13 11:55:53.576636509 +0100
+++ gcc34/testsuite/gcc.c-torture/execute/20041213-1.c	2004-12-13 11:55:43.773391137 +0100
@@ -0,0 +1,17 @@
+extern double sqrt (double);
+extern void abort (void);
+int once;
+
+double foo (void)
+{
+  if (once++)
+    abort ();
+  return 0.0 / 0.0;
+}
+
+double x;
+int main (void)
+{
+  x = sqrt (foo ());
+  return 0;
+}
-------------- next part --------------
2004-12-13  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/18951
	* builtins.c (expand_builtin_mathfn, expand_builtin_mathfn_2,
	expand_builtin_mathfn_3): Avoid using arguments passed to
	save_expr after that call.

	* gcc.c-torture/execute/20041213-1.c: New test.

--- gcc/builtins.c.jj	2004-12-07 11:29:58.000000000 +0100
+++ gcc/builtins.c	2004-12-13 12:40:46.214821873 +0100
@@ -1782,6 +1782,7 @@ expand_builtin_mathfn (tree exp, rtx tar
       narg = builtin_save_expr (arg);
       if (narg != arg)
 	{
+	  arg = narg;
 	  arglist = build_tree_list (NULL_TREE, arg);
 	  exp = build_function_call_expr (fndecl, arglist);
 	}
@@ -1921,6 +1922,7 @@ expand_builtin_mathfn_2 (tree exp, rtx t
   narg = builtin_save_expr (arg1);
   if (narg != arg1)
     {
+      arg1 = narg;
       temp = build_tree_list (NULL_TREE, narg);
       stable = false;
     }
@@ -1930,6 +1932,7 @@ expand_builtin_mathfn_2 (tree exp, rtx t
   narg = builtin_save_expr (arg0);
   if (narg != arg0)
     {
+      arg0 = narg;
       arglist = tree_cons (NULL_TREE, narg, temp);
       stable = false;
     }
@@ -2040,6 +2043,7 @@ expand_builtin_mathfn_3 (tree exp, rtx t
       narg = save_expr (arg);
       if (narg != arg)
 	{
+	  arg = narg;
 	  arglist = build_tree_list (NULL_TREE, arg);
 	  exp = build_function_call_expr (fndecl, arglist);
 	}
--- gcc/testsuite/gcc.c-torture/execute/20041213-1.c.jj	2004-12-13 12:39:45.903611207 +0100
+++ gcc/testsuite/gcc.c-torture/execute/20041213-1.c	2004-12-13 12:39:45.903611207 +0100
@@ -0,0 +1,17 @@
+extern double sqrt (double);
+extern void abort (void);
+int once;
+
+double foo (void)
+{
+  if (once++)
+    abort ();
+  return 0.0 / 0.0;
+}
+
+double x;
+int main (void)
+{
+  x = sqrt (foo ());
+  return 0;
+}


More information about the Gcc-patches mailing list