• 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/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 {
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 PassVisitor {
61 public:
EarlyElimination(Circuit * circuit,RPOVisitor * visitor,Chunk * chunk)62     EarlyElimination(Circuit* circuit, RPOVisitor* visitor, Chunk* chunk)
63         : PassVisitor(circuit, chunk, visitor), dependChains_(chunk), renames_(chunk) {}
64 
65     ~EarlyElimination() = default;
66 
67     void Initialize() override;
68     GateRef VisitGate(GateRef gate) override;
69     bool CheckReplacement(GateRef lhs, GateRef rhs);
70     bool CheckRenameReplacement(GateRef lhs, GateRef rhs);
71     bool MayAccessOneMemory(GateRef lhs, GateRef rhs);
72     bool CompareOrder(GateRef lhs, GateRef rhs);
73 private:
74 
GetDependChain(GateRef dependIn)75     DependInfoNode* GetDependChain(GateRef dependIn)
76     {
77         size_t idx = acc_.GetId(dependIn);
78         ASSERT(idx <= circuit_->GetMaxGateId());
79         return dependChains_[idx];
80     }
81 
82     GateRef VisitDependEntry(GateRef gate);
83     GateRef UpdateDependChain(GateRef gate, DependInfoNode* dependInfo);
84     DependInfoNode* UpdateWrite(GateRef gate, DependInfoNode* dependInfo);
85     GateRef TryEliminateGate(GateRef gate);
86     GateRef TryEliminateFrameState(GateRef gate);
87     GateRef TryEliminateOther(GateRef gate);
88     GateRef TryEliminateDependSelector(GateRef gate);
89     DependInfoNode* GetLoopDependInfo(GateRef depend);
90     GateRef Rename(GateRef gate);
91 
92     ChunkVector<DependInfoNode*> dependChains_;
93     ChunkVector<GateRef> renames_;
94 };
95 }  // panda::ecmascript::kungfu
96 #endif  // ECMASCRIPT_COMPILER_EARLY_ELIMINATION_H
97