Polly 20.0.0git
DumpFunctionPass.cpp
Go to the documentation of this file.
1//===------ DumpFunctionPass.cpp --------------------------------*- 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// Write a function to a file.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/IR/Module.h"
15#include "llvm/IR/PassInstrumentation.h"
16#include "llvm/Pass.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/ToolOutputFile.h"
21#include "llvm/Transforms/IPO/GlobalDCE.h"
22#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
23#include "llvm/Transforms/Utils/Cloning.h"
24
25#define DEBUG_TYPE "polly-dump-func"
26
27using namespace llvm;
28using namespace polly;
29
30namespace {
31
32static void runDumpFunction(llvm::Function &F, StringRef Suffix) {
33 StringRef FName = F.getName();
34 Module *M = F.getParent();
35
36 StringRef ModuleName = M->getName();
37 StringRef Stem = sys::path::stem(ModuleName);
38 std::string Dumpfile = (Twine(Stem) + "-" + FName + Suffix + ".ll").str();
39 LLVM_DEBUG(dbgs() << "Dumping function '" << FName << "' to '" << Dumpfile
40 << "'...\n");
41
42 ValueToValueMapTy VMap;
43 auto ShouldCloneDefinition = [&F](const GlobalValue *GV) -> bool {
44 return GV == &F;
45 };
46 std::unique_ptr<Module> CM = CloneModule(*M, VMap, ShouldCloneDefinition);
47 Function *NewF = cast<Function>(VMap.lookup(&F));
48 assert(NewF && "Expected selected function to be cloned");
49
50 LLVM_DEBUG(dbgs() << "Global DCE...\n");
51
52 // Stop F itself from being pruned
53 GlobalValue::LinkageTypes OrigLinkage = NewF->getLinkage();
54 NewF->setLinkage(GlobalValue::ExternalLinkage);
55
56 {
57 ModuleAnalysisManager MAM;
58 ModulePassManager MPM;
59
60 PassInstrumentationCallbacks PIC;
61 MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
62
63 MPM.addPass(GlobalDCEPass());
64 MPM.addPass(StripDeadPrototypesPass());
65 MPM.run(*CM, MAM);
66 }
67
68 // Restore old linkage
69 NewF->setLinkage(OrigLinkage);
70
71 LLVM_DEBUG(dbgs() << "Write to file '" << Dumpfile << "'...\n");
72
73 std::unique_ptr<ToolOutputFile> Out;
74 std::error_code EC;
75 Out.reset(new ToolOutputFile(Dumpfile, EC, sys::fs::OF_None));
76 if (EC) {
77 errs() << EC.message() << '\n';
78 return;
79 }
80
81 CM->print(Out->os(), nullptr);
82 Out->keep();
83 LLVM_DEBUG(dbgs() << "Dump file " << Dumpfile << " written successfully\n");
84}
85
86class DumpFunctionWrapperPass final : public FunctionPass {
87private:
88 DumpFunctionWrapperPass(const DumpFunctionWrapperPass &) = delete;
89 const DumpFunctionWrapperPass &
90 operator=(const DumpFunctionWrapperPass &) = delete;
91
92 std::string Suffix;
93
94public:
95 static char ID;
96
97 explicit DumpFunctionWrapperPass() : FunctionPass(ID), Suffix("-dump") {}
98
99 explicit DumpFunctionWrapperPass(std::string Suffix)
100 : FunctionPass(ID), Suffix(std::move(Suffix)) {}
101
102 /// @name FunctionPass interface
103 //@{
104 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
105 AU.setPreservesAll();
106 }
107
108 bool runOnFunction(llvm::Function &F) override {
109 runDumpFunction(F, Suffix);
110 return false;
111 }
112 //@}
113};
114
115char DumpFunctionWrapperPass::ID;
116} // namespace
117
118FunctionPass *polly::createDumpFunctionWrapperPass(std::string Suffix) {
119 return new DumpFunctionWrapperPass(std::move(Suffix));
120}
121
122llvm::PreservedAnalyses DumpFunctionPass::run(Function &F,
123 FunctionAnalysisManager &AM) {
124 runDumpFunction(F, Suffix);
125 return PreservedAnalyses::all();
126}
127
128INITIALIZE_PASS_BEGIN(DumpFunctionWrapperPass, "polly-dump-function",
129 "Polly - Dump Function", false, false)
130INITIALIZE_PASS_END(DumpFunctionWrapperPass, "polly-dump-function",
131 "Polly - Dump Function", false, false)
INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences", "Polly - Calculate dependences", false, false)
INITIALIZE_PASS_END(DependenceInfo, "polly-dependences", "Polly - Calculate dependences", false, false) namespace
polly dump function
polly dump Polly Dump Function
polly dump Polly Dump Module
static RegisterPass< ScopPrinterWrapperPass > M("dot-scops", "Polly - Print Scops of function")
const char * str
Definition: isl_test.c:2095
#define assert(exp)
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
llvm::FunctionPass * createDumpFunctionWrapperPass(std::string Suffix)
llvm::PreservedAnalyses run(llvm::Function &F, llvm::FunctionAnalysisManager &AM)