• 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_CG_CFG_H
17 #define MAPLEBE_INCLUDE_CG_CG_CFG_H
18 #include "cgbb.h"
19 
20 namespace maplebe {
21 class InsnVisitor {
22 public:
InsnVisitor(CGFunc & func)23     explicit InsnVisitor(CGFunc &func) : cgFunc(&func) {}
24 
25     virtual ~InsnVisitor() = default;
GetCGFunc()26     CGFunc *GetCGFunc() const
27     {
28         return cgFunc;
29     }
30 
31     /*
32      * Precondition:
33      * The last instruction in bb is either conditional or unconditional jump.
34      *
35      * The jump target of bb is modified to the location specified by targetLabel.
36      */
37     virtual void ModifyJumpTarget(LabelIdx targetLabel, BB &bb) = 0;
38 
39     /*
40      * Precondition:
41      * The last instruction in bb is either conditional or unconditional jump.
42      *
43      * The jump target of bb is modified to the location specified by targetOperand.
44      */
45     virtual void ModifyJumpTarget(Operand &targetOperand, BB &bb) = 0;
46 
47     /*
48      * Precondition:
49      * The last instruction in bb is either a conditional or an unconditional jump.
50      * The last instruction in newTarget is an unconditional jump.
51      *
52      * The jump target of bb is modified to newTarget's jump target.
53      */
54     virtual LabelIdx GetJumpLabel(const Insn &insn) const = 0;
55 private:
56     CGFunc *cgFunc;
57 }; /* class InsnVisitor; */
58 
59 class CGCFG {
60 public:
CGCFG(CGFunc & cgFunc)61     explicit CGCFG(CGFunc &cgFunc) : cgFunc(&cgFunc) {}
62 
63     ~CGCFG() = default;
64 
65     void BuildCFG();
66     void CheckCFG();
67 
68     void InitInsnVisitor(CGFunc &func) const;
GetInsnModifier()69     InsnVisitor *GetInsnModifier() const
70     {
71         return insnVisitor;
72     }
73 
74     static bool AreCommentAllPreds(const BB &bb);
75     bool CanMerge(const BB &merger, const BB &mergee) const;
76     bool BBJudge(const BB &first, const BB &second) const;
77     /*
78      * Merge all instructions in mergee into merger, each BB's successors and
79      * predecessors should be modified accordingly.
80      */
81     static void MergeBB(BB &merger, BB &mergee, CGFunc &func);
82 
83     /*
84      * Remove a BB from its position in the CFG.
85      * Prev, next, preds and sucs are all modified accordingly.
86      */
87     void RemoveBB(BB &curBB, bool isGotoIf = false) const;
88 
89     /*
90      * Update the preds of CommonExitBB after changing cfg,
91      * We'd better do it once after cfgo opt
92      */
93     void UpdateCommonExitBBInfo();
94 
95     static bool InSwitchTable(LabelIdx label, const CGFunc &func);
96 
97     static BB *GetTargetSuc(BB &curBB, bool branchOnly = false, bool isGotoIf = false);
98 
99     static void FindAndMarkUnreachable(CGFunc &func);
100     void FlushUnReachableStatusAndRemoveRelations(BB &bb, const CGFunc &func) const;
101     void UnreachCodeAnalysis() const;
102     void FindWillExitBBs(BB *bb, std::set<BB *, BBIdCmp> *visitedBBs);
103     void WontExitAnalysis();
104 private:
105     CGFunc *cgFunc = nullptr;
106     static InsnVisitor *insnVisitor;
107     static void MergeBB(BB &merger, BB &mergee);
108 }; /* class CGCFG */
109 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgHandleCFG, maplebe::CGFunc)
110 OVERRIDE_DEPENDENCE
111 MAPLE_FUNC_PHASE_DECLARE_END
112 } /* namespace maplebe */
113 
114 #endif /* MAPLEBE_INCLUDE_CG_CG_CFG_H */
115