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" |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Jakob Stoklund Olesen | 6b705d4 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 24 | static cl::opt<bool> |
| 25 | ViewEdgeBundles("view-edge-bundles", cl::Hidden, |
| 26 | cl::desc("Pop up a window to show edge bundle graphs")); |
| 27 | |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 28 | char EdgeBundles::ID = 0; |
| 29 | |
| 30 | INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges", |
| 31 | /* cfg = */true, /* analysis = */ true) |
| 32 | |
| 33 | char &llvm::EdgeBundlesID = EdgeBundles::ID; |
| 34 | |
| 35 | void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const { |
| 36 | AU.setPreservesAll(); |
| 37 | MachineFunctionPass::getAnalysisUsage(AU); |
| 38 | } |
| 39 | |
| 40 | bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) { |
| 41 | MF = &mf; |
| 42 | EC.clear(); |
Anna Zaks | 3c397eb | 2011-06-16 00:03:21 +0000 | [diff] [blame] | 43 | EC.grow(2 * MF->getNumBlockIDs()); |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 44 | |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 45 | for (const auto &MBB : *MF) { |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 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()); |
| 51 | } |
| 52 | EC.compress(); |
Jakob Stoklund Olesen | 6b705d4 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 53 | if (ViewEdgeBundles) |
| 54 | view(); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 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) { |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 61 | unsigned b0 = getBundle(i, false); |
| 62 | unsigned b1 = getBundle(i, true); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 63 | Blocks[b0].push_back(i); |
| 64 | if (b1 != b0) |
| 65 | Blocks[b1].push_back(i); |
| 66 | } |
| 67 | |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 71 | /// Specialize WriteGraph, the standard implementation won't work. |
Richard Smith | d24fc95 | 2014-04-24 18:49:15 +0000 | [diff] [blame] | 72 | namespace llvm { |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 73 | |
Richard Smith | 778aacb | 2014-04-24 18:27:29 +0000 | [diff] [blame] | 74 | template<> |
Richard Smith | d24fc95 | 2014-04-24 18:49:15 +0000 | [diff] [blame] | 75 | raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G, |
| 76 | bool ShortNames, |
| 77 | const Twine &Title) { |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 78 | const MachineFunction *MF = G.getMachineFunction(); |
| 79 | |
| 80 | O << "digraph {\n"; |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 81 | for (const auto &MBB : *MF) { |
| 82 | unsigned BB = MBB.getNumber(); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 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'; |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 88 | for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), |
| 89 | SE = MBB.succ_end(); SI != SE; ++SI) |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 90 | O << "\t\"" << printMBBReference(MBB) << "\" -> \"" |
| 91 | << printMBBReference(**SI) << "\" [ color=lightgray ]\n"; |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 92 | } |
| 93 | O << "}\n"; |
| 94 | return O; |
| 95 | } |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 96 | |
| 97 | } // end namespace llvm |
Richard Smith | 778aacb | 2014-04-24 18:27:29 +0000 | [diff] [blame] | 98 | |
| 99 | /// view - Visualize the annotated bipartite CFG with Graphviz. |
| 100 | void EdgeBundles::view() const { |
| 101 | ViewGraph(*this, "EdgeBundles"); |
| 102 | } |