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"
1586af62c1STom Stellard 
1686af62c1STom Stellard using namespace llvm;
1786af62c1STom Stellard 
18b292c22cSJakub Kuderski namespace llvm {
19b292c22cSJakub Kuderski template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase
20*56b52a20SJakub Kuderski 
21*56b52a20SJakub Kuderski extern bool VerifyMachineDomInfo;
22*56b52a20SJakub Kuderski } // namespace llvm
23b292c22cSJakub Kuderski 
2486af62c1STom Stellard char MachinePostDominatorTree::ID = 0;
2586af62c1STom Stellard 
2686af62c1STom Stellard //declare initializeMachinePostDominatorTreePass
2786af62c1STom Stellard INITIALIZE_PASS(MachinePostDominatorTree, "machinepostdomtree",
2886af62c1STom Stellard                 "MachinePostDominator Tree Construction", true, true)
2986af62c1STom Stellard 
30269bd15cSJakub Kuderski MachinePostDominatorTree::MachinePostDominatorTree()
31269bd15cSJakub Kuderski     : MachineFunctionPass(ID), PDT(nullptr) {
3286af62c1STom Stellard   initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
3386af62c1STom Stellard }
3486af62c1STom Stellard 
35269bd15cSJakub Kuderski FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() {
3686af62c1STom Stellard   return new MachinePostDominatorTree();
3786af62c1STom Stellard }
3886af62c1STom Stellard 
39269bd15cSJakub Kuderski bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) {
40269bd15cSJakub Kuderski   PDT = std::make_unique<PostDomTreeT>();
41269bd15cSJakub Kuderski   PDT->recalculate(F);
4286af62c1STom Stellard   return false;
4386af62c1STom Stellard }
4486af62c1STom Stellard 
45269bd15cSJakub Kuderski void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
4686af62c1STom Stellard   AU.setPreservesAll();
4786af62c1STom Stellard   MachineFunctionPass::getAnalysisUsage(AU);
4886af62c1STom Stellard }
4986af62c1STom Stellard 
50269bd15cSJakub Kuderski MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator(
51269bd15cSJakub Kuderski     ArrayRef<MachineBasicBlock *> Blocks) const {
52269bd15cSJakub Kuderski   assert(!Blocks.empty());
53269bd15cSJakub Kuderski 
54269bd15cSJakub Kuderski   MachineBasicBlock *NCD = Blocks.front();
55269bd15cSJakub Kuderski   for (MachineBasicBlock *BB : Blocks.drop_front()) {
56269bd15cSJakub Kuderski     NCD = PDT->findNearestCommonDominator(NCD, BB);
57269bd15cSJakub Kuderski 
58269bd15cSJakub Kuderski     // Stop when the root is reached.
59269bd15cSJakub Kuderski     if (PDT->isVirtualRoot(PDT->getNode(NCD)))
60269bd15cSJakub Kuderski       return nullptr;
61269bd15cSJakub Kuderski   }
62269bd15cSJakub Kuderski 
63269bd15cSJakub Kuderski   return NCD;
64269bd15cSJakub Kuderski }
65269bd15cSJakub Kuderski 
66*56b52a20SJakub Kuderski void MachinePostDominatorTree::verifyAnalysis() const {
67*56b52a20SJakub Kuderski   if (PDT && VerifyMachineDomInfo)
68*56b52a20SJakub Kuderski     if (!PDT->verify(PostDomTreeT::VerificationLevel::Basic)) {
69*56b52a20SJakub Kuderski       errs() << "MachinePostDominatorTree verification failed\n";
70*56b52a20SJakub Kuderski 
71*56b52a20SJakub Kuderski       abort();
72*56b52a20SJakub Kuderski     }
73*56b52a20SJakub Kuderski }
74*56b52a20SJakub Kuderski 
75269bd15cSJakub Kuderski void MachinePostDominatorTree::print(llvm::raw_ostream &OS,
76269bd15cSJakub Kuderski                                      const Module *M) const {
77269bd15cSJakub Kuderski   PDT->print(OS);
7886af62c1STom Stellard }
79