22 #include "llvm/Config/config.h"
55 #ifdef LLVM_VERSION_INFO
56 return PACKAGE_NAME
" version " PACKAGE_VERSION
", " LLVM_VERSION_INFO;
58 return PACKAGE_NAME
" version " PACKAGE_VERSION;
64 TargetMach(NULL), EmitDwarfDebugInfo(
false), ScopeRestrictionsDone(
false),
66 initializeLTOPasses();
71 delete NativeObjectFile;
73 NativeObjectFile = NULL;
77 for (std::vector<char *>::iterator
I = CodegenOptions.begin(),
78 E = CodegenOptions.end();
87 void LTOCodeGenerator::initializeLTOPasses() {
117 for (
int i = 0, e = undefs.size(); i != e; ++i)
118 AsmUndefinedRefs[undefs[i]] = 1;
147 EmitDwarfDebugInfo =
false;
151 EmitDwarfDebugInfo =
true;
169 std::string &errMsg) {
170 if (!determineTarget(errMsg))
174 applyScopeRestrictions();
179 if (!ErrInfo.empty()) {
180 errMsg =
"could not open bitcode file for writing: ";
190 errMsg =
"could not write bitcode file: ";
203 bool disableGVNLoadPRE,
204 std::string& errMsg) {
217 bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline,
218 disableGVNLoadPRE, errMsg);
219 objFile.os().close();
220 if (objFile.os().has_error()) {
221 objFile.os().clear_error();
232 NativeObjectPath = Filename.
c_str();
233 *name = NativeObjectPath.c_str();
240 bool disableGVNLoadPRE,
241 std::string& errMsg) {
243 if (!
compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE,
248 delete NativeObjectFile;
252 if (
error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1,
false)) {
253 errMsg = ec.message();
257 NativeObjectFile = BuffPtr.
take();
263 if (NativeObjectFile == NULL)
269 bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
270 if (TargetMach != NULL)
274 if (TripleStr.empty())
279 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
301 std::string FeatureStr = Features.
getString();
316 void LTOCodeGenerator::
319 std::vector<const char*> &MustPreserveList,
327 if (MustPreserveSymbols.
count(Buffer))
329 if (AsmUndefinedRefs.
count(Buffer))
337 if (isa<Function>(GV) &&
344 if (LLVMUsed == 0)
return;
363 Libcalls.push_back(TLI.
getName(F));
373 Libcalls.push_back(
Name);
376 Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
380 void LTOCodeGenerator::applyScopeRestrictions() {
381 if (ScopeRestrictionsDone)
390 Mangler Mangler(TargetMach);
391 std::vector<const char*> MustPreserveList;
393 std::vector<StringRef> Libcalls;
398 e = mergedModule->
end(); f != e; ++f)
399 applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler);
402 applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler);
404 e = mergedModule->
alias_end(); a != e; ++a)
405 applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler);
410 if (LLVMCompilerUsed)
413 if (!AsmUsed.
empty()) {
415 std::vector<Constant*> asmUsed2;
417 e = AsmUsed.
end(); i !=e; ++i) {
419 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
420 asmUsed2.push_back(c);
428 "llvm.compiler.used");
430 LLVMCompilerUsed->setSection(
"llvm.metadata");
436 passes.run(*mergedModule);
438 ScopeRestrictionsDone =
true;
442 bool LTOCodeGenerator::generateObjectFile(
raw_ostream &out,
445 bool DisableGVNLoadPRE,
446 std::string &errMsg) {
447 if (!this->determineTarget(errMsg))
453 this->applyScopeRestrictions();
477 PassManager codeGenPasses;
489 TargetMachine::CGFT_ObjectFile)) {
490 errMsg =
"target file type not supported";
495 passes.run(*mergedModule);
498 codeGenPasses.run(*mergedModule);
506 for (std::pair<StringRef, StringRef> o =
getToken(options);
507 !o.first.empty(); o =
getToken(o.second)) {
510 if (CodegenOptions.empty())
511 CodegenOptions.push_back(
strdup(
"libLLVMLTO"));
512 CodegenOptions.push_back(
strdup(o.first.str().c_str()));
518 if (!CodegenOptions.empty())
520 const_cast<char **
>(&CodegenOptions[0]));
void initializeFunctionAttrsPass(PassRegistry &)
static void accumulateAndSortLibcalls(std::vector< StringRef > &Libcalls, const TargetLibraryInfo &TLI, const TargetLowering *Lowering)
virtual const TargetLowering * getTargetLowering() const
void initializeInstCombinerPass(PassRegistry &)
void setTargetOptions(llvm::TargetOptions options)
Special purpose, only applies to global arrays.
int remove(const char *path);
const char * getBufferStart() const
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
static void findUsedValues(GlobalVariable *LLVMUsed, SmallPtrSet< GlobalValue *, 8 > &UsedValues)
The main container class for the LLVM Intermediate Representation.
void initializeSimpleInlinerPass(PassRegistry &)
void initializeJumpThreadingPass(PassRegistry &)
unsigned getNumOperands() const
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, bool isImplicitlyPrivate, bool UseGlobalPrefix=true)
std::string getDefaultTargetTriple()
void initializeInternalizePassPass(PassRegistry &)
bool writeMergedModules(const char *path, std::string &errMsg)
const Constant * getInitializer() const
llvm::Module * getLLVVMModule()
getLLVVMModule - Return the Module.
const std::string & getTargetTriple() const
const GlobalVariable * getGlobalVariable(StringRef Name, bool AllowInternal=false) const
void initializeSROA_DTPass(PassRegistry &)
StringRef getName() const
void initializePruneEHPass(PassRegistry &)
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
bool has(LibFunc::Func F) const
static cl::opt< bool > Aggressive("aggressive-ext-opt", cl::Hidden, cl::desc("Aggressive extension optimization"))
#define llvm_unreachable(msg)
void initializeArgPromotionPass(PassRegistry &)
void initializeGlobalOptPass(PassRegistry &)
size_type count(StringRef Key) const
global_iterator global_begin()
void parseCodeGenDebugOptions()
void setCodeGenDebugOptions(const char *opts)
const char * data() const
void populateLTOPassManager(PassManagerBase &PM, bool Internalize, bool RunInliner, bool DisableGVNLoadPRE=false)
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
void initializeGlobalsModRefPass(PassRegistry &)
void setDebugInfo(lto_debug_model)
unsigned NoFramePointerElim
void array_pod_sort(IteratorTy Start, IteratorTy End)
FPOpFusion::FPOpFusionMode AllowFPOpFusion
cl::opt< Reloc::Model > RelocModel("relocation-model", cl::desc("Choose relocation model"), cl::init(Reloc::Default), cl::values(clEnumValN(Reloc::Default,"default","Target default relocation model"), clEnumValN(Reloc::Static,"static","Non-relocatable code"), clEnumValN(Reloc::PIC_,"pic","Fully relocatable, position independent code"), clEnumValN(Reloc::DynamicNoPIC,"dynamic-no-pic","Relocatable external references, non-relocatable code"), clEnumValEnd))
alias_iterator alias_end()
void ParseCommandLineOptions(int argc, const char *const *argv, const char *Overview=0)
virtual bool addPassesToEmitFile(PassManagerBase &, formatted_raw_ostream &, CodeGenFileType, bool=true, AnalysisID=0, AnalysisID=0)
LLVM Constant Representation.
Pass * createObjCARCContractPass()
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
virtual void eraseFromParent()
bool compile_to_file(const char **name, bool disableOpt, bool disableInline, bool disableGVNLoadPRE, std::string &errMsg)
const std::vector< const char * > & getAsmUndefinedRefs()
getAsmUndefinedRefs -
unsigned GuaranteedTailCallOpt
Value * getOperand(unsigned i) const
const void * compile(size_t *length, bool disableOpt, bool disableInline, bool disableGVNLoadPRE, std::string &errMsg)
void initializeIPSCCPPass(PassRegistry &)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
void initializeLICMPass(PassRegistry &)
std::string getString() const
Features string accessors.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
char *strdup(const char *s1);
virtual void addAnalysisPasses(PassManagerBase &)
Register analysis passes for this target with a pass manager.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
global_iterator global_end()
TargetMachine * createTargetMachine(StringRef Triple, StringRef CPU, StringRef Features, const TargetOptions &Options, Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
unsigned DisableTailCalls
bool linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg)
Link Src into the composite. The source is destroyed if Mode is DestroySource and preserved if it is ...
void initializeSROA_SSAUpPass(PassRegistry &)
alias_iterator alias_begin()
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
unsigned StackAlignmentOverride
StackAlignmentOverride - Override default stack alignment for target.
void setCodePICModel(lto_codegen_model)
std::string message() const
void initializeDCEPass(PassRegistry &)
void initializeDAHPass(PassRegistry &)
void initializeSROAPass(PassRegistry &)
const StringRef getTargetTriple() const
size_t getBufferSize() const
void initializeGlobalDCEPass(PassRegistry &)
StringRef getName(LibFunc::Func F) const
bool isDeclaration() const
virtual const DataLayout * getDataLayout() const
unsigned PositionIndependentExecutable
static ArrayType * get(Type *ElementType, uint64_t NumElements)
ModulePass * createInternalizePass(ArrayRef< const char * > ExportList)
void initializeConstantMergePass(PassRegistry &)
unsigned EnableSegmentedStacks
void initializeCFGSimplifyPassPass(PassRegistry &)
void initializeMemCpyOptPass(PassRegistry &)
Module * getModule() const
unsigned HonorSignDependentRoundingFPMathOption
unsigned LessPreciseFPMADOption
static const char * getVersionString()
LLVMContext & getGlobalContext()
void WriteBitcodeToFile(const Module *M, raw_ostream &Out)
FunctionPass * createVerifierPass(VerifierFailureAction action=AbortProcessAction)
Create a verifier pass.
FloatABI::ABIType FloatABIType
bool addModule(struct LTOModule *, std::string &errMsg)
void initializeGVNPass(PassRegistry &)