• 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_CONDITION_CHAIN_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_CONDITION_CHAIN_H
18 
19 #include "compiler/optimizer/ir/basicblock.h"
20 
21 namespace ark::compiler {
22 class ConditionChainManager;
23 
24 /*
25  * Graph:
26  *          | |
27  *          v v
28  *          [A]------\
29  *           |       |
30  *           |       v
31  *           |<-----[B]
32  *           |       |
33  *           v       v
34  *       -->[S0]    [S1]<--
35  *           |       |
36  *           v       v
37  *
38  * Basic blocks A & B is a condition chain.
39  * Each basic block in the chain has S0 successor which is called `multiple_predecessor_successor' (all chain basic
40  * blocks are predecessors).
41  * Only last basic block in chain (B) has S1 successor which is called `single_predecessor_successor`.
42  * Both S0 and S1 successors can have predeccessors which are not part of the chain.
43  */
44 class ConditionChain {
45 public:
46     using BlockIterator = ArenaVector<BasicBlock *>::iterator;
47     using BlockConstIterator = ArenaVector<BasicBlock *>::const_iterator;
48 
ConditionChain(BlockIterator begin,size_t size,size_t multiplePredecessorsSuccessorIndex,size_t singlePredecessorSuccessorIndex)49     ConditionChain(BlockIterator begin, size_t size, size_t multiplePredecessorsSuccessorIndex,
50                    size_t singlePredecessorSuccessorIndex)
51         : begin_(begin),
52           size_(size),
53           multiplePredecessorsSuccessorIndex_(multiplePredecessorsSuccessorIndex),
54           singlePredecessorSuccessorIndex_(singlePredecessorSuccessorIndex)
55     {
56     }
57 
GetFirstBlock()58     BasicBlock *GetFirstBlock()
59     {
60         return *begin_;
61     }
62 
GetFirstBlock()63     const BasicBlock *GetFirstBlock() const
64     {
65         return *begin_;
66     }
67 
GetLastBlock()68     BasicBlock *GetLastBlock()
69     {
70         return *(begin_ + size_ - 1);
71     }
72 
GetLastBlock()73     const BasicBlock *GetLastBlock() const
74     {
75         return *(begin_ + size_ - 1);
76     }
77 
GetBegin()78     BlockIterator GetBegin()
79     {
80         return begin_;
81     }
82 
GetEnd()83     BlockIterator GetEnd()
84     {
85         return begin_ + size_;
86     }
87 
GetSize()88     size_t GetSize() const
89     {
90         return size_;
91     }
92 
GetBegin()93     BlockConstIterator GetBegin() const
94     {
95         return begin_;
96     }
97 
GetEnd()98     BlockConstIterator GetEnd() const
99     {
100         return begin_ + size_;
101     }
102 
Contains(const BasicBlock * bb)103     bool Contains(const BasicBlock *bb) const
104     {
105         auto last = begin_ + size_;
106         return std::find(begin_, last, bb) != last;
107     }
108 
SetFirstBlock(BasicBlock * bb)109     void SetFirstBlock(BasicBlock *bb)
110     {
111         *begin_ = bb;
112     }
113 
GetMultiplePredecessorsSuccessor()114     BasicBlock *GetMultiplePredecessorsSuccessor() const
115     {
116         return GetFirstBlock()->GetSuccessor(multiplePredecessorsSuccessorIndex_);
117     }
118 
GetSinglePredecessorSuccessor()119     BasicBlock *GetSinglePredecessorSuccessor() const
120     {
121         return GetLastBlock()->GetSuccessor(singlePredecessorSuccessorIndex_);
122     }
123 
124 private:
125     BlockIterator begin_;
126     size_t size_;
127     size_t multiplePredecessorsSuccessorIndex_;
128     size_t singlePredecessorSuccessorIndex_;
129 };
130 }  // namespace ark::compiler
131 
132 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_CONDITION_CHAIN_H
133