diff options
Diffstat (limited to 'lib/LTO/LTOCodeGenerator.cpp')
-rw-r--r-- | lib/LTO/LTOCodeGenerator.cpp | 172 |
1 files changed, 121 insertions, 51 deletions
diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp index c663d43..61c2749 100644 --- a/lib/LTO/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -15,6 +15,8 @@ #include "llvm/LTO/LTOCodeGenerator.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Analysis/Passes.h" +#include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/CodeGen/RuntimeLibcalls.h" #include "llvm/Config/config.h" @@ -24,6 +26,7 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" @@ -33,7 +36,6 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/SubtargetFeature.h" -#include "llvm/PassManager.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" @@ -44,7 +46,6 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Target/TargetLibraryInfo.h" #include "llvm/Target/TargetLowering.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetRegisterInfo.h" @@ -81,16 +82,27 @@ void LTOCodeGenerator::initialize() { CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT; DiagHandler = nullptr; DiagContext = nullptr; + OwnedModule = nullptr; initializeLTOPasses(); } +void LTOCodeGenerator::destroyMergedModule() { + if (OwnedModule) { + assert(IRLinker.getModule() == &OwnedModule->getModule() && + "The linker's module should be the same as the owned module"); + delete OwnedModule; + OwnedModule = nullptr; + } else if (IRLinker.getModule()) + IRLinker.deleteModule(); +} + LTOCodeGenerator::~LTOCodeGenerator() { + destroyMergedModule(); + delete TargetMach; TargetMach = nullptr; - IRLinker.deleteModule(); - for (std::vector<char *>::iterator I = CodegenOptions.begin(), E = CodegenOptions.end(); I != E; ++I) @@ -108,7 +120,7 @@ void LTOCodeGenerator::initializeLTOPasses() { initializeGlobalOptPass(R); initializeConstantMergePass(R); initializeDAHPass(R); - initializeInstCombinerPass(R); + initializeInstructionCombiningPassPass(R); initializeSimpleInlinerPass(R); initializePruneEHPass(R); initializeGlobalDCEPass(R); @@ -140,6 +152,22 @@ bool LTOCodeGenerator::addModule(LTOModule *mod) { return !ret; } +void LTOCodeGenerator::setModule(LTOModule *Mod) { + assert(&Mod->getModule().getContext() == &Context && + "Expected module in same context"); + + // Delete the old merged module. + destroyMergedModule(); + AsmUndefinedRefs.clear(); + + OwnedModule = Mod; + IRLinker.setModule(&Mod->getModule()); + + const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs(); + for (int I = 0, E = Undefs.size(); I != E; ++I) + AsmUndefinedRefs[Undefs[I]] = 1; +} + void LTOCodeGenerator::setTargetOptions(TargetOptions options) { Options = options; } @@ -201,12 +229,8 @@ bool LTOCodeGenerator::writeMergedModules(const char *path, return true; } -bool LTOCodeGenerator::compile_to_file(const char** name, - bool disableOpt, - bool disableInline, - bool disableGVNLoadPRE, - bool disableVectorization, - std::string& errMsg) { +bool LTOCodeGenerator::compileOptimizedToFile(const char **name, + std::string &errMsg) { // make unique temp .o file to put generated object file SmallString<128> Filename; int FD; @@ -220,9 +244,7 @@ bool LTOCodeGenerator::compile_to_file(const char** name, // generate object file tool_output_file objFile(Filename.c_str(), FD); - bool genResult = - generateObjectFile(objFile.os(), disableOpt, disableInline, - disableGVNLoadPRE, disableVectorization, errMsg); + bool genResult = compileOptimized(objFile.os(), errMsg); objFile.os().close(); if (objFile.os().has_error()) { objFile.os().clear_error(); @@ -241,15 +263,10 @@ bool LTOCodeGenerator::compile_to_file(const char** name, return true; } -const void* LTOCodeGenerator::compile(size_t* length, - bool disableOpt, - bool disableInline, - bool disableGVNLoadPRE, - bool disableVectorization, - std::string& errMsg) { +const void *LTOCodeGenerator::compileOptimized(size_t *length, + std::string &errMsg) { const char *name; - if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE, - disableVectorization, errMsg)) + if (!compileOptimizedToFile(&name, errMsg)) return nullptr; // read .o file into memory buffer @@ -272,6 +289,33 @@ const void* LTOCodeGenerator::compile(size_t* length, return NativeObjectFile->getBufferStart(); } + +bool LTOCodeGenerator::compile_to_file(const char **name, + bool disableOpt, + bool disableInline, + bool disableGVNLoadPRE, + bool disableVectorization, + std::string &errMsg) { + if (!optimize(disableOpt, disableInline, disableGVNLoadPRE, + disableVectorization, errMsg)) + return false; + + return compileOptimizedToFile(name, errMsg); +} + +const void* LTOCodeGenerator::compile(size_t *length, + bool disableOpt, + bool disableInline, + bool disableGVNLoadPRE, + bool disableVectorization, + std::string &errMsg) { + if (!optimize(disableOpt, disableInline, disableGVNLoadPRE, + disableVectorization, errMsg)) + return nullptr; + + return compileOptimized(length, errMsg); +} + bool LTOCodeGenerator::determineTarget(std::string &errMsg) { if (TargetMach) return true; @@ -368,10 +412,13 @@ static void findUsedValues(GlobalVariable *LLVMUsed, UsedValues.insert(GV); } +// Collect names of runtime library functions. User-defined functions with the +// same names are added to llvm.compiler.used to prevent them from being +// deleted by optimizations. static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, const TargetLibraryInfo& TLI, - const TargetLowering *Lowering) -{ + const Module &Mod, + const TargetMachine &TM) { // TargetLibraryInfo has info on C runtime library calls on the current // target. for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs); @@ -381,14 +428,21 @@ static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, Libcalls.push_back(TLI.getName(F)); } - // TargetLowering has info on library calls that CodeGen expects to be - // available, both from the C runtime and compiler-rt. - if (Lowering) - for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); - I != E; ++I) - if (const char *Name - = Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) - Libcalls.push_back(Name); + SmallPtrSet<const TargetLowering *, 1> TLSet; + + for (const Function &F : Mod) { + const TargetLowering *Lowering = + TM.getSubtargetImpl(F)->getTargetLowering(); + + if (Lowering && TLSet.insert(Lowering).second) + // TargetLowering has info on library calls that CodeGen expects to be + // available, both from the C runtime and compiler-rt. + for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); + I != E; ++I) + if (const char *Name = + Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) + Libcalls.push_back(Name); + } array_pod_sort(Libcalls.begin(), Libcalls.end()); Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()), @@ -401,18 +455,19 @@ void LTOCodeGenerator::applyScopeRestrictions() { Module *mergedModule = IRLinker.getModule(); // Start off with a verification pass. - PassManager passes; + legacy::PassManager passes; passes.add(createVerifierPass()); passes.add(createDebugInfoVerifierPass()); // mark which symbols can not be internalized - Mangler Mangler(TargetMach->getSubtargetImpl()->getDataLayout()); + Mangler Mangler(TargetMach->getDataLayout()); std::vector<const char*> MustPreserveList; SmallPtrSet<GlobalValue*, 8> AsmUsed; std::vector<StringRef> Libcalls; - TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple())); - accumulateAndSortLibcalls( - Libcalls, TLI, TargetMach->getSubtargetImpl()->getTargetLowering()); + TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple())); + TargetLibraryInfo TLI(TLII); + + accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach); for (Module::iterator f = mergedModule->begin(), e = mergedModule->end(); f != e; ++f) @@ -457,12 +512,11 @@ void LTOCodeGenerator::applyScopeRestrictions() { } /// Optimize merged modules using various IPO passes -bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, - bool DisableOpt, - bool DisableInline, - bool DisableGVNLoadPRE, - bool DisableVectorization, - std::string &errMsg) { +bool LTOCodeGenerator::optimize(bool DisableOpt, + bool DisableInline, + bool DisableGVNLoadPRE, + bool DisableVectorization, + std::string &errMsg) { if (!this->determineTarget(errMsg)) return false; @@ -472,10 +526,14 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, this->applyScopeRestrictions(); // Instantiate the pass manager to organize the passes. - PassManager passes; + legacy::PassManager passes; // Add an appropriate DataLayout instance for this module... - mergedModule->setDataLayout(TargetMach->getSubtargetImpl()->getDataLayout()); + mergedModule->setDataLayout(TargetMach->getDataLayout()); + + passes.add(new DataLayoutPass()); + passes.add( + createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); Triple TargetTriple(TargetMach->getTargetTriple()); PassManagerBuilder PMB; @@ -484,15 +542,30 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, PMB.SLPVectorize = !DisableVectorization; if (!DisableInline) PMB.Inliner = createFunctionInliningPass(); - PMB.LibraryInfo = new TargetLibraryInfo(TargetTriple); + PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple); if (DisableOpt) PMB.OptLevel = 0; PMB.VerifyInput = true; PMB.VerifyOutput = true; - PMB.populateLTOPassManager(passes, TargetMach); + PMB.populateLTOPassManager(passes); - PassManager codeGenPasses; + // Run our queue of passes all at once now, efficiently. + passes.run(*mergedModule); + + return true; +} + +bool LTOCodeGenerator::compileOptimized(raw_ostream &out, std::string &errMsg) { + if (!this->determineTarget(errMsg)) + return false; + + Module *mergedModule = IRLinker.getModule(); + + // Mark which symbols can not be internalized + this->applyScopeRestrictions(); + + legacy::PassManager codeGenPasses; codeGenPasses.add(new DataLayoutPass()); @@ -508,9 +581,6 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, return false; } - // Run our queue of passes all at once now, efficiently. - passes.run(*mergedModule); - // Run the code generator, and write assembly file codeGenPasses.run(*mergedModule); |