1*0b57cec5SDimitry Andric //===- MachinePostDominators.cpp -Machine Post Dominator Calculation ------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements simple dominator construction algorithms for finding
10*0b57cec5SDimitry Andric // post dominators on machine functions.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachinePostDominators.h"
15*0b57cec5SDimitry Andric #include "llvm/InitializePasses.h"
16*0b57cec5SDimitry Andric
17*0b57cec5SDimitry Andric using namespace llvm;
18*0b57cec5SDimitry Andric
19*0b57cec5SDimitry Andric namespace llvm {
20*0b57cec5SDimitry Andric template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric extern bool VerifyMachineDomInfo;
23*0b57cec5SDimitry Andric } // namespace llvm
24*0b57cec5SDimitry Andric
25*0b57cec5SDimitry Andric char MachinePostDominatorTree::ID = 0;
26*0b57cec5SDimitry Andric
27*0b57cec5SDimitry Andric //declare initializeMachinePostDominatorTreePass
28*0b57cec5SDimitry Andric INITIALIZE_PASS(MachinePostDominatorTree, "machinepostdomtree",
29*0b57cec5SDimitry Andric "MachinePostDominator Tree Construction", true, true)
30*0b57cec5SDimitry Andric
MachinePostDominatorTree()31*0b57cec5SDimitry Andric MachinePostDominatorTree::MachinePostDominatorTree()
32*0b57cec5SDimitry Andric : MachineFunctionPass(ID), PDT(nullptr) {
33*0b57cec5SDimitry Andric initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
34*0b57cec5SDimitry Andric }
35*0b57cec5SDimitry Andric
createMachinePostDominatorTreePass()36*0b57cec5SDimitry Andric FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() {
37*0b57cec5SDimitry Andric return new MachinePostDominatorTree();
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & F)40*0b57cec5SDimitry Andric bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) {
41*0b57cec5SDimitry Andric PDT = std::make_unique<PostDomTreeT>();
42*0b57cec5SDimitry Andric PDT->recalculate(F);
43*0b57cec5SDimitry Andric return false;
44*0b57cec5SDimitry Andric }
45*0b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const46*0b57cec5SDimitry Andric void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
47*0b57cec5SDimitry Andric AU.setPreservesAll();
48*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric
findNearestCommonDominator(ArrayRef<MachineBasicBlock * > Blocks) const51*0b57cec5SDimitry Andric MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator(
52*0b57cec5SDimitry Andric ArrayRef<MachineBasicBlock *> Blocks) const {
53*0b57cec5SDimitry Andric assert(!Blocks.empty());
54*0b57cec5SDimitry Andric
55*0b57cec5SDimitry Andric MachineBasicBlock *NCD = Blocks.front();
56*0b57cec5SDimitry Andric for (MachineBasicBlock *BB : Blocks.drop_front()) {
57*0b57cec5SDimitry Andric NCD = PDT->findNearestCommonDominator(NCD, BB);
58
59 // Stop when the root is reached.
60 if (PDT->isVirtualRoot(PDT->getNode(NCD)))
61 return nullptr;
62 }
63
64 return NCD;
65 }
66
verifyAnalysis() const67 void MachinePostDominatorTree::verifyAnalysis() const {
68 if (PDT && VerifyMachineDomInfo)
69 if (!PDT->verify(PostDomTreeT::VerificationLevel::Basic)) {
70 errs() << "MachinePostDominatorTree verification failed\n";
71
72 abort();
73 }
74 }
75
print(llvm::raw_ostream & OS,const Module * M) const76 void MachinePostDominatorTree::print(llvm::raw_ostream &OS,
77 const Module *M) const {
78 PDT->print(OS);
79 }
80