blob: 54c53eb1631222a0dc0439c6f47c29c7e8bd86c2 [file] [log] [blame]
Jakob Stoklund Olesen8dd070e2011-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 Olesen6b705d42011-01-05 21:50:24 +000018#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000019#include "llvm/Support/GraphWriter.h"
Eugene Zelenko3d7ca1c2016-08-25 00:45:04 +000020#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000021
22using namespace llvm;
23
Jakob Stoklund Olesen6b705d42011-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 Olesen8dd070e2011-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 Zaks3c397eb2011-06-16 00:03:21 +000043 EC.grow(2 * MF->getNumBlockIDs());
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000044
Alexey Samsonov4aef7272014-04-30 18:29:51 +000045 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen8dd070e2011-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 Olesen6b705d42011-01-05 21:50:24 +000053 if (ViewEdgeBundles)
54 view();
Jakob Stoklund Olesenf4afdfc2011-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 Zelenko3d7ca1c2016-08-25 00:45:04 +000061 unsigned b0 = getBundle(i, false);
62 unsigned b1 = getBundle(i, true);
Jakob Stoklund Olesenf4afdfc2011-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 Olesen8dd070e2011-01-04 21:10:05 +000068 return false;
69}
70
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000071/// Specialize WriteGraph, the standard implementation won't work.
Richard Smithd24fc952014-04-24 18:49:15 +000072namespace llvm {
Eugene Zelenko3d7ca1c2016-08-25 00:45:04 +000073
Richard Smith778aacb2014-04-24 18:27:29 +000074template<>
Richard Smithd24fc952014-04-24 18:49:15 +000075raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
76 bool ShortNames,
77 const Twine &Title) {
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000078 const MachineFunction *MF = G.getMachineFunction();
79
80 O << "digraph {\n";
Alexey Samsonov4aef7272014-04-30 18:29:51 +000081 for (const auto &MBB : *MF) {
82 unsigned BB = MBB.getNumber();
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +000083 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';
Alexey Samsonov4aef7272014-04-30 18:29:51 +000088 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
89 SE = MBB.succ_end(); SI != SE; ++SI)
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +000090 O << "\t\"" << printMBBReference(MBB) << "\" -> \""
91 << printMBBReference(**SI) << "\" [ color=lightgray ]\n";
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000092 }
93 O << "}\n";
94 return O;
95}
Eugene Zelenko3d7ca1c2016-08-25 00:45:04 +000096
97} // end namespace llvm
Richard Smith778aacb2014-04-24 18:27:29 +000098
99/// view - Visualize the annotated bipartite CFG with Graphviz.
100void EdgeBundles::view() const {
101 ViewGraph(*this, "EdgeBundles");
102}