blob: 486720cadd270f6386fd2238133d6a37474e868e [file] [log] [blame]
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +00001//===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file provides the implementation of the EdgeBundles analysis.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/EdgeBundles.h"
14#include "llvm/CodeGen/MachineBasicBlock.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000017#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000018#include "llvm/Support/GraphWriter.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000019#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000020
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",
Rui Ueyama49a3ad22019-07-16 04:46:31 +000030 /* cfg = */true, /* is_analysis = */ true)
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000031
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
Alexey Samsonov41b977d2014-04-30 18:29:51 +000044 for (const auto &MBB : *MF) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000045 unsigned OutE = 2 * MBB.getNumber() + 1;
46 // Join the outgoing bundle with the ingoing bundles of all successors.
47 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
48 SE = MBB.succ_end(); SI != SE; ++SI)
49 EC.join(OutE, 2 * (*SI)->getNumber());
50 }
51 EC.compress();
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000052 if (ViewEdgeBundles)
53 view();
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000054
55 // Compute the reverse mapping.
56 Blocks.clear();
57 Blocks.resize(getNumBundles());
58
59 for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
Eugene Zelenko1804a772016-08-25 00:45:04 +000060 unsigned b0 = getBundle(i, false);
61 unsigned b1 = getBundle(i, true);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000062 Blocks[b0].push_back(i);
63 if (b1 != b0)
64 Blocks[b1].push_back(i);
65 }
66
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000067 return false;
68}
69
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000070/// Specialize WriteGraph, the standard implementation won't work.
Richard Smitha4b7cfd2014-04-24 18:49:15 +000071namespace llvm {
Eugene Zelenko1804a772016-08-25 00:45:04 +000072
Richard Smith0d9ec712014-04-24 18:27:29 +000073template<>
Richard Smitha4b7cfd2014-04-24 18:49:15 +000074raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
75 bool ShortNames,
76 const Twine &Title) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000077 const MachineFunction *MF = G.getMachineFunction();
78
79 O << "digraph {\n";
Alexey Samsonov41b977d2014-04-30 18:29:51 +000080 for (const auto &MBB : *MF) {
81 unsigned BB = MBB.getNumber();
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000082 O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box ]\n"
83 << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
84 << "\"\n"
85 << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
86 << '\n';
Alexey Samsonov41b977d2014-04-30 18:29:51 +000087 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
88 SE = MBB.succ_end(); SI != SE; ++SI)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000089 O << "\t\"" << printMBBReference(MBB) << "\" -> \""
90 << printMBBReference(**SI) << "\" [ color=lightgray ]\n";
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000091 }
92 O << "}\n";
93 return O;
94}
Eugene Zelenko1804a772016-08-25 00:45:04 +000095
96} // end namespace llvm
Richard Smith0d9ec712014-04-24 18:27:29 +000097
98/// view - Visualize the annotated bipartite CFG with Graphviz.
99void EdgeBundles::view() const {
100 ViewGraph(*this, "EdgeBundles");
101}