xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/CodeGen/MachinePostDominators.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- MachinePostDominators.cpp -Machine Post Dominator Calculation ------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements simple dominator construction algorithms for finding
107330f729Sjoerg // post dominators on machine functions.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #include "llvm/CodeGen/MachinePostDominators.h"
15*82d56013Sjoerg #include "llvm/InitializePasses.h"
167330f729Sjoerg 
177330f729Sjoerg using namespace llvm;
187330f729Sjoerg 
197330f729Sjoerg namespace llvm {
207330f729Sjoerg template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase
217330f729Sjoerg 
227330f729Sjoerg extern bool VerifyMachineDomInfo;
237330f729Sjoerg } // namespace llvm
247330f729Sjoerg 
257330f729Sjoerg char MachinePostDominatorTree::ID = 0;
267330f729Sjoerg 
277330f729Sjoerg //declare initializeMachinePostDominatorTreePass
287330f729Sjoerg INITIALIZE_PASS(MachinePostDominatorTree, "machinepostdomtree",
297330f729Sjoerg                 "MachinePostDominator Tree Construction", true, true)
307330f729Sjoerg 
MachinePostDominatorTree()317330f729Sjoerg MachinePostDominatorTree::MachinePostDominatorTree()
327330f729Sjoerg     : MachineFunctionPass(ID), PDT(nullptr) {
337330f729Sjoerg   initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
347330f729Sjoerg }
357330f729Sjoerg 
createMachinePostDominatorTreePass()367330f729Sjoerg FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() {
377330f729Sjoerg   return new MachinePostDominatorTree();
387330f729Sjoerg }
397330f729Sjoerg 
runOnMachineFunction(MachineFunction & F)407330f729Sjoerg bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) {
417330f729Sjoerg   PDT = std::make_unique<PostDomTreeT>();
427330f729Sjoerg   PDT->recalculate(F);
437330f729Sjoerg   return false;
447330f729Sjoerg }
457330f729Sjoerg 
getAnalysisUsage(AnalysisUsage & AU) const467330f729Sjoerg void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
477330f729Sjoerg   AU.setPreservesAll();
487330f729Sjoerg   MachineFunctionPass::getAnalysisUsage(AU);
497330f729Sjoerg }
507330f729Sjoerg 
findNearestCommonDominator(ArrayRef<MachineBasicBlock * > Blocks) const517330f729Sjoerg MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator(
527330f729Sjoerg     ArrayRef<MachineBasicBlock *> Blocks) const {
537330f729Sjoerg   assert(!Blocks.empty());
547330f729Sjoerg 
557330f729Sjoerg   MachineBasicBlock *NCD = Blocks.front();
567330f729Sjoerg   for (MachineBasicBlock *BB : Blocks.drop_front()) {
577330f729Sjoerg     NCD = PDT->findNearestCommonDominator(NCD, BB);
587330f729Sjoerg 
597330f729Sjoerg     // Stop when the root is reached.
607330f729Sjoerg     if (PDT->isVirtualRoot(PDT->getNode(NCD)))
617330f729Sjoerg       return nullptr;
627330f729Sjoerg   }
637330f729Sjoerg 
647330f729Sjoerg   return NCD;
657330f729Sjoerg }
667330f729Sjoerg 
verifyAnalysis() const677330f729Sjoerg void MachinePostDominatorTree::verifyAnalysis() const {
687330f729Sjoerg   if (PDT && VerifyMachineDomInfo)
697330f729Sjoerg     if (!PDT->verify(PostDomTreeT::VerificationLevel::Basic)) {
707330f729Sjoerg       errs() << "MachinePostDominatorTree verification failed\n";
717330f729Sjoerg 
727330f729Sjoerg       abort();
737330f729Sjoerg     }
747330f729Sjoerg }
757330f729Sjoerg 
print(llvm::raw_ostream & OS,const Module * M) const767330f729Sjoerg void MachinePostDominatorTree::print(llvm::raw_ostream &OS,
777330f729Sjoerg                                      const Module *M) const {
787330f729Sjoerg   PDT->print(OS);
797330f729Sjoerg }
80