1 //===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the implementation of the EdgeBundles analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/EdgeBundles.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/GraphWriter.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, /* 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
->size());
44 for (MachineFunction::const_iterator I
= MF
->begin(), E
= MF
->end(); I
!= E
;
46 const MachineBasicBlock
&MBB
= *I
;
47 unsigned OutE
= 2 * MBB
.getNumber() + 1;
48 // Join the outgoing bundle with the ingoing bundles of all successors.
49 for (MachineBasicBlock::const_succ_iterator SI
= MBB
.succ_begin(),
50 SE
= MBB
.succ_end(); SI
!= SE
; ++SI
)
51 EC
.join(OutE
, 2 * (*SI
)->getNumber());
59 /// view - Visualize the annotated bipartite CFG with Graphviz.
60 void EdgeBundles::view() const {
61 ViewGraph(*this, "EdgeBundles");
64 /// Specialize WriteGraph, the standard implementation won't work.
65 raw_ostream
&llvm::WriteGraph(raw_ostream
&O
, const EdgeBundles
&G
,
67 const std::string
&Title
) {
68 const MachineFunction
*MF
= G
.getMachineFunction();
71 for (MachineFunction::const_iterator I
= MF
->begin(), E
= MF
->end();
73 unsigned BB
= I
->getNumber();
74 O
<< "\t\"BB#" << BB
<< "\" [ shape=box ]\n"
75 << '\t' << G
.getBundle(BB
, false) << " -> \"BB#" << BB
<< "\"\n"
76 << "\t\"BB#" << BB
<< "\" -> " << G
.getBundle(BB
, true) << '\n';
77 for (MachineBasicBlock::const_succ_iterator SI
= I
->succ_begin(),
78 SE
= I
->succ_end(); SI
!= SE
; ++SI
)
79 O
<< "\t\"BB#" << BB
<< "\" -> \"BB#" << (*SI
)->getNumber()
80 << "\" [ color=lightgray ]\n";