Reviewers: Sven Panne,

Description:
[turbofan] Optimize BooleanNot conditions to Branch nodes.

Also remove the weird work-around for this missing optimization in
CHECK_DATE in macros.py.

[email protected]

Please review this at https://codereview.chromium.org/1205353002/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+47, -2 lines):
  M src/compiler/common-operator-reducer.cc
  M src/macros.py
  M test/unittests/compiler/common-operator-reducer-unittest.cc


Index: src/compiler/common-operator-reducer.cc
diff --git a/src/compiler/common-operator-reducer.cc b/src/compiler/common-operator-reducer.cc index 52b9ca8389e0d02c14b51d7016d387ce7aea18c4..694c36edcc8d0f7876618be43fb213ea2737849a 100644
--- a/src/compiler/common-operator-reducer.cc
+++ b/src/compiler/common-operator-reducer.cc
@@ -65,6 +65,29 @@ Reduction CommonOperatorReducer::Reduce(Node* node) {
 Reduction CommonOperatorReducer::ReduceBranch(Node* node) {
   DCHECK_EQ(IrOpcode::kBranch, node->opcode());
   Node* const cond = node->InputAt(0);
+ // Swap IfTrue/IfFalse on {branch} if {cond} is a BooleanNot and use the input + // to BooleanNot as new condition for {branch}. Note we assume that {cond} was + // already properly optimized before we get here (as guaranteed by the graph
+  // reduction logic).
+  if (cond->opcode() == IrOpcode::kBooleanNot) {
+    for (Node* const use : node->uses()) {
+      switch (use->opcode()) {
+        case IrOpcode::kIfTrue:
+          use->set_op(common()->IfFalse());
+          break;
+        case IrOpcode::kIfFalse:
+          use->set_op(common()->IfTrue());
+          break;
+        default:
+          UNREACHABLE();
+      }
+    }
+ // Update the condition of {branch}. No need to mark the uses for revisit, + // since we tell the graph reducer that the {branch} was changed and the + // graph reduction logic will ensure that the uses are revisited properly.
+    node->ReplaceInput(0, cond->InputAt(0));
+    return Changed(node);
+  }
   Decision const decision = DecideCondition(cond);
   if (decision == Decision::kUnknown) return NoChange();
   Node* const control = node->InputAt(1);
Index: src/macros.py
diff --git a/src/macros.py b/src/macros.py
index 0b5bcb51533e96c01f42a2d4662f33d29fe96468..5e28c66a83a445530fbddb2518f8efa23ad6dae8 100644
--- a/src/macros.py
+++ b/src/macros.py
@@ -191,7 +191,7 @@ define MAX_TIME_BEFORE_UTC = 8640002592000000;

 # Gets the value of a Date object. If arg is not a Date object
 # a type error is thrown.
-macro CHECK_DATE(arg) = if (%_IsDate(arg)) {} else %_ThrowNotDateError();
+macro CHECK_DATE(arg) = if (!%_IsDate(arg)) %_ThrowNotDateError();
 macro LOCAL_DATE_VALUE(arg) = (%_DateField(arg, 0) + %_DateField(arg, 21));
 macro UTC_DATE_VALUE(arg)    = (%_DateField(arg, 0));

Index: test/unittests/compiler/common-operator-reducer-unittest.cc
diff --git a/test/unittests/compiler/common-operator-reducer-unittest.cc b/test/unittests/compiler/common-operator-reducer-unittest.cc index a443f545c7ecd9b38d26b8e68875b24bbe8fa288..05a493738e32e74f6d35a770f159a1e5cc173f04 100644
--- a/test/unittests/compiler/common-operator-reducer-unittest.cc
+++ b/test/unittests/compiler/common-operator-reducer-unittest.cc
@@ -7,6 +7,7 @@
 #include "src/compiler/machine-operator.h"
 #include "src/compiler/machine-type.h"
 #include "src/compiler/operator.h"
+#include "src/compiler/simplified-operator.h"
 #include "test/unittests/compiler/graph-reducer-unittest.h"
 #include "test/unittests/compiler/graph-unittest.h"
 #include "test/unittests/compiler/node-test-utils.h"
@@ -20,7 +21,7 @@ namespace compiler {
 class CommonOperatorReducerTest : public GraphTest {
  public:
   explicit CommonOperatorReducerTest(int num_parameters = 1)
-      : GraphTest(num_parameters), machine_(zone()) {}
+      : GraphTest(num_parameters), machine_(zone()), simplified_(zone()) {}
   ~CommonOperatorReducerTest() override {}

  protected:
@@ -39,9 +40,11 @@ class CommonOperatorReducerTest : public GraphTest {
   }

   MachineOperatorBuilder* machine() { return &machine_; }
+  SimplifiedOperatorBuilder* simplified() { return &simplified_; }

  private:
   MachineOperatorBuilder machine_;
+  SimplifiedOperatorBuilder simplified_;
 };


@@ -169,6 +172,25 @@ TEST_F(CommonOperatorReducerTest, BranchWithTrueConstant) {
 }


+TEST_F(CommonOperatorReducerTest, BranchWithBooleanNot) {
+  Node* const value = Parameter(0);
+  TRACED_FOREACH(BranchHint, hint, kBranchHints) {
+    Node* const control = graph()->start();
+    Node* const branch = graph()->NewNode(
+        common()->Branch(hint),
+        graph()->NewNode(simplified()->BooleanNot(), value), control);
+    Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
+    Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
+    Reduction const r = Reduce(branch);
+    ASSERT_TRUE(r.Changed());
+    EXPECT_EQ(branch, r.replacement());
+    EXPECT_THAT(branch, IsBranch(value, control));
+    EXPECT_THAT(if_false, IsIfTrue(branch));
+    EXPECT_THAT(if_true, IsIfFalse(branch));
+  }
+}
+
+
// -----------------------------------------------------------------------------
 // Merge



--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to