LLVM 20.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
30
32 "Bundle Machine CFG Edges",
33 /* cfg = */ true, /* is_analysis = */ true)
34
36
37void EdgeBundlesWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll();
40}
41
42AnalysisKey EdgeBundlesAnalysis::Key;
43
46 EdgeBundles Impl(MF);
47 return Impl;
48}
49
50bool EdgeBundlesWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
51 Impl.reset(new EdgeBundles(MF));
52 return false;
53}
54
55EdgeBundles::EdgeBundles(MachineFunction &MF) : MF(&MF) { init(); }
56
57void EdgeBundles::init() {
58 EC.clear();
59 EC.grow(2 * MF->getNumBlockIDs());
60
61 for (const auto &MBB : *MF) {
62 unsigned OutE = 2 * MBB.getNumber() + 1;
63 // Join the outgoing bundle with the ingoing bundles of all successors.
64 for (const MachineBasicBlock *Succ : MBB.successors())
65 EC.join(OutE, 2 * Succ->getNumber());
66 }
67 EC.compress();
69 view();
70
71 // Compute the reverse mapping.
72 Blocks.clear();
73 Blocks.resize(getNumBundles());
74
75 for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
76 unsigned b0 = getBundle(i, false);
77 unsigned b1 = getBundle(i, true);
78 Blocks[b0].push_back(i);
79 if (b1 != b0)
80 Blocks[b1].push_back(i);
81 }
82}
83
84namespace llvm {
85
86/// Specialize WriteGraph, the standard implementation won't work.
87template<>
88raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
89 bool ShortNames,
90 const Twine &Title) {
91 const MachineFunction *MF = G.getMachineFunction();
92
93 O << "digraph {\n";
94 for (const auto &MBB : *MF) {
95 unsigned BB = MBB.getNumber();
96 O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box, label=\""
97 << printMBBReference(MBB) << "\" ]\n"
98 << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB)
99 << "\"\n"
100 << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true)
101 << '\n';
102 for (const MachineBasicBlock *Succ : MBB.successors())
103 O << "\t\"" << printMBBReference(MBB) << "\" -> \""
104 << printMBBReference(*Succ) << "\" [ color=lightgray ]\n";
105 }
106 O << "}\n";
107 return O;
108}
109
110} // end namespace llvm
111
112/// view - Visualize the annotated bipartite CFG with Graphviz.
113void EdgeBundles::view() const {
114 ViewGraph(*this, "EdgeBundles");
115}
116
119 // Invalidated when CFG is not preserved
120 auto PAC = PA.getChecker<EdgeBundlesAnalysis>();
121 return !PAC.preserved() && !PAC.preservedSet<CFGAnalyses>() &&
122 !PAC.preservedSet<AllAnalysesOn<MachineFunction>>();
123}
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
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:49
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
EdgeBundles run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition: EdgeBundles.cpp:44
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
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:47
unsigned getNumBundles() const
getNumBundles - Return the total number of bundles in the CFG.
Definition: EdgeBundles.h:50
void view() const
view - Visualize the annotated bipartite CFG with Graphviz.
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.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:264
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
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
char & EdgeBundlesWrapperLegacyID
EdgeBundles analysis - Bundle machine CFG edges.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28