2008-02-26 20:26:43 +00:00
|
|
|
//===-LTOModule.h - 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-29 04:28:00 +00:00
|
|
|
//
|
2008-02-26 20:26:43 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-03-29 04:28:00 +00:00
|
|
|
// This file declares the LTOModule class.
|
2008-02-26 20:26:43 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_LTO_LTOMODULE_H
|
|
|
|
#define LLVM_LTO_LTOMODULE_H
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm-c/lto.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2014-11-19 05:49:42 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-11-04 09:24:40 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2013-12-09 20:26:40 +00:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2014-07-04 18:40:36 +00:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2008-02-26 20:26:43 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-02-27 22:25:36 +00:00
|
|
|
#include <string>
|
2012-12-04 10:44:52 +00:00
|
|
|
#include <vector>
|
2008-02-27 22:25:36 +00:00
|
|
|
|
2012-03-28 23:12:18 +00:00
|
|
|
// Forward references to llvm classes.
|
2008-02-27 22:25:36 +00:00
|
|
|
namespace llvm {
|
2012-03-28 20:46:54 +00:00
|
|
|
class Function;
|
2012-03-28 04:17:34 +00:00
|
|
|
class GlobalValue;
|
2012-03-28 20:46:54 +00:00
|
|
|
class MemoryBuffer;
|
2012-08-06 21:34:54 +00:00
|
|
|
class TargetOptions;
|
2012-03-28 04:17:34 +00:00
|
|
|
class Value;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:10:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2014-05-03 14:46:47 +00:00
|
|
|
/// C++ class which implements the opaque lto_module_t type.
|
2012-03-31 11:10:35 +00:00
|
|
|
///
|
2009-12-23 17:05:07 +00:00
|
|
|
struct LTOModule {
|
2012-03-28 20:46:54 +00:00
|
|
|
private:
|
2012-03-29 04:28:00 +00:00
|
|
|
struct NameAndAttributes {
|
2012-03-29 08:27:32 +00:00
|
|
|
const char *name;
|
|
|
|
uint32_t attributes;
|
|
|
|
bool isFunction;
|
2014-05-03 14:59:52 +00:00
|
|
|
const GlobalValue *symbol;
|
2012-03-28 23:12:18 +00:00
|
|
|
};
|
|
|
|
|
2014-11-11 23:08:05 +00:00
|
|
|
std::unique_ptr<LLVMContext> OwnedContext;
|
|
|
|
|
2014-07-04 18:40:36 +00:00
|
|
|
std::unique_ptr<object::IRObjectFile> IRFile;
|
2014-05-03 14:59:52 +00:00
|
|
|
std::unique_ptr<TargetMachine> _target;
|
2014-11-19 05:49:42 +00:00
|
|
|
StringSet<> _linkeropt_strings;
|
2014-01-21 18:31:27 +00:00
|
|
|
std::vector<const char *> _deplibs;
|
|
|
|
std::vector<const char *> _linkeropts;
|
2012-03-28 23:12:18 +00:00
|
|
|
std::vector<NameAndAttributes> _symbols;
|
|
|
|
|
|
|
|
// _defines and _undefines only needed to disambiguate tentative definitions
|
2014-11-19 05:49:42 +00:00
|
|
|
StringSet<> _defines;
|
2014-05-03 14:59:52 +00:00
|
|
|
StringMap<NameAndAttributes> _undefines;
|
2012-03-28 23:12:18 +00:00
|
|
|
std::vector<const char*> _asm_undefines;
|
|
|
|
|
2014-07-04 18:40:36 +00:00
|
|
|
LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
|
2014-11-11 23:08:05 +00:00
|
|
|
LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
|
|
|
|
std::unique_ptr<LLVMContext> Context);
|
2012-03-28 23:12:18 +00:00
|
|
|
|
2012-03-28 20:46:54 +00:00
|
|
|
public:
|
2014-11-11 23:08:05 +00:00
|
|
|
~LTOModule();
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Returns 'true' if the file or memory contents is LLVM bitcode.
|
2012-03-28 23:12:18 +00:00
|
|
|
static bool isBitcodeFile(const void *mem, size_t length);
|
|
|
|
static bool isBitcodeFile(const char *path);
|
|
|
|
|
2014-07-04 00:58:41 +00:00
|
|
|
/// Returns 'true' if the memory buffer is LLVM bitcode for the specified
|
|
|
|
/// triple.
|
|
|
|
static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
|
|
|
|
StringRef triplePrefix);
|
|
|
|
|
|
|
|
/// Create a MemoryBuffer from a memory range with an optional name.
|
2014-08-17 22:37:39 +00:00
|
|
|
static std::unique_ptr<MemoryBuffer>
|
|
|
|
makeBuffer(const void *mem, size_t length, StringRef name = "");
|
2012-03-28 23:12:18 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Create an LTOModule. N.B. These methods take ownership of the buffer. The
|
|
|
|
/// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
|
|
|
|
/// and the AsmParsers by calling:
|
2013-09-24 23:52:22 +00:00
|
|
|
///
|
|
|
|
/// InitializeAllTargets();
|
|
|
|
/// InitializeAllTargetMCs();
|
|
|
|
/// InitializeAllAsmPrinters();
|
|
|
|
/// InitializeAllAsmParsers();
|
2014-07-03 23:28:00 +00:00
|
|
|
static LTOModule *createFromFile(const char *path, TargetOptions options,
|
|
|
|
std::string &errMsg);
|
|
|
|
static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
|
|
|
|
TargetOptions options,
|
|
|
|
std::string &errMsg);
|
|
|
|
static LTOModule *createFromOpenFileSlice(int fd, const char *path,
|
|
|
|
size_t map_size, off_t offset,
|
|
|
|
TargetOptions options,
|
|
|
|
std::string &errMsg);
|
|
|
|
static LTOModule *createFromBuffer(const void *mem, size_t length,
|
|
|
|
TargetOptions options, std::string &errMsg,
|
|
|
|
StringRef path = "");
|
2012-03-28 23:12:18 +00:00
|
|
|
|
2014-11-11 23:08:05 +00:00
|
|
|
static LTOModule *createInLocalContext(const void *mem, size_t length,
|
|
|
|
TargetOptions options,
|
|
|
|
std::string &errMsg, StringRef path);
|
|
|
|
static LTOModule *createInContext(const void *mem, size_t length,
|
|
|
|
TargetOptions options, std::string &errMsg,
|
|
|
|
StringRef path, LLVMContext *Context);
|
|
|
|
|
2014-07-04 18:40:36 +00:00
|
|
|
const Module &getModule() const {
|
|
|
|
return const_cast<LTOModule*>(this)->getModule();
|
|
|
|
}
|
|
|
|
Module &getModule() {
|
|
|
|
return IRFile->getModule();
|
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Return the Module's target triple.
|
2014-07-04 14:19:41 +00:00
|
|
|
const std::string &getTargetTriple() {
|
2014-07-04 18:40:36 +00:00
|
|
|
return getModule().getTargetTriple();
|
2012-03-28 23:12:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Set the Module's target triple.
|
2014-07-04 14:19:41 +00:00
|
|
|
void setTargetTriple(StringRef Triple) {
|
2014-07-04 18:40:36 +00:00
|
|
|
getModule().setTargetTriple(Triple);
|
2012-03-28 23:12:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the number of symbols
|
2012-03-28 23:12:18 +00:00
|
|
|
uint32_t getSymbolCount() {
|
|
|
|
return _symbols.size();
|
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the attributes for a symbol at the specified index.
|
2012-03-28 23:12:18 +00:00
|
|
|
lto_symbol_attributes getSymbolAttributes(uint32_t index) {
|
|
|
|
if (index < _symbols.size())
|
2012-03-29 04:28:00 +00:00
|
|
|
return lto_symbol_attributes(_symbols[index].attributes);
|
|
|
|
return lto_symbol_attributes(0);
|
2012-03-28 23:12:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the name of the symbol at the specified index.
|
2012-03-28 23:12:18 +00:00
|
|
|
const char *getSymbolName(uint32_t index) {
|
|
|
|
if (index < _symbols.size())
|
|
|
|
return _symbols[index].name;
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2012-03-28 23:12:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the number of dependent libraries
|
2014-01-21 18:31:27 +00:00
|
|
|
uint32_t getDependentLibraryCount() {
|
|
|
|
return _deplibs.size();
|
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the dependent library at the specified index.
|
2014-01-21 18:31:27 +00:00
|
|
|
const char *getDependentLibrary(uint32_t index) {
|
|
|
|
if (index < _deplibs.size())
|
|
|
|
return _deplibs[index];
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2014-01-21 18:31:27 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the number of linker options
|
2014-01-21 18:31:27 +00:00
|
|
|
uint32_t getLinkerOptCount() {
|
|
|
|
return _linkeropts.size();
|
|
|
|
}
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get the linker option at the specified index.
|
2014-01-21 18:31:27 +00:00
|
|
|
const char *getLinkerOpt(uint32_t index) {
|
|
|
|
if (index < _linkeropts.size())
|
|
|
|
return _linkeropts[index];
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2014-01-21 18:31:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 23:12:18 +00:00
|
|
|
const std::vector<const char*> &getAsmUndefinedRefs() {
|
|
|
|
return _asm_undefines;
|
|
|
|
}
|
2008-02-26 20:26:43 +00:00
|
|
|
|
|
|
|
private:
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Parse metadata from the module
|
2014-01-21 18:31:27 +00:00
|
|
|
// FIXME: it only parses "Linker Options" metadata at the moment
|
|
|
|
void parseMetadata();
|
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Parse the symbols from the module and model-level ASM and add them to
|
|
|
|
/// either the defined or undefined lists.
|
2012-03-28 23:12:18 +00:00
|
|
|
bool parseSymbols(std::string &errMsg);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Add a symbol which isn't defined just yet to a list to be resolved later.
|
2014-07-04 18:40:36 +00:00
|
|
|
void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
|
|
|
|
bool isFunc);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Add a defined symbol to the list.
|
2014-07-04 18:40:36 +00:00
|
|
|
void addDefinedSymbol(const char *Name, const GlobalValue *def,
|
|
|
|
bool isFunction);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Add a data symbol as defined to the list.
|
2014-07-04 18:40:36 +00:00
|
|
|
void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
|
|
|
|
void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-07-04 18:40:36 +00:00
|
|
|
/// Add a function symbol as defined to the list.
|
|
|
|
void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
|
|
|
|
void addDefinedFunctionSymbol(const char *Name, const Function *F);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Add a global symbol from module-level ASM to the defined list.
|
2012-03-28 23:12:18 +00:00
|
|
|
void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Add a global symbol from module-level ASM to the undefined list.
|
2012-03-28 23:12:18 +00:00
|
|
|
void addAsmGlobalSymbolUndef(const char *);
|
2009-06-01 20:33:09 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Parse i386/ppc ObjC class data structure.
|
2014-05-03 14:59:52 +00:00
|
|
|
void addObjCClass(const GlobalVariable *clgv);
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Parse i386/ppc ObjC category data structure.
|
2014-05-03 14:59:52 +00:00
|
|
|
void addObjCCategory(const GlobalVariable *clgv);
|
2012-03-28 04:17:34 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Parse i386/ppc ObjC class list data structure.
|
2014-05-03 14:59:52 +00:00
|
|
|
void addObjCClassRef(const GlobalVariable *clgv);
|
2012-03-28 04:17:34 +00:00
|
|
|
|
2014-05-03 14:46:47 +00:00
|
|
|
/// Get string that the data pointer points to.
|
2014-05-03 14:59:52 +00:00
|
|
|
bool objcClassNameFromExpression(const Constant *c, std::string &name);
|
2012-03-28 20:46:54 +00:00
|
|
|
|
2014-08-19 18:44:46 +00:00
|
|
|
/// Create an LTOModule (private version).
|
|
|
|
static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
|
2014-11-11 23:08:05 +00:00
|
|
|
std::string &errMsg, LLVMContext *Context);
|
2008-02-26 20:26:43 +00:00
|
|
|
};
|
2014-05-03 14:59:52 +00:00
|
|
|
}
|
2014-08-13 16:26:38 +00:00
|
|
|
#endif
|