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/CodeGen/MachineBasicBlock.h"
  14. #include "llvm/CodeGen/MachineFunction.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/GraphWriter.h"
  18. #include "llvm/Support/raw_ostream.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, /* is_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, false);
  50. unsigned b1 = getBundle(i, true);
  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\"" << printMBBReference(MBB) << "\" [ shape=box ]\n"
  68. << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
  69. << "\"\n"
  70. << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
  71. << '\n';
  72. for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
  73. SE = MBB.succ_end(); SI != SE; ++SI)
  74. O << "\t\"" << printMBBReference(MBB) << "\" -> \""
  75. << printMBBReference(**SI) << "\" [ 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. }