summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/compiler/load-elimination-unittest.cc
blob: 3ad11cf43fd924a5d6f514bc5503f7184fc626b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2014 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/access-builder.h"
#include "src/compiler/load-elimination.h"
#include "src/compiler/simplified-operator.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"

namespace v8 {
namespace internal {
namespace compiler {

class LoadEliminationTest : public GraphTest {
 public:
  LoadEliminationTest() : GraphTest(3), simplified_(zone()) {}
  ~LoadEliminationTest() override {}

 protected:
  Reduction Reduce(Node* node) {
    // TODO(titzer): mock the GraphReducer here for better unit testing.
    GraphReducer graph_reducer(zone(), graph());
    LoadElimination reducer(&graph_reducer);
    return reducer.Reduce(node);
  }

  SimplifiedOperatorBuilder* simplified() { return &simplified_; }

 private:
  SimplifiedOperatorBuilder simplified_;
};


TEST_F(LoadEliminationTest, LoadFieldWithStoreField) {
  Node* object1 = Parameter(0);
  Node* object2 = Parameter(1);
  Node* value = Parameter(2);
  Node* effect = graph()->start();
  Node* control = graph()->start();

  FieldAccess access1 = AccessBuilder::ForContextSlot(42);
  Node* store1 = graph()->NewNode(simplified()->StoreField(access1), object1,
                                  value, effect, control);
  Reduction r1 = Reduce(graph()->NewNode(simplified()->LoadField(access1),
                                         object1, store1, control));
  ASSERT_TRUE(r1.Changed());
  EXPECT_EQ(value, r1.replacement());

  FieldAccess access2 = AccessBuilder::ForMap();
  Node* store2 = graph()->NewNode(simplified()->StoreField(access2), object1,
                                  object2, store1, control);
  Reduction r2 = Reduce(graph()->NewNode(simplified()->LoadField(access2),
                                         object1, store2, control));
  ASSERT_TRUE(r2.Changed());
  EXPECT_EQ(object2, r2.replacement());

  Node* store3 = graph()->NewNode(
      simplified()->StoreBuffer(BufferAccess(kExternalInt8Array)), object2,
      value, Int32Constant(10), object1, store2, control);

  Reduction r3 = Reduce(graph()->NewNode(simplified()->LoadField(access1),
                                         object2, store3, control));
  ASSERT_FALSE(r3.Changed());

  Reduction r4 = Reduce(graph()->NewNode(simplified()->LoadField(access1),
                                         object1, store3, control));
  ASSERT_TRUE(r4.Changed());
  EXPECT_EQ(value, r4.replacement());
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8