2008-02-27 04:26:43 +08: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 18:50:14 +08:00
|
|
|
//
|
2008-02-27 04:26:43 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-03-31 18:50:14 +08:00
|
|
|
// This file implements the Link Time Optimization library. This library is
|
2008-02-27 04:26:43 +08:00
|
|
|
// intended to be used by linker to optimize code at link time.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-09-25 07:52:22 +08:00
|
|
|
#include "llvm/LTO/LTOCodeGenerator.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-06-17 14:52:10 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2015-01-15 10:16:27 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-01-31 19:17:59 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2008-02-27 04:26:43 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-11-13 05:44:01 +08:00
|
|
|
#include "llvm/CodeGen/RuntimeLibcalls.h"
|
2012-03-31 19:15:43 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-16 06:04:35 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2013-07-23 02:40:34 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/LTO/LTOModule.h"
|
2014-03-06 11:42:23 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2010-03-13 02:44:54 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-06-29 09:14:12 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2010-03-13 02:44:54 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-06-18 02:05:35 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-30 02:47:54 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:47:54 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-03-04 18:07:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-11-13 05:44:01 +08:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2013-10-01 00:39:19 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2013-03-30 07:28:55 +08:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2008-02-27 04:26:43 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-03-31 18:49:43 +08:00
|
|
|
const char* LTOCodeGenerator::getVersionString() {
|
2008-02-27 04:26:43 +08:00
|
|
|
#ifdef LLVM_VERSION_INFO
|
2012-03-31 18:49:43 +08:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
|
2008-02-27 04:26:43 +08:00
|
|
|
#else
|
2012-03-31 18:49:43 +08:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION;
|
2008-02-27 04:26:43 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-31 18:50:14 +08:00
|
|
|
LTOCodeGenerator::LTOCodeGenerator()
|
2014-11-12 07:03:29 +08:00
|
|
|
: Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) {
|
2015-04-28 07:19:26 +08:00
|
|
|
initializeLTOPasses();
|
2014-11-12 07:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
|
|
|
|
: OwnedContext(std::move(Context)), Context(*OwnedContext),
|
2015-04-28 07:19:26 +08:00
|
|
|
IRLinker(new Module("ld-temp.o", *OwnedContext)) {
|
2013-07-23 02:40:34 +08:00
|
|
|
initializeLTOPasses();
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2015-02-26 05:20:53 +08: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 18:49:43 +08:00
|
|
|
LTOCodeGenerator::~LTOCodeGenerator() {
|
2015-02-26 05:20:53 +08:00
|
|
|
destroyMergedModule();
|
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
delete TargetMach;
|
2014-04-15 14:32:26 +08:00
|
|
|
TargetMach = nullptr;
|
2013-10-16 16:59:57 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
for (std::vector<char *>::iterator I = CodegenOptions.begin(),
|
|
|
|
E = CodegenOptions.end();
|
|
|
|
I != E; ++I)
|
2012-03-31 18:49:43 +08:00
|
|
|
free(*I);
|
|
|
|
}
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2013-07-23 02:40:34 +08:00
|
|
|
// Initialize LTO passes. Please keep this funciton in sync with
|
2013-07-23 14:44:34 +08:00
|
|
|
// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
|
2014-01-11 04:24:35 +08:00
|
|
|
// passes are initialized.
|
2013-07-23 02:40:34 +08:00
|
|
|
void LTOCodeGenerator::initializeLTOPasses() {
|
|
|
|
PassRegistry &R = *PassRegistry::getPassRegistry();
|
|
|
|
|
|
|
|
initializeInternalizePassPass(R);
|
|
|
|
initializeIPSCCPPass(R);
|
|
|
|
initializeGlobalOptPass(R);
|
|
|
|
initializeConstantMergePass(R);
|
|
|
|
initializeDAHPass(R);
|
2015-01-21 06:44:35 +08:00
|
|
|
initializeInstructionCombiningPassPass(R);
|
2013-07-23 02:40:34 +08: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-19 03:13:09 +08:00
|
|
|
initializeMergedLoadStoreMotionPass(R);
|
2013-07-23 02:40:34 +08:00
|
|
|
initializeGVNPass(R);
|
|
|
|
initializeMemCpyOptPass(R);
|
|
|
|
initializeDCEPass(R);
|
2013-08-06 10:43:45 +08:00
|
|
|
initializeCFGSimplifyPassPass(R);
|
2013-07-23 02:40:34 +08:00
|
|
|
}
|
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
bool LTOCodeGenerator::addModule(LTOModule *mod) {
|
2014-11-12 07:13:10 +08:00
|
|
|
assert(&mod->getModule().getContext() == &Context &&
|
|
|
|
"Expected module in same context");
|
|
|
|
|
2014-10-25 12:06:10 +08:00
|
|
|
bool ret = IRLinker.linkInModule(&mod->getModule());
|
2011-03-02 12:14:42 +08:00
|
|
|
|
|
|
|
const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
|
|
|
|
for (int i = 0, e = undefs.size(); i != e; ++i)
|
2013-09-05 01:44:24 +08:00
|
|
|
AsmUndefinedRefs[undefs[i]] = 1;
|
2011-03-02 12:14:42 +08:00
|
|
|
|
2013-08-07 13:19:23 +08:00
|
|
|
return !ret;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2015-02-24 08:45:56 +08:00
|
|
|
void LTOCodeGenerator::setModule(LTOModule *Mod) {
|
|
|
|
assert(&Mod->getModule().getContext() == &Context &&
|
|
|
|
"Expected module in same context");
|
|
|
|
|
|
|
|
// Delete the old merged module.
|
2015-02-26 05:20:53 +08:00
|
|
|
destroyMergedModule();
|
2015-02-24 08:45:56 +08:00
|
|
|
AsmUndefinedRefs.clear();
|
|
|
|
|
2015-02-26 05:20:53 +08:00
|
|
|
OwnedModule = Mod;
|
2015-02-24 08:45:56 +08: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-10-01 00:39:19 +08:00
|
|
|
void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
|
2014-06-20 06:27:46 +08:00
|
|
|
Options = options;
|
2013-10-01 00:39:19 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 13:19:23 +08:00
|
|
|
void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
|
2012-03-31 19:15:43 +08:00
|
|
|
switch (debug) {
|
|
|
|
case LTO_DEBUG_MODEL_NONE:
|
2013-09-05 01:44:24 +08:00
|
|
|
EmitDwarfDebugInfo = false;
|
2013-08-07 13:19:23 +08:00
|
|
|
return;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
case LTO_DEBUG_MODEL_DWARF:
|
2013-09-05 01:44:24 +08:00
|
|
|
EmitDwarfDebugInfo = true;
|
2013-08-07 13:19:23 +08:00
|
|
|
return;
|
2012-03-31 19:15:43 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown debug format!");
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 13:19:23 +08:00
|
|
|
void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
|
2012-03-31 19:15:43 +08: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 21:54:16 +08:00
|
|
|
case LTO_CODEGEN_PIC_MODEL_DEFAULT:
|
2013-09-05 01:44:24 +08:00
|
|
|
CodeModel = model;
|
2013-08-07 13:19:23 +08:00
|
|
|
return;
|
2012-03-31 19:15:43 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown PIC model!");
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2009-08-23 15:49:08 +08:00
|
|
|
bool LTOCodeGenerator::writeMergedModules(const char *path,
|
|
|
|
std::string &errMsg) {
|
2013-08-07 05:51:21 +08:00
|
|
|
if (!determineTarget(errMsg))
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2009-08-23 15:49:08 +08:00
|
|
|
|
2013-08-09 07:51:04 +08:00
|
|
|
// mark which symbols can not be internalized
|
|
|
|
applyScopeRestrictions();
|
2009-08-23 15:49:08 +08:00
|
|
|
|
|
|
|
// create output file
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
tool_output_file Out(path, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
2009-08-23 15:49:08 +08:00
|
|
|
errMsg = "could not open bitcode file for writing: ";
|
|
|
|
errMsg += path;
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2009-08-23 15:49:08 +08:00
|
|
|
}
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2009-08-23 15:49:08 +08:00
|
|
|
// write bitcode to it
|
2015-04-28 07:38:54 +08:00
|
|
|
WriteBitcodeToFile(IRLinker.getModule(), Out.os(), ShouldEmbedUselists);
|
2010-09-01 22:20:41 +08:00
|
|
|
Out.os().close();
|
2010-05-28 04:19:47 +08:00
|
|
|
|
2010-09-01 22:20:41 +08:00
|
|
|
if (Out.os().has_error()) {
|
2009-08-23 15:49:08 +08:00
|
|
|
errMsg = "could not write bitcode file: ";
|
|
|
|
errMsg += path;
|
2010-09-01 22:20:41 +08:00
|
|
|
Out.os().clear_error();
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2009-08-23 15:49:08 +08:00
|
|
|
}
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2010-08-21 00:59:15 +08:00
|
|
|
Out.keep();
|
2013-08-07 13:19:23 +08:00
|
|
|
return true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 02:39:15 +08:00
|
|
|
bool LTOCodeGenerator::compileOptimizedToFile(const char **name,
|
|
|
|
std::string &errMsg) {
|
2013-08-13 05:07:31 +08:00
|
|
|
// make unique temp .o file to put generated object file
|
|
|
|
SmallString<128> Filename;
|
|
|
|
int FD;
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
|
2013-08-13 05:07:31 +08:00
|
|
|
if (EC) {
|
|
|
|
errMsg = EC.message();
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2013-08-13 05:07:31 +08:00
|
|
|
}
|
2011-03-23 04:57:13 +08:00
|
|
|
|
2013-08-13 05:07:31 +08:00
|
|
|
// generate object file
|
|
|
|
tool_output_file objFile(Filename.c_str(), FD);
|
2012-03-31 19:15:43 +08:00
|
|
|
|
2015-02-04 02:39:15 +08:00
|
|
|
bool genResult = compileOptimized(objFile.os(), errMsg);
|
2013-08-13 05:07:31 +08:00
|
|
|
objFile.os().close();
|
|
|
|
if (objFile.os().has_error()) {
|
|
|
|
objFile.os().clear_error();
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2013-08-13 05:07:31 +08:00
|
|
|
}
|
2012-03-31 19:15:43 +08:00
|
|
|
|
2013-08-13 05:07:31 +08:00
|
|
|
objFile.keep();
|
|
|
|
if (!genResult) {
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2013-08-13 05:07:31 +08:00
|
|
|
}
|
2011-02-25 05:04:06 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
NativeObjectPath = Filename.c_str();
|
|
|
|
*name = NativeObjectPath.c_str();
|
2013-08-07 13:19:23 +08:00
|
|
|
return true;
|
2011-03-23 04:57:13 +08:00
|
|
|
}
|
2011-02-25 05:04:06 +08:00
|
|
|
|
2015-02-04 02:39:15 +08:00
|
|
|
const void *LTOCodeGenerator::compileOptimized(size_t *length,
|
|
|
|
std::string &errMsg) {
|
2011-03-23 04:57:13 +08:00
|
|
|
const char *name;
|
2015-02-04 02:39:15 +08:00
|
|
|
if (!compileOptimizedToFile(&name, errMsg))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-03-23 04:57:13 +08:00
|
|
|
|
|
|
|
// read .o file into memory buffer
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFile(name, -1, false);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError()) {
|
|
|
|
errMsg = EC.message();
|
2013-09-05 01:44:24 +08:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-03-23 04:57:13 +08:00
|
|
|
}
|
2014-09-03 02:21:06 +08:00
|
|
|
NativeObjectFile = std::move(*BufferOrErr);
|
2008-02-28 06:25:36 +08:00
|
|
|
|
2013-08-13 05:07:31 +08:00
|
|
|
// remove temp files
|
2013-09-05 01:44:24 +08:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2013-08-13 02:29:43 +08:00
|
|
|
|
2013-08-13 05:07:31 +08:00
|
|
|
// return buffer, unless error
|
2014-04-15 14:32:26 +08:00
|
|
|
if (!NativeObjectFile)
|
|
|
|
return nullptr;
|
2013-09-05 01:44:24 +08:00
|
|
|
*length = NativeObjectFile->getBufferSize();
|
|
|
|
return NativeObjectFile->getBufferStart();
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 02:39:15 +08:00
|
|
|
|
|
|
|
bool LTOCodeGenerator::compile_to_file(const char **name,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
bool disableVectorization,
|
|
|
|
std::string &errMsg) {
|
2015-03-20 06:01:00 +08:00
|
|
|
if (!optimize(disableInline, disableGVNLoadPRE,
|
2015-02-04 02:39:15 +08: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-20 06:01:00 +08:00
|
|
|
if (!optimize(disableInline, disableGVNLoadPRE,
|
2015-02-04 02:39:15 +08:00
|
|
|
disableVectorization, errMsg))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return compileOptimized(length, errMsg);
|
|
|
|
}
|
|
|
|
|
2013-05-24 05:21:50 +08:00
|
|
|
bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
|
2014-04-15 14:32:26 +08:00
|
|
|
if (TargetMach)
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2012-08-07 06:52:45 +08:00
|
|
|
|
2014-05-03 23:28:13 +08:00
|
|
|
std::string TripleStr = IRLinker.getModule()->getTargetTriple();
|
2012-10-13 01:39:25 +08:00
|
|
|
if (TripleStr.empty())
|
|
|
|
TripleStr = sys::getDefaultTargetTriple();
|
|
|
|
llvm::Triple Triple(TripleStr);
|
2012-08-07 06:52:45 +08:00
|
|
|
|
|
|
|
// create target machine from info for merged modules
|
2012-10-13 01:39:25 +08:00
|
|
|
const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
|
2014-04-15 14:32:26 +08:00
|
|
|
if (!march)
|
2013-08-07 05:51:21 +08:00
|
|
|
return false;
|
2012-08-07 06:52:45 +08: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-05 01:44:24 +08:00
|
|
|
switch (CodeModel) {
|
2012-08-07 06:52:45 +08: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 21:54:16 +08:00
|
|
|
case LTO_CODEGEN_PIC_MODEL_DEFAULT:
|
|
|
|
// RelocModel is already the default, so leave it that way.
|
|
|
|
break;
|
2012-03-31 19:15:43 +08:00
|
|
|
}
|
2012-08-07 06:52:45 +08:00
|
|
|
|
2014-04-26 05:46:51 +08:00
|
|
|
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
|
|
|
|
// the default set of features.
|
|
|
|
SubtargetFeatures Features(MAttr);
|
2012-10-13 01:39:25 +08:00
|
|
|
Features.getDefaultSubtargetFeatures(Triple);
|
2012-08-07 06:52:45 +08:00
|
|
|
std::string FeatureStr = Features.getString();
|
2012-10-13 01:39:25 +08:00
|
|
|
// Set a default CPU for Darwin triples.
|
2013-09-05 01:44:24 +08:00
|
|
|
if (MCpu.empty() && Triple.isOSDarwin()) {
|
2012-10-13 01:39:25 +08:00
|
|
|
if (Triple.getArch() == llvm::Triple::x86_64)
|
2013-09-05 01:44:24 +08:00
|
|
|
MCpu = "core2";
|
2012-10-13 01:39:25 +08:00
|
|
|
else if (Triple.getArch() == llvm::Triple::x86)
|
2013-09-05 01:44:24 +08:00
|
|
|
MCpu = "yonah";
|
2014-07-23 20:32:47 +08:00
|
|
|
else if (Triple.getArch() == llvm::Triple::aarch64)
|
2014-03-29 18:18:08 +08:00
|
|
|
MCpu = "cyclone";
|
2012-10-13 01:39:25 +08:00
|
|
|
}
|
2013-10-01 00:39:19 +08:00
|
|
|
|
2015-03-20 06:01:00 +08: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-05 01:44:24 +08:00
|
|
|
TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
|
|
|
|
RelocModel, CodeModel::Default,
|
2015-03-20 06:01:00 +08:00
|
|
|
CGOptLevel);
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
void LTOCodeGenerator::
|
|
|
|
applyRestriction(GlobalValue &GV,
|
2014-08-27 13:25:00 +08:00
|
|
|
ArrayRef<StringRef> Libcalls,
|
2013-09-05 04:08:46 +08:00
|
|
|
std::vector<const char*> &MustPreserveList,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<GlobalValue*> &AsmUsed,
|
2013-09-05 04:08:46 +08:00
|
|
|
Mangler &Mangler) {
|
2014-02-20 01:23:20 +08:00
|
|
|
// There are no restrictions to apply to declarations.
|
2011-03-02 12:14:42 +08:00
|
|
|
if (GV.isDeclaration())
|
|
|
|
return;
|
2014-02-20 01:23:20 +08:00
|
|
|
|
|
|
|
// There is nothing more restrictive than private linkage.
|
|
|
|
if (GV.hasPrivateLinkage())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallString<64> Buffer;
|
2014-02-20 04:30:41 +08:00
|
|
|
TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
|
2014-02-20 01:23:20 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
if (MustPreserveSymbols.count(Buffer))
|
2013-09-05 04:08:46 +08:00
|
|
|
MustPreserveList.push_back(GV.getName().data());
|
2013-09-05 01:44:24 +08:00
|
|
|
if (AsmUndefinedRefs.count(Buffer))
|
2013-09-05 04:08:46 +08:00
|
|
|
AsmUsed.insert(&GV);
|
2013-11-13 05:44:01 +08: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 12:14:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<GlobalValue*> &UsedValues) {
|
2014-04-15 14:32:26 +08:00
|
|
|
if (!LLVMUsed) return;
|
2011-03-02 12:14:42 +08:00
|
|
|
|
2013-04-25 01:54:35 +08:00
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
2011-03-02 12:14:42 +08:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
2012-03-31 18:50:14 +08:00
|
|
|
if (GlobalValue *GV =
|
2012-03-31 19:15:43 +08:00
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
2011-03-02 12:14:42 +08:00
|
|
|
UsedValues.insert(GV);
|
|
|
|
}
|
|
|
|
|
2015-01-30 09:16:24 +08: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-13 05:44:01 +08:00
|
|
|
static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
|
|
|
|
const TargetLibraryInfo& TLI,
|
2015-01-30 09:16:24 +08:00
|
|
|
const Module &Mod,
|
|
|
|
const TargetMachine &TM) {
|
2013-11-13 05:44:01 +08: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 09:16:24 +08: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-13 05:44:01 +08:00
|
|
|
|
2013-11-17 00:15:56 +08:00
|
|
|
array_pod_sort(Libcalls.begin(), Libcalls.end());
|
2013-11-13 05:44:01 +08:00
|
|
|
Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
|
|
|
|
Libcalls.end());
|
|
|
|
}
|
|
|
|
|
2010-03-13 02:44:54 +08:00
|
|
|
void LTOCodeGenerator::applyScopeRestrictions() {
|
2015-04-18 01:10:09 +08:00
|
|
|
if (ScopeRestrictionsDone || !ShouldInternalize)
|
2013-09-05 01:44:24 +08:00
|
|
|
return;
|
2014-05-03 23:28:13 +08:00
|
|
|
Module *mergedModule = IRLinker.getModule();
|
2010-03-13 02:44:54 +08:00
|
|
|
|
|
|
|
// Start off with a verification pass.
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager passes;
|
2010-03-13 02:44:54 +08:00
|
|
|
passes.add(createVerifierPass());
|
|
|
|
|
2012-03-31 18:50:14 +08:00
|
|
|
// mark which symbols can not be internalized
|
2015-01-27 03:03:15 +08:00
|
|
|
Mangler Mangler(TargetMach->getDataLayout());
|
2013-09-05 04:08:46 +08:00
|
|
|
std::vector<const char*> MustPreserveList;
|
|
|
|
SmallPtrSet<GlobalValue*, 8> AsmUsed;
|
2013-11-13 05:44:01 +08: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.
llvm-svn: 226981
2015-01-24 10:06:09 +08:00
|
|
|
TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
|
|
|
|
TargetLibraryInfo TLI(TLII);
|
2015-01-30 09:16:24 +08:00
|
|
|
|
|
|
|
accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach);
|
2011-03-02 12:14:42 +08:00
|
|
|
|
|
|
|
for (Module::iterator f = mergedModule->begin(),
|
|
|
|
e = mergedModule->end(); f != e; ++f)
|
2013-11-13 05:44:01 +08:00
|
|
|
applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2012-03-31 18:50:14 +08:00
|
|
|
for (Module::global_iterator v = mergedModule->global_begin(),
|
2011-03-02 12:14:42 +08:00
|
|
|
e = mergedModule->global_end(); v != e; ++v)
|
2013-11-13 05:44:01 +08:00
|
|
|
applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2011-03-02 12:14:42 +08:00
|
|
|
for (Module::alias_iterator a = mergedModule->alias_begin(),
|
|
|
|
e = mergedModule->alias_end(); a != e; ++a)
|
2013-11-13 05:44:01 +08:00
|
|
|
applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2011-03-02 12:14:42 +08:00
|
|
|
|
|
|
|
GlobalVariable *LLVMCompilerUsed =
|
|
|
|
mergedModule->getGlobalVariable("llvm.compiler.used");
|
2013-09-05 04:08:46 +08:00
|
|
|
findUsedValues(LLVMCompilerUsed, AsmUsed);
|
2011-03-02 12:14:42 +08:00
|
|
|
if (LLVMCompilerUsed)
|
|
|
|
LLVMCompilerUsed->eraseFromParent();
|
|
|
|
|
2013-09-05 04:08:46 +08:00
|
|
|
if (!AsmUsed.empty()) {
|
2013-09-05 01:44:24 +08:00
|
|
|
llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
|
2013-04-25 01:54:35 +08:00
|
|
|
std::vector<Constant*> asmUsed2;
|
2014-05-06 04:06:41 +08:00
|
|
|
for (auto *GV : AsmUsed) {
|
2013-04-25 01:54:35 +08: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-13 02:44:54 +08:00
|
|
|
}
|
2011-03-02 12:14:42 +08:00
|
|
|
|
2014-04-03 06:05:57 +08:00
|
|
|
passes.add(createInternalizePass(MustPreserveList));
|
2011-03-02 12:14:42 +08:00
|
|
|
|
2010-03-13 02:44:54 +08:00
|
|
|
// apply scope restrictions
|
|
|
|
passes.run(*mergedModule);
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
ScopeRestrictionsDone = true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimize merged modules using various IPO passes
|
2015-03-20 06:01:00 +08:00
|
|
|
bool LTOCodeGenerator::optimize(bool DisableInline,
|
2015-02-04 02:39:15 +08:00
|
|
|
bool DisableGVNLoadPRE,
|
|
|
|
bool DisableVectorization,
|
|
|
|
std::string &errMsg) {
|
2013-08-07 05:51:21 +08:00
|
|
|
if (!this->determineTarget(errMsg))
|
|
|
|
return false;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2014-05-03 23:28:13 +08:00
|
|
|
Module *mergedModule = IRLinker.getModule();
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2013-05-24 05:21:50 +08:00
|
|
|
// Mark which symbols can not be internalized
|
2012-04-10 06:18:01 +08:00
|
|
|
this->applyScopeRestrictions();
|
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
// Instantiate the pass manager to organize the passes.
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager passes;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-10-09 00:39:34 +08:00
|
|
|
// Add an appropriate DataLayout instance for this module...
|
2015-03-05 02:43:29 +08:00
|
|
|
mergedModule->setDataLayout(*TargetMach->getDataLayout());
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2015-02-01 20:26:09 +08:00
|
|
|
passes.add(
|
|
|
|
createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
|
2015-01-30 21:33:42 +08:00
|
|
|
|
2014-08-22 02:49:52 +08:00
|
|
|
Triple TargetTriple(TargetMach->getTargetTriple());
|
2014-08-22 04:03:44 +08:00
|
|
|
PassManagerBuilder PMB;
|
|
|
|
PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
|
2014-10-27 05:50:58 +08:00
|
|
|
PMB.LoopVectorize = !DisableVectorization;
|
|
|
|
PMB.SLPVectorize = !DisableVectorization;
|
2014-08-22 04:03:44 +08: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.
llvm-svn: 226981
2015-01-24 10:06:09 +08:00
|
|
|
PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
|
2015-03-20 06:01:00 +08:00
|
|
|
PMB.OptLevel = OptLevel;
|
2014-08-22 04:03:44 +08:00
|
|
|
PMB.VerifyInput = true;
|
|
|
|
PMB.VerifyOutput = true;
|
|
|
|
|
2015-01-30 21:33:42 +08:00
|
|
|
PMB.populateLTOPassManager(passes);
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2015-02-04 02:39:15 +08:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
passes.run(*mergedModule);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:14:34 +08:00
|
|
|
bool LTOCodeGenerator::compileOptimized(raw_pwrite_stream &out,
|
|
|
|
std::string &errMsg) {
|
2015-02-04 02:39:15 +08:00
|
|
|
if (!this->determineTarget(errMsg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Module *mergedModule = IRLinker.getModule();
|
|
|
|
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager codeGenPasses;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2013-03-30 07:28:55 +08: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-10 05:06:08 +08:00
|
|
|
if (TargetMach->addPassesToEmitFile(codeGenPasses, out,
|
2013-09-05 01:44:24 +08:00
|
|
|
TargetMachine::CGFT_ObjectFile)) {
|
2012-03-31 19:15:43 +08:00
|
|
|
errMsg = "target file type not supported";
|
2013-08-07 05:51:21 +08:00
|
|
|
return false;
|
2012-03-31 19:15:43 +08:00
|
|
|
}
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
// Run the code generator, and write assembly file
|
2013-03-14 05:18:46 +08:00
|
|
|
codeGenPasses.run(*mergedModule);
|
2009-07-27 06:16:39 +08:00
|
|
|
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2012-03-31 18:49:43 +08: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-05 01:44:24 +08:00
|
|
|
if (CodegenOptions.empty())
|
2013-09-25 07:52:22 +08:00
|
|
|
CodegenOptions.push_back(strdup("libLLVMLTO"));
|
2013-09-05 01:44:24 +08:00
|
|
|
CodegenOptions.push_back(strdup(o.first.str().c_str()));
|
2012-03-31 18:49:43 +08:00
|
|
|
}
|
2008-07-09 05:14:10 +08:00
|
|
|
}
|
2013-10-02 22:36:23 +08:00
|
|
|
|
|
|
|
void LTOCodeGenerator::parseCodeGenDebugOptions() {
|
|
|
|
// if options were requested, set them
|
|
|
|
if (!CodegenOptions.empty())
|
|
|
|
cl::ParseCommandLineOptions(CodegenOptions.size(),
|
|
|
|
const_cast<char **>(&CodegenOptions[0]));
|
|
|
|
}
|
2014-01-16 06:04:35 +08: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 17:08:45 +08:00
|
|
|
case DS_Remark:
|
|
|
|
Severity = LTO_DS_REMARK;
|
|
|
|
break;
|
2014-01-16 06:04:35 +08:00
|
|
|
case DS_Note:
|
|
|
|
Severity = LTO_DS_NOTE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Create the string that will be reported to the external diagnostic handler.
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string MsgStorage;
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
2014-01-16 06:04:35 +08:00
|
|
|
DI.print(DP);
|
2014-06-27 06:52:05 +08:00
|
|
|
Stream.flush();
|
2014-01-16 06:04:35 +08: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-27 06:52:05 +08:00
|
|
|
(*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
|
2014-01-16 06:04:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
|
|
|
|
void *Ctxt) {
|
|
|
|
this->DiagHandler = DiagHandler;
|
|
|
|
this->DiagContext = Ctxt;
|
|
|
|
if (!DiagHandler)
|
2014-04-15 14:32:26 +08:00
|
|
|
return Context.setDiagnosticHandler(nullptr, nullptr);
|
2014-01-16 06:04:35 +08:00
|
|
|
// Register the LTOCodeGenerator stub in the LLVMContext to forward the
|
|
|
|
// diagnostic to the external DiagHandler.
|
2014-10-03 05:11:04 +08:00
|
|
|
Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
|
|
|
|
/* RespectFilters */ true);
|
2014-01-16 06:04:35 +08:00
|
|
|
}
|