v8/test/mjsunit/optimized-includes-polymorph.js
Matt Gardner c22bb466d8 Inline indexOf/includes at polymorphic sites
This commit allows inlining of Array#indexOf and Array#includes when the array type is polymorphic for types that are compatable for array iteration.

Bug: v8:8388
Change-Id: Ib826bad857c7dfe0ee7af99bb456b50b7a8b6ef9
Reviewed-on: https://chromium-review.googlesource.com/c/1450137
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59473}
2019-02-08 19:45:10 +00:00

118 lines
2.7 KiB
JavaScript

// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
var o1 = {};
var o2 = {};
var a = [0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,0,0];
var b = [,,,,,2,3,4];
var c = [o1, o2];
var d = [,,, o2, o1];
var e = [0.5,3,4];
var f = [,,,,0.5,3,4];
function checkIncludes(ary, value) {
return ary.includes(value)
}
function checkIndexOf(ary, value, expected) {
return ary.indexOf(value) == expected;
}
function expectIncludes(ary, value) {
assertTrue(checkIncludes(ary, value));
}
function expectNotIncludes(ary, value) {
assertFalse(checkIncludes(ary, value));
}
function expectIndexOf(ary, value, expected) {
assertTrue(checkIndexOf(ary, value, expected));
}
var testIncludes = {
polymorphic: function() {
expectIncludes(a, 21);
expectIncludes(b, 4);
expectIncludes(c, o2);
expectIncludes(d, o1);
expectNotIncludes(a, o1);
expectNotIncludes(b, o2);
expectNotIncludes(c, 3);
expectNotIncludes(d, 4);
},
polymorphicDouble: function() {
expectIncludes(e, 3);
expectIncludes(f, 0.5);
expectNotIncludes(e, 10);
expectNotIncludes(f, 0.25);
},
polymorphicMixed: function() {
expectIncludes(a, 21);
expectIncludes(b, 4);
expectIncludes(c, o2);
expectIncludes(d, o1);
expectIncludes(e, 3);
expectIncludes(f, 0.5);
expectNotIncludes(a, o1);
expectNotIncludes(b, o2);
expectNotIncludes(c, 3);
expectNotIncludes(d, 4);
expectNotIncludes(e, 10);
expectNotIncludes(f, 0.25);
},
};
var testIndexOf = {
polymorphic: function() {
expectIndexOf(a, 21, 21);
expectIndexOf(b, 4, 7);
expectIndexOf(c, o2, 1);
expectIndexOf(d, o1, 4);
expectIndexOf(a, o1, -1);
expectIndexOf(b, o2, -1);
expectIndexOf(c, 3, -1);
expectIndexOf(d, 4, -1);
},
polymorphicDouble: function() {
expectIndexOf(e, 3, 1);
expectIndexOf(f, 0.5, 4);
expectIndexOf(e, 10, -1);
expectIndexOf(f, 0.25, -1);
},
polymorphicMixed: function() {
expectIndexOf(a, 21, 21);
expectIndexOf(b, 4, 7);
expectIndexOf(c, o2, 1);
expectIndexOf(d, o1, 4);
expectIndexOf(e, 3, 1);
expectIndexOf(f, 0.5, 4);
expectIndexOf(a, o1, -1);
expectIndexOf(b, o2, -1);
expectIndexOf(c, 3, -1);
expectIndexOf(d, 4, -1);
expectIndexOf(e, 10, -1);
expectIndexOf(f, 0.25, -1);
},
};
function runTests(tests, func) {
for (test in tests) {
%DeoptimizeFunction(func);
%ClearFunctionFeedback(func);
tests[test]();
%OptimizeFunctionOnNextCall(func);
tests[test]();
}
}
runTests(testIncludes, checkIncludes)
runTests(testIndexOf, checkIndexOf)