LTOCodeGenerator.cpp [plain text]
#include "LTOModule.h"
#include "LTOCodeGenerator.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Linker.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/Target/Mangler.h"
#include "llvm/Target/SubtargetFeature.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/StandardPasses.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/System/Host.h"
#include "llvm/System/Program.h"
#include "llvm/System/Signals.h"
#include "llvm/Config/config.h"
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
using namespace llvm;
static cl::opt<bool> DisableInline("disable-inlining",
cl::desc("Do not run the inliner pass"));
const char* LTOCodeGenerator::getVersionString()
{
#ifdef LLVM_VERSION_INFO
return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
#else
return PACKAGE_NAME " version " PACKAGE_VERSION;
#endif
}
LTOCodeGenerator::LTOCodeGenerator()
: _context(getGlobalContext()),
_linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
_emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
_codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
_nativeObjectFile(NULL), _assemblerPath(NULL)
{
InitializeAllTargets();
InitializeAllAsmPrinters();
}
LTOCodeGenerator::~LTOCodeGenerator()
{
delete _target;
delete _nativeObjectFile;
}
bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
{
return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
}
bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
{
switch (debug) {
case LTO_DEBUG_MODEL_NONE:
_emitDwarfDebugInfo = false;
return false;
case LTO_DEBUG_MODEL_DWARF:
_emitDwarfDebugInfo = true;
return false;
}
errMsg = "unknown debug format";
return true;
}
bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
std::string& errMsg)
{
switch (model) {
case LTO_CODEGEN_PIC_MODEL_STATIC:
case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
_codeModel = model;
return false;
}
errMsg = "unknown pic model";
return true;
}
void LTOCodeGenerator::setAssemblerPath(const char* path)
{
if ( _assemblerPath )
delete _assemblerPath;
_assemblerPath = new sys::Path(path);
}
void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
{
_mustPreserveSymbols[sym] = 1;
}
bool LTOCodeGenerator::writeMergedModules(const char *path,
std::string &errMsg) {
if (determineTarget(errMsg))
return true;
applyScopeRestrictions();
std::string ErrInfo;
raw_fd_ostream Out(path, ErrInfo,
raw_fd_ostream::F_Binary);
if (!ErrInfo.empty()) {
errMsg = "could not open bitcode file for writing: ";
errMsg += path;
return true;
}
WriteBitcodeToFile(_linker.getModule(), Out);
if (Out.has_error()) {
errMsg = "could not write bitcode file: ";
errMsg += path;
return true;
}
return false;
}
const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
{
sys::Path uniqueAsmPath("lto-llvm.s");
if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
return NULL;
sys::RemoveFileOnSignal(uniqueAsmPath);
bool genResult = false;
{
raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg);
formatted_raw_ostream asmFile(asmFD);
if (!errMsg.empty())
return NULL;
genResult = this->generateAssemblyCode(asmFile, errMsg);
}
if ( genResult ) {
if ( uniqueAsmPath.exists() )
uniqueAsmPath.eraseFromDisk();
return NULL;
}
sys::PathWithStatus uniqueObjPath("lto-llvm.o");
if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
if ( uniqueAsmPath.exists() )
uniqueAsmPath.eraseFromDisk();
return NULL;
}
sys::RemoveFileOnSignal(uniqueObjPath);
const std::string& uniqueObjStr = uniqueObjPath.str();
bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg);
if ( !asmResult ) {
delete _nativeObjectFile;
_nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
}
uniqueAsmPath.eraseFromDisk();
uniqueObjPath.eraseFromDisk();
if ( _nativeObjectFile == NULL )
return NULL;
*length = _nativeObjectFile->getBufferSize();
return _nativeObjectFile->getBufferStart();
}
bool LTOCodeGenerator::assemble(const std::string& asmPath,
const std::string& objPath, std::string& errMsg)
{
sys::Path tool;
bool needsCompilerOptions = true;
if ( _assemblerPath ) {
tool = *_assemblerPath;
needsCompilerOptions = false;
} else {
tool = sys::Program::FindProgramByName("gcc");
if ( tool.isEmpty() ) {
errMsg = "can't locate gcc";
return true;
}
}
std::vector<const char*> args;
llvm::Triple targetTriple(_linker.getModule()->getTargetTriple());
const char *arch = targetTriple.getArchNameForAssembler();
args.push_back(tool.c_str());
if (targetTriple.getOS() == Triple::Darwin) {
if (arch != NULL) {
args.push_back("-arch");
args.push_back(arch);
}
if ( (_assemblerPath != NULL) &&
(_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
args.push_back("-static");
}
if ( needsCompilerOptions ) {
args.push_back("-c");
args.push_back("-x");
args.push_back("assembler");
}
args.push_back("-o");
args.push_back(objPath.c_str());
args.push_back(asmPath.c_str());
args.push_back(0);
if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
errMsg = "error in assembly";
return true;
}
return false; }
bool LTOCodeGenerator::determineTarget(std::string& errMsg)
{
if ( _target == NULL ) {
std::string Triple = _linker.getModule()->getTargetTriple();
if (Triple.empty())
Triple = sys::getHostTriple();
const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
if ( march == NULL )
return true;
switch( _codeModel ) {
case LTO_CODEGEN_PIC_MODEL_STATIC:
TargetMachine::setRelocationModel(Reloc::Static);
break;
case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
TargetMachine::setRelocationModel(Reloc::PIC_);
break;
case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
break;
}
SubtargetFeatures Features;
Features.getDefaultSubtargetFeatures("" , llvm::Triple(Triple));
std::string FeatureStr = Features.getString();
_target = march->createTargetMachine(Triple, FeatureStr);
}
return false;
}
void LTOCodeGenerator::applyScopeRestrictions() {
if (_scopeRestrictionsDone) return;
Module *mergedModule = _linker.getModule();
PassManager passes;
passes.add(createVerifierPass());
if (!_mustPreserveSymbols.empty()) {
MCContext Context(*_target->getMCAsmInfo());
Mangler mangler(Context, *_target->getTargetData());
std::vector<const char*> mustPreserveList;
for (Module::iterator f = mergedModule->begin(),
e = mergedModule->end(); f != e; ++f) {
if (!f->isDeclaration() &&
_mustPreserveSymbols.count(mangler.getNameWithPrefix(f)))
mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
}
for (Module::global_iterator v = mergedModule->global_begin(),
e = mergedModule->global_end(); v != e; ++v) {
if (!v->isDeclaration() &&
_mustPreserveSymbols.count(mangler.getNameWithPrefix(v)))
mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
}
passes.add(createInternalizePass(mustPreserveList));
}
passes.run(*mergedModule);
_scopeRestrictionsDone = true;
}
bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out,
std::string& errMsg)
{
if ( this->determineTarget(errMsg) )
return true;
this->applyScopeRestrictions();
Module* mergedModule = _linker.getModule();
switch (_target->getMCAsmInfo()->getExceptionHandlingType()) {
case ExceptionHandling::Dwarf:
llvm::DwarfExceptionHandling = true;
break;
case ExceptionHandling::SjLj:
llvm::SjLjExceptionHandling = true;
break;
case ExceptionHandling::None:
break;
default:
assert (0 && "Unknown exception handling model!");
}
if ( !_codegenOptions.empty() )
cl::ParseCommandLineOptions(_codegenOptions.size(),
(char**)&_codegenOptions[0]);
PassManager passes;
passes.add(createVerifierPass());
passes.add(new TargetData(*_target->getTargetData()));
createStandardLTOPasses(&passes, false, !DisableInline,
false);
passes.add(createVerifierPass());
FunctionPassManager* codeGenPasses = new FunctionPassManager(mergedModule);
codeGenPasses->add(new TargetData(*_target->getTargetData()));
if (_target->addPassesToEmitFile(*codeGenPasses, out,
TargetMachine::CGFT_AssemblyFile,
CodeGenOpt::Aggressive)) {
errMsg = "target file type not supported";
return true;
}
passes.run(*mergedModule);
codeGenPasses->doInitialization();
for (Module::iterator
it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
if (!it->isDeclaration())
codeGenPasses->run(*it);
codeGenPasses->doFinalization();
return false; }
void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
{
for (std::pair<StringRef, StringRef> o = getToken(options);
!o.first.empty(); o = getToken(o.second)) {
if ( _codegenOptions.empty() )
_codegenOptions.push_back("libLTO");
_codegenOptions.push_back(strdup(o.first.str().c_str()));
}
}