1 //===- llvm/CodeGen/MachinePostDominators.h ----------------------*- C++ -*-==// 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 exposes interfaces to post dominance information for 10 // target-specific code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_MACHINEPOSTDOMINATORS_H 15 #define LLVM_CODEGEN_MACHINEPOSTDOMINATORS_H 16 17 #include "llvm/CodeGen/MachineDominators.h" 18 19 namespace llvm { 20 21 extern template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTree 22 23 namespace DomTreeBuilder { 24 using MBBPostDomTree = PostDomTreeBase<MachineBasicBlock>; 25 using MBBPostDomTreeGraphDiff = GraphDiff<MachineBasicBlock *, true>; 26 27 extern template void Calculate<MBBPostDomTree>(MBBPostDomTree &DT); 28 extern template void InsertEdge<MBBPostDomTree>(MBBPostDomTree &DT, 29 MachineBasicBlock *From, 30 MachineBasicBlock *To); 31 extern template void DeleteEdge<MBBPostDomTree>(MBBPostDomTree &DT, 32 MachineBasicBlock *From, 33 MachineBasicBlock *To); 34 extern template void ApplyUpdates<MBBPostDomTree>(MBBPostDomTree &DT, 35 MBBPostDomTreeGraphDiff &, 36 MBBPostDomTreeGraphDiff *); 37 extern template bool 38 Verify<MBBPostDomTree>(const MBBPostDomTree &DT, 39 MBBPostDomTree::VerificationLevel VL); 40 } // namespace DomTreeBuilder 41 42 /// 43 /// MachinePostDominatorTree - an analysis pass wrapper for DominatorTree 44 /// used to compute the post-dominator tree for MachineFunctions. 45 /// 46 class MachinePostDominatorTree : public PostDomTreeBase<MachineBasicBlock> { 47 using Base = PostDomTreeBase<MachineBasicBlock>; 48 49 public: 50 MachinePostDominatorTree() = default; 51 MachinePostDominatorTree(MachineFunction & MF)52 explicit MachinePostDominatorTree(MachineFunction &MF) { recalculate(MF); } 53 54 /// Handle invalidation explicitly. 55 bool invalidate(MachineFunction &, const PreservedAnalyses &PA, 56 MachineFunctionAnalysisManager::Invalidator &); 57 58 /// Make findNearestCommonDominator(const NodeT *A, const NodeT *B) available. 59 using Base::findNearestCommonDominator; 60 61 /// Returns the nearest common dominator of the given blocks. 62 /// If that tree node is a virtual root, a nullptr will be returned. 63 MachineBasicBlock * 64 findNearestCommonDominator(ArrayRef<MachineBasicBlock *> Blocks) const; 65 }; 66 67 class MachinePostDominatorTreeAnalysis 68 : public AnalysisInfoMixin<MachinePostDominatorTreeAnalysis> { 69 friend AnalysisInfoMixin<MachinePostDominatorTreeAnalysis>; 70 71 static AnalysisKey Key; 72 73 public: 74 using Result = MachinePostDominatorTree; 75 76 Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM); 77 }; 78 79 class MachinePostDominatorTreePrinterPass 80 : public PassInfoMixin<MachinePostDominatorTreePrinterPass> { 81 raw_ostream &OS; 82 83 public: MachinePostDominatorTreePrinterPass(raw_ostream & OS)84 explicit MachinePostDominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {} 85 PreservedAnalyses run(MachineFunction &MF, 86 MachineFunctionAnalysisManager &MFAM); isRequired()87 static bool isRequired() { return true; } 88 }; 89 90 class MachinePostDominatorTreeWrapperPass : public MachineFunctionPass { 91 std::optional<MachinePostDominatorTree> PDT; 92 93 public: 94 static char ID; 95 96 MachinePostDominatorTreeWrapperPass(); 97 getPostDomTree()98 MachinePostDominatorTree &getPostDomTree() { return *PDT; } getPostDomTree()99 const MachinePostDominatorTree &getPostDomTree() const { return *PDT; } 100 101 bool runOnMachineFunction(MachineFunction &MF) override; 102 void getAnalysisUsage(AnalysisUsage &AU) const override; releaseMemory()103 void releaseMemory() override { PDT.reset(); } 104 void verifyAnalysis() const override; 105 void print(llvm::raw_ostream &OS, const Module *M = nullptr) const override; 106 }; 107 } //end of namespace llvm 108 109 #endif 110