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 MAPLE_PHASE_INCLUDE_PHASE_IMPL_H 17 #define MAPLE_PHASE_INCLUDE_PHASE_IMPL_H 18 #include "class_hierarchy_phase.h" 19 #include "mir_builder.h" 20 #include "mpl_scheduler.h" 21 #include "utils.h" 22 23 namespace maple { 24 class FuncOptimizeImpl : public MplTaskParam { 25 public: 26 FuncOptimizeImpl(MIRModule &mod, KlassHierarchy *kh = nullptr, bool trace = false); 27 virtual ~FuncOptimizeImpl(); 28 // Each phase needs to implement its own Clone 29 virtual FuncOptimizeImpl *Clone() = 0; GetMIRModule()30 MIRModule &GetMIRModule() 31 { 32 return *module; 33 } 34 GetMIRModule()35 const MIRModule &GetMIRModule() const 36 { 37 return *module; 38 } 39 SetDump(bool dumpFunc)40 void SetDump(bool dumpFunc) 41 { 42 dump = dumpFunc; 43 } 44 45 virtual void CreateLocalBuilder(pthread_mutex_t &mtx); 46 virtual void ProcessFunc(MIRFunction *func); Finish()47 virtual void Finish() {} 48 49 protected: SetCurrentFunction(MIRFunction & func)50 void SetCurrentFunction(MIRFunction &func) 51 { 52 currFunc = &func; 53 DEBUG_ASSERT(builder != nullptr, "builder is null in FuncOptimizeImpl::SetCurrentFunction"); 54 builder->SetCurrentFunction(func); 55 module->SetCurFunction(&func); 56 } 57 SetCurrentBlock(BlockNode & block)58 void SetCurrentBlock(BlockNode &block) 59 { 60 currBlock = █ 61 } 62 63 virtual void ProcessBlock(StmtNode &stmt); 64 // Each phase needs to implement its own ProcessStmt ProcessStmt(StmtNode &)65 virtual void ProcessStmt(StmtNode &) {} 66 67 KlassHierarchy *klassHierarchy = nullptr; 68 MIRFunction *currFunc = nullptr; 69 BlockNode *currBlock = nullptr; 70 MIRBuilderExt *builder = nullptr; 71 bool trace = false; 72 bool dump = false; 73 74 private: 75 MIRModule *module = nullptr; 76 }; 77 78 class FuncOptimizeIterator : public MplScheduler { 79 public: 80 class Task : public MplTask { 81 public: Task(MIRFunction & func)82 explicit Task(MIRFunction &func) : function(&func) {} 83 84 ~Task() = default; 85 86 protected: RunImpl(MplTaskParam * param)87 int RunImpl(MplTaskParam *param) override 88 { 89 auto &impl = static_cast<FuncOptimizeImpl &>(utils::ToRef(param)); 90 impl.ProcessFunc(function); 91 return 0; 92 } 93 FinishImpl(MplTaskParam *)94 int FinishImpl(MplTaskParam *) override 95 { 96 return 0; 97 } 98 99 private: 100 MIRFunction *function; 101 }; 102 103 FuncOptimizeIterator(const std::string &phaseName, std::unique_ptr<FuncOptimizeImpl> phaseImpl); 104 virtual ~FuncOptimizeIterator(); 105 virtual void Run(uint32 threadNum = 1, bool isSeq = false); 106 107 protected: 108 thread_local static FuncOptimizeImpl *phaseImplLocal; 109 void RunSerial(); 110 void RunParallel(uint32 threadNum, bool isSeq = false); CallbackGetTaskRunParam()111 virtual MplTaskParam *CallbackGetTaskRunParam() const 112 { 113 return phaseImplLocal; 114 } 115 CallbackGetTaskFinishParam()116 virtual MplTaskParam *CallbackGetTaskFinishParam() const 117 { 118 return phaseImplLocal; 119 } 120 CallbackThreadMainStart()121 virtual void CallbackThreadMainStart() 122 { 123 phaseImplLocal = phaseImpl->Clone(); 124 utils::ToRef(phaseImplLocal).CreateLocalBuilder(mutexGlobal); 125 } 126 CallbackThreadMainEnd()127 virtual void CallbackThreadMainEnd() 128 { 129 delete phaseImplLocal; 130 phaseImplLocal = nullptr; 131 } 132 133 private: 134 bool mplDumpTime = false; 135 std::unique_ptr<FuncOptimizeImpl> phaseImpl; 136 std::vector<std::unique_ptr<MplTask>> tasksUniquePtr; 137 }; 138 139 #define OPT_TEMPLATE(OPT_NAME) \ 140 auto *kh = static_cast<KlassHierarchy *>(mrm->GetAnalysisResult(MoPhase_CHA, mod)); \ 141 ASSERT_NOT_NULL(kh); \ 142 std::unique_ptr<FuncOptimizeImpl> funcOptImpl = std::make_unique<OPT_NAME>(*mod, kh, TRACE_PHASE); \ 143 ASSERT_NOT_NULL(funcOptImpl); \ 144 FuncOptimizeIterator opt(PhaseName(), std::move(funcOptImpl)); \ 145 opt.Init(); \ 146 opt.Run(); 147 148 #define OPT_TEMPLATE_NEWPM(OPT_NAME, PHASEKEY) \ 149 auto *kh = GET_ANALYSIS(M2MKlassHierarchy, PHASEKEY); \ 150 ASSERT_NOT_NULL((kh)); \ 151 std::unique_ptr<FuncOptimizeImpl> funcOptImpl = std::make_unique<OPT_NAME>(m, (kh), TRACE_MAPLE_PHASE); \ 152 ASSERT_NOT_NULL(funcOptImpl); \ 153 FuncOptimizeIterator opt(PhaseName(), std::move(funcOptImpl)); \ 154 opt.Init(); \ 155 opt.Run(); 156 } // namespace maple 157 #endif // MAPLE_PHASE_INCLUDE_PHASE_IMPL_H 158