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.
7 #include "test/cctest/compiler/function-tester.h"
11 using namespace v8::internal;
12 using namespace v8::internal::compiler;
14 // Helper to determine inline count via JavaScriptFrame::GetInlineCount.
15 // Note that a count of 1 indicates that no inlining has occured.
16 static void AssertInlineCount(const v8::FunctionCallbackInfo<v8::Value>& args) {
17 StackTraceFrameIterator it(CcTest::i_isolate());
19 JavaScriptFrame* topmost = it.frame();
21 JavaScriptFrame* frame = it.frame();
22 PrintF("%d %s, inline count: %d\n", frames_seen,
23 frame->function()->shared()->DebugName()->ToCString().get(),
24 frame->GetInlineCount());
28 CHECK_EQ(args[0]->ToInt32(args.GetIsolate())->Value(),
29 topmost->GetInlineCount());
33 static void InstallAssertInlineCountHelper(v8::Isolate* isolate) {
34 v8::Local<v8::Context> context = isolate->GetCurrentContext();
35 v8::Local<v8::FunctionTemplate> t =
36 v8::FunctionTemplate::New(isolate, AssertInlineCount);
37 context->Global()->Set(v8_str("AssertInlineCount"), t->GetFunction());
41 static uint32_t kInlineFlags = CompilationInfo::kInliningEnabled |
42 CompilationInfo::kContextSpecializing |
43 CompilationInfo::kTypingEnabled;
46 TEST(SimpleInlining) {
47 FLAG_turbo_deoptimization = true;
50 " function foo(s) { AssertInlineCount(2); return s; };"
51 " function bar(s, t) { return foo(s); };"
56 InstallAssertInlineCountHelper(CcTest::isolate());
57 T.CheckCall(T.Val(1), T.Val(1), T.Val(2));
61 TEST(SimpleInliningDeopt) {
62 FLAG_turbo_deoptimization = true;
65 " function foo(s) { %DeoptimizeFunction(bar); return s; };"
66 " function bar(s, t) { return foo(s); };"
71 InstallAssertInlineCountHelper(CcTest::isolate());
72 T.CheckCall(T.Val(1), T.Val(1), T.Val(2));
76 TEST(SimpleInliningContext) {
77 FLAG_turbo_deoptimization = true;
80 " function foo(s) { AssertInlineCount(2); var x = 12; return s + x; };"
81 " function bar(s, t) { return foo(s); };"
86 InstallAssertInlineCountHelper(CcTest::isolate());
87 T.CheckCall(T.Val(13), T.Val(1), T.Val(2));
91 TEST(SimpleInliningContextDeopt) {
92 FLAG_turbo_deoptimization = true;
96 " AssertInlineCount(2); %DeoptimizeFunction(bar); var x = 12;"
99 " function bar(s, t) { return foo(s); };"
104 InstallAssertInlineCountHelper(CcTest::isolate());
105 T.CheckCall(T.Val(13), T.Val(1), T.Val(2));
109 TEST(CaptureContext) {
110 FLAG_turbo_deoptimization = true;
112 "var f = (function () {"
114 " function bar(s) { return x + s; };"
115 " return (function (s) { return bar(s); });"
117 "(function (s) { return f(s) })",
120 InstallAssertInlineCountHelper(CcTest::isolate());
121 T.CheckCall(T.Val(42 + 12), T.Val(12), T.undefined());
125 // TODO(sigurds) For now we do not inline any native functions. If we do at
126 // some point, change this test.
127 TEST(DontInlineEval) {
128 FLAG_turbo_deoptimization = true;
132 " function bar(s, t) { return eval(\"AssertInlineCount(1); x\") };"
137 InstallAssertInlineCountHelper(CcTest::isolate());
138 T.CheckCall(T.Val(42), T.Val("x"), T.undefined());
142 TEST(InlineOmitArguments) {
143 FLAG_turbo_deoptimization = true;
147 " function bar(s, t, u, v) { AssertInlineCount(2); return x + s; };"
148 " return (function (s,t) { return bar(s); });"
152 InstallAssertInlineCountHelper(CcTest::isolate());
153 T.CheckCall(T.Val(42 + 12), T.Val(12), T.undefined());
157 TEST(InlineOmitArgumentsDeopt) {
158 FLAG_turbo_deoptimization = true;
161 " function foo(s,t,u,v) { AssertInlineCount(2);"
162 " %DeoptimizeFunction(bar); return baz(); };"
163 " function bar() { return foo(11); };"
164 " function baz() { return foo.arguments.length == 1 &&"
165 " foo.arguments[0] == 11; }"
170 InstallAssertInlineCountHelper(CcTest::isolate());
171 T.CheckCall(T.true_value(), T.Val(12), T.Val(14));
175 TEST(InlineSurplusArguments) {
176 FLAG_turbo_deoptimization = true;
180 " function foo(s) { AssertInlineCount(2); return x + s; };"
181 " function bar(s,t) { return foo(s,t,13); };"
186 InstallAssertInlineCountHelper(CcTest::isolate());
187 T.CheckCall(T.Val(42 + 12), T.Val(12), T.undefined());
191 TEST(InlineSurplusArgumentsDeopt) {
192 FLAG_turbo_deoptimization = true;
195 " function foo(s) { AssertInlineCount(2); %DeoptimizeFunction(bar);"
197 " function bar() { return foo(13, 14, 15); };"
198 " function baz() { return foo.arguments.length == 3 &&"
199 " foo.arguments[0] == 13 &&"
200 " foo.arguments[1] == 14 &&"
201 " foo.arguments[2] == 15; }"
206 InstallAssertInlineCountHelper(CcTest::isolate());
207 T.CheckCall(T.true_value(), T.Val(12), T.Val(14));
212 FLAG_turbo_deoptimization = true;
216 " function bar(s) { AssertInlineCount(2); return x + s; };"
217 " return (function (s,t) { return bar(s) + bar(t); });"
221 InstallAssertInlineCountHelper(CcTest::isolate());
222 T.CheckCall(T.Val(2 * 42 + 12 + 4), T.Val(12), T.Val(4));
226 TEST(InlineTwiceDependent) {
227 FLAG_turbo_deoptimization = true;
231 " function foo(s) { AssertInlineCount(2); return x + s; };"
232 " function bar(s,t) { return foo(foo(s)); };"
237 InstallAssertInlineCountHelper(CcTest::isolate());
238 T.CheckCall(T.Val(42 + 42 + 12), T.Val(12), T.Val(4));
242 TEST(InlineTwiceDependentDiamond) {
243 FLAG_turbo_deoptimization = true;
247 " function foo(s) { AssertInlineCount(2); if (s % 2 == 0) {"
248 " return x - s } else { return x + s; } };"
249 " function bar(s,t) { return foo(foo(s)); };"
254 InstallAssertInlineCountHelper(CcTest::isolate());
255 T.CheckCall(T.Val(-11), T.Val(11), T.Val(4));
259 TEST(InlineTwiceDependentDiamondDifferent) {
260 FLAG_turbo_deoptimization = true;
264 " function foo(s,t) { AssertInlineCount(2); if (s % 2 == 0) {"
265 " return x - s * t } else { return x + s * t; } };"
266 " function bar(s,t) { return foo(foo(s, 3), 5); };"
271 InstallAssertInlineCountHelper(CcTest::isolate());
272 T.CheckCall(T.Val(-329), T.Val(11), T.Val(4));
276 TEST(InlineLoopGuardedEmpty) {
277 FLAG_turbo_deoptimization = true;
280 " function foo(s) { AssertInlineCount(2); if (s) while (s); return s; };"
281 " function bar(s,t) { return foo(s); };"
286 InstallAssertInlineCountHelper(CcTest::isolate());
287 T.CheckCall(T.Val(0.0), T.Val(0.0), T.Val(4));
291 TEST(InlineLoopGuardedOnce) {
292 FLAG_turbo_deoptimization = true;
295 " function foo(s,t) { AssertInlineCount(2); if (t > 0) while (s > 0) {"
296 " s = s - 1; }; return s; };"
297 " function bar(s,t) { return foo(s,t); };"
302 InstallAssertInlineCountHelper(CcTest::isolate());
303 T.CheckCall(T.Val(0.0), T.Val(11), T.Val(4));
307 TEST(InlineLoopGuardedTwice) {
308 FLAG_turbo_deoptimization = true;
311 " function foo(s,t) { AssertInlineCount(2); if (t > 0) while (s > 0) {"
312 " s = s - 1; }; return s; };"
313 " function bar(s,t) { return foo(foo(s,t),t); };"
318 InstallAssertInlineCountHelper(CcTest::isolate());
319 T.CheckCall(T.Val(0.0), T.Val(11), T.Val(4));
323 TEST(InlineStrictIntoNonStrict) {
324 FLAG_turbo_deoptimization = true;
327 " var x = Object.create({}, { y: { value:42, writable:false } });"
328 " function foo(s) { 'use strict';"
330 " function bar(s,t) { return foo(s); };"
335 InstallAssertInlineCountHelper(CcTest::isolate());
336 T.CheckThrows(T.undefined(), T.undefined());
340 TEST(InlineNonStrictIntoStrict) {
341 FLAG_turbo_deoptimization = true;
344 " var x = Object.create({}, { y: { value:42, writable:false } });"
345 " function foo(s) { x.y = 9; return x.y; };"
346 " function bar(s,t) { \'use strict\'; return foo(s); };"
351 InstallAssertInlineCountHelper(CcTest::isolate());
352 T.CheckCall(T.Val(42), T.undefined(), T.undefined());
356 TEST(InlineIntrinsicIsSmi) {
357 FLAG_turbo_deoptimization = true;
361 " function bar(s,t) { return %_IsSmi(x); };"
366 InstallAssertInlineCountHelper(CcTest::isolate());
367 T.CheckCall(T.true_value(), T.Val(12), T.Val(4));
371 TEST(InlineIntrinsicIsNonNegativeSmi) {
372 FLAG_turbo_deoptimization = true;
376 " function bar(s,t) { return %_IsNonNegativeSmi(x); };"
381 InstallAssertInlineCountHelper(CcTest::isolate());
382 T.CheckCall(T.true_value(), T.Val(12), T.Val(4));
386 TEST(InlineIntrinsicIsArray) {
387 FLAG_turbo_deoptimization = true;
391 " function bar(s,t) { return %_IsArray(x); };"
396 InstallAssertInlineCountHelper(CcTest::isolate());
397 T.CheckCall(T.true_value(), T.Val(12), T.Val(4));
402 " function bar(s,t) { return %_IsArray(x); };"
407 T2.CheckCall(T.false_value(), T.Val(12), T.Val(4));
412 " function bar(s,t) { return %_IsArray(x); };"
417 T3.CheckCall(T.false_value(), T.Val(12), T.Val(4));
421 TEST(InlineWithArguments) {
422 FLAG_turbo_deoptimization = true;
425 " function foo(s,t,u) { AssertInlineCount(2);"
426 " return foo.arguments.length == 3 &&"
427 " foo.arguments[0] == 13 &&"
428 " foo.arguments[1] == 14 &&"
429 " foo.arguments[2] == 15;"
431 " function bar() { return foo(13, 14, 15); };"
436 InstallAssertInlineCountHelper(CcTest::isolate());
437 T.CheckCall(T.true_value(), T.Val(12), T.Val(14));
440 #endif // V8_TURBOFAN_TARGET