MachinePostDominators.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===- MachinePostDominators.cpp -Machine Post Dominator Calculation ------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements simple dominator construction algorithms for finding
  10. // post dominators on machine functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachinePostDominators.h"
  14. #include "llvm/InitializePasses.h"
  15. using namespace llvm;
  16. namespace llvm {
  17. template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTreeBase
  18. extern bool VerifyMachineDomInfo;
  19. } // namespace llvm
  20. char MachinePostDominatorTree::ID = 0;
  21. //declare initializeMachinePostDominatorTreePass
  22. INITIALIZE_PASS(MachinePostDominatorTree, "machinepostdomtree",
  23. "MachinePostDominator Tree Construction", true, true)
  24. MachinePostDominatorTree::MachinePostDominatorTree()
  25. : MachineFunctionPass(ID), PDT(nullptr) {
  26. initializeMachinePostDominatorTreePass(*PassRegistry::getPassRegistry());
  27. }
  28. FunctionPass *MachinePostDominatorTree::createMachinePostDominatorTreePass() {
  29. return new MachinePostDominatorTree();
  30. }
  31. bool MachinePostDominatorTree::runOnMachineFunction(MachineFunction &F) {
  32. PDT = std::make_unique<PostDomTreeT>();
  33. PDT->recalculate(F);
  34. return false;
  35. }
  36. void MachinePostDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
  37. AU.setPreservesAll();
  38. MachineFunctionPass::getAnalysisUsage(AU);
  39. }
  40. MachineBasicBlock *MachinePostDominatorTree::findNearestCommonDominator(
  41. ArrayRef<MachineBasicBlock *> Blocks) const {
  42. assert(!Blocks.empty());
  43. MachineBasicBlock *NCD = Blocks.front();
  44. for (MachineBasicBlock *BB : Blocks.drop_front()) {
  45. NCD = PDT->findNearestCommonDominator(NCD, BB);
  46. // Stop when the root is reached.
  47. if (PDT->isVirtualRoot(PDT->getNode(NCD)))
  48. return nullptr;
  49. }
  50. return NCD;
  51. }
  52. void MachinePostDominatorTree::verifyAnalysis() const {
  53. if (PDT && VerifyMachineDomInfo)
  54. if (!PDT->verify(PostDomTreeT::VerificationLevel::Basic)) {
  55. errs() << "MachinePostDominatorTree verification failed\n";
  56. abort();
  57. }
  58. }
  59. void MachinePostDominatorTree::print(llvm::raw_ostream &OS,
  60. const Module *M) const {
  61. PDT->print(OS);
  62. }