#include "llvm/Analysis/RegionPrinter.h"
#include "llvm/Analysis/DOTGraphTraitsPass.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Analysis/RegionIterator.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#ifndef NDEBUG
#include "llvm/IR/LegacyPassManager.h"
#endif
usingnamespacellvm;
static cl::opt<bool>
onlySimpleRegions("only-simple-regions",
cl::desc("Show only simple regions in the graphviz viewer"),
cl::Hidden,
cl::init(false));
namespace llvm {
std::string DOTGraphTraits<RegionNode *>::getNodeLabel(RegionNode *Node,
RegionNode *Graph) { … }
template <>
struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> { … };
}
namespace {
struct RegionInfoPassGraphTraits { … };
struct RegionPrinter
: public DOTGraphTraitsPrinterWrapperPass<
RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> { … };
char RegionPrinter::ID = …;
struct RegionOnlyPrinter
: public DOTGraphTraitsPrinterWrapperPass<
RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> { … };
char RegionOnlyPrinter::ID = …;
struct RegionViewer
: public DOTGraphTraitsViewerWrapperPass<
RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> { … };
char RegionViewer::ID = …;
struct RegionOnlyViewer
: public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
RegionInfoPassGraphTraits> { … };
char RegionOnlyViewer::ID = …;
}
INITIALIZE_PASS(…)
INITIALIZE_PASS(…)
INITIALIZE_PASS(…)
INITIALIZE_PASS(…)
FunctionPass *llvm::createRegionPrinterPass() { … }
FunctionPass *llvm::createRegionOnlyPrinterPass() { … }
FunctionPass* llvm::createRegionViewerPass() { … }
FunctionPass* llvm::createRegionOnlyViewerPass() { … }
#ifndef NDEBUG
static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
assert(RI && "Argument must be non-null");
llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
llvm::ViewGraph(RI, "reg", ShortNames,
Twine(GraphName) + " for '" + F->getName() + "' function");
}
static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
assert(F && "Argument must be non-null");
assert(!F->isDeclaration() && "Function must have an implementation");
auto NonConstF = const_cast<Function *>(F);
llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
FPM.add(ViewerPass);
FPM.doInitialization();
FPM.run(*NonConstF);
FPM.doFinalization();
}
void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
void llvm::viewRegion(const Function *F) {
invokeFunctionPass(F, createRegionViewerPass());
}
void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
void llvm::viewRegionOnly(const Function *F) {
invokeFunctionPass(F, createRegionOnlyViewerPass());
}
#endif