• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMPILER_OPTIMIZER_ANALYSIS_COUNTABLE_LOOP_PARSER_H_
16 #define COMPILER_OPTIMIZER_ANALYSIS_COUNTABLE_LOOP_PARSER_H_
17 
18 #include "optimizer/ir/inst.h"
19 
20 namespace panda::compiler {
21 class Loop;
22 
23 /**
24  * Example of code
25  *  ---------------
26  * for (init(a); if_imm(compare(a,test)); update(a)) {...}
27  */
28 struct CountableLoopInfo {
29     Inst *if_imm;
30     Inst *init;
31     Inst *test;
32     Inst *update;
33     Inst *index;
34     uint64_t const_step;
35     ConditionCode normalized_cc;  // cc between `update` and `test`
36 };
37 
38 /**
39  * Helper class to check if loop is countable and to get its parameters
40  */
41 class CountableLoopParser {
42 public:
CountableLoopParser(const Loop & loop)43     explicit CountableLoopParser(const Loop &loop) : loop_(loop) {}
44 
45     NO_MOVE_SEMANTIC(CountableLoopParser);
46     NO_COPY_SEMANTIC(CountableLoopParser);
47     ~CountableLoopParser() = default;
48 
49     std::optional<CountableLoopInfo> Parse();
50 
51 private:
52     Inst *SetIndexAndRetrunConstInst();
53 
54 private:
55     const Loop &loop_;
56     CountableLoopInfo loop_info_ {};
57     bool is_head_loop_exit_ = false;
58 };
59 }  // namespace panda::compiler
60 
61 #endif  // COMPILER_OPTIMIZER_ANALYSIS_COUNTABLE_LOOP_PARSER_H_
62