Some code paths of cp_fold return early instead of falling through to
the end of the function and so in these cases we fail to cache the
return value of the function into the fold_cache.

This patch splits cp_fold into two functions, with the wrapper function
being responsible for caching the output of the worker function.  Does
this look OK to commit after bootstrap and regtest?

gcc/cp/ChangeLog:

        * cp-gimplify.c (cp_fold_1): Split out from ...
        (cp_fold): ... here.  Cache the output of cp_fold_1 into the
        fold_cache.
---
 gcc/cp/cp-gimplify.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 7b63db4..aaf5f48 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -39,6 +39,7 @@ along with GCC; see the file COPYING3.  If not see
 static tree cp_genericize_r (tree *, int *, void *);
 static tree cp_fold_r (tree *, int *, void *);
 static void cp_genericize_tree (tree*);
+static tree cp_fold_1 (tree);
 static tree cp_fold (tree);
 
 /* Local declarations.  */
@@ -1934,11 +1935,7 @@ clear_fold_cache (void)
 static tree
 cp_fold (tree x)
 {
-  tree op0, op1, op2, op3;
-  tree org_x = x, r = NULL_TREE;
-  enum tree_code code;
-  location_t loc;
-  bool rval_ops = true;
+  tree org_x = x;
 
   if (!x || x == error_mark_node)
     return x;
@@ -1957,6 +1954,27 @@ cp_fold (tree x)
   if (tree *cached = fold_cache->get (x))
     return *cached;
 
+  x = cp_fold_1 (x);
+
+  fold_cache->put (org_x, x);
+  /* Prevent that we try to fold an already folded result again.  */
+  if (x != org_x)
+    fold_cache->put (x, x);
+
+  return x;
+}
+
+/* Worker function for cp_fold.  */
+
+static tree
+cp_fold_1 (tree x)
+{
+  tree op0, op1, op2, op3;
+  tree org_x = x, r = NULL_TREE;
+  enum tree_code code;
+  location_t loc;
+  bool rval_ops = true;
+
   code = TREE_CODE (x);
   switch (code)
     {
@@ -2319,11 +2337,6 @@ cp_fold (tree x)
       return org_x;
     }
 
-  fold_cache->put (org_x, x);
-  /* Prevent that we try to fold an already folded result again.  */
-  if (x != org_x)
-    fold_cache->put (x, x);
-
   return x;
 }
 
-- 
2.9.0.rc0.29.gabd6606

Reply via email to