MachineLoopInfo.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/CodeGen/MachineDominators.h"
  18. #include "llvm/CodeGen/Passes.h"
  19. #include "llvm/Support/Debug.h"
  20. using namespace llvm;
  21. namespace llvm {
  22. #define MLB class LoopBase<MachineBasicBlock, MachineLoop>
  23. TEMPLATE_INSTANTIATION(MLB);
  24. #undef MLB
  25. #define MLIB class LoopInfoBase<MachineBasicBlock, MachineLoop>
  26. TEMPLATE_INSTANTIATION(MLIB);
  27. #undef MLIB
  28. }
  29. char MachineLoopInfo::ID = 0;
  30. INITIALIZE_PASS(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.Calculate(getAnalysis<MachineDominatorTree>().getBase()); // Update
  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 = prior(MachineFunction::iterator(TopMBB));
  48. while (contains(PriorMBB)) {
  49. TopMBB = PriorMBB;
  50. if (TopMBB == Begin) break;
  51. PriorMBB = prior(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 != prior(End)) {
  60. MachineBasicBlock *NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
  61. while (contains(NextMBB)) {
  62. BotMBB = NextMBB;
  63. if (BotMBB == llvm::next(MachineFunction::iterator(BotMBB))) break;
  64. NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
  65. }
  66. }
  67. return BotMBB;
  68. }
  69. void MachineLoop::dump() const {
  70. print(dbgs());
  71. }