• 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 #include "alignment.h"
17 #include "optimize_common.h"
18 #include "cgfunc.h"
19 #include "cg.h"
20 #include "cg_option.h"
21 
22 namespace maplebe {
23 #define ALIGN_ANALYZE_DUMP_NEWPW CG_DEBUG_FUNC(func)
24 
AnalysisAlignment()25 void AlignAnalysis::AnalysisAlignment()
26 {
27     FindLoopHeader();
28     FindJumpTarget();
29     ComputeLoopAlign();
30     ComputeJumpAlign();
31     if (CGOptions::DoCondBrAlign()) {
32         ComputeCondBranchAlign();
33     }
34 }
35 
Dump()36 void AlignAnalysis::Dump()
37 {
38     MIRSymbol *funcSt = GlobalTables::GetGsymTable().GetSymbolFromStidx(cgFunc->GetFunction().GetStIdx().Idx());
39     DEBUG_ASSERT(funcSt != nullptr, "null ptr check");
40     LogInfo::MapleLogger() << "\n********* alignment for " << funcSt->GetName() << " *********\n";
41     LogInfo::MapleLogger() << "------ jumpTargetBBs: " << jumpTargetBBs.size() << " total ------\n";
42     for (auto *jumpLabel : jumpTargetBBs) {
43         LogInfo::MapleLogger() << " === BB_" << jumpLabel->GetId() << " (" << std::hex << jumpLabel << ")" << std::dec
44                                << " <" << jumpLabel->GetKindName();
45         if (jumpLabel->GetLabIdx() != MIRLabelTable::GetDummyLabel()) {
46             LogInfo::MapleLogger() << "[labeled with " << jumpLabel->GetLabIdx() << "]> ===\n";
47         }
48         if (!jumpLabel->GetPreds().empty()) {
49             LogInfo::MapleLogger() << "\tpreds: [ ";
50             for (auto *pred : jumpLabel->GetPreds()) {
51                 LogInfo::MapleLogger() << "BB_" << pred->GetId();
52                 if (pred->GetLabIdx() != MIRLabelTable::GetDummyLabel()) {
53                     LogInfo::MapleLogger() << "<labeled with " << jumpLabel->GetLabIdx() << ">";
54                 }
55                 LogInfo::MapleLogger() << " (" << std::hex << pred << ") " << std::dec << " ";
56             }
57             LogInfo::MapleLogger() << "]\n";
58         }
59         if (jumpLabel->GetPrev() != nullptr) {
60             LogInfo::MapleLogger() << "\tprev: [ ";
61             LogInfo::MapleLogger() << "BB_" << jumpLabel->GetPrev()->GetId();
62             if (jumpLabel->GetPrev()->GetLabIdx() != MIRLabelTable::GetDummyLabel()) {
63                 LogInfo::MapleLogger() << "<labeled with " << jumpLabel->GetLabIdx() << ">";
64             }
65             LogInfo::MapleLogger() << " (" << std::hex << jumpLabel->GetPrev() << ") " << std::dec << " ";
66             LogInfo::MapleLogger() << "]\n";
67         }
68         FOR_BB_INSNS_CONST(insn, jumpLabel) {
69             insn->Dump();
70         }
71     }
72     LogInfo::MapleLogger() << "\n------ loopHeaderBBs: " << loopHeaderBBs.size() << " total ------\n";
73     for (auto *loopHeader : loopHeaderBBs) {
74         LogInfo::MapleLogger() << " === BB_" << loopHeader->GetId() << " (" << std::hex << loopHeader << ")" << std::dec
75                                << " <" << loopHeader->GetKindName();
76         if (loopHeader->GetLabIdx() != MIRLabelTable::GetDummyLabel()) {
77             LogInfo::MapleLogger() << "[labeled with " << loopHeader->GetLabIdx() << "]> ===\n";
78         }
79         LogInfo::MapleLogger() << "\tLoop Level: " << loopHeader->GetLoop()->GetLoopLevel() << "\n";
80         FOR_BB_INSNS_CONST(insn, loopHeader) {
81             insn->Dump();
82         }
83     }
84     LogInfo::MapleLogger() << "\n------ alignInfos: " << alignInfos.size() << " total ------\n";
85     MapleUnorderedMap<BB *, uint32>::iterator iter;
86     for (iter = alignInfos.begin(); iter != alignInfos.end(); ++iter) {
87         BB *bb = iter->first;
88         LogInfo::MapleLogger() << " === BB_" << bb->GetId() << " (" << std::hex << bb << ")" << std::dec << " <"
89                                << bb->GetKindName();
90         if (bb->GetLabIdx() != MIRLabelTable::GetDummyLabel()) {
91             LogInfo::MapleLogger() << "[labeled with " << bb->GetLabIdx() << "]> ===\n";
92         }
93         LogInfo::MapleLogger() << "\talignPower: " << iter->second << "\n";
94     }
95 }
96 
PhaseRun(maplebe::CGFunc & func)97 bool CgAlignAnalysis::PhaseRun(maplebe::CGFunc &func)
98 {
99     if (ALIGN_ANALYZE_DUMP_NEWPW) {
100         DotGenerator::GenerateDot("alignanalysis", func, func.GetMirModule(), true, func.GetName());
101     }
102     MemPool *alignMemPool = GetPhaseMemPool();
103     AlignAnalysis *alignAnalysis = func.GetCG()->CreateAlignAnalysis(*alignMemPool, func);
104 
105     CHECK_FATAL(alignAnalysis != nullptr, "AlignAnalysis instance create failure");
106     alignAnalysis->AnalysisAlignment();
107     if (ALIGN_ANALYZE_DUMP_NEWPW) {
108         alignAnalysis->Dump();
109     }
110     return true;
111 }
112 } /* namespace maplebe */
113