LLVM  8.0.1
EdgeBundles.cpp
Go to the documentation of this file.
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 
17 #include "llvm/CodeGen/Passes.h"
21 
22 using namespace llvm;
23 
24 static cl::opt<bool>
25 ViewEdgeBundles("view-edge-bundles", cl::Hidden,
26  cl::desc("Pop up a window to show edge bundle graphs"));
27 
28 char EdgeBundles::ID = 0;
29 
30 INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
31  /* cfg = */true, /* analysis = */ true)
32 
34 
35 void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
36  AU.setPreservesAll();
38 }
39 
40 bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
41  MF = &mf;
42  EC.clear();
43  EC.grow(2 * MF->getNumBlockIDs());
44 
45  for (const auto &MBB : *MF) {
46  unsigned OutE = 2 * MBB.getNumber() + 1;
47  // Join the outgoing bundle with the ingoing bundles of all successors.
48  for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
49  SE = MBB.succ_end(); SI != SE; ++SI)
50  EC.join(OutE, 2 * (*SI)->getNumber());
51  }
52  EC.compress();
53  if (ViewEdgeBundles)
54  view();
55 
56  // Compute the reverse mapping.
57  Blocks.clear();
58  Blocks.resize(getNumBundles());
59 
60  for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
61  unsigned b0 = getBundle(i, false);
62  unsigned b1 = getBundle(i, true);
63  Blocks[b0].push_back(i);
64  if (b1 != b0)
65  Blocks[b1].push_back(i);
66  }
67 
68  return false;
69 }
70 
71 /// Specialize WriteGraph, the standard implementation won't work.
72 namespace llvm {
73 
74 template<>
75 raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
76  bool ShortNames,
77  const Twine &Title) {
78  const MachineFunction *MF = G.getMachineFunction();
79 
80  O << "digraph {\n";
81  for (const auto &MBB : *MF) {
82  unsigned BB = MBB.getNumber();
83  O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box ]\n"
84  << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
85  << "\"\n"
86  << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
87  << '\n';
88  for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
89  SE = MBB.succ_end(); SI != SE; ++SI)
90  O << "\t\"" << printMBBReference(MBB) << "\" -> \""
91  << printMBBReference(**SI) << "\" [ color=lightgray ]\n";
92  }
93  O << "}\n";
94  return O;
95 }
96 
97 } // end namespace llvm
98 
99 /// view - Visualize the annotated bipartite CFG with Graphviz.
100 void EdgeBundles::view() const {
101  ViewGraph(*this, "EdgeBundles");
102 }
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:43
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:51
*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:367
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
static char ID
Definition: EdgeBundles.h:38
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:34
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.
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
unsigned getNumBundles() const
getNumBundles - Return the total number of bundles in the CFG.
Definition: EdgeBundles.h:46
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:46