1 //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the implementation of the EdgeBundles analysis.
12 //===----------------------------------------------------------------------===//
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"
22 char EdgeBundles::ID = 0;
24 INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
25 /* cfg = */true, /* analysis = */ true)
27 char &llvm::EdgeBundlesID = EdgeBundles::ID;
29 void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
31 MachineFunctionPass::getAnalysisUsage(AU);
34 bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
37 EC.grow(2 * MF->size());
39 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
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());
52 /// view - Visualize the annotated bipartite CFG with Graphviz.
53 void EdgeBundles::view() const {
54 ViewGraph(*this, "EdgeBundles");
57 /// Specialize WriteGraph, the standard implementation won't work.
58 raw_ostream &llvm::WriteGraph(raw_ostream &O, const EdgeBundles &G,
60 const std::string &Title) {
61 const MachineFunction *MF = G.getMachineFunction();
64 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
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";