• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gate_accessor.h"
21 #include "ecmascript/compiler/graph_visitor.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 {
NodeNode49         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 GraphVisitor {
61 public:
EarlyElimination(Circuit * circuit,bool enableLog,const std::string & name,Chunk * chunk)62     EarlyElimination(Circuit *circuit, bool enableLog, const std::string& name, Chunk* chunk)
63         : GraphVisitor(circuit, chunk), enableLog_(enableLog),
64         methodName_(name), dependChains_(chunk), renames_(chunk) {}
65 
66     ~EarlyElimination() = default;
67 
68     void Run();
69 
70     GateRef VisitGate(GateRef gate) override;
71     bool CheckReplacement(GateRef lhs, GateRef rhs);
72     bool CheckRenameReplacement(GateRef lhs, GateRef rhs);
73     bool MayAccessOneMemory(GateRef lhs, GateRef rhs);
74     bool CompareOrder(GateRef lhs, GateRef rhs);
75 private:
IsLogEnabled()76     bool IsLogEnabled() const
77     {
78         return enableLog_;
79     }
80 
GetMethodName()81     const std::string& GetMethodName() const
82     {
83         return methodName_;
84     }
85 
GetDependChain(GateRef dependIn)86     DependInfoNode* GetDependChain(GateRef dependIn)
87     {
88         size_t idx = acc_.GetId(dependIn);
89         ASSERT(idx <= circuit_->GetMaxGateId());
90         return dependChains_[idx];
91     }
92 
93     GateRef VisitDependEntry(GateRef gate);
94     GateRef UpdateDependChain(GateRef gate, DependInfoNode* dependInfo);
95     DependInfoNode* UpdateWrite(GateRef gate, DependInfoNode* dependInfo);
96     GateRef TryEliminateGate(GateRef gate);
97     GateRef TryEliminateFrameState(GateRef gate);
98     GateRef TryEliminateOther(GateRef gate);
99     GateRef TryEliminateDependSelector(GateRef gate);
100     DependInfoNode* GetLoopDependInfo(GateRef depend);
101     GateRef Rename(GateRef gate);
102 
103     bool enableLog_ {false};
104     std::string methodName_;
105     ChunkVector<DependInfoNode*> dependChains_;
106     ChunkVector<GateRef> renames_;
107 };
108 }  // panda::ecmascript::kungfu
109 #endif  // ECMASCRIPT_COMPILER_EARLY_ELIMINATION_H
110