Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/EdgeBundles.cpp
35233 views
//===-------- EdgeBundles.cpp - Bundles of CFG edges ----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides the implementation of the EdgeBundles analysis.9//10//===----------------------------------------------------------------------===//1112#include "llvm/CodeGen/EdgeBundles.h"13#include "llvm/ADT/Twine.h"14#include "llvm/CodeGen/MachineBasicBlock.h"15#include "llvm/CodeGen/MachineFunction.h"16#include "llvm/CodeGen/Passes.h"17#include "llvm/InitializePasses.h"18#include "llvm/Support/CommandLine.h"19#include "llvm/Support/GraphWriter.h"20#include "llvm/Support/raw_ostream.h"2122using namespace llvm;2324static cl::opt<bool>25ViewEdgeBundles("view-edge-bundles", cl::Hidden,26cl::desc("Pop up a window to show edge bundle graphs"));2728char EdgeBundles::ID = 0;2930INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",31/* cfg = */true, /* is_analysis = */ true)3233char &llvm::EdgeBundlesID = EdgeBundles::ID;3435void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {36AU.setPreservesAll();37MachineFunctionPass::getAnalysisUsage(AU);38}3940bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {41MF = &mf;42EC.clear();43EC.grow(2 * MF->getNumBlockIDs());4445for (const auto &MBB : *MF) {46unsigned OutE = 2 * MBB.getNumber() + 1;47// Join the outgoing bundle with the ingoing bundles of all successors.48for (const MachineBasicBlock *Succ : MBB.successors())49EC.join(OutE, 2 * Succ->getNumber());50}51EC.compress();52if (ViewEdgeBundles)53view();5455// Compute the reverse mapping.56Blocks.clear();57Blocks.resize(getNumBundles());5859for (unsigned i = 0, e = MF->getNumBlockIDs(); i != e; ++i) {60unsigned b0 = getBundle(i, false);61unsigned b1 = getBundle(i, true);62Blocks[b0].push_back(i);63if (b1 != b0)64Blocks[b1].push_back(i);65}6667return false;68}6970namespace llvm {7172/// Specialize WriteGraph, the standard implementation won't work.73template<>74raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,75bool ShortNames,76const Twine &Title) {77const MachineFunction *MF = G.getMachineFunction();7879O << "digraph {\n";80for (const auto &MBB : *MF) {81unsigned BB = MBB.getNumber();82O << "\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';87for (const MachineBasicBlock *Succ : MBB.successors())88O << "\t\"" << printMBBReference(MBB) << "\" -> \""89<< printMBBReference(*Succ) << "\" [ color=lightgray ]\n";90}91O << "}\n";92return O;93}9495} // end namespace llvm9697/// view - Visualize the annotated bipartite CFG with Graphviz.98void EdgeBundles::view() const {99ViewGraph(*this, "EdgeBundles");100}101102103