number-divide.js 6.84 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 --noalways-opt
6 7 8 9 10 11 12

// Test that NumberDivide with Number feedback works if only in the
// end SimplifiedLowering figures out that the inputs to this operation
// are actually Unsigned32.
(function() {
  // We need a separately polluted % with NumberOrOddball feedback.
  function bar(x) { return x / 2; }
13
  %EnsureFeedbackVectorForFunction(bar);
14 15 16 17 18 19 20 21 22 23 24
  bar(undefined);  // The % feedback is now NumberOrOddball.

  // Now just use the gadget above in a way that only after RETYPE
  // in SimplifiedLowering we find out that the `x` is actually in
  // Unsigned32 range (based on taking the SignedSmall feedback on
  // the + operator).
  function foo(x) {
    x = (x >>> 0) + 1;
    return bar(x) | 0;
  }

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

// Test that NumberDivide with Number feedback works if only in the
// end SimplifiedLowering figures out that the inputs to this operation
// are actually Signed32.
(function() {
  // We need a separately polluted % with NumberOrOddball feedback.
  function bar(x) { return x / 2; }
44
  %EnsureFeedbackVectorForFunction(bar);
45 46 47 48 49 50 51 52 53 54 55
  bar(undefined);  // The % feedback is now NumberOrOddball.

  // Now just use the gadget above in a way that only after RETYPE
  // in SimplifiedLowering we find out that the `x` is actually in
  // Signed32 range (based on taking the SignedSmall feedback on
  // the + operator).
  function foo(x) {
    x = (x | 0) + 1;
    return bar(x) | 0;
  }

56
  %PrepareFunctionForOptimization(foo);
57 58 59 60 61 62 63 64 65 66 67
  assertEquals(1, foo(1));
  assertEquals(1, foo(2));
  assertEquals(2, foo(3));
  assertEquals(2, foo(4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(1));
  assertEquals(1, foo(2));
  assertEquals(2, foo(3));
  assertEquals(2, foo(4));
  assertOptimized(foo);
})();
68 69 70 71 72 73 74

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the "known power of two divisor" optimization works correctly.
(function() {
  function foo(x) { return (x | 0) / 2; }

  // Warmup with proper int32 divisions.
75
  %PrepareFunctionForOptimization(foo);
76 77 78 79 80 81 82 83 84 85 86
  assertEquals(1, foo(2));
  assertEquals(2, foo(4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo(6));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(0.5, foo(1));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
87
  %PrepareFunctionForOptimization(foo);
88 89 90 91 92 93 94 95 96 97 98 99 100
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(4, foo(8));
  assertOptimized(foo);
  assertEquals(0.5, foo(1));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the optimized code properly bails out on "division by zero".
(function() {
  function foo(x, y) { return x / y; }

  // Warmup with proper int32 divisions.
101
  %PrepareFunctionForOptimization(foo);
102 103 104 105 106 107 108 109 110 111 112
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(Infinity, foo(1, 0));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
113
  %PrepareFunctionForOptimization(foo);
114 115 116 117 118 119 120 121 122 123 124 125 126
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(Infinity, foo(1, 0));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the optimized code properly bails out on minus zero.
(function() {
  function foo(x, y) { return x / y; }

  // Warmup with proper int32 divisions.
127
  %PrepareFunctionForOptimization(foo);
128 129 130 131 132 133 134 135 136 137 138
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(-0, foo(0, -1));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
139
  %PrepareFunctionForOptimization(foo);
140 141 142 143 144 145 146 147 148 149 150 151 152
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(-0, foo(0, -1));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the optimized code properly bails out if result is -kMinInt.
(function() {
  function foo(x, y) { return x / y; }

  // Warmup with proper int32 divisions.
153
  %PrepareFunctionForOptimization(foo);
154 155 156 157 158 159 160 161 162 163 164
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(2147483648, foo(-2147483648, -1));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
165
  %PrepareFunctionForOptimization(foo);
166 167 168 169 170 171 172 173 174 175 176 177 178
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(2147483648, foo(-2147483648, -1));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedUint32Div, and
// that the "known power of two divisor" optimization works correctly.
(function() {
  function foo(s) { return s.length / 2; }

  // Warmup with proper uint32 divisions.
179
  %PrepareFunctionForOptimization(foo);
180 181 182 183 184 185 186 187 188 189 190
  assertEquals(1, foo("ab".repeat(1)));
  assertEquals(2, foo("ab".repeat(2)));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo("ab".repeat(3)));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(0.5, foo("a"));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
191
  %PrepareFunctionForOptimization(foo);
192 193 194 195 196 197 198 199 200 201 202 203 204
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(4, foo("ab".repeat(4)));
  assertOptimized(foo);
  assertEquals(0.5, foo("a"));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedUint32Div, and
// that the optimized code properly bails out on "division by zero".
(function() {
  function foo(x, y) { return (x >>> 0) / (y >>> 0); }

  // Warmup with proper uint32 divisions.
205
  %PrepareFunctionForOptimization(foo);
206 207 208 209 210 211 212 213 214 215 216
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(Infinity, foo(1, 0));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
217
  %PrepareFunctionForOptimization(foo);
218 219 220 221 222 223
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(Infinity, foo(1, 0));
  assertOptimized(foo);
})();