v8/test/mjsunit/compiler/consecutive-addition.js
Z Nguyen-Huu e93a369f7a [turbofan] Reduce consecutive overflow addition with constants
Using associative property of addition: (x + A) + B => x + (A + B).
Note: A and B need to have the same sign and we need to check that
(x + A) isn't used anywhere else.

20% perf improvement of the following function.

function f(n) {
  var c = 0;
  for (var i = 0; i < n; i++) {
    c = c + 2 + 3;
  }
  return c;
}
for n = 10_000_000.

Before: 7.31s.
After: 6.05s.

Bug: v8:10305
Change-Id: If45d1cad6128a9a25cb9f43a4828ae28d594a84b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2365221
Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70064}
2020-09-22 16:17:53 +00:00

31 lines
701 B
JavaScript

// Copyright 2020 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
function f1(n) {
var a = n + 2;
return a + 3;
}
%PrepareFunctionForOptimization(f1);
assertEquals(f1(1), 6);
assertEquals(f1(2), 7);
%OptimizeFunctionOnNextCall(f1);
assertEquals(f1(13), 18);
assertEquals(f1(14), 19);
function f2(n, odd) {
var a = n + 2;
if (odd) return a;
return a + 3;
}
%PrepareFunctionForOptimization(f2);
assertEquals(f2(1, true), 3);
assertEquals(f2(2, false), 7);
%OptimizeFunctionOnNextCall(f2);
assertEquals(f2(13, true), 15);
assertEquals(f2(14, false), 19);