1//===-------- EdgeBundles.h - Bundles of CFG edges --------------*- c++ -*-===//
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// The EdgeBundles analysis forms equivalence classes of CFG edges such that all
10// edges leaving a machine basic block are in the same bundle, and all edges
11// entering a machine basic block are in the same bundle.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_EDGEBUNDLES_H
16#define LLVM_CODEGEN_EDGEBUNDLES_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/IntEqClasses.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/IR/PassManager.h"
22
23namespace llvm {
24class EdgeBundlesWrapperLegacy;
25class EdgeBundlesAnalysis;
26
27class EdgeBundles {
28 friend class EdgeBundlesWrapperLegacy;
29 friend class EdgeBundlesAnalysis;
30
31 const MachineFunction *MF = nullptr;
32
33 /// EC - Each edge bundle is an equivalence class. The keys are:
34 /// 2*BB->getNumber() -> Ingoing bundle.
35 /// 2*BB->getNumber()+1 -> Outgoing bundle.
36 IntEqClasses EC;
37
38 /// Blocks - Map each bundle to a list of basic block numbers.
39 SmallVector<SmallVector<unsigned, 8>, 4> Blocks;
40
41 void init();
42 EdgeBundles(MachineFunction &MF);
43
44public:
45 /// getBundle - Return the ingoing (Out = false) or outgoing (Out = true)
46 /// bundle number for basic block #N
47 unsigned getBundle(unsigned N, bool Out) const { return EC[2 * N + Out]; }
48
49 /// getNumBundles - Return the total number of bundles in the CFG.
50 unsigned getNumBundles() const { return EC.getNumClasses(); }
51
52 /// getBlocks - Return an array of blocks that are connected to Bundle.
53 ArrayRef<unsigned> getBlocks(unsigned Bundle) const { return Blocks[Bundle]; }
54
55 /// getMachineFunction - Return the last machine function computed.
56 const MachineFunction *getMachineFunction() const { return MF; }
57
58 /// view - Visualize the annotated bipartite CFG with Graphviz.
59 void view() const;
60
61 // Handle invalidation for the new pass manager
62 bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
63 MachineFunctionAnalysisManager::Invalidator &Inv);
64};
65
66class EdgeBundlesWrapperLegacy : public MachineFunctionPass {
67public:
68 static char ID;
69 EdgeBundlesWrapperLegacy() : MachineFunctionPass(ID) {}
70
71 EdgeBundles &getEdgeBundles() { return *Impl; }
72 const EdgeBundles &getEdgeBundles() const { return *Impl; }
73
74private:
75 std::unique_ptr<EdgeBundles> Impl;
76 bool runOnMachineFunction(MachineFunction &MF) override;
77 void getAnalysisUsage(AnalysisUsage&) const override;
78};
79
80class EdgeBundlesAnalysis : public AnalysisInfoMixin<EdgeBundlesAnalysis> {
81 friend AnalysisInfoMixin<EdgeBundlesAnalysis>;
82 static AnalysisKey Key;
83
84public:
85 using Result = EdgeBundles;
86 EdgeBundles run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
87};
88
89} // end namespace llvm
90
91#endif
92