• 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 #include "optimizer/ir/analysis.h"
17 #include "condition_chain_cache.h"
18 #include "condition_chain.h"
19 
20 namespace ark::compiler {
ConditionChainCache(ArenaAllocator * allocator)21 ConditionChainCache::ConditionChainCache(ArenaAllocator *allocator) : cache_(allocator->Adapter()) {}
22 
Insert(ConditionChain * chain,Inst * phiInst)23 void ConditionChainCache::Insert(ConditionChain *chain, Inst *phiInst)
24 {
25     cache_.insert({chain, phiInst});
26 }
27 
Contains(const ConditionChain * chain0,const ConditionChain * chain1)28 static bool Contains(const ConditionChain *chain0, const ConditionChain *chain1)
29 {
30     auto mps0 = chain0->GetMultiplePredecessorsSuccessor();
31     auto mps1 = chain1->GetMultiplePredecessorsSuccessor();
32     for (auto it0 = chain0->GetBegin(); it0 != chain0->GetEnd(); ++it0) {
33         bool ret = false;
34         auto bb0 = *it0;
35         auto inverted0 = bb0->GetTrueSuccessor() != mps0;
36         for (auto it1 = chain1->GetBegin(); it1 != chain1->GetEnd(); ++it1) {
37             auto bb1 = *it1;
38             auto inverted1 = bb1->GetTrueSuccessor() != mps1;
39             if (IsConditionEqual((*it0)->GetLastInst(), (*it1)->GetLastInst(), inverted0 != inverted1)) {
40                 ret = true;
41                 break;
42             }
43         }
44         if (!ret) {
45             return false;
46         }
47     }
48     return true;
49 }
50 
Equal(const ConditionChain * chain0,const ConditionChain * chain1)51 static bool Equal(const ConditionChain *chain0, const ConditionChain *chain1)
52 {
53     return Contains(chain0, chain1) && Contains(chain1, chain0);
54 }
55 
FindPhi(const ConditionChain * chain)56 Inst *ConditionChainCache::FindPhi(const ConditionChain *chain)
57 {
58     for (auto &[c, phi] : cache_) {
59         if (Equal(c, chain)) {
60             return phi;
61         }
62     }
63     return nullptr;
64 }
65 
Clear()66 void ConditionChainCache::Clear()
67 {
68     cache_.clear();
69 }
70 }  // namespace ark::compiler
71