• 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 
16 #ifndef MAPLEBE_INCLUDE_CG_ALIGNMENT_H
17 #define MAPLEBE_INCLUDE_CG_ALIGNMENT_H
18 
19 #include "cg_phase.h"
20 #include "maple_phase.h"
21 #include "cgbb.h"
22 #include "loop.h"
23 
24 namespace maplebe {
25 class AlignAnalysis {
26 public:
AlignAnalysis(CGFunc & func,MemPool & memP)27     AlignAnalysis(CGFunc &func, MemPool &memP)
28         : cgFunc(&func),
29           alignAllocator(&memP),
30           loopHeaderBBs(alignAllocator.Adapter()),
31           jumpTargetBBs(alignAllocator.Adapter()),
32           alignInfos(alignAllocator.Adapter()),
33           sameTargetBranches(alignAllocator.Adapter())
34     {
35     }
36 
37     virtual ~AlignAnalysis() = default;
38 
39     void AnalysisAlignment();
40     void Dump();
41     virtual void FindLoopHeader() = 0;
42     virtual void FindJumpTarget() = 0;
43     virtual void ComputeLoopAlign() = 0;
44     virtual void ComputeJumpAlign() = 0;
45     virtual void ComputeCondBranchAlign() = 0;
46 
47     /* filter condition */
48     virtual bool IsIncludeCall(BB &bb) = 0;
49     virtual bool IsInSizeRange(BB &bb) = 0;
50     virtual bool HasFallthruEdge(BB &bb) = 0;
51 
PhaseName()52     std::string PhaseName() const
53     {
54         return "alignanalysis";
55     }
GetLoopHeaderBBs()56     const MapleUnorderedSet<BB *> &GetLoopHeaderBBs() const
57     {
58         return loopHeaderBBs;
59     }
GetJumpTargetBBs()60     const MapleUnorderedSet<BB *> &GetJumpTargetBBs() const
61     {
62         return jumpTargetBBs;
63     }
GetAlignInfos()64     const MapleUnorderedMap<BB *, uint32> &GetAlignInfos() const
65     {
66         return alignInfos;
67     }
GetAlignPower(BB & bb)68     uint32 GetAlignPower(BB &bb)
69     {
70         return alignInfos[&bb];
71     }
72 
InsertLoopHeaderBBs(BB & bb)73     void InsertLoopHeaderBBs(BB &bb)
74     {
75         loopHeaderBBs.insert(&bb);
76     }
InsertJumpTargetBBs(BB & bb)77     void InsertJumpTargetBBs(BB &bb)
78     {
79         jumpTargetBBs.insert(&bb);
80     }
InsertAlignInfos(BB & bb,uint32 power)81     void InsertAlignInfos(BB &bb, uint32 power)
82     {
83         alignInfos[&bb] = power;
84     }
85 
86 protected:
87     CGFunc *cgFunc;
88     MapleAllocator alignAllocator;
89     MapleUnorderedSet<BB *> loopHeaderBBs;
90     MapleUnorderedSet<BB *> jumpTargetBBs;
91     MapleUnorderedMap<BB *, uint32> alignInfos;
92     MapleUnorderedMap<LabelIdx, uint32> sameTargetBranches;
93 };
94 
95 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgAlignAnalysis, maplebe::CGFunc)
96 MAPLE_FUNC_PHASE_DECLARE_END
97 } /* namespace maplebe */
98 
99 #endif /* MAPLEBE_INCLUDE_CG_ALIGNMENT_H */
100