EdgeBundles.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
  41. SE = MBB.succ_end(); SI != SE; ++SI)
  42. EC.join(OutE, 2 * (*SI)->getNumber());
  43. }
  44. EC.compress();
  45. if (ViewEdgeBundles)
  46. view();
  47. // Compute the reverse mapping.
  48. Blocks.clear();
  49. Blocks.resize(getNumBundles());
  50. for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
  51. unsigned b0 = getBundle(i, false);
  52. unsigned b1 = getBundle(i, true);
  53. Blocks[b0].push_back(i);
  54. if (b1 != b0)
  55. Blocks[b1].push_back(i);
  56. }
  57. return false;
  58. }
  59. /// Specialize WriteGraph, the standard implementation won't work.
  60. namespace llvm {
  61. template<>
  62. raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
  63. bool ShortNames,
  64. const Twine &Title) {
  65. const MachineFunction *MF = G.getMachineFunction();
  66. O << "digraph {\n";
  67. for (const auto &MBB : *MF) {
  68. unsigned BB = MBB.getNumber();
  69. O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box ]\n"
  70. << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
  71. << "\"\n"
  72. << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
  73. << '\n';
  74. for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
  75. SE = MBB.succ_end(); SI != SE; ++SI)
  76. O << "\t\"" << printMBBReference(MBB) << "\" -> \""
  77. << printMBBReference(**SI) << "\" [ color=lightgray ]\n";
  78. }
  79. O << "}\n";
  80. return O;
  81. }
  82. } // end namespace llvm
  83. /// view - Visualize the annotated bipartite CFG with Graphviz.
  84. void EdgeBundles::view() const {
  85. ViewGraph(*this, "EdgeBundles");
  86. }