1 //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the implementation of the EdgeBundles analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/EdgeBundles.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/GraphWriter.h"
20 #include "llvm/Support/raw_ostream.h"
25 ViewEdgeBundles("view-edge-bundles", cl::Hidden,
26 cl::desc("Pop up a window to show edge bundle graphs"));
28 char EdgeBundles::ID = 0;
30 INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
31 /* cfg = */true, /* analysis = */ true)
33 char &llvm::EdgeBundlesID = EdgeBundles::ID;
35 void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
37 MachineFunctionPass::getAnalysisUsage(AU);
40 bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
43 EC.grow(2 * MF->getNumBlockIDs());
45 for (const auto &MBB : *MF) {
46 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());
56 // Compute the reverse mapping.
58 Blocks.resize(getNumBundles());
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);
65 Blocks[b1].push_back(i);
71 /// Specialize WriteGraph, the standard implementation won't work.
75 raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
78 const MachineFunction *MF = G.getMachineFunction();
81 for (const auto &MBB : *MF) {
82 unsigned BB = MBB.getNumber();
83 O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
84 << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
85 << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
86 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
87 SE = MBB.succ_end(); SI != SE; ++SI)
88 O << "\t\"BB#" << BB << "\" -> \"BB#" << (*SI)->getNumber()
89 << "\" [ color=lightgray ]\n";
95 } // end namespace llvm
97 /// view - Visualize the annotated bipartite CFG with Graphviz.
98 void EdgeBundles::view() const {
99 ViewGraph(*this, "EdgeBundles");