LLVM 19.0.0git
EdgeBundles.cpp
Go to the documentation of this file.
1//===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides the implementation of the EdgeBundles analysis.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/Passes.h"
22
23using namespace llvm;
24
25static cl::opt<bool>
26ViewEdgeBundles("view-edge-bundles", cl::Hidden,
27 cl::desc("Pop up a window to show edge bundle graphs"));
28
29char EdgeBundles::ID = 0;
30
31INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
32 /* cfg = */true, /* is_analysis = */ true)
33
35
36void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.setPreservesAll();
39}
40
41bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
42 MF = &mf;
43 EC.clear();
44 EC.grow(2 * MF->getNumBlockIDs());
45
46 for (const auto &MBB : *MF) {
47 unsigned OutE = 2 * MBB.getNumber() + 1;
48 // Join the outgoing bundle with the ingoing bundles of all successors.
49 for (const MachineBasicBlock *Succ : MBB.successors())
50 EC.join(OutE, 2 * Succ->getNumber());
51 }
52 EC.compress();
54 view();
55
56 // Compute the reverse mapping.
57 Blocks.clear();
58 Blocks.resize(getNumBundles());
59
60 for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
61 unsigned b0 = getBundle(i, false);
62 unsigned b1 = getBundle(i, true);
63 Blocks[b0].push_back(i);
64 if (b1 != b0)
65 Blocks[b1].push_back(i);
66 }
67
68 return false;
69}
70
71namespace llvm {
72
73/// Specialize WriteGraph, the standard implementation won't work.
74template<>
75raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
76 bool ShortNames,
77 const Twine &Title) {
78 const MachineFunction *MF = G.getMachineFunction();
79
80 O << "digraph {\n";
81 for (const auto &MBB : *MF) {
82 unsigned BB = MBB.getNumber();
83 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';
88 for (const MachineBasicBlock *Succ : MBB.successors())
89 O << "\t\"" << printMBBReference(MBB) << "\" -> \""
90 << printMBBReference(*Succ) << "\" [ color=lightgray ]\n";
91 }
92 O << "}\n";
93 return O;
94}
95
96} // end namespace llvm
97
98/// view - Visualize the annotated bipartite CFG with Graphviz.
99void EdgeBundles::view() const {
100 ViewGraph(*this, "EdgeBundles");
101}
MachineBasicBlock & MBB
static cl::opt< bool > ViewEdgeBundles("view-edge-bundles", cl::Hidden, cl::desc("Pop up a window to show edge bundle graphs"))
#define G(x, y, z)
Definition: MD5.cpp:56
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
Represent the analysis usage information of a pass.
unsigned getBundle(unsigned N, bool Out) const
getBundle - Return the ingoing (Out = false) or outgoing (Out = true) bundle number for basic block N
Definition: EdgeBundles.h:41
unsigned getNumBundles() const
getNumBundles - Return the total number of bundles in the CFG.
Definition: EdgeBundles.h:44
void view() const
view - Visualize the annotated bipartite CFG with Graphviz.
Definition: EdgeBundles.cpp:99
static char ID
Definition: EdgeBundles.h:36
void compress()
compress - Compress equivalence classes by numbering them 0 .
void clear()
clear - Clear all classes so that grow() will assign a unique class to every integer.
Definition: IntEqClasses.h:51
unsigned join(unsigned a, unsigned b)
Join the equivalence classes of a and b.
void grow(unsigned N)
grow - Increase capacity to hold 0 .
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
iterator_range< succ_iterator > successors()
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & EdgeBundlesID
EdgeBundles analysis - Bundle machine CFG edges.
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
Definition: GraphWriter.h:427
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.