blob: b3a25544be39c1313f4ab890cf45e15f57d6c0e9 [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"
Eugene Zelenko1804a772016-08-25 00:45:04 +000020#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000021
22using namespace llvm;
23
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000024static cl::opt<bool>
25ViewEdgeBundles("view-edge-bundles", cl::Hidden,
26 cl::desc("Pop up a window to show edge bundle graphs"));
27
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000028char EdgeBundles::ID = 0;
29
30INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
31 /* cfg = */true, /* analysis = */ true)
32
33char &llvm::EdgeBundlesID = EdgeBundles::ID;
34
35void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.setPreservesAll();
37 MachineFunctionPass::getAnalysisUsage(AU);
38}
39
40bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
41 MF = &mf;
42 EC.clear();
Anna Zaks2c2aa9a2011-06-16 00:03:21 +000043 EC.grow(2 * MF->getNumBlockIDs());
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000044
Alexey Samsonov41b977d2014-04-30 18:29:51 +000045 for (const auto &MBB : *MF) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000046 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();
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000053 if (ViewEdgeBundles)
54 view();
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000055
56 // Compute the reverse mapping.
57 Blocks.clear();
58 Blocks.resize(getNumBundles());
59
60 for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
Eugene Zelenko1804a772016-08-25 00:45:04 +000061 unsigned b0 = getBundle(i, false);
62 unsigned b1 = getBundle(i, true);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000063 Blocks[b0].push_back(i);
64 if (b1 != b0)
65 Blocks[b1].push_back(i);
66 }
67
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000068 return false;
69}
70
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000071/// Specialize WriteGraph, the standard implementation won't work.
Richard Smitha4b7cfd2014-04-24 18:49:15 +000072namespace llvm {
Eugene Zelenko1804a772016-08-25 00:45:04 +000073
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";
Alexey Samsonov41b977d2014-04-30 18:29:51 +000081 for (const auto &MBB : *MF) {
82 unsigned BB = MBB.getNumber();
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000083 O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
84 << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
85 << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
Alexey Samsonov41b977d2014-04-30 18:29:51 +000086 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
87 SE = MBB.succ_end(); SI != SE; ++SI)
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000088 O << "\t\"BB#" << BB << "\" -> \"BB#" << (*SI)->getNumber()
89 << "\" [ color=lightgray ]\n";
90 }
91 O << "}\n";
92 return O;
93}
Eugene Zelenko1804a772016-08-25 00:45:04 +000094
95} // end namespace llvm
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}