[crankshaft] DCE must not eliminate (observable) math operations.
authorbmeurer <bmeurer@chromium.org>
Tue, 25 Aug 2015 06:24:40 +0000 (23:24 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 25 Aug 2015 06:24:55 +0000 (06:24 +0000)
The HUnaryMathOperation cannot be eliminated in general, because the
spec requires a ToNumber conversion on the input, which is observable
of course.

BUG=v8:4389
LOG=y

Review URL: https://codereview.chromium.org/1307413003

Cr-Commit-Position: refs/heads/master@{#30343}

src/hydrogen-instructions.h
test/mjsunit/compiler/regress-4389-1.js [new file with mode: 0644]
test/mjsunit/compiler/regress-4389-2.js [new file with mode: 0644]
test/mjsunit/compiler/regress-4389-3.js [new file with mode: 0644]
test/mjsunit/compiler/regress-4389-4.js [new file with mode: 0644]
test/mjsunit/compiler/regress-4389-5.js [new file with mode: 0644]
test/mjsunit/compiler/regress-4389-6.js [new file with mode: 0644]

index 077e916..ddcf21a 100644 (file)
@@ -2635,7 +2635,12 @@ class HUnaryMathOperation final : public HTemplateInstruction<2> {
     SetFlag(kAllowUndefinedAsNaN);
   }
 
-  bool IsDeletable() const override { return true; }
+  bool IsDeletable() const override {
+    // TODO(crankshaft): This should be true, however the semantics of this
+    // instruction also include the ToNumber conversion that is mentioned in the
+    // spec, which is of course observable.
+    return false;
+  }
 
   HValue* SimplifiedDividendForMathFloorOfDiv(HDiv* hdiv);
   HValue* SimplifiedDivisorForMathFloorOfDiv(HDiv* hdiv);
diff --git a/test/mjsunit/compiler/regress-4389-1.js b/test/mjsunit/compiler/regress-4389-1.js
new file mode 100644 (file)
index 0000000..c58ce2d
--- /dev/null
@@ -0,0 +1,11 @@
+// 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.
+
+// Flags: --allow-natives-syntax --dead-code-elimination
+
+function foo(x) { Math.fround(x); }
+foo(1);
+foo(2);
+%OptimizeFunctionOnNextCall(foo);
+assertThrows(function() { foo(Symbol()) }, TypeError);
diff --git a/test/mjsunit/compiler/regress-4389-2.js b/test/mjsunit/compiler/regress-4389-2.js
new file mode 100644 (file)
index 0000000..3b720a5
--- /dev/null
@@ -0,0 +1,11 @@
+// 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.
+
+// Flags: --allow-natives-syntax --dead-code-elimination
+
+function foo(x) { Math.sqrt(x); }
+foo(1);
+foo(2);
+%OptimizeFunctionOnNextCall(foo);
+assertThrows(function() { foo(Symbol()) }, TypeError);
diff --git a/test/mjsunit/compiler/regress-4389-3.js b/test/mjsunit/compiler/regress-4389-3.js
new file mode 100644 (file)
index 0000000..9aa72d1
--- /dev/null
@@ -0,0 +1,11 @@
+// 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.
+
+// Flags: --allow-natives-syntax --dead-code-elimination
+
+function foo(x) { Math.floor(x); }
+foo(1);
+foo(2);
+%OptimizeFunctionOnNextCall(foo);
+assertThrows(function() { foo(Symbol()) }, TypeError);
diff --git a/test/mjsunit/compiler/regress-4389-4.js b/test/mjsunit/compiler/regress-4389-4.js
new file mode 100644 (file)
index 0000000..e824973
--- /dev/null
@@ -0,0 +1,11 @@
+// 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.
+
+// Flags: --allow-natives-syntax --dead-code-elimination
+
+function foo(x) { Math.round(x); }
+foo(1);
+foo(2);
+%OptimizeFunctionOnNextCall(foo);
+assertThrows(function() { foo(Symbol()) }, TypeError);
diff --git a/test/mjsunit/compiler/regress-4389-5.js b/test/mjsunit/compiler/regress-4389-5.js
new file mode 100644 (file)
index 0000000..64797bc
--- /dev/null
@@ -0,0 +1,11 @@
+// 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.
+
+// Flags: --allow-natives-syntax --dead-code-elimination
+
+function foo(x) { Math.abs(x); }
+foo(1);
+foo(2);
+%OptimizeFunctionOnNextCall(foo);
+assertThrows(function() { foo(Symbol()) }, TypeError);
diff --git a/test/mjsunit/compiler/regress-4389-6.js b/test/mjsunit/compiler/regress-4389-6.js
new file mode 100644 (file)
index 0000000..fe06570
--- /dev/null
@@ -0,0 +1,11 @@
+// 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.
+
+// Flags: --allow-natives-syntax --dead-code-elimination
+
+function foo(x) { Math.log(x); }
+foo(1);
+foo(2);
+%OptimizeFunctionOnNextCall(foo);
+assertThrows(function() { foo(Symbol()) }, TypeError);