ensure-growing-store-learns.js 2.91 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
// Overwrite the value for --noverify-heap and
6
// --noenable-slow-asserts, which the test runner already set to true before.
7 8 9 10
//  Due to flag contradiction checking, this requires
// --allow-overwriting-for-next-flag to avoid an error.
// Flags: --allow-overwriting-for-next-flag --noverify-heap
// Flags: --allow-overwriting-for-next-flag --noenable-slow-asserts
11
// Flags: --allow-natives-syntax --turbofan --no-always-turbofan
12

13 14
// --noverify-heap and --noenable-slow-asserts are set because the test is too
// slow with it on.
15

16 17 18
// Ensure that keyed stores work, and optimized functions learn if the
// store required change to dictionary mode. Verify that stores that grow
// the array into large object space don't cause a deopt.
19 20 21 22 23 24 25
(function() {
  var a = [];

  function foo(a, i) {
    a[i] = 5.3;
  }

26
  %PrepareFunctionForOptimization(foo);
27 28 29 30 31 32 33 34
  foo(a, 1);
  foo(a, 2);
  foo(a, 3);
  %OptimizeFunctionOnNextCall(foo);
  a[3] = 0;
  foo(a, 3);
  assertEquals(a[3], 5.3);
  foo(a, 50000);
35 36 37 38 39
  // TODO(v8:11457) We don't currently support inlining element stores if there
  // is a dictionary mode prototypes on the prototype chain. Therefore, if
  // v8_dict_property_const_tracking is enabled, the optimized code only
  // contains a call to the IC handler and doesn't get deopted.
  assertEquals(%IsDictPropertyConstTrackingEnabled(), isOptimized(foo));
40 41
  assertTrue(%HasDictionaryElements(a));

42
  %PrepareFunctionForOptimization(foo);
43 44 45 46 47 48 49
  var b = [];
  foo(b, 1);
  foo(b, 2);
  // Put b in dictionary mode.
  b[10000] = 5;
  assertTrue(%HasDictionaryElements(b));
  foo(b, 3);
50
  %PrepareFunctionForOptimization(foo);
51 52 53 54 55 56 57
  %OptimizeFunctionOnNextCall(foo);
  foo(b, 50000);
  assertOptimized(foo);
  assertTrue(%HasDictionaryElements(b));

  // Clearing feedback for the StoreIC in foo is important for runs with
  // flag --stress-opt.
58
  %ClearFunctionFeedback(foo);
59 60 61 62 63 64 65 66 67 68 69
})();


(function() {
  var a = new Array(10);

  function foo2(a, i) {
    a[i] = 50;
  }

  // The KeyedStoreIC will learn GROW_MODE.
70
  %PrepareFunctionForOptimization(foo2);
71 72 73 74 75 76
  foo2(a, 10);
  foo2(a, 12);
  foo2(a, 31);
  %OptimizeFunctionOnNextCall(foo2);
  foo2(a, 40);

77
  assertOptimized(foo2);
78
  assertTrue(%HasSmiElements(a));
79

80
  // Grow a large array into large object space through the keyed store
81
  // without deoptimizing. Grow by 9s. If we set elements too sparsely, the
82 83
  // array will convert to dictionary mode.
  a = new Array(99999);
84
  assertTrue(%HasSmiElements(a));
85
  for (var i = 0; i < 263000; i += 9) {
86
    foo2(a, i);
87 88
  }

89 90 91
  // Verify that we are over 1 page in size, and foo2 remains optimized.
  // This means we've smoothly transitioned to allocating in large object
  // space.
92
  assertTrue(%HasSmiElements(a));
93 94 95
  assertTrue(a.length * 4 > (1024 * 1024));
  assertOptimized(foo2);

96
  %ClearFunctionFeedback(foo2);
97
})();