redundancy-elimination.js 5.34 KB
Newer Older
1 2 3 4
// Copyright 2018 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
// Flags: --allow-natives-syntax --opt
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeNumberAdd with
// Number feedback.
(function() {
  function bar(i) {
    return ++i;
  }
  bar(0.1);

  function foo(a, i) {
    const x = a[i];
    const y = a[bar(i)];
    return x + y;
  }

22
  %PrepareFunctionForOptimization(foo);
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  assertEquals(3, foo([1, 2], 0));
  assertEquals(3, foo([1, 2], 0));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo([1, 2], 0));
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeNumberAdd with
// NumberOrOddball feedback.
(function() {
  function bar(i) {
    return ++i;
  }
  assertEquals(NaN, bar(undefined));

  function foo(a, i) {
    const x = a[i];
    const y = a[bar(i)];
    return x + y;
  }

44
  %PrepareFunctionForOptimization(foo);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  assertEquals(3, foo([1, 2], 0));
  assertEquals(3, foo([1, 2], 0));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo([1, 2], 0));
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeNumberSubtract with
// Number feedback.
(function() {
  function bar(i) {
    return --i;
  }
  assertEquals(-0.9, bar(0.1));

  function foo(a, i) {
    const x = a[i];
    const y = a[bar(i)];
    return x + y;
  }

66
  %PrepareFunctionForOptimization(foo);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  assertEquals(3, foo([1, 2], 1));
  assertEquals(3, foo([1, 2], 1));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo([1, 2], 1));
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeNumberSubtract with
// NumberOrOddball feedback.
(function() {
  function bar(i) {
    return --i;
  }
  assertEquals(NaN, bar(undefined));

  function foo(a, i) {
    const x = a[i];
    const y = a[bar(i)];
    return x + y;
  }

88
  %PrepareFunctionForOptimization(foo);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  assertEquals(3, foo([1, 2], 1));
  assertEquals(3, foo([1, 2], 1));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo([1, 2], 1));
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeToNumber.
(function() {
  function foo(a, i) {
    const x = a[i];
    const y = i++;
    return x + y;
  }

104
  %PrepareFunctionForOptimization(foo);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 0));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo([1, 2], 0));
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeSafeIntegerAdd.
(function() {
  function foo(a, i) {
    const x = a[i];
    const y = a[++i];
    return x + y;
  }

120
  %PrepareFunctionForOptimization(foo);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  assertEquals(3, foo([1, 2], 0));
  assertEquals(3, foo([1, 2], 0));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo([1, 2], 0));
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberOperation()
// TurboFan optimization for the case of SpeculativeSafeIntegerSubtract.
(function() {
  function foo(a, i) {
    const x = a[i];
    const y = a[--i];
    return x + y;
  }

136
  %PrepareFunctionForOptimization(foo);
137 138 139 140 141
  assertEquals(3, foo([1, 2], 1));
  assertEquals(3, foo([1, 2], 1));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo([1, 2], 1));
})();
142 143 144 145 146 147 148 149 150 151

// Test the RedundancyElimination::ReduceSpeculativeNumberComparison()
// TurboFan optimization for the case of SpeculativeNumberEqual.
(function() {
  function foo(a, i) {
    const x = a[i];
    if (i === 0) return x;
    return i;
  }

152
  %PrepareFunctionForOptimization(foo);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 1));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 1));
  // Even passing -0 should not deoptimize and
  // of course still pass the equality test above.
  assertEquals(9, foo([9, 2], -0));
  assertOptimized(foo);
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberComparison()
// TurboFan optimization for the case of SpeculativeNumberLessThan.
(function() {
  function foo(a, i) {
    const x = a[i];
    if (i < 1) return x;
    return i;
  }

173
  %PrepareFunctionForOptimization(foo);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 1));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 1));
  // Even passing -0 should not deoptimize and
  // of course still pass the equality test above.
  assertEquals(9, foo([9, 2], -0));
  assertOptimized(foo);
})();

// Test the RedundancyElimination::ReduceSpeculativeNumberComparison()
// TurboFan optimization for the case of SpeculativeNumberLessThanOrEqual.
(function() {
  function foo(a, i) {
    const x = a[i];
    if (i <= 0) return x;
    return i;
  }

194
  %PrepareFunctionForOptimization(foo);
195 196 197 198 199 200 201 202 203 204
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 1));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo([1, 2], 0));
  assertEquals(1, foo([1, 2], 1));
  // Even passing -0 should not deoptimize and
  // of course still pass the equality test above.
  assertEquals(9, foo([9, 2], -0));
  assertOptimized(foo);
})();