MachineLoopInfo.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the MachineLoopInfo class that is used to identify natural
  11. // loops and determine the loop depth of various nodes of the CFG. Note that
  12. // the loops identified may actually be several natural loops that share the
  13. // same header node... not just a single natural loop.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/CodeGen/MachineLoopInfo.h"
  17. #include "llvm/Analysis/LoopInfoImpl.h"
  18. #include "llvm/CodeGen/MachineDominators.h"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/Support/Debug.h"
  21. using namespace llvm;
  22. // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
  23. template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
  24. template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
  25. char MachineLoopInfo::ID = 0;
  26. INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
  27. "Machine Natural Loop Construction", true, true)
  28. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  29. INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
  30. "Machine Natural Loop Construction", true, true)
  31. char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
  32. bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
  33. releaseMemory();
  34. LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
  35. return false;
  36. }
  37. void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  38. AU.setPreservesAll();
  39. AU.addRequired<MachineDominatorTree>();
  40. MachineFunctionPass::getAnalysisUsage(AU);
  41. }
  42. MachineBasicBlock *MachineLoop::getTopBlock() {
  43. MachineBasicBlock *TopMBB = getHeader();
  44. MachineFunction::iterator Begin = TopMBB->getParent()->begin();
  45. if (TopMBB != Begin) {
  46. MachineBasicBlock *PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
  47. while (contains(PriorMBB)) {
  48. TopMBB = PriorMBB;
  49. if (TopMBB == Begin) break;
  50. PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
  51. }
  52. }
  53. return TopMBB;
  54. }
  55. MachineBasicBlock *MachineLoop::getBottomBlock() {
  56. MachineBasicBlock *BotMBB = getHeader();
  57. MachineFunction::iterator End = BotMBB->getParent()->end();
  58. if (BotMBB != std::prev(End)) {
  59. MachineBasicBlock *NextMBB = std::next(MachineFunction::iterator(BotMBB));
  60. while (contains(NextMBB)) {
  61. BotMBB = NextMBB;
  62. if (BotMBB == std::next(MachineFunction::iterator(BotMBB))) break;
  63. NextMBB = std::next(MachineFunction::iterator(BotMBB));
  64. }
  65. }
  66. return BotMBB;
  67. }
  68. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  69. void MachineLoop::dump() const {
  70. print(dbgs());
  71. }
  72. #endif