Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 1 | //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides the implementation of the EdgeBundles analysis. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/CodeGen/EdgeBundles.h" |
| 14 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 15 | #include "llvm/CodeGen/MachineFunction.h" |
| 16 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | f3ac733 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 18 | #include "llvm/Support/GraphWriter.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Jakob Stoklund Olesen | f3ac733 | 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 | f96ae68 | 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", |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 30 | /* cfg = */true, /* is_analysis = */ true) |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 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 | 2c2aa9a | 2011-06-16 00:03:21 +0000 | [diff] [blame] | 42 | EC.grow(2 * MF->getNumBlockIDs()); |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 43 | |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 44 | for (const auto &MBB : *MF) { |
Jakob Stoklund Olesen | f96ae68 | 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 | f3ac733 | 2011-01-05 21:50:24 +0000 | [diff] [blame] | 52 | if (ViewEdgeBundles) |
| 53 | view(); |
Jakob Stoklund Olesen | ed47ed4 | 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) { |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 60 | unsigned b0 = getBundle(i, false); |
| 61 | unsigned b1 = getBundle(i, true); |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 62 | Blocks[b0].push_back(i); |
| 63 | if (b1 != b0) |
| 64 | Blocks[b1].push_back(i); |
| 65 | } |
| 66 | |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 70 | /// Specialize WriteGraph, the standard implementation won't work. |
Richard Smith | a4b7cfd | 2014-04-24 18:49:15 +0000 | [diff] [blame] | 71 | namespace llvm { |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 72 | |
Richard Smith | 0d9ec71 | 2014-04-24 18:27:29 +0000 | [diff] [blame] | 73 | template<> |
Richard Smith | a4b7cfd | 2014-04-24 18:49:15 +0000 | [diff] [blame] | 74 | raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G, |
| 75 | bool ShortNames, |
| 76 | const Twine &Title) { |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 77 | const MachineFunction *MF = G.getMachineFunction(); |
| 78 | |
| 79 | O << "digraph {\n"; |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 80 | for (const auto &MBB : *MF) { |
| 81 | unsigned BB = MBB.getNumber(); |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 82 | O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box ]\n" |
| 83 | << '\t' << G.getBundle(BB, false) << " -> \"" << printMBBReference(MBB) |
| 84 | << "\"\n" |
| 85 | << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(BB, true) |
| 86 | << '\n'; |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 87 | for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), |
| 88 | SE = MBB.succ_end(); SI != SE; ++SI) |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 89 | O << "\t\"" << printMBBReference(MBB) << "\" -> \"" |
| 90 | << printMBBReference(**SI) << "\" [ color=lightgray ]\n"; |
Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 91 | } |
| 92 | O << "}\n"; |
| 93 | return O; |
| 94 | } |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 95 | |
| 96 | } // end namespace llvm |
Richard Smith | 0d9ec71 | 2014-04-24 18:27:29 +0000 | [diff] [blame] | 97 | |
| 98 | /// view - Visualize the annotated bipartite CFG with Graphviz. |
| 99 | void EdgeBundles::view() const { |
| 100 | ViewGraph(*this, "EdgeBundles"); |
| 101 | } |