EdgeBundles.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
  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 provides the implementation of the EdgeBundles analysis.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/EdgeBundles.h"
  14. #include "llvm/CodeGen/MachineBasicBlock.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/GraphWriter.h"
  19. using namespace llvm;
  20. static cl::opt<bool>
  21. ViewEdgeBundles("view-edge-bundles", cl::Hidden,
  22. cl::desc("Pop up a window to show edge bundle graphs"));
  23. char EdgeBundles::ID = 0;
  24. INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
  25. /* cfg = */true, /* analysis = */ true)
  26. char &llvm::EdgeBundlesID = EdgeBundles::ID;
  27. void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
  28. AU.setPreservesAll();
  29. MachineFunctionPass::getAnalysisUsage(AU);
  30. }
  31. bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
  32. MF = &mf;
  33. EC.clear();
  34. EC.grow(2 * MF->getNumBlockIDs());
  35. for (const auto &MBB : *MF) {
  36. unsigned OutE = 2 * MBB.getNumber() + 1;
  37. // Join the outgoing bundle with the ingoing bundles of all successors.
  38. for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
  39. SE = MBB.succ_end(); SI != SE; ++SI)
  40. EC.join(OutE, 2 * (*SI)->getNumber());
  41. }
  42. EC.compress();
  43. if (ViewEdgeBundles)
  44. view();
  45. // Compute the reverse mapping.
  46. Blocks.clear();
  47. Blocks.resize(getNumBundles());
  48. for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
  49. unsigned b0 = getBundle(i, 0);
  50. unsigned b1 = getBundle(i, 1);
  51. Blocks[b0].push_back(i);
  52. if (b1 != b0)
  53. Blocks[b1].push_back(i);
  54. }
  55. return false;
  56. }
  57. /// Specialize WriteGraph, the standard implementation won't work.
  58. namespace llvm {
  59. template<>
  60. raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
  61. bool ShortNames,
  62. const Twine &Title) {
  63. const MachineFunction *MF = G.getMachineFunction();
  64. O << "digraph {\n";
  65. for (const auto &MBB : *MF) {
  66. unsigned BB = MBB.getNumber();
  67. O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
  68. << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
  69. << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
  70. for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
  71. SE = MBB.succ_end(); SI != SE; ++SI)
  72. O << "\t\"BB#" << BB << "\" -> \"BB#" << (*SI)->getNumber()
  73. << "\" [ color=lightgray ]\n";
  74. }
  75. O << "}\n";
  76. return O;
  77. }
  78. }
  79. /// view - Visualize the annotated bipartite CFG with Graphviz.
  80. void EdgeBundles::view() const {
  81. ViewGraph(*this, "EdgeBundles");
  82. }