LLVM  10.0.0svn
EdgeBundles.cpp
Go to the documentation of this file.
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 
16 #include "llvm/CodeGen/Passes.h"
20 
21 using namespace llvm;
22 
23 static cl::opt<bool>
24 ViewEdgeBundles("view-edge-bundles", cl::Hidden,
25  cl::desc("Pop up a window to show edge bundle graphs"));
26 
27 char EdgeBundles::ID = 0;
28 
29 INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
30  /* cfg = */true, /* is_analysis = */ true)
31 
33 
34 void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
35  AU.setPreservesAll();
37 }
38 
39 bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
40  MF = &mf;
41  EC.clear();
42  EC.grow(2 * MF->getNumBlockIDs());
43 
44  for (const auto &MBB : *MF) {
45  unsigned OutE = 2 * MBB.getNumber() + 1;
46  // Join the outgoing bundle with the ingoing bundles of all successors.
47  for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
48  SE = MBB.succ_end(); SI != SE; ++SI)
49  EC.join(OutE, 2 * (*SI)->getNumber());
50  }
51  EC.compress();
52  if (ViewEdgeBundles)
53  view();
54 
55  // Compute the reverse mapping.
56  Blocks.clear();
57  Blocks.resize(getNumBundles());
58 
59  for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
60  unsigned b0 = getBundle(i, false);
61  unsigned b1 = getBundle(i, true);
62  Blocks[b0].push_back(i);
63  if (b1 != b0)
64  Blocks[b1].push_back(i);
65  }
66 
67  return false;
68 }
69 
70 /// Specialize WriteGraph, the standard implementation won't work.
71 namespace llvm {
72 
73 template<>
74 raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
75  bool ShortNames,
76  const Twine &Title) {
77  const MachineFunction *MF = G.getMachineFunction();
78 
79  O << "digraph {\n";
80  for (const auto &MBB : *MF) {
81  unsigned BB = MBB.getNumber();
82  O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box ]\n"
83  << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
84  << "\"\n"
85  << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
86  << '\n';
87  for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
88  SE = MBB.succ_end(); SI != SE; ++SI)
89  O << "\t\"" << printMBBReference(MBB) << "\" -> \""
90  << printMBBReference(**SI) << "\" [ color=lightgray ]\n";
91  }
92  O << "}\n";
93  return O;
94 }
95 
96 } // end namespace llvm
97 
98 /// view - Visualize the annotated bipartite CFG with Graphviz.
99 void EdgeBundles::view() const {
100  ViewGraph(*this, "EdgeBundles");
101 }
void grow(unsigned N)
grow - Increase capacity to hold 0 .
unsigned getBundle(unsigned N, bool Out) const
getBundle - Return the ingoing (Out = false) or outgoing (Out = true) bundle number for basic block N...
Definition: EdgeBundles.h:42
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID&#39;s allocated.
void compress()
compress - Compress equivalence classes by numbering them 0 .
void clear()
clear - Clear all classes so that grow() will assign a unique class to every integer.
Definition: IntEqClasses.h:50
*ViewGraph Emit a dot run run gv on the postscript *then cleanup For use from the debugger *void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:366
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
static char ID
Definition: EdgeBundles.h:37
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:33
unsigned join(unsigned a, unsigned b)
Join the equivalence classes of a and b.
void view() const
view - Visualize the annotated bipartite CFG with Graphviz.
Definition: EdgeBundles.cpp:99
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
unsigned getNumBundles() const
getNumBundles - Return the total number of bundles in the CFG.
Definition: EdgeBundles.h:45
static cl::opt< bool > ViewEdgeBundles("view-edge-bundles", cl::Hidden, cl::desc("Pop up a window to show edge bundle graphs"))
char & EdgeBundlesID
EdgeBundles analysis - Bundle machine CFG edges.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45