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