blob: 0b2ffda50a39cd9dbbda5476abbff97b3d14c3bb [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"
Simon Pilgrim89f6ca02020-04-11 12:20:20 +010014#include "llvm/ADT/Twine.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/Passes.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080018#include "llvm/InitializePasses.h"
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000019#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000020#include "llvm/Support/GraphWriter.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000021#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000022
23using namespace llvm;
24
Jakob Stoklund Olesenf3ac7332011-01-05 21:50:24 +000025static cl::opt<bool>
26ViewEdgeBundles("view-edge-bundles", cl::Hidden,
27 cl::desc("Pop up a window to show edge bundle graphs"));
28
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000029char EdgeBundles::ID = 0;
30
31INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
Rui Ueyama49a3ad22019-07-16 04:46:31 +000032 /* cfg = */true, /* is_analysis = */ true)
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000033
34char &llvm::EdgeBundlesID = EdgeBundles::ID;
35
36void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.setPreservesAll();
38 MachineFunctionPass::getAnalysisUsage(AU);
39}
40
41bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
42 MF = &mf;
43 EC.clear();
Anna Zaks2c2aa9a2011-06-16 00:03:21 +000044 EC.grow(2 * MF->getNumBlockIDs());
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000045
Alexey Samsonov41b977d2014-04-30 18:29:51 +000046 for (const auto &MBB : *MF) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000047 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) {
Eugene Zelenko1804a772016-08-25 00:45:04 +000062 unsigned b0 = getBundle(i, false);
63 unsigned b1 = getBundle(i, true);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +000064 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 {
Eugene Zelenko1804a772016-08-25 00:45:04 +000074
Richard Smith0d9ec712014-04-24 18:27:29 +000075template<>
Richard Smitha4b7cfd2014-04-24 18:49:15 +000076raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
77 bool ShortNames,
78 const Twine &Title) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000079 const MachineFunction *MF = G.getMachineFunction();
80
81 O << "digraph {\n";
Alexey Samsonov41b977d2014-04-30 18:29:51 +000082 for (const auto &MBB : *MF) {
83 unsigned BB = MBB.getNumber();
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000084 O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box ]\n"
85 << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
86 << "\"\n"
87 << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
88 << '\n';
Alexey Samsonov41b977d2014-04-30 18:29:51 +000089 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
90 SE = MBB.succ_end(); SI != SE; ++SI)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000091 O << "\t\"" << printMBBReference(MBB) << "\" -> \""
92 << printMBBReference(**SI) << "\" [ color=lightgray ]\n";
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000093 }
94 O << "}\n";
95 return O;
96}
Eugene Zelenko1804a772016-08-25 00:45:04 +000097
98} // end namespace llvm
Richard Smith0d9ec712014-04-24 18:27:29 +000099
100/// view - Visualize the annotated bipartite CFG with Graphviz.
101void EdgeBundles::view() const {
102 ViewGraph(*this, "EdgeBundles");
103}