blob: 534d476c28eefc6d42fd13323c80c00f7c3b9c2f [file] [log] [blame]
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +00001//===-------- 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"
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000018#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000019#include "llvm/Support/GraphWriter.h"
20
21using namespace llvm;
22
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000023static cl::opt<bool>
24ViewEdgeBundles("view-edge-bundles", cl::Hidden,
25 cl::desc("Pop up a window to show edge bundle graphs"));
26
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000027char EdgeBundles::ID = 0;
28
29INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
30 /* cfg = */true, /* analysis = */ true)
31
32char &llvm::EdgeBundlesID = EdgeBundles::ID;
33
34void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.setPreservesAll();
36 MachineFunctionPass::getAnalysisUsage(AU);
37}
38
39bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
40 MF = &mf;
41 EC.clear();
Anna Zaks2c2aa9a2011-06-16 00:03:21 +000042 EC.grow(2 * MF->getNumBlockIDs());
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000043
44 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
45 ++I) {
46 const MachineBasicBlock &MBB = *I;
47 unsigned OutE = 2 * MBB.getNumber() + 1;
48 // Join the outgoing bundle with the ingoing bundles of all successors.
49 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
50 SE = MBB.succ_end(); SI != SE; ++SI)
51 EC.join(OutE, 2 * (*SI)->getNumber());
52 }
53 EC.compress();
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000054 if (ViewEdgeBundles)
55 view();
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000056
57 // Compute the reverse mapping.
58 Blocks.clear();
59 Blocks.resize(getNumBundles());
60
61 for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
62 unsigned b0 = getBundle(i, 0);
63 unsigned b1 = getBundle(i, 1);
64 Blocks[b0].push_back(i);
65 if (b1 != b0)
66 Blocks[b1].push_back(i);
67 }
68
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000069 return false;
70}
71
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000072/// Specialize WriteGraph, the standard implementation won't work.
Richard Smitha4b7cfd2014-04-24 18:49:15 +000073namespace llvm {
Richard Smith0d9ec712014-04-24 18:27:29 +000074template<>
Richard Smitha4b7cfd2014-04-24 18:49:15 +000075raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
76 bool ShortNames,
77 const Twine &Title) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000078 const MachineFunction *MF = G.getMachineFunction();
79
80 O << "digraph {\n";
81 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
82 I != E; ++I) {
83 unsigned BB = I->getNumber();
84 O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
85 << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
86 << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
87 for (MachineBasicBlock::const_succ_iterator SI = I->succ_begin(),
88 SE = I->succ_end(); SI != SE; ++SI)
89 O << "\t\"BB#" << BB << "\" -> \"BB#" << (*SI)->getNumber()
90 << "\" [ color=lightgray ]\n";
91 }
92 O << "}\n";
93 return O;
94}
Richard Smitha4b7cfd2014-04-24 18:49:15 +000095}
Richard Smith0d9ec712014-04-24 18:27:29 +000096
97/// view - Visualize the annotated bipartite CFG with Graphviz.
98void EdgeBundles::view() const {
99 ViewGraph(*this, "EdgeBundles");
100}