MachineLoopInfo.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
  24. template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
  25. template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
  26. char MachineLoopInfo::ID = 0;
  27. INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
  28. "Machine Natural Loop Construction", true, true)
  29. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  30. INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
  31. "Machine Natural Loop Construction", true, true)
  32. char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
  33. bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
  34. releaseMemory();
  35. LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
  36. return false;
  37. }
  38. void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  39. AU.setPreservesAll();
  40. AU.addRequired<MachineDominatorTree>();
  41. MachineFunctionPass::getAnalysisUsage(AU);
  42. }
  43. MachineBasicBlock *MachineLoop::getTopBlock() {
  44. MachineBasicBlock *TopMBB = getHeader();
  45. MachineFunction::iterator Begin = TopMBB->getParent()->begin();
  46. if (TopMBB != Begin) {
  47. MachineBasicBlock *PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
  48. while (contains(PriorMBB)) {
  49. TopMBB = PriorMBB;
  50. if (TopMBB == Begin) break;
  51. PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
  52. }
  53. }
  54. return TopMBB;
  55. }
  56. MachineBasicBlock *MachineLoop::getBottomBlock() {
  57. MachineBasicBlock *BotMBB = getHeader();
  58. MachineFunction::iterator End = BotMBB->getParent()->end();
  59. if (BotMBB != std::prev(End)) {
  60. MachineBasicBlock *NextMBB = std::next(MachineFunction::iterator(BotMBB));
  61. while (contains(NextMBB)) {
  62. BotMBB = NextMBB;
  63. if (BotMBB == std::next(MachineFunction::iterator(BotMBB))) break;
  64. NextMBB = std::next(MachineFunction::iterator(BotMBB));
  65. }
  66. }
  67. return BotMBB;
  68. }
  69. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  70. void MachineLoop::dump() const {
  71. print(dbgs());
  72. }
  73. #endif