1 //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides the implementation of the EdgeBundles analysis.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/EdgeBundles.h"
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/GraphWriter.h"
19 #include "llvm/Support/raw_ostream.h"
24 ViewEdgeBundles("view-edge-bundles", cl::Hidden
,
25 cl::desc("Pop up a window to show edge bundle graphs"));
27 char EdgeBundles::ID
= 0;
29 INITIALIZE_PASS(EdgeBundles
, "edge-bundles", "Bundle Machine CFG Edges",
30 /* cfg = */true, /* is_analysis = */ true)
32 char &llvm::EdgeBundlesID
= EdgeBundles::ID
;
34 void EdgeBundles::getAnalysisUsage(AnalysisUsage
&AU
) const {
36 MachineFunctionPass::getAnalysisUsage(AU
);
39 bool EdgeBundles::runOnMachineFunction(MachineFunction
&mf
) {
42 EC
.grow(2 * MF
->getNumBlockIDs());
44 for (const auto &MBB
: *MF
) {
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());
55 // Compute the reverse mapping.
57 Blocks
.resize(getNumBundles());
59 for (unsigned i
= 0, e
= MF
->getNumBlockIDs(); i
!= e
; ++i
) {
60 unsigned b0
= getBundle(i
, false);
61 unsigned b1
= getBundle(i
, true);
62 Blocks
[b0
].push_back(i
);
64 Blocks
[b1
].push_back(i
);
70 /// Specialize WriteGraph, the standard implementation won't work.
74 raw_ostream
&WriteGraph
<>(raw_ostream
&O
, const EdgeBundles
&G
,
77 const MachineFunction
*MF
= G
.getMachineFunction();
80 for (const auto &MBB
: *MF
) {
81 unsigned BB
= MBB
.getNumber();
82 O
<< "\t\"" << printMBBReference(MBB
) << "\" [ shape=box ]\n"
83 << '\t' << G
.getBundle(BB
, false) << " -> \"" << printMBBReference(MBB
)
85 << "\t\"" << printMBBReference(MBB
) << "\" -> " << G
.getBundle(BB
, true)
87 for (MachineBasicBlock::const_succ_iterator SI
= MBB
.succ_begin(),
88 SE
= MBB
.succ_end(); SI
!= SE
; ++SI
)
89 O
<< "\t\"" << printMBBReference(MBB
) << "\" -> \""
90 << printMBBReference(**SI
) << "\" [ color=lightgray ]\n";
96 } // end namespace llvm
98 /// view - Visualize the annotated bipartite CFG with Graphviz.
99 void EdgeBundles::view() const {
100 ViewGraph(*this, "EdgeBundles");