2015-01-16 11:04:01 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "src/compiler/common-operator.h"
|
2015-01-29 09:17:45 +00:00
|
|
|
#include "src/compiler/node-properties.h"
|
2015-01-16 11:04:01 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
|
|
|
|
2015-02-18 13:55:24 +00:00
|
|
|
using testing::AnyOf;
|
2015-02-20 09:54:49 +00:00
|
|
|
using testing::ElementsAre;
|
2015-01-16 11:04:01 +00:00
|
|
|
using testing::IsNull;
|
2015-03-19 15:44:29 +00:00
|
|
|
using testing::UnorderedElementsAre;
|
2015-01-16 11:04:01 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
typedef TestWithZone NodePropertiesTest;
|
|
|
|
|
|
|
|
|
2015-02-20 09:54:49 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const Operator kMockOperator(IrOpcode::kDead, Operator::kNoProperties,
|
|
|
|
"MockOperator", 0, 0, 0, 1, 0, 0);
|
|
|
|
const Operator kMockOpEffect(IrOpcode::kDead, Operator::kNoProperties,
|
|
|
|
"MockOpEffect", 0, 1, 0, 1, 1, 0);
|
|
|
|
const Operator kMockOpControl(IrOpcode::kDead, Operator::kNoProperties,
|
|
|
|
"MockOpControl", 0, 0, 1, 1, 0, 1);
|
2015-02-20 14:05:10 +00:00
|
|
|
const Operator kMockCallOperator(IrOpcode::kCall, Operator::kNoProperties,
|
|
|
|
"MockCallOperator", 0, 0, 0, 0, 0, 2);
|
2015-02-20 09:54:49 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(NodePropertiesTest, ReplaceWithValue_ValueUse) {
|
|
|
|
CommonOperatorBuilder common(zone());
|
|
|
|
Node* node = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
|
|
|
|
Node* use_value = Node::New(zone(), 0, common.Return(), 1, &node, false);
|
|
|
|
Node* replacement = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
|
|
|
|
NodeProperties::ReplaceWithValue(node, replacement);
|
|
|
|
EXPECT_EQ(replacement, use_value->InputAt(0));
|
|
|
|
EXPECT_EQ(0, node->UseCount());
|
|
|
|
EXPECT_EQ(1, replacement->UseCount());
|
|
|
|
EXPECT_THAT(replacement->uses(), ElementsAre(use_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(NodePropertiesTest, ReplaceWithValue_EffectUse) {
|
|
|
|
CommonOperatorBuilder common(zone());
|
|
|
|
Node* start = Node::New(zone(), 0, common.Start(1), 0, nullptr, false);
|
|
|
|
Node* node = Node::New(zone(), 0, &kMockOpEffect, 1, &start, false);
|
|
|
|
Node* use_effect = Node::New(zone(), 0, common.EffectPhi(1), 1, &node, false);
|
|
|
|
Node* replacement = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
|
|
|
|
NodeProperties::ReplaceWithValue(node, replacement);
|
|
|
|
EXPECT_EQ(start, use_effect->InputAt(0));
|
|
|
|
EXPECT_EQ(0, node->UseCount());
|
|
|
|
EXPECT_EQ(2, start->UseCount());
|
|
|
|
EXPECT_EQ(0, replacement->UseCount());
|
2015-03-19 15:44:29 +00:00
|
|
|
EXPECT_THAT(start->uses(), UnorderedElementsAre(use_effect, node));
|
2015-02-20 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(NodePropertiesTest, ReplaceWithValue_ControlUse) {
|
|
|
|
CommonOperatorBuilder common(zone());
|
|
|
|
Node* start = Node::New(zone(), 0, common.Start(1), 0, nullptr, false);
|
|
|
|
Node* node = Node::New(zone(), 0, &kMockOpControl, 1, &start, false);
|
|
|
|
Node* success = Node::New(zone(), 0, common.IfSuccess(), 1, &node, false);
|
|
|
|
Node* use_control = Node::New(zone(), 0, common.Merge(1), 1, &success, false);
|
|
|
|
Node* replacement = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
|
|
|
|
NodeProperties::ReplaceWithValue(node, replacement);
|
|
|
|
EXPECT_EQ(start, use_control->InputAt(0));
|
|
|
|
EXPECT_EQ(0, node->UseCount());
|
|
|
|
EXPECT_EQ(2, start->UseCount());
|
|
|
|
EXPECT_EQ(0, replacement->UseCount());
|
2015-03-19 15:44:29 +00:00
|
|
|
EXPECT_THAT(start->uses(), UnorderedElementsAre(use_control, node));
|
2015-02-20 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 11:04:01 +00:00
|
|
|
TEST_F(NodePropertiesTest, FindProjection) {
|
|
|
|
CommonOperatorBuilder common(zone());
|
|
|
|
Node* start = Node::New(zone(), 0, common.Start(1), 0, nullptr, false);
|
|
|
|
Node* proj0 = Node::New(zone(), 1, common.Projection(0), 1, &start, false);
|
|
|
|
Node* proj1 = Node::New(zone(), 2, common.Projection(1), 1, &start, false);
|
|
|
|
EXPECT_EQ(proj0, NodeProperties::FindProjection(start, 0));
|
|
|
|
EXPECT_EQ(proj1, NodeProperties::FindProjection(start, 1));
|
|
|
|
EXPECT_THAT(NodeProperties::FindProjection(start, 2), IsNull());
|
|
|
|
EXPECT_THAT(NodeProperties::FindProjection(start, 1234567890), IsNull());
|
|
|
|
}
|
|
|
|
|
2015-02-18 13:55:24 +00:00
|
|
|
|
|
|
|
TEST_F(NodePropertiesTest, CollectControlProjections_Branch) {
|
|
|
|
Node* result[2];
|
|
|
|
CommonOperatorBuilder common(zone());
|
|
|
|
Node* branch = Node::New(zone(), 1, common.Branch(), 0, nullptr, false);
|
|
|
|
Node* if_false = Node::New(zone(), 2, common.IfFalse(), 1, &branch, false);
|
|
|
|
Node* if_true = Node::New(zone(), 3, common.IfTrue(), 1, &branch, false);
|
|
|
|
NodeProperties::CollectControlProjections(branch, result, arraysize(result));
|
|
|
|
EXPECT_EQ(if_true, result[0]);
|
|
|
|
EXPECT_EQ(if_false, result[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 14:05:10 +00:00
|
|
|
TEST_F(NodePropertiesTest, CollectControlProjections_Call) {
|
|
|
|
Node* result[2];
|
|
|
|
CommonOperatorBuilder common(zone());
|
2015-05-28 13:22:48 +00:00
|
|
|
IfExceptionHint h = IfExceptionHint::kLocallyUncaught;
|
2015-02-20 14:05:10 +00:00
|
|
|
Node* call = Node::New(zone(), 1, &kMockCallOperator, 0, nullptr, false);
|
2015-05-28 13:22:48 +00:00
|
|
|
Node* if_ex = Node::New(zone(), 2, common.IfException(h), 1, &call, false);
|
2015-02-20 14:05:10 +00:00
|
|
|
Node* if_ok = Node::New(zone(), 3, common.IfSuccess(), 1, &call, false);
|
|
|
|
NodeProperties::CollectControlProjections(call, result, arraysize(result));
|
|
|
|
EXPECT_EQ(if_ok, result[0]);
|
|
|
|
EXPECT_EQ(if_ex, result[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-18 13:55:24 +00:00
|
|
|
TEST_F(NodePropertiesTest, CollectControlProjections_Switch) {
|
|
|
|
Node* result[3];
|
|
|
|
CommonOperatorBuilder common(zone());
|
|
|
|
Node* sw = Node::New(zone(), 1, common.Switch(3), 0, nullptr, false);
|
|
|
|
Node* if_default = Node::New(zone(), 2, common.IfDefault(), 1, &sw, false);
|
|
|
|
Node* if_value1 = Node::New(zone(), 3, common.IfValue(1), 1, &sw, false);
|
|
|
|
Node* if_value2 = Node::New(zone(), 4, common.IfValue(2), 1, &sw, false);
|
|
|
|
NodeProperties::CollectControlProjections(sw, result, arraysize(result));
|
|
|
|
EXPECT_THAT(result[0], AnyOf(if_value1, if_value2));
|
|
|
|
EXPECT_THAT(result[1], AnyOf(if_value1, if_value2));
|
|
|
|
EXPECT_EQ(if_default, result[2]);
|
|
|
|
}
|
|
|
|
|
2015-01-16 11:04:01 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|