1 /* 2 * Copyright (c) 2021-2024 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_ANALYSIS_IF_CONVERSION_H 17 #define COMPILER_OPTIMIZER_ANALYSIS_IF_CONVERSION_H 18 19 #include "optimizer/ir/graph.h" 20 #include "utils/arena_containers.h" 21 22 namespace ark::compiler { 23 class BasicBlock; 24 class Graph; 25 26 class IfConversion : public Optimization { 27 public: Optimization(graph)28 explicit IfConversion(Graph *graph, uint32_t limit = 2) : Optimization(graph), limit_(limit) 29 { 30 canEncodeFloatSelect_ = graph->GetEncoder()->CanEncodeFloatSelect(); 31 } 32 33 NO_MOVE_SEMANTIC(IfConversion); 34 NO_COPY_SEMANTIC(IfConversion); 35 ~IfConversion() override = default; 36 37 bool RunImpl() override; 38 IsEnable()39 bool IsEnable() const override 40 { 41 return g_options.IsCompilerIfConversion(); 42 } 43 GetPassName()44 const char *GetPassName() const override 45 { 46 return "IfConversion"; 47 } 48 49 void InvalidateAnalyses() override; 50 51 private: 52 uint32_t limit_; 53 bool canEncodeFloatSelect_ {false}; 54 bool TryTriangle(BasicBlock *bb); 55 bool TryDiamond(BasicBlock *bb); 56 static bool LoopInvariantPreventConversion(BasicBlock *bb); 57 static bool IsConvertable(BasicBlock *bb, uint32_t *instCount); 58 bool IsPhisAllowed(BasicBlock *bb, BasicBlock *pred1, BasicBlock *pred2, uint32_t *phiCount); 59 static bool IsConditionChainPhi(Inst *phi); 60 uint32_t GetIfcLimit(BasicBlock *bb); 61 }; 62 } // namespace ark::compiler 63 64 #endif // COMPILER_OPTIMIZER_ANALYSIS_IF_CONVERSION_H 65