186af62c1STom Stellard //===- MachinePostDominators.cpp -Machine Post Dominator Calculation ------===//
286af62c1STom Stellard //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
686af62c1STom Stellard //
786af62c1STom Stellard //===----------------------------------------------------------------------===//
886af62c1STom Stellard //
986af62c1STom Stellard // This file implements simple dominator construction algorithms for finding
1086af62c1STom Stellard // post dominators on machine functions.
1186af62c1STom Stellard //
1286af62c1STom Stellard //===----------------------------------------------------------------------===//
1386af62c1STom Stellard 
1486af62c1STom Stellard #include "llvm/CodeGen/MachinePostDominators.h"
15*05da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
1686af62c1STom Stellard 
1786af62c1STom Stellard using namespace llvm;
1886af62c1STom Stellard 
19b292c22cSJakub Kuderski namespace llvm {
20b292c22cSJakub Kuderski template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase
2156b52a20SJakub Kuderski 
2256b52a20SJakub Kuderski extern bool VerifyMachineDomInfo;
2356b52a20SJakub Kuderski } // namespace llvm
24b292c22cSJakub Kuderski 
2586af62c1STom Stellard char MachinePostDominatorTree::ID = 0;
2686af62c1STom Stellard 
2786af62c1STom Stellard //declare initializeMachinePostDominatorTreePass
2886af62c1STom Stellard INITIALIZE_PASS(MachinePostDominatorTree, "machinepostdomtree",
2986af62c1STom Stellard                 "MachinePostDominator Tree Construction", true, true)
3086af62c1STom Stellard 
MachinePostDominatorTree()31269bd15cSJakub Kuderski MachinePostDominatorTree::MachinePostDominatorTree()
32269bd15cSJakub Kuderski     : MachineFunctionPass(ID), PDT(nullptr) {
3386af62c1STom Stellard   initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
3486af62c1STom Stellard }
3586af62c1STom Stellard 
createMachinePostDominatorTreePass()36269bd15cSJakub Kuderski FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() {
3786af62c1STom Stellard   return new MachinePostDominatorTree();
3886af62c1STom Stellard }
3986af62c1STom Stellard 
runOnMachineFunction(MachineFunction & F)40269bd15cSJakub Kuderski bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) {
41269bd15cSJakub Kuderski   PDT = std::make_unique<PostDomTreeT>();
42269bd15cSJakub Kuderski   PDT->recalculate(F);
4386af62c1STom Stellard   return false;
4486af62c1STom Stellard }
4586af62c1STom Stellard 
getAnalysisUsage(AnalysisUsage & AU) const46269bd15cSJakub Kuderski void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
4786af62c1STom Stellard   AU.setPreservesAll();
4886af62c1STom Stellard   MachineFunctionPass::getAnalysisUsage(AU);
4986af62c1STom Stellard }
5086af62c1STom Stellard 
findNearestCommonDominator(ArrayRef<MachineBasicBlock * > Blocks) const51269bd15cSJakub Kuderski MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator(
52269bd15cSJakub Kuderski     ArrayRef<MachineBasicBlock *> Blocks) const {
53269bd15cSJakub Kuderski   assert(!Blocks.empty());
54269bd15cSJakub Kuderski 
55269bd15cSJakub Kuderski   MachineBasicBlock *NCD = Blocks.front();
56269bd15cSJakub Kuderski   for (MachineBasicBlock *BB : Blocks.drop_front()) {
57269bd15cSJakub Kuderski     NCD = PDT->findNearestCommonDominator(NCD, BB);
58269bd15cSJakub Kuderski 
59269bd15cSJakub Kuderski     // Stop when the root is reached.
60269bd15cSJakub Kuderski     if (PDT->isVirtualRoot(PDT->getNode(NCD)))
61269bd15cSJakub Kuderski       return nullptr;
62269bd15cSJakub Kuderski   }
63269bd15cSJakub Kuderski 
64269bd15cSJakub Kuderski   return NCD;
65269bd15cSJakub Kuderski }
66269bd15cSJakub Kuderski 
verifyAnalysis() const6756b52a20SJakub Kuderski void MachinePostDominatorTree::verifyAnalysis() const {
6856b52a20SJakub Kuderski   if (PDT && VerifyMachineDomInfo)
6956b52a20SJakub Kuderski     if (!PDT->verify(PostDomTreeT::VerificationLevel::Basic)) {
7056b52a20SJakub Kuderski       errs() << "MachinePostDominatorTree verification failed\n";
7156b52a20SJakub Kuderski 
7256b52a20SJakub Kuderski       abort();
7356b52a20SJakub Kuderski     }
7456b52a20SJakub Kuderski }
7556b52a20SJakub Kuderski 
print(llvm::raw_ostream & OS,const Module * M) const76269bd15cSJakub Kuderski void MachinePostDominatorTree::print(llvm::raw_ostream &OS,
77269bd15cSJakub Kuderski                                      const Module *M) const {
78269bd15cSJakub Kuderski   PDT->print(OS);
7986af62c1STom Stellard }
80