1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_COMPILER_EARLY_ELIMINATION_H
17 #define ECMASCRIPT_COMPILER_EARLY_ELIMINATION_H
18 
19 #include "ecmascript/compiler/circuit_builder.h"
20 #include "ecmascript/compiler/combined_pass_visitor.h"
21 #include "ecmascript/compiler/gate_accessor.h"
22 #include "ecmascript/mem/chunk_containers.h"
23 
24 namespace panda::ecmascript::kungfu {
25 class EarlyElimination;
26 
27 class DependInfoNode : public ChunkObject {
28 public:
DependInfoNode(Chunk* chunk)29     DependInfoNode(Chunk* chunk) : chunk_(chunk) {}
30     ~DependInfoNode() = default;
31 
32     GateRef LookupFrameState() const;
33     GateRef LookupNode(EarlyElimination* elimination, GateRef gate);
34     GateRef LookupCheckedNode(EarlyElimination* elimination, GateRef gate);
35     DependInfoNode* UpdateNode(GateRef gate);
36     DependInfoNode* UpdateFrameState(GateRef framestate);
37     DependInfoNode* UpdateStoreProperty(EarlyElimination* elimination, GateRef gate);
38     bool Equals(DependInfoNode* that);
39     void Merge(EarlyElimination* elimination, DependInfoNode* that);
40     void GetGates(std::vector<GateRef>& gates) const;
CopyFrom(DependInfoNode *other)41     void CopyFrom(DependInfoNode *other)
42     {
43         head_ = other->head_;
44         size_ = other->size_;
45         frameState_ = other->frameState_;
46     }
47 private:
48     struct Node {
Nodepanda::ecmascript::kungfu::DependInfoNode::Node49         Node(GateRef gate, Node* next) : gate(gate), next(next) {}
50         GateRef gate;
51         Node *next;
52     };
53 
54     GateRef frameState_ {Circuit::NullGate()};
55     Node *head_{nullptr};
56     size_t size_ {0};
57     Chunk* chunk_;
58 };
59 
60 class EarlyElimination : public PassVisitor {
61 public:
EarlyElimination(Circuit* circuit, RPOVisitor* visitor, Chunk* chunk, bool enableMemoryAnalysis, bool enableFrameStateElimination)62     EarlyElimination(Circuit* circuit, RPOVisitor* visitor, Chunk* chunk,
63         bool enableMemoryAnalysis, bool enableFrameStateElimination)
64         : PassVisitor(circuit, chunk, visitor), dependChains_(chunk), renames_(chunk),
65           enableMemoryAnalysis_(enableMemoryAnalysis),
66           enableFrameStateElimination_(enableFrameStateElimination) {}
67 
68     ~EarlyElimination() = default;
69 
70     void Initialize() override;
71     GateRef VisitGate(GateRef gate) override;
72     bool CheckReplacement(GateRef lhs, GateRef rhs);
73     bool CheckRenameReplacement(GateRef lhs, GateRef rhs);
74     bool MayAccessOneMemory(GateRef lhs, GateRef rhs);
75     bool CompareOrder(GateRef lhs, GateRef rhs);
76 private:
77 
GetDependChain(GateRef dependIn)78     DependInfoNode* GetDependChain(GateRef dependIn)
79     {
80         size_t idx = acc_.GetId(dependIn);
81         ASSERT(idx <= circuit_->GetMaxGateId());
82         return dependChains_[idx];
83     }
84 
85     GateRef VisitDependEntry(GateRef gate);
86     GateRef UpdateDependChain(GateRef gate, DependInfoNode* dependInfo);
87     DependInfoNode* UpdateWrite(GateRef gate, DependInfoNode* dependInfo);
88     GateRef TryEliminateGate(GateRef gate);
89     GateRef TryEliminateFrameState(GateRef gate);
90     GateRef TryEliminateOther(GateRef gate);
91     GateRef TryEliminateDependSelector(GateRef gate);
92     DependInfoNode* GetLoopDependInfo(GateRef depend);
93     GateRef Rename(GateRef gate);
94 
95     ChunkVector<DependInfoNode*> dependChains_;
96     ChunkVector<GateRef> renames_;
97     bool enableMemoryAnalysis_ {true};
98     bool enableFrameStateElimination_ {true};
99 };
100 }  // panda::ecmascript::kungfu
101 #endif  // ECMASCRIPT_COMPILER_EARLY_ELIMINATION_H
102