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"
|
|
|
|
#include "llvm/LTO/LTOModule.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"
|
2011-08-03 05:50:27 +08:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2008-02-27 04:26:43 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.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"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-07-23 02:40:34 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/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"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/PassManager.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-03-13 02:44:54 +08:00
|
|
|
#include "llvm/Support/FormattedStream.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"
|
2010-12-10 02:06:07 +08:00
|
|
|
#include "llvm/Support/system_error.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"
|
2013-09-25 07:52:22 +08:00
|
|
|
#include "llvm/Target/Mangler.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"
|
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()
|
2013-09-05 01:44:24 +08:00
|
|
|
: Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
|
|
|
|
TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
|
|
|
|
CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) {
|
2013-07-23 02:40:34 +08:00
|
|
|
initializeLTOPasses();
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2012-03-31 18:49:43 +08:00
|
|
|
LTOCodeGenerator::~LTOCodeGenerator() {
|
2013-09-05 01:44:24 +08:00
|
|
|
delete TargetMach;
|
|
|
|
delete NativeObjectFile;
|
2013-10-16 16:59:57 +08:00
|
|
|
TargetMach = NULL;
|
|
|
|
NativeObjectFile = NULL;
|
|
|
|
|
|
|
|
Linker.deleteModule();
|
2008-02-27 04:26:43 +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
|
2013-07-23 02:40:34 +08:00
|
|
|
// passes are initialized.
|
|
|
|
//
|
|
|
|
void LTOCodeGenerator::initializeLTOPasses() {
|
|
|
|
PassRegistry &R = *PassRegistry::getPassRegistry();
|
|
|
|
|
|
|
|
initializeInternalizePassPass(R);
|
|
|
|
initializeIPSCCPPass(R);
|
|
|
|
initializeGlobalOptPass(R);
|
|
|
|
initializeConstantMergePass(R);
|
|
|
|
initializeDAHPass(R);
|
|
|
|
initializeInstCombinerPass(R);
|
|
|
|
initializeSimpleInlinerPass(R);
|
|
|
|
initializePruneEHPass(R);
|
|
|
|
initializeGlobalDCEPass(R);
|
|
|
|
initializeArgPromotionPass(R);
|
|
|
|
initializeJumpThreadingPass(R);
|
|
|
|
initializeSROAPass(R);
|
|
|
|
initializeSROA_DTPass(R);
|
|
|
|
initializeSROA_SSAUpPass(R);
|
|
|
|
initializeFunctionAttrsPass(R);
|
|
|
|
initializeGlobalsModRefPass(R);
|
|
|
|
initializeLICMPass(R);
|
|
|
|
initializeGVNPass(R);
|
|
|
|
initializeMemCpyOptPass(R);
|
|
|
|
initializeDCEPass(R);
|
2013-08-06 10:43:45 +08:00
|
|
|
initializeCFGSimplifyPassPass(R);
|
2013-07-23 02:40:34 +08:00
|
|
|
}
|
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
|
2013-09-05 01:44:24 +08:00
|
|
|
bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg);
|
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
|
|
|
|
2013-10-01 00:39:19 +08:00
|
|
|
void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
|
|
|
|
Options.LessPreciseFPMADOption = options.LessPreciseFPMADOption;
|
|
|
|
Options.NoFramePointerElim = options.NoFramePointerElim;
|
|
|
|
Options.AllowFPOpFusion = options.AllowFPOpFusion;
|
|
|
|
Options.UnsafeFPMath = options.UnsafeFPMath;
|
|
|
|
Options.NoInfsFPMath = options.NoInfsFPMath;
|
|
|
|
Options.NoNaNsFPMath = options.NoNaNsFPMath;
|
|
|
|
Options.HonorSignDependentRoundingFPMathOption =
|
|
|
|
options.HonorSignDependentRoundingFPMathOption;
|
|
|
|
Options.UseSoftFloat = options.UseSoftFloat;
|
|
|
|
Options.FloatABIType = options.FloatABIType;
|
|
|
|
Options.NoZerosInBSS = options.NoZerosInBSS;
|
|
|
|
Options.GuaranteedTailCallOpt = options.GuaranteedTailCallOpt;
|
|
|
|
Options.DisableTailCalls = options.DisableTailCalls;
|
|
|
|
Options.StackAlignmentOverride = options.StackAlignmentOverride;
|
|
|
|
Options.TrapFuncName = options.TrapFuncName;
|
|
|
|
Options.PositionIndependentExecutable = options.PositionIndependentExecutable;
|
|
|
|
Options.EnableSegmentedStacks = options.EnableSegmentedStacks;
|
|
|
|
Options.UseInitArray = options.UseInitArray;
|
|
|
|
}
|
|
|
|
|
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:
|
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
|
|
|
|
std::string ErrInfo;
|
2013-07-17 03:44:17 +08:00
|
|
|
tool_output_file Out(path, ErrInfo, sys::fs::F_Binary);
|
2009-08-23 15:49:08 +08:00
|
|
|
if (!ErrInfo.empty()) {
|
|
|
|
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
|
2013-09-05 01:44:24 +08:00
|
|
|
WriteBitcodeToFile(Linker.getModule(), Out.os());
|
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
|
|
|
}
|
|
|
|
|
2013-10-01 00:39:19 +08:00
|
|
|
bool LTOCodeGenerator::compile_to_file(const char** name,
|
|
|
|
bool disableOpt,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
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;
|
|
|
|
error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
|
|
|
|
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
|
|
|
|
2013-10-01 00:39:19 +08:00
|
|
|
bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline,
|
|
|
|
disableGVNLoadPRE, 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
|
|
|
|
2013-10-01 00:39:19 +08:00
|
|
|
const void* LTOCodeGenerator::compile(size_t* length,
|
|
|
|
bool disableOpt,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
std::string& errMsg) {
|
2011-03-23 04:57:13 +08:00
|
|
|
const char *name;
|
2013-10-01 00:39:19 +08:00
|
|
|
if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE,
|
|
|
|
errMsg))
|
2011-03-23 04:57:13 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// remove old buffer if compile() called twice
|
2013-09-05 01:44:24 +08:00
|
|
|
delete NativeObjectFile;
|
2011-03-23 04:57:13 +08:00
|
|
|
|
|
|
|
// read .o file into memory buffer
|
|
|
|
OwningPtr<MemoryBuffer> BuffPtr;
|
|
|
|
if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
|
|
|
|
errMsg = ec.message();
|
2013-09-05 01:44:24 +08:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2013-08-13 05:07:31 +08:00
|
|
|
return NULL;
|
2011-03-23 04:57:13 +08:00
|
|
|
}
|
2013-09-05 01:44:24 +08:00
|
|
|
NativeObjectFile = BuffPtr.take();
|
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
|
2013-09-05 01:44:24 +08:00
|
|
|
if (NativeObjectFile == NULL)
|
2013-08-13 05:07:31 +08:00
|
|
|
return NULL;
|
2013-09-05 01:44:24 +08:00
|
|
|
*length = NativeObjectFile->getBufferSize();
|
|
|
|
return NativeObjectFile->getBufferStart();
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2013-05-24 05:21:50 +08:00
|
|
|
bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
|
2013-09-05 01:44:24 +08:00
|
|
|
if (TargetMach != NULL)
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2012-08-07 06:52:45 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
std::string TripleStr = Linker.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);
|
2012-08-09 06:03:50 +08:00
|
|
|
if (march == NULL)
|
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;
|
2012-03-31 19:15:43 +08:00
|
|
|
}
|
2012-08-07 06:52:45 +08:00
|
|
|
|
|
|
|
// construct LTOModule, hand over ownership of module and target
|
|
|
|
SubtargetFeatures Features;
|
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";
|
2012-10-13 01:39:25 +08:00
|
|
|
}
|
2013-10-01 00:39:19 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
|
|
|
|
RelocModel, CodeModel::Default,
|
|
|
|
CodeGenOpt::Aggressive);
|
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,
|
2013-09-05 04:08:46 +08:00
|
|
|
std::vector<const char*> &MustPreserveList,
|
2013-10-04 02:29:09 +08:00
|
|
|
std::vector<const char*> &DSOList,
|
2013-09-05 04:08:46 +08:00
|
|
|
SmallPtrSet<GlobalValue*, 8> &AsmUsed,
|
|
|
|
Mangler &Mangler) {
|
2011-03-02 12:14:42 +08:00
|
|
|
SmallString<64> Buffer;
|
2013-09-05 04:08:46 +08:00
|
|
|
Mangler.getNameWithPrefix(Buffer, &GV, false);
|
2011-03-02 12:14:42 +08:00
|
|
|
|
|
|
|
if (GV.isDeclaration())
|
|
|
|
return;
|
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-10-04 02:29:09 +08:00
|
|
|
if (DSOSymbols.count(Buffer))
|
|
|
|
DSOList.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);
|
2011-03-02 12:14:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
|
|
|
SmallPtrSet<GlobalValue*, 8> &UsedValues) {
|
|
|
|
if (LLVMUsed == 0) return;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-03-13 02:44:54 +08:00
|
|
|
void LTOCodeGenerator::applyScopeRestrictions() {
|
2013-09-05 01:44:24 +08:00
|
|
|
if (ScopeRestrictionsDone)
|
|
|
|
return;
|
|
|
|
Module *mergedModule = Linker.getModule();
|
2010-03-13 02:44:54 +08:00
|
|
|
|
|
|
|
// Start off with a verification pass.
|
|
|
|
PassManager passes;
|
|
|
|
passes.add(createVerifierPass());
|
|
|
|
|
2012-03-31 18:50:14 +08:00
|
|
|
// mark which symbols can not be internalized
|
2013-10-30 01:28:26 +08:00
|
|
|
Mangler Mangler(TargetMach);
|
2013-09-05 04:08:46 +08:00
|
|
|
std::vector<const char*> MustPreserveList;
|
2013-10-04 02:29:09 +08:00
|
|
|
std::vector<const char*> DSOList;
|
2013-09-05 04:08:46 +08:00
|
|
|
SmallPtrSet<GlobalValue*, 8> AsmUsed;
|
2011-03-02 12:14:42 +08:00
|
|
|
|
|
|
|
for (Module::iterator f = mergedModule->begin(),
|
|
|
|
e = mergedModule->end(); f != e; ++f)
|
2013-10-04 02:29:09 +08:00
|
|
|
applyRestriction(*f, MustPreserveList, DSOList, 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-10-04 02:29:09 +08:00
|
|
|
applyRestriction(*v, MustPreserveList, DSOList, 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-10-04 02:29:09 +08:00
|
|
|
applyRestriction(*a, MustPreserveList, DSOList, 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;
|
2013-09-05 04:08:46 +08:00
|
|
|
for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = AsmUsed.begin(),
|
|
|
|
e = AsmUsed.end(); i !=e; ++i) {
|
2013-04-25 01:54:35 +08:00
|
|
|
GlobalValue *GV = *i;
|
|
|
|
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
|
|
|
|
2013-10-04 02:29:09 +08:00
|
|
|
passes.add(createInternalizePass(MustPreserveList, DSOList));
|
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
|
2011-05-22 08:20:07 +08:00
|
|
|
bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
|
2013-10-01 00:39:19 +08:00
|
|
|
bool DisableOpt,
|
|
|
|
bool DisableInline,
|
|
|
|
bool DisableGVNLoadPRE,
|
2011-05-22 08:20:07 +08:00
|
|
|
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
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
Module *mergedModule = Linker.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.
|
|
|
|
PassManager passes;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
// Start off with a verification pass.
|
|
|
|
passes.add(createVerifierPass());
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-10-09 00:39:34 +08:00
|
|
|
// Add an appropriate DataLayout instance for this module...
|
2013-09-05 01:44:24 +08:00
|
|
|
passes.add(new DataLayout(*TargetMach->getDataLayout()));
|
|
|
|
TargetMach->addAnalysisPasses(passes);
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2012-04-16 18:58:38 +08:00
|
|
|
// Enabling internalize here would use its AllButMain variant. It
|
|
|
|
// keeps only main if it exists and does nothing for libraries. Instead
|
|
|
|
// we create the pass ourselves with the symbol list provided by the linker.
|
2013-05-24 05:21:50 +08:00
|
|
|
if (!DisableOpt)
|
2013-02-28 22:11:10 +08:00
|
|
|
PassManagerBuilder().populateLTOPassManager(passes,
|
2012-12-11 05:33:45 +08:00
|
|
|
/*Internalize=*/false,
|
2012-04-03 06:16:50 +08:00
|
|
|
!DisableInline,
|
|
|
|
DisableGVNLoadPRE);
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
// Make sure everything is still good.
|
|
|
|
passes.add(createVerifierPass());
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2013-03-14 05:18:46 +08:00
|
|
|
PassManager codeGenPasses;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout()));
|
|
|
|
TargetMach->addAnalysisPasses(codeGenPasses);
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
formatted_raw_ostream Out(out);
|
2010-09-01 22:20:41 +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());
|
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
if (TargetMach->addPassesToEmitFile(codeGenPasses, Out,
|
|
|
|
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 our queue of passes all at once now, efficiently.
|
|
|
|
passes.run(*mergedModule);
|
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]));
|
|
|
|
}
|