blob: f4daff667e86df70202bca25143bda8165e8db5e [file] [log] [blame]
Tom Stellard86af62c2012-09-17 14:08:37 +00001//===- MachinePostDominators.cpp -Machine Post Dominator Calculation ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellard86af62c2012-09-17 14:08:37 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements simple dominator construction algorithms for finding
10// post dominators on machine functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachinePostDominators.h"
15
16using namespace llvm;
17
Jakub Kuderskib292c222017-07-14 18:26:09 +000018namespace llvm {
19template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase
Jakub Kuderski56b52a22019-10-01 15:23:27 +000020
21extern bool VerifyMachineDomInfo;
22} // namespace llvm
Jakub Kuderskib292c222017-07-14 18:26:09 +000023
Tom Stellard86af62c2012-09-17 14:08:37 +000024char MachinePostDominatorTree::ID = 0;
25
26//declare initializeMachinePostDominatorTreePass
27INITIALIZE_PASS(MachinePostDominatorTree, "machinepostdomtree",
28 "MachinePostDominator Tree Construction", true, true)
29
Jakub Kuderski269bd152019-09-25 14:04:36 +000030MachinePostDominatorTree::MachinePostDominatorTree()
31 : MachineFunctionPass(ID), PDT(nullptr) {
Tom Stellard86af62c2012-09-17 14:08:37 +000032 initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
Tom Stellard86af62c2012-09-17 14:08:37 +000033}
34
Jakub Kuderski269bd152019-09-25 14:04:36 +000035FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() {
Tom Stellard86af62c2012-09-17 14:08:37 +000036 return new MachinePostDominatorTree();
37}
38
Jakub Kuderski269bd152019-09-25 14:04:36 +000039bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) {
40 PDT = std::make_unique<PostDomTreeT>();
41 PDT->recalculate(F);
Tom Stellard86af62c2012-09-17 14:08:37 +000042 return false;
43}
44
Jakub Kuderski269bd152019-09-25 14:04:36 +000045void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
Tom Stellard86af62c2012-09-17 14:08:37 +000046 AU.setPreservesAll();
47 MachineFunctionPass::getAnalysisUsage(AU);
48}
49
Jakub Kuderski269bd152019-09-25 14:04:36 +000050MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator(
51 ArrayRef<MachineBasicBlock *> Blocks) const {
52 assert(!Blocks.empty());
53
54 MachineBasicBlock *NCD = Blocks.front();
55 for (MachineBasicBlock *BB : Blocks.drop_front()) {
56 NCD = PDT->findNearestCommonDominator(NCD, BB);
57
58 // Stop when the root is reached.
59 if (PDT->isVirtualRoot(PDT->getNode(NCD)))
60 return nullptr;
61 }
62
63 return NCD;
64}
65
Jakub Kuderski56b52a22019-10-01 15:23:27 +000066void MachinePostDominatorTree::verifyAnalysis() const {
67 if (PDT && VerifyMachineDomInfo)
68 if (!PDT->verify(PostDomTreeT::VerificationLevel::Basic)) {
69 errs() << "MachinePostDominatorTree verification failed\n";
70
71 abort();
72 }
73}
74
Jakub Kuderski269bd152019-09-25 14:04:36 +000075void MachinePostDominatorTree::print(llvm::raw_ostream &OS,
76 const Module *M) const {
77 PDT->print(OS);
Tom Stellard86af62c2012-09-17 14:08:37 +000078}