2008-02-26 20:26:43 +00:00
|
|
|
//===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2012-03-31 10:50:14 +00:00
|
|
|
//
|
2008-02-26 20:26:43 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-03-31 10:50:14 +00:00
|
|
|
// This file implements the Link Time Optimization library. This library is
|
2008-02-26 20:26:43 +00:00
|
|
|
// intended to be used by linker to optimize code at link time.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-09-24 23:52:22 +00:00
|
|
|
#include "llvm/LTO/LTOCodeGenerator.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-06-17 06:52:10 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2015-01-15 02:16:27 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-01-31 11:17:59 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2008-02-26 20:26:43 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-11-12 21:44:01 +00:00
|
|
|
#include "llvm/CodeGen/RuntimeLibcalls.h"
|
2012-03-31 11:15:43 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-15 22:04:35 +00:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-02-13 10:01:29 +00:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-01-07 21:19:40 +00:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2013-07-22 18:40:34 +00:00
|
|
|
#include "llvm/InitializePasses.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/LTO/LTOModule.h"
|
2014-03-06 03:42:23 +00:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2010-03-12 18:44:54 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-06-29 01:14:12 +00:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2010-03-12 18:44:54 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-06-17 18:05:35 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-04-09 15:54:59 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2010-11-29 18:47:54 +00:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-29 18:47:54 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-03-04 10:07:28 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-11-12 21:44:01 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2013-09-30 16:39:19 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2013-03-29 23:28:55 +00:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2014-06-12 17:38:55 +00:00
|
|
|
#include <system_error>
|
2008-02-26 20:26:43 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-03-31 10:49:43 +00:00
|
|
|
const char* LTOCodeGenerator::getVersionString() {
|
2008-02-26 20:26:43 +00:00
|
|
|
#ifdef LLVM_VERSION_INFO
|
2012-03-31 10:49:43 +00:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
|
2008-02-26 20:26:43 +00:00
|
|
|
#else
|
2012-03-31 10:49:43 +00:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION;
|
2008-02-26 20:26:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-31 10:50:14 +00:00
|
|
|
LTOCodeGenerator::LTOCodeGenerator()
|
2014-11-11 23:03:29 +00:00
|
|
|
: Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
|
|
|
|
: OwnedContext(std::move(Context)), Context(*OwnedContext),
|
2015-03-19 22:01:00 +00:00
|
|
|
IRLinker(new Module("ld-temp.o", *OwnedContext)), OptLevel(2) {
|
2014-11-11 23:03:29 +00:00
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LTOCodeGenerator::initialize() {
|
|
|
|
TargetMach = nullptr;
|
|
|
|
EmitDwarfDebugInfo = false;
|
|
|
|
ScopeRestrictionsDone = false;
|
|
|
|
CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT;
|
|
|
|
DiagHandler = nullptr;
|
|
|
|
DiagContext = nullptr;
|
2015-02-25 21:20:53 +00:00
|
|
|
OwnedModule = nullptr;
|
2014-11-11 23:03:29 +00:00
|
|
|
|
2013-07-22 18:40:34 +00:00
|
|
|
initializeLTOPasses();
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 21:20:53 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-03-31 10:49:43 +00:00
|
|
|
LTOCodeGenerator::~LTOCodeGenerator() {
|
2015-02-25 21:20:53 +00:00
|
|
|
destroyMergedModule();
|
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
delete TargetMach;
|
2014-04-15 06:32:26 +00:00
|
|
|
TargetMach = nullptr;
|
2013-10-16 08:59:57 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
for (std::vector<char *>::iterator I = CodegenOptions.begin(),
|
|
|
|
E = CodegenOptions.end();
|
|
|
|
I != E; ++I)
|
2012-03-31 10:49:43 +00:00
|
|
|
free(*I);
|
|
|
|
}
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-07-22 18:40:34 +00:00
|
|
|
// Initialize LTO passes. Please keep this funciton in sync with
|
2013-07-23 06:44:34 +00:00
|
|
|
// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
|
2014-01-10 20:24:35 +00:00
|
|
|
// passes are initialized.
|
2013-07-22 18:40:34 +00:00
|
|
|
void LTOCodeGenerator::initializeLTOPasses() {
|
|
|
|
PassRegistry &R = *PassRegistry::getPassRegistry();
|
|
|
|
|
|
|
|
initializeInternalizePassPass(R);
|
|
|
|
initializeIPSCCPPass(R);
|
|
|
|
initializeGlobalOptPass(R);
|
|
|
|
initializeConstantMergePass(R);
|
|
|
|
initializeDAHPass(R);
|
2015-01-20 22:44:35 +00:00
|
|
|
initializeInstructionCombiningPassPass(R);
|
2013-07-22 18:40:34 +00:00
|
|
|
initializeSimpleInlinerPass(R);
|
|
|
|
initializePruneEHPass(R);
|
|
|
|
initializeGlobalDCEPass(R);
|
|
|
|
initializeArgPromotionPass(R);
|
|
|
|
initializeJumpThreadingPass(R);
|
|
|
|
initializeSROAPass(R);
|
|
|
|
initializeSROA_DTPass(R);
|
|
|
|
initializeSROA_SSAUpPass(R);
|
|
|
|
initializeFunctionAttrsPass(R);
|
|
|
|
initializeGlobalsModRefPass(R);
|
|
|
|
initializeLICMPass(R);
|
2014-07-18 19:13:09 +00:00
|
|
|
initializeMergedLoadStoreMotionPass(R);
|
2013-07-22 18:40:34 +00:00
|
|
|
initializeGVNPass(R);
|
|
|
|
initializeMemCpyOptPass(R);
|
|
|
|
initializeDCEPass(R);
|
2013-08-06 02:43:45 +00:00
|
|
|
initializeCFGSimplifyPassPass(R);
|
2013-07-22 18:40:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 04:06:10 +00:00
|
|
|
bool LTOCodeGenerator::addModule(LTOModule *mod) {
|
2014-11-11 23:13:10 +00:00
|
|
|
assert(&mod->getModule().getContext() == &Context &&
|
|
|
|
"Expected module in same context");
|
|
|
|
|
2014-10-25 04:06:10 +00:00
|
|
|
bool ret = IRLinker.linkInModule(&mod->getModule());
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
|
|
|
|
for (int i = 0, e = undefs.size(); i != e; ++i)
|
2013-09-04 17:44:24 +00:00
|
|
|
AsmUndefinedRefs[undefs[i]] = 1;
|
2011-03-02 04:14:42 +00:00
|
|
|
|
2013-08-07 05:19:23 +00:00
|
|
|
return !ret;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2015-02-24 00:45:56 +00:00
|
|
|
void LTOCodeGenerator::setModule(LTOModule *Mod) {
|
|
|
|
assert(&Mod->getModule().getContext() == &Context &&
|
|
|
|
"Expected module in same context");
|
|
|
|
|
|
|
|
// Delete the old merged module.
|
2015-02-25 21:20:53 +00:00
|
|
|
destroyMergedModule();
|
2015-02-24 00:45:56 +00:00
|
|
|
AsmUndefinedRefs.clear();
|
|
|
|
|
2015-02-25 21:20:53 +00:00
|
|
|
OwnedModule = Mod;
|
2015-02-24 00:45:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-30 16:39:19 +00:00
|
|
|
void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
|
2014-06-19 22:27:46 +00:00
|
|
|
Options = options;
|
2013-09-30 16:39:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 05:19:23 +00:00
|
|
|
void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
|
2012-03-31 11:15:43 +00:00
|
|
|
switch (debug) {
|
|
|
|
case LTO_DEBUG_MODEL_NONE:
|
2013-09-04 17:44:24 +00:00
|
|
|
EmitDwarfDebugInfo = false;
|
2013-08-07 05:19:23 +00:00
|
|
|
return;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
case LTO_DEBUG_MODEL_DWARF:
|
2013-09-04 17:44:24 +00:00
|
|
|
EmitDwarfDebugInfo = true;
|
2013-08-07 05:19:23 +00:00
|
|
|
return;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown debug format!");
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 05:19:23 +00:00
|
|
|
void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
|
2012-03-31 11:15:43 +00:00
|
|
|
switch (model) {
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_STATIC:
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
|
2014-04-14 13:54:16 +00:00
|
|
|
case LTO_CODEGEN_PIC_MODEL_DEFAULT:
|
2013-09-04 17:44:24 +00:00
|
|
|
CodeModel = model;
|
2013-08-07 05:19:23 +00:00
|
|
|
return;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown PIC model!");
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2009-08-23 07:49:08 +00:00
|
|
|
bool LTOCodeGenerator::writeMergedModules(const char *path,
|
|
|
|
std::string &errMsg) {
|
2013-08-06 21:51:21 +00:00
|
|
|
if (!determineTarget(errMsg))
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2009-08-23 07:49:08 +00:00
|
|
|
|
2013-08-08 23:51:04 +00:00
|
|
|
// mark which symbols can not be internalized
|
|
|
|
applyScopeRestrictions();
|
2009-08-23 07:49:08 +00:00
|
|
|
|
|
|
|
// create output file
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
|
|
|
tool_output_file Out(path, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
2009-08-23 07:49:08 +00:00
|
|
|
errMsg = "could not open bitcode file for writing: ";
|
|
|
|
errMsg += path;
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2009-08-23 07:49:08 +00:00
|
|
|
}
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2009-08-23 07:49:08 +00:00
|
|
|
// write bitcode to it
|
2014-05-03 15:28:13 +00:00
|
|
|
WriteBitcodeToFile(IRLinker.getModule(), Out.os());
|
2010-09-01 14:20:41 +00:00
|
|
|
Out.os().close();
|
2010-05-27 20:19:47 +00:00
|
|
|
|
2010-09-01 14:20:41 +00:00
|
|
|
if (Out.os().has_error()) {
|
2009-08-23 07:49:08 +00:00
|
|
|
errMsg = "could not write bitcode file: ";
|
|
|
|
errMsg += path;
|
2010-09-01 14:20:41 +00:00
|
|
|
Out.os().clear_error();
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2009-08-23 07:49:08 +00:00
|
|
|
}
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2010-08-20 16:59:15 +00:00
|
|
|
Out.keep();
|
2013-08-07 05:19:23 +00:00
|
|
|
return true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 18:39:15 +00:00
|
|
|
bool LTOCodeGenerator::compileOptimizedToFile(const char **name,
|
|
|
|
std::string &errMsg) {
|
2013-08-12 21:07:31 +00:00
|
|
|
// make unique temp .o file to put generated object file
|
|
|
|
SmallString<128> Filename;
|
|
|
|
int FD;
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
|
2013-08-12 21:07:31 +00:00
|
|
|
if (EC) {
|
|
|
|
errMsg = EC.message();
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2013-08-12 21:07:31 +00:00
|
|
|
}
|
2011-03-22 20:57:13 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
// generate object file
|
|
|
|
tool_output_file objFile(Filename.c_str(), FD);
|
2012-03-31 11:15:43 +00:00
|
|
|
|
2015-02-03 18:39:15 +00:00
|
|
|
bool genResult = compileOptimized(objFile.os(), errMsg);
|
2013-08-12 21:07:31 +00:00
|
|
|
objFile.os().close();
|
|
|
|
if (objFile.os().has_error()) {
|
|
|
|
objFile.os().clear_error();
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2013-08-12 21:07:31 +00:00
|
|
|
}
|
2012-03-31 11:15:43 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
objFile.keep();
|
|
|
|
if (!genResult) {
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2013-08-12 21:07:31 +00:00
|
|
|
}
|
2011-02-24 21:04:06 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
NativeObjectPath = Filename.c_str();
|
|
|
|
*name = NativeObjectPath.c_str();
|
2013-08-07 05:19:23 +00:00
|
|
|
return true;
|
2011-03-22 20:57:13 +00:00
|
|
|
}
|
2011-02-24 21:04:06 +00:00
|
|
|
|
2015-02-03 18:39:15 +00:00
|
|
|
const void *LTOCodeGenerator::compileOptimized(size_t *length,
|
|
|
|
std::string &errMsg) {
|
2011-03-22 20:57:13 +00:00
|
|
|
const char *name;
|
2015-02-03 18:39:15 +00:00
|
|
|
if (!compileOptimizedToFile(&name, errMsg))
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2011-03-22 20:57:13 +00:00
|
|
|
|
|
|
|
// read .o file into memory buffer
|
2014-07-06 17:43:13 +00:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFile(name, -1, false);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError()) {
|
|
|
|
errMsg = EC.message();
|
2013-09-04 17:44:24 +00:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2011-03-22 20:57:13 +00:00
|
|
|
}
|
2014-09-02 18:21:06 +00:00
|
|
|
NativeObjectFile = std::move(*BufferOrErr);
|
2008-02-27 22:25:36 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
// remove temp files
|
2013-09-04 17:44:24 +00:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2013-08-12 18:29:43 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
// return buffer, unless error
|
2014-04-15 06:32:26 +00:00
|
|
|
if (!NativeObjectFile)
|
|
|
|
return nullptr;
|
2013-09-04 17:44:24 +00:00
|
|
|
*length = NativeObjectFile->getBufferSize();
|
|
|
|
return NativeObjectFile->getBufferStart();
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 18:39:15 +00:00
|
|
|
|
|
|
|
bool LTOCodeGenerator::compile_to_file(const char **name,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
bool disableVectorization,
|
|
|
|
std::string &errMsg) {
|
2015-03-19 22:01:00 +00:00
|
|
|
if (!optimize(disableInline, disableGVNLoadPRE,
|
2015-02-03 18:39:15 +00:00
|
|
|
disableVectorization, errMsg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return compileOptimizedToFile(name, errMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* LTOCodeGenerator::compile(size_t *length,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
bool disableVectorization,
|
|
|
|
std::string &errMsg) {
|
2015-03-19 22:01:00 +00:00
|
|
|
if (!optimize(disableInline, disableGVNLoadPRE,
|
2015-02-03 18:39:15 +00:00
|
|
|
disableVectorization, errMsg))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return compileOptimized(length, errMsg);
|
|
|
|
}
|
|
|
|
|
2013-05-23 21:21:50 +00:00
|
|
|
bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
|
2014-04-15 06:32:26 +00:00
|
|
|
if (TargetMach)
|
2013-08-06 21:51:21 +00:00
|
|
|
return true;
|
2012-08-06 22:52:45 +00:00
|
|
|
|
2014-05-03 15:28:13 +00:00
|
|
|
std::string TripleStr = IRLinker.getModule()->getTargetTriple();
|
2012-10-12 17:39:25 +00:00
|
|
|
if (TripleStr.empty())
|
|
|
|
TripleStr = sys::getDefaultTargetTriple();
|
|
|
|
llvm::Triple Triple(TripleStr);
|
2012-08-06 22:52:45 +00:00
|
|
|
|
|
|
|
// create target machine from info for merged modules
|
2012-10-12 17:39:25 +00:00
|
|
|
const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
|
2014-04-15 06:32:26 +00:00
|
|
|
if (!march)
|
2013-08-06 21:51:21 +00:00
|
|
|
return false;
|
2012-08-06 22:52:45 +00:00
|
|
|
|
|
|
|
// 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;
|
2013-09-04 17:44:24 +00:00
|
|
|
switch (CodeModel) {
|
2012-08-06 22:52:45 +00:00
|
|
|
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;
|
2014-04-14 13:54:16 +00:00
|
|
|
case LTO_CODEGEN_PIC_MODEL_DEFAULT:
|
|
|
|
// RelocModel is already the default, so leave it that way.
|
|
|
|
break;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
2012-08-06 22:52:45 +00:00
|
|
|
|
2014-04-25 21:46:51 +00:00
|
|
|
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
|
|
|
|
// the default set of features.
|
|
|
|
SubtargetFeatures Features(MAttr);
|
2012-10-12 17:39:25 +00:00
|
|
|
Features.getDefaultSubtargetFeatures(Triple);
|
2012-08-06 22:52:45 +00:00
|
|
|
std::string FeatureStr = Features.getString();
|
2012-10-12 17:39:25 +00:00
|
|
|
// Set a default CPU for Darwin triples.
|
2013-09-04 17:44:24 +00:00
|
|
|
if (MCpu.empty() && Triple.isOSDarwin()) {
|
2012-10-12 17:39:25 +00:00
|
|
|
if (Triple.getArch() == llvm::Triple::x86_64)
|
2013-09-04 17:44:24 +00:00
|
|
|
MCpu = "core2";
|
2012-10-12 17:39:25 +00:00
|
|
|
else if (Triple.getArch() == llvm::Triple::x86)
|
2013-09-04 17:44:24 +00:00
|
|
|
MCpu = "yonah";
|
2014-07-23 12:32:47 +00:00
|
|
|
else if (Triple.getArch() == llvm::Triple::aarch64)
|
2014-03-29 10:18:08 +00:00
|
|
|
MCpu = "cyclone";
|
2012-10-12 17:39:25 +00:00
|
|
|
}
|
2013-09-30 16:39:19 +00:00
|
|
|
|
2015-03-19 22:01:00 +00:00
|
|
|
CodeGenOpt::Level CGOptLevel;
|
|
|
|
switch (OptLevel) {
|
|
|
|
case 0:
|
|
|
|
CGOptLevel = CodeGenOpt::None;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
CGOptLevel = CodeGenOpt::Less;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
CGOptLevel = CodeGenOpt::Default;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
CGOptLevel = CodeGenOpt::Aggressive;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
|
|
|
|
RelocModel, CodeModel::Default,
|
2015-03-19 22:01:00 +00:00
|
|
|
CGOptLevel);
|
2013-08-06 21:51:21 +00:00
|
|
|
return true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
void LTOCodeGenerator::
|
|
|
|
applyRestriction(GlobalValue &GV,
|
2014-08-27 05:25:00 +00:00
|
|
|
ArrayRef<StringRef> Libcalls,
|
2013-09-04 20:08:46 +00:00
|
|
|
std::vector<const char*> &MustPreserveList,
|
2014-08-21 05:55:13 +00:00
|
|
|
SmallPtrSetImpl<GlobalValue*> &AsmUsed,
|
2013-09-04 20:08:46 +00:00
|
|
|
Mangler &Mangler) {
|
2014-02-19 17:23:20 +00:00
|
|
|
// There are no restrictions to apply to declarations.
|
2011-03-02 04:14:42 +00:00
|
|
|
if (GV.isDeclaration())
|
|
|
|
return;
|
2014-02-19 17:23:20 +00:00
|
|
|
|
|
|
|
// There is nothing more restrictive than private linkage.
|
|
|
|
if (GV.hasPrivateLinkage())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallString<64> Buffer;
|
2014-02-19 20:30:41 +00:00
|
|
|
TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
|
2014-02-19 17:23:20 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
if (MustPreserveSymbols.count(Buffer))
|
2013-09-04 20:08:46 +00:00
|
|
|
MustPreserveList.push_back(GV.getName().data());
|
2013-09-04 17:44:24 +00:00
|
|
|
if (AsmUndefinedRefs.count(Buffer))
|
2013-09-04 20:08:46 +00:00
|
|
|
AsmUsed.insert(&GV);
|
2013-11-12 21:44:01 +00:00
|
|
|
|
|
|
|
// Conservatively append user-supplied runtime library functions to
|
|
|
|
// llvm.compiler.used. These could be internalized and deleted by
|
|
|
|
// optimizations like -globalopt, causing problems when later optimizations
|
|
|
|
// add new library calls (e.g., llvm.memset => memset and printf => puts).
|
|
|
|
// Leave it to the linker to remove any dead code (e.g. with -dead_strip).
|
|
|
|
if (isa<Function>(GV) &&
|
|
|
|
std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName()))
|
|
|
|
AsmUsed.insert(&GV);
|
2011-03-02 04:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
2014-08-21 05:55:13 +00:00
|
|
|
SmallPtrSetImpl<GlobalValue*> &UsedValues) {
|
2014-04-15 06:32:26 +00:00
|
|
|
if (!LLVMUsed) return;
|
2011-03-02 04:14:42 +00:00
|
|
|
|
2013-04-24 17:54:35 +00:00
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
2011-03-02 04:14:42 +00:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
2012-03-31 10:50:14 +00:00
|
|
|
if (GlobalValue *GV =
|
2012-03-31 11:15:43 +00:00
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
2011-03-02 04:14:42 +00:00
|
|
|
UsedValues.insert(GV);
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:16:24 +00:00
|
|
|
// 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.
|
2013-11-12 21:44:01 +00:00
|
|
|
static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
|
|
|
|
const TargetLibraryInfo& TLI,
|
2015-01-30 01:16:24 +00:00
|
|
|
const Module &Mod,
|
|
|
|
const TargetMachine &TM) {
|
2013-11-12 21:44:01 +00:00
|
|
|
// TargetLibraryInfo has info on C runtime library calls on the current
|
|
|
|
// target.
|
|
|
|
for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
|
|
|
|
I != E; ++I) {
|
|
|
|
LibFunc::Func F = static_cast<LibFunc::Func>(I);
|
|
|
|
if (TLI.has(F))
|
|
|
|
Libcalls.push_back(TLI.getName(F));
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:16:24 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-11-12 21:44:01 +00:00
|
|
|
|
2013-11-16 16:15:56 +00:00
|
|
|
array_pod_sort(Libcalls.begin(), Libcalls.end());
|
2013-11-12 21:44:01 +00:00
|
|
|
Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
|
|
|
|
Libcalls.end());
|
|
|
|
}
|
|
|
|
|
2010-03-12 18:44:54 +00:00
|
|
|
void LTOCodeGenerator::applyScopeRestrictions() {
|
2014-04-02 22:05:57 +00:00
|
|
|
if (ScopeRestrictionsDone)
|
2013-09-04 17:44:24 +00:00
|
|
|
return;
|
2014-05-03 15:28:13 +00:00
|
|
|
Module *mergedModule = IRLinker.getModule();
|
2010-03-12 18:44:54 +00:00
|
|
|
|
|
|
|
// Start off with a verification pass.
|
2015-02-13 10:01:29 +00:00
|
|
|
legacy::PassManager passes;
|
2010-03-12 18:44:54 +00:00
|
|
|
passes.add(createVerifierPass());
|
|
|
|
|
2012-03-31 10:50:14 +00:00
|
|
|
// mark which symbols can not be internalized
|
2015-01-26 19:03:15 +00:00
|
|
|
Mangler Mangler(TargetMach->getDataLayout());
|
2013-09-04 20:08:46 +00:00
|
|
|
std::vector<const char*> MustPreserveList;
|
|
|
|
SmallPtrSet<GlobalValue*, 8> AsmUsed;
|
2013-11-12 21:44:01 +00:00
|
|
|
std::vector<StringRef> Libcalls;
|
[PM] Rework how the TargetLibraryInfo pass integrates with the new pass
manager to support the actual uses of it. =]
When I ported instcombine to the new pass manager I discover that it
didn't work because TLI wasn't available in the right places. This is
a somewhat surprising and/or subtle aspect of the new pass manager
design that came up before but I think is useful to be reminded of:
While the new pass manager *allows* a function pass to query a module
analysis, it requires that the module analysis is already run and cached
prior to the function pass manager starting up, possibly with
a 'require<foo>' style utility in the pass pipeline. This is an
intentional hurdle because using a module analysis from a function pass
*requires* that the module analysis is run prior to entering the
function pass manager. Otherwise the other functions in the module could
be in who-knows-what state, etc.
A somewhat surprising consequence of this design decision (at least to
me) is that you have to design a function pass that leverages
a module analysis to do so as an optional feature. Even if that means
your function pass does no work in the absence of the module analysis,
you have to handle that possibility and remain conservatively correct.
This is a natural consequence of things being able to invalidate the
module analysis and us being unable to re-run it. And it's a generally
good thing because it lets us reorder passes arbitrarily without
breaking correctness, etc.
This ends up causing problems in one case. What if we have a module
analysis that is *definitionally* impossible to invalidate. In the
places this might come up, the analysis is usually also definitionally
trivial to run even while other transformation passes run on the module,
regardless of the state of anything. And so, it follows that it is
natural to have a hard requirement on such analyses from a function
pass.
It turns out, that TargetLibraryInfo is just such an analysis, and
InstCombine has a hard requirement on it.
The approach I've taken here is to produce an analysis that models this
flexibility by making it both a module and a function analysis. This
exposes the fact that it is in fact safe to compute at any point. We can
even make it a valid CGSCC analysis at some point if that is useful.
However, we don't want to have a copy of the actual target library info
state for each function! This state is specific to the triple. The
somewhat direct and blunt approach here is to turn TLI into a pimpl,
with the state and mutators in the implementation class and the query
routines primarily in the wrapper. Then the analysis can lazily
construct and cache the implementations, keyed on the triple, and
on-demand produce wrappers of them for each function.
One minor annoyance is that we will end up with a wrapper for each
function in the module. While this is a bit wasteful (one pointer per
function) it seems tolerable. And it has the advantage of ensuring that
we pay the absolute minimum synchronization cost to access this
information should we end up with a nice parallel function pass manager
in the future. We could look into trying to mark when analysis results
are especially cheap to recompute and more eagerly GC-ing the cached
results, or we could look at supporting a variant of analyses whose
results are specifically *not* cached and expected to just be used and
discarded by the consumer. Either way, these seem like incremental
enhancements that should happen when we start profiling the memory and
CPU usage of the new pass manager and not before.
The other minor annoyance is that if we end up using the TLI in both
a module pass and a function pass, those will be produced by two
separate analyses, and thus will point to separate copies of the
implementation state. While a minor issue, I dislike this and would like
to find a way to cleanly allow a single analysis instance to be used
across multiple IR unit managers. But I don't have a good solution to
this today, and I don't want to hold up all of the work waiting to come
up with one. This too seems like a reasonable thing to incrementally
improve later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226981 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-24 02:06:09 +00:00
|
|
|
TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
|
|
|
|
TargetLibraryInfo TLI(TLII);
|
2015-01-30 01:16:24 +00:00
|
|
|
|
|
|
|
accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach);
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
for (Module::iterator f = mergedModule->begin(),
|
|
|
|
e = mergedModule->end(); f != e; ++f)
|
2013-11-12 21:44:01 +00:00
|
|
|
applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2012-03-31 10:50:14 +00:00
|
|
|
for (Module::global_iterator v = mergedModule->global_begin(),
|
2011-03-02 04:14:42 +00:00
|
|
|
e = mergedModule->global_end(); v != e; ++v)
|
2013-11-12 21:44:01 +00:00
|
|
|
applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2011-03-02 04:14:42 +00:00
|
|
|
for (Module::alias_iterator a = mergedModule->alias_begin(),
|
|
|
|
e = mergedModule->alias_end(); a != e; ++a)
|
2013-11-12 21:44:01 +00:00
|
|
|
applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
GlobalVariable *LLVMCompilerUsed =
|
|
|
|
mergedModule->getGlobalVariable("llvm.compiler.used");
|
2013-09-04 20:08:46 +00:00
|
|
|
findUsedValues(LLVMCompilerUsed, AsmUsed);
|
2011-03-02 04:14:42 +00:00
|
|
|
if (LLVMCompilerUsed)
|
|
|
|
LLVMCompilerUsed->eraseFromParent();
|
|
|
|
|
2013-09-04 20:08:46 +00:00
|
|
|
if (!AsmUsed.empty()) {
|
2013-09-04 17:44:24 +00:00
|
|
|
llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
|
2013-04-24 17:54:35 +00:00
|
|
|
std::vector<Constant*> asmUsed2;
|
2014-05-05 20:06:41 +00:00
|
|
|
for (auto *GV : AsmUsed) {
|
2013-04-24 17:54:35 +00:00
|
|
|
Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
|
|
|
|
asmUsed2.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
|
|
|
|
LLVMCompilerUsed =
|
|
|
|
new llvm::GlobalVariable(*mergedModule, ATy, false,
|
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
|
|
|
llvm::ConstantArray::get(ATy, asmUsed2),
|
|
|
|
"llvm.compiler.used");
|
|
|
|
|
|
|
|
LLVMCompilerUsed->setSection("llvm.metadata");
|
2010-03-12 18:44:54 +00:00
|
|
|
}
|
2011-03-02 04:14:42 +00:00
|
|
|
|
2014-04-02 22:05:57 +00:00
|
|
|
passes.add(createInternalizePass(MustPreserveList));
|
2011-03-02 04:14:42 +00:00
|
|
|
|
2010-03-12 18:44:54 +00:00
|
|
|
// apply scope restrictions
|
|
|
|
passes.run(*mergedModule);
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
ScopeRestrictionsDone = true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimize merged modules using various IPO passes
|
2015-03-19 22:01:00 +00:00
|
|
|
bool LTOCodeGenerator::optimize(bool DisableInline,
|
2015-02-03 18:39:15 +00:00
|
|
|
bool DisableGVNLoadPRE,
|
|
|
|
bool DisableVectorization,
|
|
|
|
std::string &errMsg) {
|
2013-08-06 21:51:21 +00:00
|
|
|
if (!this->determineTarget(errMsg))
|
|
|
|
return false;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2014-05-03 15:28:13 +00:00
|
|
|
Module *mergedModule = IRLinker.getModule();
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-05-23 21:21:50 +00:00
|
|
|
// Mark which symbols can not be internalized
|
2012-04-09 22:18:01 +00:00
|
|
|
this->applyScopeRestrictions();
|
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Instantiate the pass manager to organize the passes.
|
2015-02-13 10:01:29 +00:00
|
|
|
legacy::PassManager passes;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-10-08 16:39:34 +00:00
|
|
|
// Add an appropriate DataLayout instance for this module...
|
2015-03-04 18:43:29 +00:00
|
|
|
mergedModule->setDataLayout(*TargetMach->getDataLayout());
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2015-02-01 12:26:09 +00:00
|
|
|
passes.add(
|
|
|
|
createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
|
2015-01-30 13:33:42 +00:00
|
|
|
|
2014-08-21 18:49:52 +00:00
|
|
|
Triple TargetTriple(TargetMach->getTargetTriple());
|
2014-08-21 20:03:44 +00:00
|
|
|
PassManagerBuilder PMB;
|
|
|
|
PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
|
2014-10-26 21:50:58 +00:00
|
|
|
PMB.LoopVectorize = !DisableVectorization;
|
|
|
|
PMB.SLPVectorize = !DisableVectorization;
|
2014-08-21 20:03:44 +00:00
|
|
|
if (!DisableInline)
|
|
|
|
PMB.Inliner = createFunctionInliningPass();
|
[PM] Rework how the TargetLibraryInfo pass integrates with the new pass
manager to support the actual uses of it. =]
When I ported instcombine to the new pass manager I discover that it
didn't work because TLI wasn't available in the right places. This is
a somewhat surprising and/or subtle aspect of the new pass manager
design that came up before but I think is useful to be reminded of:
While the new pass manager *allows* a function pass to query a module
analysis, it requires that the module analysis is already run and cached
prior to the function pass manager starting up, possibly with
a 'require<foo>' style utility in the pass pipeline. This is an
intentional hurdle because using a module analysis from a function pass
*requires* that the module analysis is run prior to entering the
function pass manager. Otherwise the other functions in the module could
be in who-knows-what state, etc.
A somewhat surprising consequence of this design decision (at least to
me) is that you have to design a function pass that leverages
a module analysis to do so as an optional feature. Even if that means
your function pass does no work in the absence of the module analysis,
you have to handle that possibility and remain conservatively correct.
This is a natural consequence of things being able to invalidate the
module analysis and us being unable to re-run it. And it's a generally
good thing because it lets us reorder passes arbitrarily without
breaking correctness, etc.
This ends up causing problems in one case. What if we have a module
analysis that is *definitionally* impossible to invalidate. In the
places this might come up, the analysis is usually also definitionally
trivial to run even while other transformation passes run on the module,
regardless of the state of anything. And so, it follows that it is
natural to have a hard requirement on such analyses from a function
pass.
It turns out, that TargetLibraryInfo is just such an analysis, and
InstCombine has a hard requirement on it.
The approach I've taken here is to produce an analysis that models this
flexibility by making it both a module and a function analysis. This
exposes the fact that it is in fact safe to compute at any point. We can
even make it a valid CGSCC analysis at some point if that is useful.
However, we don't want to have a copy of the actual target library info
state for each function! This state is specific to the triple. The
somewhat direct and blunt approach here is to turn TLI into a pimpl,
with the state and mutators in the implementation class and the query
routines primarily in the wrapper. Then the analysis can lazily
construct and cache the implementations, keyed on the triple, and
on-demand produce wrappers of them for each function.
One minor annoyance is that we will end up with a wrapper for each
function in the module. While this is a bit wasteful (one pointer per
function) it seems tolerable. And it has the advantage of ensuring that
we pay the absolute minimum synchronization cost to access this
information should we end up with a nice parallel function pass manager
in the future. We could look into trying to mark when analysis results
are especially cheap to recompute and more eagerly GC-ing the cached
results, or we could look at supporting a variant of analyses whose
results are specifically *not* cached and expected to just be used and
discarded by the consumer. Either way, these seem like incremental
enhancements that should happen when we start profiling the memory and
CPU usage of the new pass manager and not before.
The other minor annoyance is that if we end up using the TLI in both
a module pass and a function pass, those will be produced by two
separate analyses, and thus will point to separate copies of the
implementation state. While a minor issue, I dislike this and would like
to find a way to cleanly allow a single analysis instance to be used
across multiple IR unit managers. But I don't have a good solution to
this today, and I don't want to hold up all of the work waiting to come
up with one. This too seems like a reasonable thing to incrementally
improve later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226981 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-24 02:06:09 +00:00
|
|
|
PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
|
2015-03-19 22:01:00 +00:00
|
|
|
PMB.OptLevel = OptLevel;
|
2014-08-21 20:03:44 +00:00
|
|
|
PMB.VerifyInput = true;
|
|
|
|
PMB.VerifyOutput = true;
|
|
|
|
|
2015-01-30 13:33:42 +00:00
|
|
|
PMB.populateLTOPassManager(passes);
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2015-02-03 18:39:15 +00:00
|
|
|
// 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();
|
|
|
|
|
2015-02-13 10:01:29 +00:00
|
|
|
legacy::PassManager codeGenPasses;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2015-04-09 15:54:59 +00:00
|
|
|
formatted_raw_ostream Out(out);
|
|
|
|
|
2013-03-29 23:28:55 +00:00
|
|
|
// If the bitcode files contain ARC code and were compiled with optimization,
|
|
|
|
// the ObjCARCContractPass must be run, so do it unconditionally here.
|
|
|
|
codeGenPasses.add(createObjCARCContractPass());
|
|
|
|
|
2015-04-09 15:54:59 +00:00
|
|
|
if (TargetMach->addPassesToEmitFile(codeGenPasses, Out,
|
2013-09-04 17:44:24 +00:00
|
|
|
TargetMachine::CGFT_ObjectFile)) {
|
2012-03-31 11:15:43 +00:00
|
|
|
errMsg = "target file type not supported";
|
2013-08-06 21:51:21 +00:00
|
|
|
return false;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Run the code generator, and write assembly file
|
2013-03-13 21:18:46 +00:00
|
|
|
codeGenPasses.run(*mergedModule);
|
2009-07-26 22:16:39 +00:00
|
|
|
|
2013-08-06 21:51:21 +00:00
|
|
|
return true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 10:49:43 +00:00
|
|
|
/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
|
|
|
|
/// 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.
|
2013-09-04 17:44:24 +00:00
|
|
|
if (CodegenOptions.empty())
|
2013-09-24 23:52:22 +00:00
|
|
|
CodegenOptions.push_back(strdup("libLLVMLTO"));
|
2013-09-04 17:44:24 +00:00
|
|
|
CodegenOptions.push_back(strdup(o.first.str().c_str()));
|
2012-03-31 10:49:43 +00:00
|
|
|
}
|
2008-07-08 21:14:10 +00:00
|
|
|
}
|
2013-10-02 14:36:23 +00:00
|
|
|
|
|
|
|
void LTOCodeGenerator::parseCodeGenDebugOptions() {
|
|
|
|
// if options were requested, set them
|
|
|
|
if (!CodegenOptions.empty())
|
|
|
|
cl::ParseCommandLineOptions(CodegenOptions.size(),
|
|
|
|
const_cast<char **>(&CodegenOptions[0]));
|
|
|
|
}
|
2014-01-15 22:04:35 +00:00
|
|
|
|
|
|
|
void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
|
|
|
|
void *Context) {
|
|
|
|
((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
|
|
|
|
// Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
|
|
|
|
lto_codegen_diagnostic_severity_t Severity;
|
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error:
|
|
|
|
Severity = LTO_DS_ERROR;
|
|
|
|
break;
|
|
|
|
case DS_Warning:
|
|
|
|
Severity = LTO_DS_WARNING;
|
|
|
|
break;
|
2014-02-28 09:08:45 +00:00
|
|
|
case DS_Remark:
|
|
|
|
Severity = LTO_DS_REMARK;
|
|
|
|
break;
|
2014-01-15 22:04:35 +00:00
|
|
|
case DS_Note:
|
|
|
|
Severity = LTO_DS_NOTE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Create the string that will be reported to the external diagnostic handler.
|
2014-06-26 22:52:05 +00:00
|
|
|
std::string MsgStorage;
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
2014-01-15 22:04:35 +00:00
|
|
|
DI.print(DP);
|
2014-06-26 22:52:05 +00:00
|
|
|
Stream.flush();
|
2014-01-15 22:04:35 +00:00
|
|
|
|
|
|
|
// If this method has been called it means someone has set up an external
|
|
|
|
// diagnostic handler. Assert on that.
|
|
|
|
assert(DiagHandler && "Invalid diagnostic handler");
|
2014-06-26 22:52:05 +00:00
|
|
|
(*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
|
2014-01-15 22:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
|
|
|
|
void *Ctxt) {
|
|
|
|
this->DiagHandler = DiagHandler;
|
|
|
|
this->DiagContext = Ctxt;
|
|
|
|
if (!DiagHandler)
|
2014-04-15 06:32:26 +00:00
|
|
|
return Context.setDiagnosticHandler(nullptr, nullptr);
|
2014-01-15 22:04:35 +00:00
|
|
|
// Register the LTOCodeGenerator stub in the LLVMContext to forward the
|
|
|
|
// diagnostic to the external DiagHandler.
|
2014-10-02 21:11:04 +00:00
|
|
|
Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
|
|
|
|
/* RespectFilters */ true);
|
2014-01-15 22:04:35 +00:00
|
|
|
}
|