• 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 MAPLEBE_INCLUDE_CG_CFGO_H
17 #define MAPLEBE_INCLUDE_CG_CFGO_H
18 #include "cg_cfg.h"
19 #include "optimize_common.h"
20 
21 namespace maplebe {
22 class ChainingPattern : public OptimizationPattern {
23 public:
ChainingPattern(CGFunc & func)24     explicit ChainingPattern(CGFunc &func) : OptimizationPattern(func)
25     {
26         patternName = "BB Chaining";
27         dotColor = kCfgoChaining;
28     }
29 
30     virtual ~ChainingPattern() = default;
31     bool Optimize(BB &curBB) override;
32 
33 protected:
34     bool NoInsnBetween(const BB &from, const BB &to) const;
35     bool DoSameThing(const BB &bb1, const Insn &last1, const BB &bb2, const Insn &last2) const;
36     bool MergeFallthuBB(BB &curBB);
37     bool MergeGotoBB(BB &curBB, BB &sucBB);
38     bool MoveSuccBBAsCurBBNext(BB &curBB, BB &sucBB);
39     bool RemoveGotoInsn(BB &curBB, BB &sucBB);
40     bool ClearCurBBAndResetTargetBB(BB &curBB, BB &sucBB);
41 };
42 
43 class SequentialJumpPattern : public OptimizationPattern {
44 public:
SequentialJumpPattern(CGFunc & func)45     explicit SequentialJumpPattern(CGFunc &func) : OptimizationPattern(func)
46     {
47         patternName = "Sequential Jump";
48         dotColor = kCfgoSj;
49     }
50 
51     virtual ~SequentialJumpPattern() = default;
52     bool Optimize(BB &curBB) override;
53 
54 protected:
55     void SkipSucBB(BB &curBB, BB &sucBB);
56     void UpdateSwitchSucc(BB &curBB, BB &sucBB);
57 };
58 
59 class FlipBRPattern : public OptimizationPattern {
60 public:
FlipBRPattern(CGFunc & func)61     explicit FlipBRPattern(CGFunc &func) : OptimizationPattern(func)
62     {
63         patternName = "Condition Flip";
64         dotColor = kCfgoFlipCond;
65     }
66 
67     virtual ~FlipBRPattern() = default;
68     bool Optimize(BB &curBB) override;
69 
70 protected:
71     void RelocateThrowBB(BB &curBB);
72 
73 private:
74     virtual uint32 GetJumpTargetIdx(const Insn &insn) = 0;
75     virtual MOperator FlipConditionOp(MOperator flippedOp) = 0;
76 };
77 
78 /* This class represents the scenario that the BB is unreachable. */
79 class UnreachBBPattern : public OptimizationPattern {
80 public:
UnreachBBPattern(CGFunc & func)81     explicit UnreachBBPattern(CGFunc &func) : OptimizationPattern(func)
82     {
83         patternName = "Unreachable BB";
84         dotColor = kCfgoUnreach;
85         func.GetTheCFG()->FindAndMarkUnreachable(*cgFunc);
86     }
87 
88     virtual ~UnreachBBPattern() = default;
89     bool Optimize(BB &curBB) override;
90 };
91 
92 /*
93  * This class represents the scenario that a common jump BB can be duplicated
94  * to one of its another predecessor.
95  */
96 class DuplicateBBPattern : public OptimizationPattern {
97 public:
DuplicateBBPattern(CGFunc & func)98     explicit DuplicateBBPattern(CGFunc &func) : OptimizationPattern(func)
99     {
100         patternName = "Duplicate BB";
101         dotColor = kCfgoDup;
102     }
103 
104     virtual ~DuplicateBBPattern() = default;
105     bool Optimize(BB &curBB) override;
106 
107 private:
108     static constexpr int kThreshold = 10;
109 };
110 
111 /*
112  * This class represents the scenario that a BB contains nothing.
113  */
114 class EmptyBBPattern : public OptimizationPattern {
115 public:
EmptyBBPattern(CGFunc & func)116     explicit EmptyBBPattern(CGFunc &func) : OptimizationPattern(func)
117     {
118         patternName = "Empty BB";
119         dotColor = kCfgoEmpty;
120     }
121 
122     virtual ~EmptyBBPattern() = default;
123     bool Optimize(BB &curBB) override;
124 };
125 
126 class CFGOptimizer : public Optimizer {
127 public:
CFGOptimizer(CGFunc & func,MemPool & memPool)128     CFGOptimizer(CGFunc &func, MemPool &memPool) : Optimizer(func, memPool)
129     {
130         name = "CFGO";
131     }
132 
133     virtual ~CFGOptimizer() = default;
134 };
135 
136 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgCfgo, maplebe::CGFunc)
137 MAPLE_FUNC_PHASE_DECLARE_END
138 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPostCfgo, maplebe::CGFunc)
139 MAPLE_FUNC_PHASE_DECLARE_END
140 } /* namespace maplebe */
141 
142 #endif /* MAPLEBE_INCLUDE_CG_CFGO_H */
143