• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_TRY_CATCH_RESOLVING_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_TRY_CATCH_RESOLVING_H
18 
19 #include "optimizer/ir/graph.h"
20 #include "optimizer/pass.h"
21 
22 namespace ark::compiler {
23 class TryCatchResolving : public Optimization {
24 public:
25     explicit TryCatchResolving(Graph *graph);
26     NO_MOVE_SEMANTIC(TryCatchResolving);
27     NO_COPY_SEMANTIC(TryCatchResolving);
~TryCatchResolving()28     ~TryCatchResolving() override
29     {
30         tryBlocks_.clear();
31         throwInsts_.clear();
32         throwInsts0_.clear();
33         catchBlocks_.clear();
34         phiInsts_.clear();
35         cphi2phi_.clear();
36         catch2cphis_.clear();
37     }
38 
39     bool RunImpl() override;
40 
GetPassName()41     const char *GetPassName() const override
42     {
43         return "TryCatchResolving";
44     }
45     void InvalidateAnalyses() override;
46 
47 private:
48     void DeoptimizeIfs();
49     void CollectCandidates();
50     BasicBlock *FindCatchBeginBlock(BasicBlock *bb);
51     void VisitTryInst(TryInst *tryInst);
52     void ConnectThrowCatch();
53     void ConnectThrowCatchImpl(BasicBlock *catchBlock, BasicBlock *throwBlock, uint32_t catchPc, Inst *newObj,
54                                Inst *thr0w);
55     void DeleteTryCatchEdges(BasicBlock *tryBegin, BasicBlock *tryEnd);
56     void RemoveCatchPhis(BasicBlock *cphisBlock, BasicBlock *catchBlock, Inst *throwInst, Inst *phiInst);
57     void RemoveCatchPhisImpl(CatchPhiInst *catchPhi, BasicBlock *catchBlock, Inst *throwInst);
58 
59 private:
60     ArenaVector<BasicBlock *> tryBlocks_;
61     ArenaVector<Inst *> throwInsts_;
62     ArenaVector<Inst *> throwInsts0_;
63     ArenaMap<uint32_t, BasicBlock *> catchBlocks_;
64     ArenaMap<uint32_t, PhiInst *> phiInsts_;
65     ArenaMap<CatchPhiInst *, PhiInst *> cphi2phi_;
66     ArenaMap<BasicBlock *, BasicBlock *> catch2cphis_;
67 };
68 }  // namespace ark::compiler
69 
70 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_TRY_CATCH_RESOLVING_H
71