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