number-add.js 2.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// 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.

// Flags: --allow-natives-syntax

// This tests that NumberAdd passes on the right truncations
// even if it figures out during SimplifiedLowering that it
// can indeed do a Word32 operation (based on the feedback
// baked in for its inputs by other operators).
(function() {
  // We need a + with Number feedback to get to a NumberAdd
  // during the typed lowering pass of TurboFan's frontend.
  function foo(x, y) { return x + y; }
  foo(0.1, 0.2);
  foo(0.1, 0.2);

  // Now we need to fool TurboFan to think that it has to
  // perform the `foo(x,-1)` on Float64 values until the
  // very last moment (after the RETYPE phase of the
  // SimplifiedLowering) where it realizes that the inputs
  // and outputs of the NumberAdd allow it perform the
  // operation on Word32.
  function bar(x) {
    x = Math.trunc(foo(x - 1, 1));
    return foo(x, -1);
  }

29
  %PrepareFunctionForOptimization(bar);
30 31 32 33 34
  assertEquals(0, bar(1));
  assertEquals(1, bar(2));
  %OptimizeFunctionOnNextCall(bar);
  assertEquals(2, bar(3));
})();
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

// This tests that SpeculativeNumberAdd can still lower to
// Int32Add in SimplifiedLowering, which requires some magic
// to make sure that SpeculativeNumberAdd survives to that
// point, especially the JSTypedLowering needs to be unable
// to tell that the inputs to SpeculativeNumberAdd are non
// String primitives.
(function() {
  // We need a function that has a + with feedback Number or
  // NumberOrOddball, but for whose inputs the JSTypedLowering
  // cannot reduce it to NumberAdd (with SpeculativeToNumber
  // conversions). We achieve this utilizing an object literal
  // indirection here.
  function baz(x) {
    return {x}.x + x;
  }
  baz(null);
  baz(undefined);

  // Now we just need to truncate the result.
  function foo(x) {
    return baz(1) | 0;
  }

59
  %PrepareFunctionForOptimization(foo);
60 61 62 63 64
  assertEquals(2, foo());
  assertEquals(2, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo());
})();