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_LATER_ELIMINATION_H
17 #define ECMASCRIPT_COMPILER_LATER_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/compiler/base/depend_chain_helper.h"
23 #include "ecmascript/mem/chunk_containers.h"
24 
25 namespace panda::ecmascript::kungfu {
26 class DependChains;
27 class LaterElimination : public PassVisitor {
28 public:
LaterElimination(Circuit* circuit, RPOVisitor* visitor, Chunk* chunk)29     LaterElimination(Circuit* circuit, RPOVisitor* visitor, Chunk* chunk)
30         : PassVisitor(circuit, chunk, visitor), dependChains_(chunk) {}
31 
32     ~LaterElimination() = default;
33 
34     void Initialize() override;
35     GateRef VisitGate(GateRef gate) override;
36     bool CheckReplacement(GateRef lhs, GateRef rhs);
37 private:
38 
GetDependChain(GateRef dependIn)39     DependChains* GetDependChain(GateRef dependIn)
40     {
41         size_t idx = acc_.GetId(dependIn);
42         ASSERT(idx <= circuit_->GetMaxGateId());
43         return dependChains_[idx];
44     }
45 
LookupNode(DependChains* dependChain, GateRef gate)46     GateRef LookupNode(DependChains* dependChain, GateRef gate)
47     {
48         for (auto iter = dependChain->begin(); iter != dependChain->end(); ++iter) {
49             GateRef curGate = iter.GetCurrentGate();
50             if (CheckReplacement(curGate, gate)) {
51                 return curGate;
52             }
53         }
54         return Circuit::NullGate();
55     }
56 
57     GateRef VisitDependEntry(GateRef gate);
58     GateRef UpdateDependChain(GateRef gate, DependChains* dependInfo);
59     GateRef TryEliminateGate(GateRef gate);
60     GateRef TryEliminateOther(GateRef gate);
61     GateRef TryEliminateDependSelector(GateRef gate);
62 
63     ChunkVector<DependChains*> dependChains_;
64 };
65 }  // panda::ecmascript::kungfu
66 #endif  // ECMASCRIPT_COMPILER_LATER_ELIMINATION_H