1 /** 2 * Copyright (c) 2021-2022 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 COMPILER_OPTIMIZER_PASS_H 17 #define COMPILER_OPTIMIZER_PASS_H 18 19 #include <string> 20 #include "utils/bit_field.h" 21 22 namespace panda::compiler { 23 class Graph; 24 25 class Pass { 26 public: Pass(Graph * graph)27 explicit Pass(Graph *graph) : graph_(graph) {} 28 virtual ~Pass() = default; 29 30 /** 31 * Run pass execution. 32 * @return true if succeeded. 33 */ 34 virtual bool RunImpl() = 0; 35 36 virtual const char *GetPassName() const = 0; 37 AbortIfFailed()38 virtual bool AbortIfFailed() const 39 { 40 return false; 41 } 42 43 virtual bool ShouldDump() const = 0; 44 45 bool Run(); 46 GetGraph()47 Graph *GetGraph() const 48 { 49 return graph_; 50 } 51 IsAnalysis()52 bool IsAnalysis() const 53 { 54 return ReadField<IsAnalysisField>(); 55 } 56 IsValid()57 bool IsValid() const 58 { 59 return ReadField<IsValidField>(); 60 } 61 SetValid(bool value)62 void SetValid(bool value) 63 { 64 WriteField<IsValidField>(value); 65 } 66 67 template <typename T> ReadField()68 typename T::ValueType ReadField() const 69 { 70 return T::Decode(bit_fields_); 71 } 72 73 template <typename T> WriteField(typename T::ValueType v)74 void WriteField(typename T::ValueType v) 75 { 76 T::Set(v, &bit_fields_); 77 } 78 79 NO_MOVE_SEMANTIC(Pass); 80 NO_COPY_SEMANTIC(Pass); 81 82 protected: 83 using IsAnalysisField = BitField<bool, 0>; 84 using IsValidField = IsAnalysisField::NextFlag; 85 using LastField = IsValidField; 86 87 private: 88 Graph *graph_ {nullptr}; 89 uint32_t bit_fields_ {0}; 90 }; 91 92 class Optimization : public Pass { 93 public: 94 using Pass::Pass; IsEnable()95 virtual bool IsEnable() const 96 { 97 return true; 98 } 99 ShouldDump()100 bool ShouldDump() const override 101 { 102 return true; 103 } InvalidateAnalyses()104 virtual void InvalidateAnalyses() {} 105 }; 106 107 class Analysis : public Pass { 108 public: Analysis(Graph * graph)109 explicit Analysis(Graph *graph) : Pass(graph) 110 { 111 WriteField<IsAnalysisField>(true); 112 } 113 NO_MOVE_SEMANTIC(Analysis); 114 NO_COPY_SEMANTIC(Analysis); 115 ~Analysis() override = default; 116 117 /** 118 * Is the IR should be dumped after the pass finished. 119 */ ShouldDump()120 bool ShouldDump() const override 121 { 122 return false; 123 } 124 }; 125 } // namespace panda::compiler 126 127 #endif // COMPILER_OPTIMIZER_PASS_H 128