Turn the EdgeBundles class into a stand-alone machine CFG analysis pass.
[oota-llvm.git] / lib / CodeGen / EdgeBundles.cpp
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
14 #include "llvm/CodeGen/EdgeBundles.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/Support/GraphWriter.h"
19
20 using namespace llvm;
21
22 char EdgeBundles::ID = 0;
23
24 INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
25                 /* cfg = */true, /* analysis = */ true)
26
27 char &llvm::EdgeBundlesID = EdgeBundles::ID;
28
29 void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
30   AU.setPreservesAll();
31   MachineFunctionPass::getAnalysisUsage(AU);
32 }
33
34 bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
35   MF = &mf;
36   EC.clear();
37   EC.grow(2 * MF->size());
38
39   for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
40        ++I) {
41     const MachineBasicBlock &MBB = *I;
42     unsigned OutE = 2 * MBB.getNumber() + 1;
43     // Join the outgoing bundle with the ingoing bundles of all successors.
44     for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
45            SE = MBB.succ_end(); SI != SE; ++SI)
46       EC.join(OutE, 2 * (*SI)->getNumber());
47   }
48   EC.compress();
49   return false;
50 }
51
52 /// view - Visualize the annotated bipartite CFG with Graphviz.
53 void EdgeBundles::view() const {
54   ViewGraph(*this, "EdgeBundles");
55 }
56
57 /// Specialize WriteGraph, the standard implementation won't work.
58 raw_ostream &llvm::WriteGraph(raw_ostream &O, const EdgeBundles &G,
59                               bool ShortNames,
60                               const std::string &Title) {
61   const MachineFunction *MF = G.getMachineFunction();
62
63   O << "digraph {\n";
64   for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
65        I != E; ++I) {
66     unsigned BB = I->getNumber();
67     O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
68       << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
69       << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
70     for (MachineBasicBlock::const_succ_iterator SI = I->succ_begin(),
71            SE = I->succ_end(); SI != SE; ++SI)
72       O << "\t\"BB#" << BB << "\" -> \"BB#" << (*SI)->getNumber()
73         << "\" [ color=lightgray ]\n";
74   }
75   O << "}\n";
76   return O;
77 }
78
79