1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "src/v8.h" 6 7 #include "test/cctest/cctest.h" 8 #include "test/cctest/compiler/function-tester.h" 9 10 using namespace v8; 11 using namespace v8::internal; 12 using namespace v8::internal::compiler; 13 14 #if V8_TURBOFAN_TARGET 15 IsOptimized(const FunctionCallbackInfo<v8::Value> & args)16static void IsOptimized(const FunctionCallbackInfo<v8::Value>& args) { 17 JavaScriptFrameIterator it(CcTest::i_isolate()); 18 JavaScriptFrame* frame = it.frame(); 19 return args.GetReturnValue().Set(frame->is_optimized()); 20 } 21 22 InstallIsOptimizedHelper(v8::Isolate * isolate)23static void InstallIsOptimizedHelper(v8::Isolate* isolate) { 24 Local<v8::Context> context = isolate->GetCurrentContext(); 25 Local<v8::FunctionTemplate> t = FunctionTemplate::New(isolate, IsOptimized); 26 context->Global()->Set(v8_str("IsOptimized"), t->GetFunction()); 27 } 28 29 TEST(TurboSimpleDeopt)30TEST(TurboSimpleDeopt) { 31 FLAG_allow_natives_syntax = true; 32 FLAG_turbo_deoptimization = true; 33 34 FunctionTester T( 35 "(function f(a) {" 36 "var b = 1;" 37 "if (!IsOptimized()) return 0;" 38 "%DeoptimizeFunction(f);" 39 "if (IsOptimized()) return 0;" 40 "return a + b; })"); 41 42 InstallIsOptimizedHelper(CcTest::isolate()); 43 T.CheckCall(T.Val(2), T.Val(1)); 44 } 45 46 TEST(TurboSimpleDeoptInExpr)47TEST(TurboSimpleDeoptInExpr) { 48 FLAG_allow_natives_syntax = true; 49 FLAG_turbo_deoptimization = true; 50 51 FunctionTester T( 52 "(function f(a) {" 53 "var b = 1;" 54 "var c = 2;" 55 "if (!IsOptimized()) return 0;" 56 "var d = b + (%DeoptimizeFunction(f), c);" 57 "if (IsOptimized()) return 0;" 58 "return d + a; })"); 59 60 InstallIsOptimizedHelper(CcTest::isolate()); 61 T.CheckCall(T.Val(6), T.Val(3)); 62 } 63 64 #endif 65 TEST(TurboTrivialDeopt)66TEST(TurboTrivialDeopt) { 67 FLAG_allow_natives_syntax = true; 68 FLAG_turbo_deoptimization = true; 69 70 FunctionTester T( 71 "(function foo() {" 72 "%DeoptimizeFunction(foo);" 73 "return 1; })"); 74 75 T.CheckCall(T.Val(1)); 76 } 77