Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 1 | //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the implementation of the EdgeBundles analysis. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/EdgeBundles.h" |
| 15 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | 6b705d4 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/GraphWriter.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
Jakob Stoklund Olesen | 6b705d4 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 23 | static cl::opt<bool> |
| 24 | ViewEdgeBundles("view-edge-bundles", cl::Hidden, |
| 25 | cl::desc("Pop up a window to show edge bundle graphs")); |
| 26 | |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 27 | char EdgeBundles::ID = 0; |
| 28 | |
| 29 | INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges", |
| 30 | /* cfg = */true, /* analysis = */ true) |
| 31 | |
| 32 | char &llvm::EdgeBundlesID = EdgeBundles::ID; |
| 33 | |
| 34 | void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const { |
| 35 | AU.setPreservesAll(); |
| 36 | MachineFunctionPass::getAnalysisUsage(AU); |
| 37 | } |
| 38 | |
| 39 | bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) { |
| 40 | MF = &mf; |
| 41 | EC.clear(); |
Anna Zaks | 3c397eb | 2011-06-16 00:03:21 +0000 | [diff] [blame] | 42 | EC.grow(2 * MF->getNumBlockIDs()); |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 43 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 44 | for (const auto &MBB : *MF) { |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 45 | 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 Olesen | 6b705d4 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 52 | if (ViewEdgeBundles) |
| 53 | view(); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 54 | |
| 55 | // Compute the reverse mapping. |
| 56 | Blocks.clear(); |
| 57 | Blocks.resize(getNumBundles()); |
| 58 | |
| 59 | for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) { |
| 60 | unsigned b0 = getBundle(i, 0); |
| 61 | unsigned b1 = getBundle(i, 1); |
| 62 | Blocks[b0].push_back(i); |
| 63 | if (b1 != b0) |
| 64 | Blocks[b1].push_back(i); |
| 65 | } |
| 66 | |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 70 | /// Specialize WriteGraph, the standard implementation won't work. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 71 | namespace llvm { |
| 72 | template<> |
| 73 | raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G, |
| 74 | bool ShortNames, |
| 75 | const Twine &Title) { |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 76 | const MachineFunction *MF = G.getMachineFunction(); |
| 77 | |
| 78 | O << "digraph {\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 79 | for (const auto &MBB : *MF) { |
| 80 | unsigned BB = MBB.getNumber(); |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 81 | O << "\t\"BB#" << BB << "\" [ shape=box ]\n" |
| 82 | << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n" |
| 83 | << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n'; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 84 | for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), |
| 85 | SE = MBB.succ_end(); SI != SE; ++SI) |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 86 | O << "\t\"BB#" << BB << "\" -> \"BB#" << (*SI)->getNumber() |
| 87 | << "\" [ color=lightgray ]\n"; |
| 88 | } |
| 89 | O << "}\n"; |
| 90 | return O; |
| 91 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 92 | } |
| 93 | |
| 94 | /// view - Visualize the annotated bipartite CFG with Graphviz. |
| 95 | void EdgeBundles::view() const { |
| 96 | ViewGraph(*this, "EdgeBundles"); |
| 97 | } |