EdgeBundles.cpp 3.1 KB

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