• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 MAPLE_PGO_INCLUDE_CFG_MST_H
16 #define MAPLE_PGO_INCLUDE_CFG_MST_H
17 
18 #include "types_def.h"
19 #include "mempool_allocator.h"
20 
21 namespace maple {
22 template <class Edge, class BB>
23 class CFGMST {
24 public:
CFGMST(MemPool & mp)25     explicit CFGMST(MemPool &mp) : mp(&mp), alloc(&mp), allEdges(alloc.Adapter()), bbGroups(alloc.Adapter()) {}
~CFGMST()26     virtual ~CFGMST()
27     {
28         mp = nullptr;
29     }
30     void ComputeMST(BB *commonEntry, BB *commonExit);
31     void BuildEdges(BB *commonEntry, BB *commonExit);
32     void SortEdges();
33     void AddEdge(BB *src, BB *dest, uint64 w, bool isCritical = false, bool isFake = false);
IsCritialEdge(const BB * src,const BB * dest)34     bool IsCritialEdge(const BB *src, const BB *dest) const
35     {
36         return src->GetSuccs().size() > 1 && dest->GetPreds().size() > 1;
37     }
GetAllEdges()38     const MapleVector<Edge *> &GetAllEdges() const
39     {
40         return allEdges;
41     }
42 
GetAllEdgesSize()43     size_t GetAllEdgesSize() const
44     {
45         return allEdges.size();
46     }
47 
GetAllBBs()48     uint32 GetAllBBs() const
49     {
50         return totalBB;
51     }
52 
GetInstrumentEdges(std::vector<Edge * > & instrumentEdges)53     void GetInstrumentEdges(std::vector<Edge *> &instrumentEdges) const
54     {
55         for (const auto &e : allEdges) {
56             if (!e->IsInMST()) {
57                 instrumentEdges.push_back(e);
58             }
59         }
60     }
61     void DumpEdgesInfo() const;
62 
63 private:
64     uint32 FindGroup(uint32 bbId);
65     bool UnionGroups(uint32 srcId, uint32 destId);
66     static constexpr int kNormalEdgeWeight = 2;
67     static constexpr int kExitEdgeWeight = 3;
68     static constexpr int kFakeExitEdgeWeight = 4;
69     static constexpr int kCriticalEdgeWeight = 4;
70     MemPool *mp;
71     MapleAllocator alloc;
72     MapleVector<Edge *> allEdges;
73     MapleMap<uint32, uint32> bbGroups;  // bbId - gourpId
74     uint32 totalBB = 0;
75 };
76 } /* namespace maple */
77 #endif  // MAPLE_PGO_INCLUDE_CFG_MST_H
78