xref: /llvm-project/llvm/lib/CodeGen/MachineDominators.cpp (revision 1562b70eaf6e0b95910fa684dfc53bd5ca6252e7)
1 //===- MachineDominators.cpp - Machine Dominator Calculation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements simple dominator construction algorithms for finding
10 // forward dominators on machine functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/MachineDominators.h"
15 #include "llvm/ADT/SmallBitVector.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/PassRegistry.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/GenericDomTreeConstruction.h"
22 
23 using namespace llvm;
24 
25 namespace llvm {
26 // Always verify dominfo if expensive checking is enabled.
27 #ifdef EXPENSIVE_CHECKS
28 bool VerifyMachineDomInfo = true;
29 #else
30 bool VerifyMachineDomInfo = false;
31 #endif
32 } // namespace llvm
33 
34 static cl::opt<bool, true> VerifyMachineDomInfoX(
35     "verify-machine-dom-info", cl::location(VerifyMachineDomInfo), cl::Hidden,
36     cl::desc("Verify machine dominator info (time consuming)"));
37 
38 namespace llvm {
39 template class DomTreeNodeBase<MachineBasicBlock>;
40 template class DominatorTreeBase<MachineBasicBlock, false>; // DomTreeBase
41 
42 namespace DomTreeBuilder {
43 template void Calculate<MBBDomTree>(MBBDomTree &DT);
44 template void CalculateWithUpdates<MBBDomTree>(MBBDomTree &DT, MBBUpdates U);
45 
46 template void InsertEdge<MBBDomTree>(MBBDomTree &DT, MachineBasicBlock *From,
47                                      MachineBasicBlock *To);
48 
49 template void DeleteEdge<MBBDomTree>(MBBDomTree &DT, MachineBasicBlock *From,
50                                      MachineBasicBlock *To);
51 
52 template void ApplyUpdates<MBBDomTree>(MBBDomTree &DT, MBBDomTreeGraphDiff &,
53                                        MBBDomTreeGraphDiff *);
54 
55 template bool Verify<MBBDomTree>(const MBBDomTree &DT,
56                                  MBBDomTree::VerificationLevel VL);
57 } // namespace DomTreeBuilder
58 }
59 
60 bool MachineDominatorTree::invalidate(
61     MachineFunction &, const PreservedAnalyses &PA,
62     MachineFunctionAnalysisManager::Invalidator &) {
63   // Check whether the analysis, all analyses on machine functions, or the
64   // machine function's CFG have been preserved.
65   auto PAC = PA.getChecker<MachineDominatorTreeAnalysis>();
66   return !PAC.preserved() &&
67          !PAC.preservedSet<AllAnalysesOn<MachineFunction>>() &&
68          !PAC.preservedSet<CFGAnalyses>();
69 }
70 
71 AnalysisKey MachineDominatorTreeAnalysis::Key;
72 
73 MachineDominatorTreeAnalysis::Result
74 MachineDominatorTreeAnalysis::run(MachineFunction &MF,
75                                   MachineFunctionAnalysisManager &) {
76   return MachineDominatorTree(MF);
77 }
78 
79 PreservedAnalyses
80 MachineDominatorTreePrinterPass::run(MachineFunction &MF,
81                                      MachineFunctionAnalysisManager &MFAM) {
82   OS << "MachineDominatorTree for machine function: " << MF.getName() << '\n';
83   MFAM.getResult<MachineDominatorTreeAnalysis>(MF).print(OS);
84   return PreservedAnalyses::all();
85 }
86 
87 char MachineDominatorTreeWrapperPass::ID = 0;
88 
89 INITIALIZE_PASS(MachineDominatorTreeWrapperPass, "machinedomtree",
90                 "MachineDominator Tree Construction", true, true)
91 
92 MachineDominatorTreeWrapperPass::MachineDominatorTreeWrapperPass()
93     : MachineFunctionPass(ID) {
94   initializeMachineDominatorTreeWrapperPassPass(
95       *PassRegistry::getPassRegistry());
96 }
97 
98 char &llvm::MachineDominatorsID = MachineDominatorTreeWrapperPass::ID;
99 
100 bool MachineDominatorTreeWrapperPass::runOnMachineFunction(MachineFunction &F) {
101   DT = MachineDominatorTree(F);
102   return false;
103 }
104 
105 void MachineDominatorTreeWrapperPass::releaseMemory() { DT.reset(); }
106 
107 void MachineDominatorTreeWrapperPass::verifyAnalysis() const {
108   if (VerifyMachineDomInfo && DT)
109     if (!DT->verify(MachineDominatorTree::VerificationLevel::Basic))
110       report_fatal_error("MachineDominatorTree verification failed!");
111 }
112 
113 void MachineDominatorTreeWrapperPass::print(raw_ostream &OS,
114                                             const Module *) const {
115   if (DT)
116     DT->print(OS);
117 }
118