diff mbox

Go patch committed: Don't create a closure if not needed

Message ID mcrbolcklo4.fsf@dhcp-172-18-216-180.mtv.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor May 25, 2012, 9:14 p.m. UTC
This patch to the Go frontend changes it to not produce a closure for an
embedded function if one is not needed.  Earlier, when Go permitted
comparisons of function types, it was necessary to always create a
closure for an embedded function, so that each embedded function would
be reliably distinct.  Go 1 removed function comparisons, for this and
other reasons.  Now, if an embedded function does not require a closure,
because it does not refer to any variables defined in enclosing
functions, we don't need to create a closure.  Bootstrapped and ran Go
testsuite on x86_64-unknown-linux-gnu.  Committed to mainline and 4.7
branch.

Ian
diff mbox

Patch

diff -r b7d2ae6bea1b go/expressions.cc
--- a/go/expressions.cc	Fri May 25 11:21:08 2012 -0700
+++ b/go/expressions.cc	Fri May 25 14:11:54 2012 -0700
@@ -1312,30 +1312,18 @@ 
 	     && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
   TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
 
-  // For a normal non-nested function call, that is all we have to do.
-  if (!this->function_->is_function()
-      || this->function_->func_value()->enclosing() == NULL)
-    {
-      go_assert(this->closure_ == NULL);
-      return fnaddr;
-    }
-
-  // For a nested function call, we have to always allocate a
-  // trampoline.  If we don't always allocate, then closures will not
-  // be reliably distinct.
-  Expression* closure = this->closure_;
-  tree closure_tree;
-  if (closure == NULL)
-    closure_tree = null_pointer_node;
-  else
-    {
-      // Get the value of the closure.  This will be a pointer to
-      // space allocated on the heap.
-      closure_tree = closure->get_tree(context);
-      if (closure_tree == error_mark_node)
-	return error_mark_node;
-      go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
-    }
+  // If there is no closure, that is all have to do.
+  if (this->closure_ == NULL)
+    return fnaddr;
+
+  go_assert(this->function_->func_value()->enclosing() != NULL);
+
+  // Get the value of the closure.  This will be a pointer to space
+  // allocated on the heap.
+  tree closure_tree = this->closure_->get_tree(context);
+  if (closure_tree == error_mark_node)
+    return error_mark_node;
+  go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
 
   // Now we need to build some code on the heap.  This code will load
   // the static chain pointer with the closure and then jump to the