• 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_OPTIMIZE_COMMON_H
17 #define MAPLEBE_INCLUDE_CG_OPTIMIZE_COMMON_H
18 #include "cgfunc.h"
19 
20 namespace maplebe {
21 inline const std::string kCfgoChaining = "red";
22 inline const std::string kCfgoSj = "burlywood1";
23 inline const std::string kCfgoFlipCond = "cadetblue1";
24 inline const std::string kCfgoAlways = "green";
25 inline const std::string kCfgoUnreach = "yellow";
26 inline const std::string kCfgoDup = "orange";
27 inline const std::string kCfgoEmpty = "purple";
28 inline const std::string kIcoIte = "blue"; /* if conversion optimization, if-then-else */
29 inline const std::string kIcoIt = "grey";  /* if conversion optimization, if-then-else */
30 
31 class OptimizationPattern {
32 public:
OptimizationPattern(CGFunc & func)33     explicit OptimizationPattern(CGFunc &func)
34         : patternName(func.GetMemoryPool()), cgFunc(&func), dotColor(func.GetMemoryPool())
35     {
36     }
37     virtual ~OptimizationPattern() = default;
38 
IsKeepPosition()39     bool IsKeepPosition() const
40     {
41         return keepPosition;
42     }
43 
SetKeepPosition(bool flag)44     void SetKeepPosition(bool flag)
45     {
46         keepPosition = flag;
47     }
48 
IsLabelInLSDAOrSwitchTable(LabelIdx label)49     bool IsLabelInLSDAOrSwitchTable(LabelIdx label) const
50     {
51         EHFunc *ehFunc = cgFunc->GetEHFunc();
52         return (ehFunc != nullptr && cgFunc->GetTheCFG()->InLSDA(label, *ehFunc)) ||
53                cgFunc->GetTheCFG()->InSwitchTable(label, *cgFunc);
54     }
55 
56     void Search2Op(bool noOptimize);
57     virtual bool Optimize(BB &curBB) = 0;
58 
59 protected:
60     void Log(uint32 bbID);
61 
62     bool keepPosition = false;
63     MapleString patternName;
64     CGFunc *cgFunc;
65     MapleString dotColor;
66     bool checkOnly = false;
67 };
68 
69 class Optimizer {
70 public:
Optimizer(CGFunc & func,MemPool & memPool)71     Optimizer(CGFunc &func, MemPool &memPool)
72         : cgFunc(&func),
73           name(nullptr),
74           memPool(&memPool),
75           alloc(&memPool),
76           diffPassPatterns(alloc.Adapter()),
77           singlePassPatterns(alloc.Adapter())
78     {
79         func.GetTheCFG()->InitInsnVisitor(func);
80     }
81 
82     virtual ~Optimizer() = default;
83     void Run(const std::string &funcName, bool checkOnly = false);
84     virtual void InitOptimizePatterns() = 0;
85 
86 protected:
87     CGFunc *cgFunc;
88     const char *name;
89     MemPool *memPool;
90     MapleAllocator alloc;
91     /* patterns need to run in different passes of cgFunc */
92     MapleVector<OptimizationPattern *> diffPassPatterns;
93     /* patterns can run in a single pass of cgFunc */
94     MapleVector<OptimizationPattern *> singlePassPatterns;
95 };
96 
97 class OptimizeLogger {
98 public:
GetLogger()99     static OptimizeLogger &GetLogger()
100     {
101         static OptimizeLogger instance;
102         return instance;
103     }
104 
105     void Log(const std::string &patternName);
106     void ClearLocal();
107     void Print(const std::string &funcName);
108 
109 private:
110     OptimizeLogger() = default;
111 
112     ~OptimizeLogger() = default;
113 
114     OptimizeLogger(const OptimizeLogger &);
115     OptimizeLogger &operator=(const OptimizeLogger &);
116 
117     std::map<const std::string, int> globalStat;
118     std::map<const std::string, int> localStat;
119 };
120 
121 class DotGenerator {
122 public:
123     static void SetColor(uint32 bbID, const std::string &color);
124     static void GenerateDot(const std::string &preFix, const CGFunc &cgFunc, const MIRModule &mod,
125                             bool includeEH = false, const std::string fname = "", regno_t vReg = 0);
126 
127 private:
128     static std::map<uint32, std::string> coloringMap;
129     static std::string GetFileName(const MIRModule &mirModule, const std::string &filePreFix);
130     static bool IsBackEdge(const CGFunc &cgFunction, const BB &from, const BB &to);
131     static void DumpEdge(const CGFunc &cgFunction, std::ofstream &cfgFileOfStream, bool isIncludeEH);
132     static void DumpBBInstructions(const CGFunc &cgFunction, regno_t vReg, std::ofstream &cfgFile);
133     static bool FoundListOpndRegNum(ListOperand &listOpnd, const Insn &insnObj, regno_t vReg);
134     static bool FoundMemAccessOpndRegNum(const MemOperand &memOperand, const Insn &insnObj, regno_t vReg);
135     static bool FoundNormalOpndRegNum(const RegOperand &regOpnd, const Insn &insnObj, regno_t vReg);
136 };
137 } /* namespace maplebe */
138 
139 #endif /* MAPLEBE_INCLUDE_CG_OPTIMIZE_COMMON_H */
140