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 16*269bd15cSJakub Kuderski #include "llvm/ADT/STLExtras.h" 17*269bd15cSJakub Kuderski 1886af62c1STom Stellard using namespace llvm; 1986af62c1STom Stellard 20b292c22cSJakub Kuderski namespace llvm { 21b292c22cSJakub Kuderski template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase 22b292c22cSJakub Kuderski } 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 30*269bd15cSJakub Kuderski MachinePostDominatorTree::MachinePostDominatorTree() 31*269bd15cSJakub Kuderski : MachineFunctionPass(ID), PDT(nullptr) { 3286af62c1STom Stellard initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry()); 3386af62c1STom Stellard } 3486af62c1STom Stellard 35*269bd15cSJakub Kuderski FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() { 3686af62c1STom Stellard return new MachinePostDominatorTree(); 3786af62c1STom Stellard } 3886af62c1STom Stellard 39*269bd15cSJakub Kuderski bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) { 40*269bd15cSJakub Kuderski PDT = std::make_unique<PostDomTreeT>(); 41*269bd15cSJakub Kuderski PDT->recalculate(F); 4286af62c1STom Stellard return false; 4386af62c1STom Stellard } 4486af62c1STom Stellard 45*269bd15cSJakub Kuderski void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const { 4686af62c1STom Stellard AU.setPreservesAll(); 4786af62c1STom Stellard MachineFunctionPass::getAnalysisUsage(AU); 4886af62c1STom Stellard } 4986af62c1STom Stellard 50*269bd15cSJakub Kuderski MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator( 51*269bd15cSJakub Kuderski ArrayRef<MachineBasicBlock *> Blocks) const { 52*269bd15cSJakub Kuderski assert(!Blocks.empty()); 53*269bd15cSJakub Kuderski 54*269bd15cSJakub Kuderski MachineBasicBlock *NCD = Blocks.front(); 55*269bd15cSJakub Kuderski for (MachineBasicBlock *BB : Blocks.drop_front()) { 56*269bd15cSJakub Kuderski NCD = PDT->findNearestCommonDominator(NCD, BB); 57*269bd15cSJakub Kuderski 58*269bd15cSJakub Kuderski // Stop when the root is reached. 59*269bd15cSJakub Kuderski if (PDT->isVirtualRoot(PDT->getNode(NCD))) 60*269bd15cSJakub Kuderski return nullptr; 61*269bd15cSJakub Kuderski } 62*269bd15cSJakub Kuderski 63*269bd15cSJakub Kuderski return NCD; 64*269bd15cSJakub Kuderski } 65*269bd15cSJakub Kuderski 66*269bd15cSJakub Kuderski void MachinePostDominatorTree::print(llvm::raw_ostream &OS, 67*269bd15cSJakub Kuderski const Module *M) const { 68*269bd15cSJakub Kuderski PDT->print(OS); 6986af62c1STom Stellard } 70