Reviewers: Yang,

Description:
Fix %OptimizeFunctionOnNextCall to actually work when the function has not yet
been compiled.

[email protected]
BUG=

Please review this at https://codereview.chromium.org/543643002/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+26, -33 lines):
  M src/compiler.cc
  M src/objects.cc
  M src/objects-inl.h
  M src/runtime.cc
  A + test/mjsunit/compiler/opt-next-call.js


Index: src/compiler.cc
diff --git a/src/compiler.cc b/src/compiler.cc
index f628938fa22851274b6b4bfa5d94416d036d4d0b..30a608633d998a57a2514c07698fce7bccf4e4d5 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -1246,7 +1246,13 @@ MaybeHandle<Code> Compiler::GetOptimizedCode(Handle<JSFunction> function,
   PostponeInterruptsScope postpone(isolate);

   Handle<SharedFunctionInfo> shared = info->shared_info();
-  DCHECK_NE(ScopeInfo::Empty(isolate), shared->scope_info());
+  if (ScopeInfo::Empty(isolate) == shared->scope_info()) {
+    // The function was never compiled. Compile it unoptimized first.
+    CompilationInfoWithZone nested(function);
+    if (!GetUnoptimizedCodeCommon(&nested).ToHandle(&current_code)) {
+      return MaybeHandle<Code>();
+    }
+  }
   int compiled_size = shared->end_position() - shared->start_position();
   isolate->counters()->total_compile_size()->Increment(compiled_size);
   current_code->set_profiler_ticks(0);
Index: src/objects-inl.h
diff --git a/src/objects-inl.h b/src/objects-inl.h
index c49bfc8b9e74a11346820590dc2a4fa0ecfa7384..2123fa6e128154631a9d83a407420fcea173d336 100644
--- a/src/objects-inl.h
+++ b/src/objects-inl.h
@@ -4777,9 +4777,10 @@ int Code::profiler_ticks() {


 void Code::set_profiler_ticks(int ticks) {
-  DCHECK_EQ(FUNCTION, kind());
   DCHECK(ticks < 256);
-  WRITE_BYTE_FIELD(this, kProfilerTicksOffset, ticks);
+  if (kind() == FUNCTION) {
+    WRITE_BYTE_FIELD(this, kProfilerTicksOffset, ticks);
+  }
 }


Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index d2943f9f6dfed04b0b1b5e6ca86bdd56d4273f5a..d1ecbd49c1cd25827af53cd142dcd9a932d8f925 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -9140,7 +9140,6 @@ void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) {


 void JSFunction::MarkForOptimization() {
-  DCHECK(is_compiled() || GetIsolate()->DebuggerHasBreakPoints());
   DCHECK(!IsOptimized());
   DCHECK(shared()->allows_lazy_compilation() ||
          code()->optimizable());
Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index e6277060732e9d71feb9d592af657a3dff31e406..dc1e2a9a46ce77103cbaaaefeab66192886712df 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -8457,15 +8457,9 @@ RUNTIME_FUNCTION(Runtime_CompileOptimized) {
   CONVERT_BOOLEAN_ARG_CHECKED(concurrent, 1);

   Handle<Code> unoptimized(function->shared()->code());
-  if (!function->shared()->is_compiled()) {
-    // If the function is not compiled, do not optimize.
-    // This can happen if the debugger is activated and
-    // the function is returned to the not compiled state.
-    // TODO(yangguo): reconsider this.
-    function->ReplaceCode(function->shared()->code());
-  } else if (!isolate->use_crankshaft() ||
-             function->shared()->optimization_disabled() ||
-             isolate->DebuggerHasBreakPoints()) {
+  if (!isolate->use_crankshaft() ||
+      function->shared()->optimization_disabled() ||
+      isolate->DebuggerHasBreakPoints()) {
     // If the function is not optimizable or debugger is active continue
     // using the code from the full compiler.
     if (FLAG_trace_opt) {
@@ -8476,16 +8470,16 @@ RUNTIME_FUNCTION(Runtime_CompileOptimized) {
           isolate->DebuggerHasBreakPoints() ? "T" : "F");
     }
     function->ReplaceCode(*unoptimized);
+    return function->code();
+  }
+
+  Compiler::ConcurrencyMode mode =
+      concurrent ? Compiler::CONCURRENT : Compiler::NOT_CONCURRENT;
+  Handle<Code> code;
+ if (Compiler::GetOptimizedCode(function, unoptimized, mode).ToHandle(&code)) {
+    function->ReplaceCode(*code);
   } else {
-    Compiler::ConcurrencyMode mode = concurrent ? Compiler::CONCURRENT
-                                                : Compiler::NOT_CONCURRENT;
-    Handle<Code> code;
-    if (Compiler::GetOptimizedCode(
-            function, unoptimized, mode).ToHandle(&code)) {
-      function->ReplaceCode(*code);
-    } else {
-      function->ReplaceCode(*unoptimized);
-    }
+    function->ReplaceCode(*unoptimized);
   }

   DCHECK(function->code()->kind() == Code::FUNCTION ||
@@ -8641,11 +8635,7 @@ RUNTIME_FUNCTION(Runtime_OptimizeFunctionOnNextCall) {
   RUNTIME_ASSERT(args.length() == 1 || args.length() == 2);
   CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);

-  if (!function->IsOptimizable() &&
-      !function->IsMarkedForConcurrentOptimization() &&
-      !function->IsInOptimizationQueue()) {
-    return isolate->heap()->undefined_value();
-  }
+  if (function->IsOptimized()) return isolate->heap()->undefined_value();

   function->MarkForOptimization();

Index: test/mjsunit/compiler/opt-next-call.js
diff --git a/test/mjsunit/regress/regress-347906.js b/test/mjsunit/compiler/opt-next-call.js
similarity index 70%
copy from test/mjsunit/regress/regress-347906.js
copy to test/mjsunit/compiler/opt-next-call.js
index c751618928c93015679087ee1b500637657aa341..17a0754c2314a43479fa13bd8fdede0d4cf7b0ed 100644
--- a/test/mjsunit/regress/regress-347906.js
+++ b/test/mjsunit/compiler/opt-next-call.js
@@ -2,13 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.

-// Flags: --allow-natives-syntax --harmony
-
 function foo() {
-  return Math.clz32(12.34);
+  return "fooed";
 }

-foo();
-foo();
 %OptimizeFunctionOnNextCall(foo);
-foo();
+assertEquals("fooed", foo());
+assertOptimized(foo);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to