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
13#include "llvm/CodeGen/EdgeBundles.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/InitializePasses.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/GraphWriter.h"
21#include "llvm/Support/raw_ostream.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
29char EdgeBundlesWrapperLegacy::ID = 0;
30
31INITIALIZE_PASS(EdgeBundlesWrapperLegacy, "edge-bundles",
32 "Bundle Machine CFG Edges",
33 /* cfg = */ true, /* is_analysis = */ true)
34
35char &llvm::EdgeBundlesWrapperLegacyID = EdgeBundlesWrapperLegacy::ID;
36
37void EdgeBundlesWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll();
39 MachineFunctionPass::getAnalysisUsage(AU);
40}
41
42AnalysisKey EdgeBundlesAnalysis::Key;
43
44EdgeBundles EdgeBundlesAnalysis::run(MachineFunction &MF,
45 MachineFunctionAnalysisManager &MFAM) {
46 EdgeBundles Impl(MF);
47 return Impl;
48}
49
50bool EdgeBundlesWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
51 Impl.reset(p: 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(N: 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(a: OutE, b: 2 * Succ->getNumber());
66 }
67 EC.compress();
68 if (ViewEdgeBundles)
69 view();
70
71 // Compute the reverse mapping.
72 Blocks.clear();
73 Blocks.resize(N: getNumBundles());
74
75 for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {
76 unsigned b0 = getBundle(N: i, Out: false);
77 unsigned b1 = getBundle(N: i, Out: true);
78 Blocks[b0].push_back(Elt: i);
79 if (b1 != b0)
80 Blocks[b1].push_back(Elt: i);
81 }
82}
83
84/// Specialize WriteGraph, the standard implementation won't work.
85template <>
86raw_ostream &llvm::WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
87 bool ShortNames, const Twine &Title) {
88 const MachineFunction *MF = G.getMachineFunction();
89
90 O << "digraph {\n";
91 for (const auto &MBB : *MF) {
92 unsigned BB = MBB.getNumber();
93 O << "\t\"" << printMBBReference(MBB) << "\" [ shape=box, label=\""
94 << printMBBReference(MBB) << "\" ]\n"
95 << '\t' << G.getBundle(N: BB, Out: false) << " -> \"" << printMBBReference(MBB)
96 << "\"\n"
97 << "\t\"" << printMBBReference(MBB) << "\" -> " << G.getBundle(N: BB, Out: true)
98 << '\n';
99 for (const MachineBasicBlock *Succ : MBB.successors())
100 O << "\t\"" << printMBBReference(MBB) << "\" -> \""
101 << printMBBReference(MBB: *Succ) << "\" [ color=lightgray ]\n";
102 }
103 O << "}\n";
104 return O;
105}
106
107/// view - Visualize the annotated bipartite CFG with Graphviz.
108void EdgeBundles::view() const {
109 ViewGraph(G: *this, Name: "EdgeBundles");
110}
111
112bool EdgeBundles::invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
113 MachineFunctionAnalysisManager::Invalidator &Inv) {
114 // Invalidated when CFG is not preserved
115 auto PAC = PA.getChecker<EdgeBundlesAnalysis>();
116 return !PAC.preserved() && !PAC.preservedSet<CFGAnalyses>() &&
117 !PAC.preservedSet<AllAnalysesOn<MachineFunction>>();
118}
119