1 //===- MachinePostDominators.cpp -Machine Post Dominator Calculation ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements simple dominator construction algorithms for finding
10 // post dominators on machine functions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachinePostDominators.h"
15 #include "llvm/InitializePasses.h"
20 template class DominatorTreeBase
<MachineBasicBlock
, true>; // PostDomTreeBase
22 extern bool VerifyMachineDomInfo
;
25 char MachinePostDominatorTree::ID
= 0;
27 //declare initializeMachinePostDominatorTreePass
28 INITIALIZE_PASS(MachinePostDominatorTree
, "machinepostdomtree",
29 "MachinePostDominator Tree Construction", true, true)
31 MachinePostDominatorTree::MachinePostDominatorTree()
32 : MachineFunctionPass(ID
), PDT(nullptr) {
33 initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
36 FunctionPass
*MachinePostDominatorTree::createMachinePostDominatorTreePass() {
37 return new MachinePostDominatorTree();
40 bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction
&F
) {
41 PDT
= std::make_unique
<PostDomTreeT
>();
46 void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage
&AU
) const {
48 MachineFunctionPass::getAnalysisUsage(AU
);
51 MachineBasicBlock
*MachinePostDominatorTree::findNearestCommonDominator(
52 ArrayRef
<MachineBasicBlock
*> Blocks
) const {
53 assert(!Blocks
.empty());
55 MachineBasicBlock
*NCD
= Blocks
.front();
56 for (MachineBasicBlock
*BB
: Blocks
.drop_front()) {
57 NCD
= PDT
->findNearestCommonDominator(NCD
, BB
);
59 // Stop when the root is reached.
60 if (PDT
->isVirtualRoot(PDT
->getNode(NCD
)))
67 void MachinePostDominatorTree::verifyAnalysis() const {
68 if (PDT
&& VerifyMachineDomInfo
)
69 if (!PDT
->verify(PostDomTreeT::VerificationLevel::Basic
)) {
70 errs() << "MachinePostDominatorTree verification failed\n";
76 void MachinePostDominatorTree::print(llvm::raw_ostream
&OS
,
77 const Module
*M
) const {