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 13 #include "polly/Support/DumpFunctionPass.h" 14 #include "llvm/IR/Module.h" 15 #include "llvm/Pass.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/Path.h" 19 #include "llvm/Support/ToolOutputFile.h" 20 #include "llvm/Transforms/IPO/GlobalDCE.h" 21 #include "llvm/Transforms/IPO/StripDeadPrototypes.h" 22 #include "llvm/Transforms/Utils/Cloning.h" 23 24 #define DEBUG_TYPE "polly-dump-func" 25 26 using namespace llvm; 27 using namespace polly; 28 29 namespace { 30 31 static void runDumpFunction(llvm::Function &F, StringRef Suffix) { 32 StringRef FName = F.getName(); 33 Module *M = F.getParent(); 34 35 StringRef ModuleName = M->getName(); 36 StringRef Stem = sys::path::stem(ModuleName); 37 std::string Dumpfile = (Twine(Stem) + "-" + FName + Suffix + ".ll").str(); 38 LLVM_DEBUG(dbgs() << "Dumping function '" << FName << "' to '" << Dumpfile 39 << "'...\n"); 40 41 ValueToValueMapTy VMap; 42 auto ShouldCloneDefinition = [&F](const GlobalValue *GV) -> bool { 43 return GV == &F; 44 }; 45 std::unique_ptr<Module> CM = CloneModule(*M, VMap, ShouldCloneDefinition); 46 47 LLVM_DEBUG(dbgs() << "Global DCE...\n"); 48 49 { 50 ModuleAnalysisManager MAM; 51 ModulePassManager MPM; 52 53 PassInstrumentationCallbacks PIC; 54 MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); }); 55 56 MPM.addPass(GlobalDCEPass()); 57 MPM.addPass(StripDeadPrototypesPass()); 58 MPM.run(*CM, MAM); 59 } 60 61 LLVM_DEBUG(dbgs() << "Write to file '" << Dumpfile << "'...\n"); 62 63 std::unique_ptr<ToolOutputFile> Out; 64 std::error_code EC; 65 Out.reset(new ToolOutputFile(Dumpfile, EC, sys::fs::OF_None)); 66 if (EC) { 67 errs() << EC.message() << '\n'; 68 return; 69 } 70 71 CM->print(Out->os(), nullptr); 72 Out->keep(); 73 LLVM_DEBUG(dbgs() << "Dump file " << Dumpfile << " written successfully\n"); 74 } 75 76 class DumpFunctionWrapperPass : public FunctionPass { 77 private: 78 DumpFunctionWrapperPass(const DumpFunctionWrapperPass &) = delete; 79 const DumpFunctionWrapperPass & 80 operator=(const DumpFunctionWrapperPass &) = delete; 81 82 std::string Suffix; 83 84 public: 85 static char ID; 86 87 explicit DumpFunctionWrapperPass() : FunctionPass(ID), Suffix("-dump") {} 88 89 explicit DumpFunctionWrapperPass(std::string Suffix) 90 : FunctionPass(ID), Suffix(std::move(Suffix)) {} 91 92 /// @name FunctionPass interface 93 //@{ 94 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { 95 AU.setPreservesAll(); 96 } 97 98 virtual bool runOnFunction(llvm::Function &F) override { 99 runDumpFunction(F, Suffix); 100 return false; 101 } 102 //@} 103 }; 104 105 char DumpFunctionWrapperPass::ID; 106 } // namespace 107 108 FunctionPass *polly::createDumpFunctionWrapperPass(std::string Suffix) { 109 return new DumpFunctionWrapperPass(std::move(Suffix)); 110 } 111 112 llvm::PreservedAnalyses DumpFunctionPass::run(Function &F, 113 FunctionAnalysisManager &AM) { 114 runDumpFunction(F, Suffix); 115 return PreservedAnalyses::all(); 116 } 117 118 INITIALIZE_PASS_BEGIN(DumpFunctionWrapperPass, "polly-dump-function", 119 "Polly - Dump Function", false, false) 120 INITIALIZE_PASS_END(DumpFunctionWrapperPass, "polly-dump-function", 121 "Polly - Dump Function", false, false) 122