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"
|
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"
|
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"
|
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"
|
2014-03-04 18:07:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-10 02:06:07 +08:00
|
|
|
#include "llvm/Support/system_error.h"
|
2013-11-13 05:44:01 +08:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
|
|
|
#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"
|
|
|
|
#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)),
|
2014-04-15 14:32:26 +08:00
|
|
|
TargetMach(nullptr), EmitDwarfDebugInfo(false),
|
|
|
|
ScopeRestrictionsDone(false), CodeModel(LTO_CODEGEN_PIC_MODEL_DEFAULT),
|
|
|
|
NativeObjectFile(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {
|
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;
|
2014-04-15 14:32:26 +08:00
|
|
|
TargetMach = nullptr;
|
|
|
|
NativeObjectFile = nullptr;
|
2013-10-16 16:59:57 +08:00
|
|
|
|
|
|
|
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
|
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);
|
|
|
|
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.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:
|
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
|
|
|
|
std::string ErrInfo;
|
2014-02-25 02:20:12 +08:00
|
|
|
tool_output_file Out(path, ErrInfo, sys::fs::F_None);
|
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
|
|
|
}
|
|
|
|
|
2014-01-11 04:24:35 +08:00
|
|
|
bool LTOCodeGenerator::compile_to_file(const char** name,
|
2013-10-01 00:39:19 +08:00
|
|
|
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))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-03-23 04:57:13 +08:00
|
|
|
|
|
|
|
// 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
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> BuffPtr;
|
2011-03-23 04:57:13 +08:00
|
|
|
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);
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-03-23 04:57:13 +08:00
|
|
|
}
|
2014-03-05 18:19:29 +08:00
|
|
|
NativeObjectFile = BuffPtr.release();
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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);
|
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-03-29 18:18:08 +08:00
|
|
|
else if (Triple.getArch() == llvm::Triple::arm64)
|
|
|
|
MCpu = "cyclone";
|
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-11-13 05:44:01 +08:00
|
|
|
const ArrayRef<StringRef> &Libcalls,
|
2013-09-05 04:08:46 +08:00
|
|
|
std::vector<const char*> &MustPreserveList,
|
|
|
|
SmallPtrSet<GlobalValue*, 8> &AsmUsed,
|
|
|
|
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,
|
|
|
|
SmallPtrSet<GlobalValue*, 8> &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);
|
|
|
|
}
|
|
|
|
|
2013-11-13 05:44:01 +08:00
|
|
|
static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
|
|
|
|
const TargetLibraryInfo& TLI,
|
|
|
|
const TargetLowering *Lowering)
|
|
|
|
{
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetLowering has info on library calls that CodeGen expects to be
|
|
|
|
// available, both from the C runtime and compiler-rt.
|
|
|
|
if (Lowering)
|
|
|
|
for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
|
|
|
|
I != E; ++I)
|
|
|
|
if (const char *Name
|
|
|
|
= Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
|
|
|
|
Libcalls.push_back(Name);
|
|
|
|
|
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() {
|
2014-04-03 06:05:57 +08:00
|
|
|
if (ScopeRestrictionsDone)
|
2013-09-05 01:44:24 +08:00
|
|
|
return;
|
|
|
|
Module *mergedModule = Linker.getModule();
|
2010-03-13 02:44:54 +08:00
|
|
|
|
|
|
|
// Start off with a verification pass.
|
|
|
|
PassManager passes;
|
|
|
|
passes.add(createVerifierPass());
|
2014-04-16 00:27:38 +08:00
|
|
|
passes.add(createDebugInfoVerifierPass());
|
2010-03-13 02:44:54 +08:00
|
|
|
|
2012-03-31 18:50:14 +08:00
|
|
|
// mark which symbols can not be internalized
|
2014-01-04 03:21:54 +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;
|
|
|
|
TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple()));
|
|
|
|
accumulateAndSortLibcalls(Libcalls, TLI, TargetMach->getTargetLowering());
|
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;
|
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
|
|
|
|
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
|
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());
|
2014-04-16 00:27:38 +08:00
|
|
|
passes.add(createDebugInfoVerifierPass());
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-10-09 00:39:34 +08:00
|
|
|
// Add an appropriate DataLayout instance for this module...
|
2014-02-26 07:25:17 +08:00
|
|
|
mergedModule->setDataLayout(TargetMach->getDataLayout());
|
|
|
|
passes.add(new DataLayoutPass(mergedModule));
|
2013-12-12 09:37:39 +08:00
|
|
|
|
2014-01-11 04:24:35 +08:00
|
|
|
// Add appropriate TargetLibraryInfo for this module.
|
2013-12-12 09:37:39 +08:00
|
|
|
passes.add(new TargetLibraryInfo(Triple(TargetMach->getTargetTriple())));
|
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
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());
|
2014-04-16 00:27:38 +08:00
|
|
|
passes.add(createDebugInfoVerifierPass());
|
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
|
|
|
|
2014-02-26 07:25:17 +08:00
|
|
|
codeGenPasses.add(new DataLayoutPass(mergedModule));
|
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]));
|
|
|
|
}
|
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.
|
|
|
|
std::string MsgStorage;
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
DI.print(DP);
|
|
|
|
Stream.flush();
|
|
|
|
|
|
|
|
// If this method has been called it means someone has set up an external
|
|
|
|
// diagnostic handler. Assert on that.
|
|
|
|
assert(DiagHandler && "Invalid diagnostic handler");
|
|
|
|
(*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this);
|
|
|
|
}
|