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 IsLabelInSwitchTable(LabelIdx label)51 bool IsLabelInSwitchTable(LabelIdx label) const 52 { 53 return CGCFG::InSwitchTable(label, *cgFunc); 54 } 55 56 bool Search2Op(bool noOptimize); InitPattern()57 virtual void InitPattern() {} 58 virtual bool Optimize(BB &curBB) = 0; 59 60 protected: 61 void Log(uint32 bbID); 62 63 bool keepPosition = false; 64 MapleString patternName; 65 CGFunc *cgFunc; 66 MapleString dotColor; 67 bool checkOnly = false; 68 }; 69 70 class Optimizer { 71 public: Optimizer(CGFunc & func,MemPool & memPool)72 Optimizer(CGFunc &func, MemPool &memPool) 73 : cgFunc(&func), 74 name(nullptr), 75 memPool(&memPool), 76 alloc(&memPool), 77 diffPassPatterns(alloc.Adapter()), 78 singlePassPatterns(alloc.Adapter()) 79 { 80 func.GetTheCFG()->InitInsnVisitor(func); 81 } 82 83 virtual ~Optimizer() = default; 84 void Run(const std::string &funcName, bool checkOnly = false); 85 virtual void InitOptimizePatterns() = 0; 86 87 protected: 88 CGFunc *cgFunc; 89 const char *name; 90 MemPool *memPool; 91 MapleAllocator alloc; 92 /* patterns need to run in different passes of cgFunc */ 93 MapleVector<OptimizationPattern *> diffPassPatterns; 94 /* patterns can run in a single pass of cgFunc */ 95 MapleVector<OptimizationPattern *> singlePassPatterns; 96 }; 97 98 class OptimizeLogger { 99 public: GetLogger()100 static OptimizeLogger &GetLogger() 101 { 102 static OptimizeLogger instance; 103 return instance; 104 } 105 106 void Log(const std::string &patternName); 107 void ClearLocal(); 108 void Print(const std::string &funcName); 109 110 private: 111 OptimizeLogger() = default; 112 113 ~OptimizeLogger() = default; 114 115 OptimizeLogger(const OptimizeLogger &); 116 OptimizeLogger &operator=(const OptimizeLogger &); 117 118 std::map<const std::string, int> globalStat; 119 std::map<const std::string, int> localStat; 120 }; 121 122 class DotGenerator { 123 public: 124 static void SetColor(uint32 bbID, const std::string &color); 125 static void GenerateDot(const std::string &preFix, const CGFunc &cgFunc, const MIRModule &mod, 126 const std::string fname = "", regno_t vReg = 0); 127 128 private: 129 static std::map<uint32, std::string> coloringMap; 130 static std::string GetFileName(const MIRModule &mirModule, const std::string &filePreFix); 131 static void DumpEdge(const CGFunc &cgFunction, std::ofstream &cfgFileOfStream); 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 ®Opnd, const Insn &insnObj, regno_t vReg); 136 }; 137 } /* namespace maplebe */ 138 139 #endif /* MAPLEBE_INCLUDE_CG_OPTIMIZE_COMMON_H */ 140