• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 PANDA_COMPILER_OPTIMIZER_OPTIMIZATIONS_LOOP_IDIOMS_H
17 #define PANDA_COMPILER_OPTIMIZER_OPTIMIZATIONS_LOOP_IDIOMS_H
18 
19 #include "optimizer/optimizations/loop_transform.h"
20 #include "compiler_options.h"
21 
22 // Find loops representing some idiom (like memcpy or memset) and replace
23 // it with an intrinsics.
24 namespace ark::compiler {
25 
26 struct CountableLoopInfo;
27 
28 class LoopIdioms : public LoopTransform<LoopExitPoint::LOOP_EXIT_HEADER> {
29 public:
LoopIdioms(Graph * graph)30     explicit LoopIdioms(Graph *graph) : LoopTransform(graph) {}
31     ~LoopIdioms() override = default;
32     NO_COPY_SEMANTIC(LoopIdioms);
33     NO_MOVE_SEMANTIC(LoopIdioms);
34 
35     bool RunImpl() override;
36 
IsEnable()37     bool IsEnable() const override
38     {
39         return g_options.IsCompilerLoopIdioms();
40     }
41 
GetPassName()42     const char *GetPassName() const override
43     {
44         return "LoopIdioms";
45     }
46 
47     void InvalidateAnalyses() override;
48 
49 private:
50     // Jump into intrinsic only if there will be more iterations
51     static constexpr size_t ITERATIONS_THRESHOLD = 6;
52 
53     bool TransformLoop(Loop *loop) override;
54     bool TryTransformArrayInitIdiom(Loop *loop);
55     Inst *CreateArrayInitIntrinsic(StoreInst *store, CountableLoopInfo *info);
56     bool ReplaceArrayInitLoop(Loop *loop, CountableLoopInfo *loopInfo, StoreInst *store, bool alwaysJump);
57 
58     bool TryTransformArrayMoveIdiom(Loop *loop);
59 
60     bool isApplied_ {false};
61 };
62 
63 }  // namespace ark::compiler
64 
65 #endif  // PANDA_COMPILER_OPTIMIZER_OPTIMIZATIONS_LOOP_IDIOMS_H