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_OPTIMIZATIONS_LOOP_UNROLL_H_ 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_LOOP_UNROLL_H_ 18 19 #include "optimizer/optimizations/loop_transform.h" 20 #include "compiler_options.h" 21 22 namespace panda::compiler { 23 /** 24 * Loop unroll optimization 25 * @param inst_limit - the maximum number of loop instructions after its unrolling 26 * @param unroll_factor - the number of loop body copies including the original one 27 */ 28 class LoopUnroll : public LoopTransform<LoopExitPoint::LOOP_EXIT_BACKEDGE> { 29 struct UnrollParams { 30 uint32_t unroll_factor; 31 uint32_t cloneable_insts; 32 bool has_call; 33 }; 34 35 public: LoopUnroll(Graph * graph,uint32_t inst_limit,uint32_t unroll_factor)36 LoopUnroll(Graph *graph, uint32_t inst_limit, uint32_t unroll_factor) 37 : LoopTransform(graph), INST_LIMIT(inst_limit), UNROLL_FACTOR(unroll_factor) 38 { 39 } 40 41 bool RunImpl() override; 42 IsEnable()43 bool IsEnable() const override 44 { 45 return options.IsCompilerLoopUnroll(); 46 } 47 GetPassName()48 const char *GetPassName() const override 49 { 50 return "LoopUnroll"; 51 } 52 53 void InvalidateAnalyses() override; 54 55 private: 56 bool TransformLoop(Loop *loop) override; 57 UnrollParams GetUnrollParams(Loop *loop); 58 void FixCompareInst(const CountableLoopInfo &loop_info, BasicBlock *header, uint32_t unroll_factor); 59 Inst *CreateNewTestInst(const CountableLoopInfo &loop_info, Inst *const_inst, Inst *pre_header_cmp); 60 bool HasPreHeaderCompare(Loop *loop, const CountableLoopInfo &loop_info); 61 62 private: 63 const uint32_t INST_LIMIT {0}; 64 const uint32_t UNROLL_FACTOR {0}; 65 bool is_applied_ {false}; 66 }; 67 } // namespace panda::compiler 68 69 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_LOOP_UNROLL_H_ 70