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