• 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_IF_MERGING_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_IF_MERGING_H
18 
19 #include <optional>
20 
21 #include "optimizer/ir/graph.h"
22 #include "optimizer/pass.h"
23 
24 namespace ark::compiler {
25 class IfMerging : public Optimization {
26 public:
27     PANDA_PUBLIC_API explicit IfMerging(Graph *graph);
28 
29     NO_MOVE_SEMANTIC(IfMerging);
30     NO_COPY_SEMANTIC(IfMerging);
31     ~IfMerging() override = default;
32 
33     bool RunImpl() override;
34 
IsEnable()35     bool IsEnable() const override
36     {
37         return g_options.IsCompilerIfMerging();
38     }
39 
GetPassName()40     const char *GetPassName() const override
41     {
42         return "IfMerging";
43     }
44 
45     void InvalidateAnalyses() override;
46 
47 private:
48     bool TrySimplifyConstantPhi(BasicBlock *block);
49     bool TryRemoveConstantPhiIf(BasicBlock *ifBlock);
50     IfImmInst *GetIfImm(BasicBlock *block);
51     bool TryMergeEquivalentIfs(BasicBlock *bb);
52     bool TryRemoveConstantPhiIf(IfImmInst *ifImm, PhiInst *phi, uint64_t constant, ConditionCode cc);
53     bool MarkInstBranches(BasicBlock *bb, BasicBlock *trueBb, BasicBlock *falseBb);
54     std::optional<bool> GetUserBranch(Inst *userInst, BasicBlock *bb, BasicBlock *trueBb, BasicBlock *falseBb);
55     bool IsDominateEdge(BasicBlock *edgeBb, BasicBlock *targetBb);
56     void SplitBlockWithEquivalentIf(BasicBlock *bb, BasicBlock *trueBb, bool invertedIf);
57     void SplitBlockWithConstantPhi(BasicBlock *bb, BasicBlock *trueBb, PhiInst *phi, uint64_t constant,
58                                    ConditionCode cc);
59     BasicBlock *SplitBlock(BasicBlock *bb);
60     void FixDominatorsTree(BasicBlock *trueBranchBb, BasicBlock *falseBranchBb);
61     void TryJoinSuccessorBlock(BasicBlock *bb);
62     void TryUpdateDominator(BasicBlock *bb);
63 
64 #ifndef NDEBUG
65     void CheckDomTreeValid();
66 #endif
67 
68 private:
69     bool isApplied_ {false};
70     Marker trueBranchMarker_ {};
71 };
72 }  // namespace ark::compiler
73 
74 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_IF_MERGING_H
75