1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/compiler/access-builder.h"
6 #include "src/compiler/load-elimination.h"
7 #include "src/compiler/simplified-operator.h"
8 #include "test/unittests/compiler/graph-unittest.h"
9 #include "test/unittests/compiler/node-test-utils.h"
10
11 namespace v8 {
12 namespace internal {
13 namespace compiler {
14
15 class LoadEliminationTest : public GraphTest {
16 public:
LoadEliminationTest()17 LoadEliminationTest() : GraphTest(3), simplified_(zone()) {}
~LoadEliminationTest()18 ~LoadEliminationTest() override {}
19
20 protected:
Reduce(Node * node)21 Reduction Reduce(Node* node) {
22 // TODO(titzer): mock the GraphReducer here for better unit testing.
23 GraphReducer graph_reducer(zone(), graph());
24 LoadElimination reducer(&graph_reducer);
25 return reducer.Reduce(node);
26 }
27
simplified()28 SimplifiedOperatorBuilder* simplified() { return &simplified_; }
29
30 private:
31 SimplifiedOperatorBuilder simplified_;
32 };
33
34
TEST_F(LoadEliminationTest,LoadFieldWithStoreField)35 TEST_F(LoadEliminationTest, LoadFieldWithStoreField) {
36 Node* object1 = Parameter(0);
37 Node* object2 = Parameter(1);
38 Node* value = Parameter(2);
39 Node* effect = graph()->start();
40 Node* control = graph()->start();
41
42 FieldAccess access1 = AccessBuilder::ForContextSlot(42);
43 Node* store1 = graph()->NewNode(simplified()->StoreField(access1), object1,
44 value, effect, control);
45 Reduction r1 = Reduce(graph()->NewNode(simplified()->LoadField(access1),
46 object1, store1, control));
47 ASSERT_TRUE(r1.Changed());
48 EXPECT_EQ(value, r1.replacement());
49
50 FieldAccess access2 = AccessBuilder::ForMap();
51 Node* store2 = graph()->NewNode(simplified()->StoreField(access2), object1,
52 object2, store1, control);
53 Reduction r2 = Reduce(graph()->NewNode(simplified()->LoadField(access2),
54 object1, store2, control));
55 ASSERT_TRUE(r2.Changed());
56 EXPECT_EQ(object2, r2.replacement());
57
58 Node* store3 = graph()->NewNode(
59 simplified()->StoreBuffer(BufferAccess(kExternalInt8Array)), object2,
60 value, Int32Constant(10), object1, store2, control);
61
62 Reduction r3 = Reduce(graph()->NewNode(simplified()->LoadField(access1),
63 object2, store3, control));
64 ASSERT_FALSE(r3.Changed());
65
66 Reduction r4 = Reduce(graph()->NewNode(simplified()->LoadField(access1),
67 object1, store3, control));
68 ASSERT_TRUE(r4.Changed());
69 EXPECT_EQ(value, r4.replacement());
70 }
71
72 } // namespace compiler
73 } // namespace internal
74 } // namespace v8
75