1 /* 2 * Copyright (c) 2021-2025 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 auto encoder = graph->GetEncoder(); 31 ASSERT(encoder != nullptr); 32 canEncodeFloatSelect_ = encoder->CanEncodeFloatSelect(); 33 } 34 35 NO_MOVE_SEMANTIC(IfConversion); 36 NO_COPY_SEMANTIC(IfConversion); 37 ~IfConversion() override = default; 38 39 bool RunImpl() override; 40 IsEnable()41 bool IsEnable() const override 42 { 43 return g_options.IsCompilerIfConversion(); 44 } 45 GetPassName()46 const char *GetPassName() const override 47 { 48 return "IfConversion"; 49 } 50 51 void InvalidateAnalyses() override; 52 53 private: 54 uint32_t limit_; 55 bool canEncodeFloatSelect_ {false}; 56 bool TryTriangle(BasicBlock *bb); 57 bool TryDiamond(BasicBlock *bb); 58 static bool LoopInvariantPreventConversion(BasicBlock *bb); 59 static bool IsConvertable(BasicBlock *bb, uint32_t *instCount); 60 bool IsPhisAllowed(BasicBlock *bb, BasicBlock *pred1, BasicBlock *pred2, uint32_t *phiCount); 61 static bool IsConditionChainPhi(Inst *phi); 62 uint32_t GetIfcLimit(BasicBlock *bb); 63 }; 64 } // namespace ark::compiler 65 66 #endif // COMPILER_OPTIMIZER_ANALYSIS_IF_CONVERSION_H 67