#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
-#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/Verifier.h"
#include "llvm/InitializePasses.h"
#include "llvm/LTO/LTOModule.h"
LTOCodeGenerator::LTOCodeGenerator()
: Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) {
- initialize();
+ initializeLTOPasses();
}
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
: OwnedContext(std::move(Context)), Context(*OwnedContext),
- IRLinker(new Module("ld-temp.o", *OwnedContext)), OptLevel(2) {
- initialize();
-}
-
-void LTOCodeGenerator::initialize() {
- TargetMach = nullptr;
- EmitDwarfDebugInfo = false;
- ScopeRestrictionsDone = false;
- CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT;
- DiagHandler = nullptr;
- DiagContext = nullptr;
- OwnedModule = nullptr;
-
+ IRLinker(new Module("ld-temp.o", *OwnedContext)) {
initializeLTOPasses();
}
LTOCodeGenerator::~LTOCodeGenerator() {
destroyMergedModule();
-
- delete TargetMach;
- TargetMach = nullptr;
-
- for (std::vector<char *>::iterator I = CodegenOptions.begin(),
- E = CodegenOptions.end();
- I != E; ++I)
- free(*I);
}
// Initialize LTO passes. Please keep this funciton in sync with
llvm_unreachable("Unknown debug format!");
}
-void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
- switch (model) {
- case LTO_CODEGEN_PIC_MODEL_STATIC:
- case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
- case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
- case LTO_CODEGEN_PIC_MODEL_DEFAULT:
- CodeModel = model;
- return;
- }
- llvm_unreachable("Unknown PIC model!");
-}
-
bool LTOCodeGenerator::writeMergedModules(const char *path,
std::string &errMsg) {
if (!determineTarget(errMsg))
}
// write bitcode to it
- WriteBitcodeToFile(IRLinker.getModule(), Out.os(),
- shouldPreserveBitcodeUseListOrder());
+ WriteBitcodeToFile(IRLinker.getModule(), Out.os(), ShouldEmbedUselists);
Out.os().close();
if (Out.os().has_error()) {
return true;
}
-const void *LTOCodeGenerator::compileOptimized(size_t *length,
- std::string &errMsg) {
+std::unique_ptr<MemoryBuffer>
+LTOCodeGenerator::compileOptimized(std::string &errMsg) {
const char *name;
if (!compileOptimizedToFile(&name, errMsg))
return nullptr;
sys::fs::remove(NativeObjectPath);
return nullptr;
}
- NativeObjectFile = std::move(*BufferOrErr);
// remove temp files
sys::fs::remove(NativeObjectPath);
- // return buffer, unless error
- if (!NativeObjectFile)
- return nullptr;
- *length = NativeObjectFile->getBufferSize();
- return NativeObjectFile->getBufferStart();
+ return std::move(*BufferOrErr);
}
return compileOptimizedToFile(name, errMsg);
}
-const void* LTOCodeGenerator::compile(size_t *length,
- bool disableInline,
- bool disableGVNLoadPRE,
- bool disableVectorization,
- std::string &errMsg) {
+std::unique_ptr<MemoryBuffer>
+LTOCodeGenerator::compile(bool disableInline, bool disableGVNLoadPRE,
+ bool disableVectorization, std::string &errMsg) {
if (!optimize(disableInline, disableGVNLoadPRE,
disableVectorization, errMsg))
return nullptr;
- return compileOptimized(length, errMsg);
+ return compileOptimized(errMsg);
}
bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
if (!march)
return false;
- // The relocation model is actually a static member of TargetMachine and
- // needs to be set before the TargetMachine is instantiated.
- Reloc::Model RelocModel = Reloc::Default;
- switch (CodeModel) {
- case LTO_CODEGEN_PIC_MODEL_STATIC:
- RelocModel = Reloc::Static;
- break;
- case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
- RelocModel = Reloc::PIC_;
- break;
- case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
- RelocModel = Reloc::DynamicNoPIC;
- break;
- case LTO_CODEGEN_PIC_MODEL_DEFAULT:
- // RelocModel is already the default, so leave it that way.
- break;
- }
-
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
// the default set of features.
SubtargetFeatures Features(MAttr);
break;
}
- TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
- RelocModel, CodeModel::Default,
- CGOptLevel);
+ TargetMach.reset(march->createTargetMachine(TripleStr, MCpu, FeatureStr,
+ Options, RelocModel,
+ CodeModel::Default, CGOptLevel));
return true;
}
}
void LTOCodeGenerator::applyScopeRestrictions() {
- if (ScopeRestrictionsDone)
+ if (ScopeRestrictionsDone || !ShouldInternalize)
return;
Module *mergedModule = IRLinker.getModule();
passes.add(createVerifierPass());
// mark which symbols can not be internalized
- Mangler Mangler(TargetMach->getDataLayout());
+ Mangler Mangler;
std::vector<const char*> MustPreserveList;
SmallPtrSet<GlobalValue*, 8> AsmUsed;
std::vector<StringRef> Libcalls;
legacy::PassManager passes;
// Add an appropriate DataLayout instance for this module...
- mergedModule->setDataLayout(*TargetMach->getDataLayout());
+ mergedModule->setDataLayout(TargetMach->createDataLayout());
passes.add(
createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
/// LTO problems.
void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
for (std::pair<StringRef, StringRef> o = getToken(options);
- !o.first.empty(); o = getToken(o.second)) {
- // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
- // that.
- if (CodegenOptions.empty())
- CodegenOptions.push_back(strdup("libLLVMLTO"));
- CodegenOptions.push_back(strdup(o.first.str().c_str()));
- }
+ !o.first.empty(); o = getToken(o.second))
+ CodegenOptions.push_back(o.first);
}
void LTOCodeGenerator::parseCodeGenDebugOptions() {
- // Turn on -preserve-bc-uselistorder by default, but let the command-line
- // override it.
- setPreserveBitcodeUseListOrder(true);
-
// if options were requested, set them
- if (!CodegenOptions.empty())
- cl::ParseCommandLineOptions(CodegenOptions.size(),
- const_cast<char **>(&CodegenOptions[0]));
+ if (!CodegenOptions.empty()) {
+ // ParseCommandLineOptions() expects argv[0] to be program name.
+ std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
+ for (std::string &Arg : CodegenOptions)
+ CodegenArgv.push_back(Arg.c_str());
+ cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
+ }
}
void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,