2008-02-26 21:26:43 +01: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 06:28:00 +02:00
|
|
|
//
|
2008-02-26 21:26:43 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-03-29 06:28:00 +02:00
|
|
|
// This file declares the LTOModule class.
|
2008-02-26 21:26:43 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LTO_MODULE_H
|
|
|
|
#define LTO_MODULE_H
|
|
|
|
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm-c/lto.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2014-01-07 22:19:40 +01:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-11-04 10:24:40 +01:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2013-12-09 21:26:40 +01:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2008-02-26 21:26:43 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-02-27 23:25:36 +01:00
|
|
|
#include <string>
|
2012-12-04 11:44:52 +01:00
|
|
|
#include <vector>
|
2008-02-27 23:25:36 +01:00
|
|
|
|
2012-03-29 01:12:18 +02:00
|
|
|
// Forward references to llvm classes.
|
2008-02-27 23:25:36 +01:00
|
|
|
namespace llvm {
|
2012-03-28 22:46:54 +02:00
|
|
|
class Function;
|
2012-03-28 06:17:34 +02:00
|
|
|
class GlobalValue;
|
2012-03-28 22:46:54 +02:00
|
|
|
class MemoryBuffer;
|
2012-08-06 23:34:54 +02:00
|
|
|
class TargetOptions;
|
2012-03-28 06:17:34 +02:00
|
|
|
class Value;
|
2008-02-26 21:26:43 +01:00
|
|
|
|
2012-03-31 13:10:35 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2014-05-03 16:46:47 +02:00
|
|
|
/// C++ class which implements the opaque lto_module_t type.
|
2012-03-31 13:10:35 +02:00
|
|
|
///
|
2009-12-23 18:05:07 +01:00
|
|
|
struct LTOModule {
|
2012-03-28 22:46:54 +02:00
|
|
|
private:
|
2014-05-03 16:59:52 +02:00
|
|
|
typedef StringMap<uint8_t> StringSet;
|
2012-03-29 06:28:00 +02:00
|
|
|
|
|
|
|
struct NameAndAttributes {
|
2012-03-29 10:27:32 +02:00
|
|
|
const char *name;
|
|
|
|
uint32_t attributes;
|
|
|
|
bool isFunction;
|
2014-05-03 16:59:52 +02:00
|
|
|
const GlobalValue *symbol;
|
2012-03-29 01:12:18 +02:00
|
|
|
};
|
|
|
|
|
2014-05-03 16:59:52 +02:00
|
|
|
std::unique_ptr<Module> _module;
|
|
|
|
std::unique_ptr<TargetMachine> _target;
|
|
|
|
MCObjectFileInfo ObjFileInfo;
|
2014-01-21 19:31:27 +01:00
|
|
|
StringSet _linkeropt_strings;
|
|
|
|
std::vector<const char *> _deplibs;
|
|
|
|
std::vector<const char *> _linkeropts;
|
2012-03-29 01:12:18 +02:00
|
|
|
std::vector<NameAndAttributes> _symbols;
|
|
|
|
|
|
|
|
// _defines and _undefines only needed to disambiguate tentative definitions
|
2012-03-29 06:28:00 +02:00
|
|
|
StringSet _defines;
|
2014-05-03 16:59:52 +02:00
|
|
|
StringMap<NameAndAttributes> _undefines;
|
2012-03-29 01:12:18 +02:00
|
|
|
std::vector<const char*> _asm_undefines;
|
2014-05-03 16:59:52 +02:00
|
|
|
MCContext _context;
|
2012-03-29 01:12:18 +02:00
|
|
|
|
|
|
|
// Use mangler to add GlobalPrefix to names to match linker names.
|
2014-05-03 16:59:52 +02:00
|
|
|
Mangler _mangler;
|
|
|
|
|
2014-07-04 00:43:03 +02:00
|
|
|
LTOModule(std::unique_ptr<Module> M, TargetMachine *TM);
|
2012-03-29 01:12:18 +02:00
|
|
|
|
2012-03-28 22:46:54 +02:00
|
|
|
public:
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Returns 'true' if the file or memory contents is LLVM bitcode.
|
2012-03-29 01:12:18 +02:00
|
|
|
static bool isBitcodeFile(const void *mem, size_t length);
|
|
|
|
static bool isBitcodeFile(const char *path);
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Returns 'true' if the file or memory contents is LLVM bitcode for the
|
|
|
|
/// specified triple.
|
2012-03-29 06:28:00 +02:00
|
|
|
static bool isBitcodeFileForTarget(const void *mem,
|
2012-03-29 01:12:18 +02:00
|
|
|
size_t length,
|
|
|
|
const char *triplePrefix);
|
2012-03-29 06:28:00 +02:00
|
|
|
static bool isBitcodeFileForTarget(const char *path,
|
2012-03-29 01:12:18 +02:00
|
|
|
const char *triplePrefix);
|
|
|
|
|
2014-05-03 16:46:47 +02: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-25 01:52:22 +02:00
|
|
|
///
|
|
|
|
/// InitializeAllTargets();
|
|
|
|
/// InitializeAllTargetMCs();
|
|
|
|
/// InitializeAllAsmPrinters();
|
|
|
|
/// InitializeAllAsmParsers();
|
2014-05-03 16:59:52 +02:00
|
|
|
static LTOModule *makeLTOModule(const char *path, TargetOptions options,
|
2012-03-29 01:12:18 +02:00
|
|
|
std::string &errMsg);
|
2014-05-03 16:59:52 +02:00
|
|
|
static LTOModule *makeLTOModule(int fd, const char *path, size_t size,
|
|
|
|
TargetOptions options, std::string &errMsg);
|
|
|
|
static LTOModule *makeLTOModule(int fd, const char *path, size_t map_size,
|
|
|
|
off_t offset, TargetOptions options,
|
2013-09-30 18:39:19 +02:00
|
|
|
std::string &errMsg);
|
2012-03-29 01:12:18 +02:00
|
|
|
static LTOModule *makeLTOModule(const void *mem, size_t length,
|
2014-05-03 16:59:52 +02:00
|
|
|
TargetOptions options, std::string &errMsg,
|
|
|
|
StringRef path = "");
|
2012-03-29 01:12:18 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Return the Module's target triple.
|
2012-03-29 01:12:18 +02:00
|
|
|
const char *getTargetTriple() {
|
|
|
|
return _module->getTargetTriple().c_str();
|
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Set the Module's target triple.
|
2012-03-29 01:12:18 +02:00
|
|
|
void setTargetTriple(const char *triple) {
|
|
|
|
_module->setTargetTriple(triple);
|
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the number of symbols
|
2012-03-29 01:12:18 +02:00
|
|
|
uint32_t getSymbolCount() {
|
|
|
|
return _symbols.size();
|
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the attributes for a symbol at the specified index.
|
2012-03-29 01:12:18 +02:00
|
|
|
lto_symbol_attributes getSymbolAttributes(uint32_t index) {
|
|
|
|
if (index < _symbols.size())
|
2012-03-29 06:28:00 +02:00
|
|
|
return lto_symbol_attributes(_symbols[index].attributes);
|
|
|
|
return lto_symbol_attributes(0);
|
2012-03-29 01:12:18 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the name of the symbol at the specified index.
|
2012-03-29 01:12:18 +02:00
|
|
|
const char *getSymbolName(uint32_t index) {
|
|
|
|
if (index < _symbols.size())
|
|
|
|
return _symbols[index].name;
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2012-03-29 01:12:18 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the number of dependent libraries
|
2014-01-21 19:31:27 +01:00
|
|
|
uint32_t getDependentLibraryCount() {
|
|
|
|
return _deplibs.size();
|
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the dependent library at the specified index.
|
2014-01-21 19:31:27 +01:00
|
|
|
const char *getDependentLibrary(uint32_t index) {
|
|
|
|
if (index < _deplibs.size())
|
|
|
|
return _deplibs[index];
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2014-01-21 19:31:27 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the number of linker options
|
2014-01-21 19:31:27 +01:00
|
|
|
uint32_t getLinkerOptCount() {
|
|
|
|
return _linkeropts.size();
|
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get the linker option at the specified index.
|
2014-01-21 19:31:27 +01:00
|
|
|
const char *getLinkerOpt(uint32_t index) {
|
|
|
|
if (index < _linkeropts.size())
|
|
|
|
return _linkeropts[index];
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2014-01-21 19:31:27 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Return the Module.
|
2014-05-03 16:59:52 +02:00
|
|
|
Module *getLLVVMModule() { return _module.get(); }
|
2012-03-29 01:12:18 +02:00
|
|
|
|
|
|
|
const std::vector<const char*> &getAsmUndefinedRefs() {
|
|
|
|
return _asm_undefines;
|
|
|
|
}
|
2008-02-26 21:26:43 +01:00
|
|
|
|
|
|
|
private:
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Parse metadata from the module
|
2014-01-21 19:31:27 +01:00
|
|
|
// FIXME: it only parses "Linker Options" metadata at the moment
|
|
|
|
void parseMetadata();
|
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Parse the symbols from the module and model-level ASM and add them to
|
|
|
|
/// either the defined or undefined lists.
|
2012-03-29 01:12:18 +02:00
|
|
|
bool parseSymbols(std::string &errMsg);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add a symbol which isn't defined just yet to a list to be resolved later.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addPotentialUndefinedSymbol(const GlobalValue *dcl, bool isFunc);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add a defined symbol to the list.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addDefinedSymbol(const GlobalValue *def, bool isFunction);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add a function symbol as defined to the list.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addDefinedFunctionSymbol(const Function *f);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add a data symbol as defined to the list.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addDefinedDataSymbol(const GlobalValue *v);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add global symbols from module-level ASM to the defined or undefined
|
|
|
|
/// lists.
|
2012-03-29 01:12:18 +02:00
|
|
|
bool addAsmGlobalSymbols(std::string &errMsg);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add a global symbol from module-level ASM to the defined list.
|
2012-03-29 01:12:18 +02:00
|
|
|
void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Add a global symbol from module-level ASM to the undefined list.
|
2012-03-29 01:12:18 +02:00
|
|
|
void addAsmGlobalSymbolUndef(const char *);
|
2009-06-01 22:33:09 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Parse i386/ppc ObjC class data structure.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addObjCClass(const GlobalVariable *clgv);
|
2008-02-26 21:26:43 +01:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Parse i386/ppc ObjC category data structure.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addObjCCategory(const GlobalVariable *clgv);
|
2012-03-28 06:17:34 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Parse i386/ppc ObjC class list data structure.
|
2014-05-03 16:59:52 +02:00
|
|
|
void addObjCClassRef(const GlobalVariable *clgv);
|
2012-03-28 06:17:34 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Get string that the data pointer points to.
|
2014-05-03 16:59:52 +02:00
|
|
|
bool objcClassNameFromExpression(const Constant *c, std::string &name);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Returns 'true' if the memory buffer is for the specified target triple.
|
2014-05-03 16:59:52 +02:00
|
|
|
static bool isTargetMatch(MemoryBuffer *memBuffer, const char *triplePrefix);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-05-03 16:46:47 +02:00
|
|
|
/// Create an LTOModule (private version). N.B. This method takes ownership of
|
|
|
|
/// the buffer.
|
2014-07-04 00:43:03 +02:00
|
|
|
static LTOModule *makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
|
|
|
|
TargetOptions options, std::string &errMsg);
|
2012-03-28 22:46:54 +02:00
|
|
|
|
2014-02-11 00:26:14 +01:00
|
|
|
/// Create a MemoryBuffer from a memory range with an optional name.
|
2014-05-03 16:59:52 +02:00
|
|
|
static MemoryBuffer *makeBuffer(const void *mem, size_t length,
|
|
|
|
StringRef name = "");
|
2008-02-26 21:26:43 +01:00
|
|
|
};
|
2014-05-03 16:59:52 +02:00
|
|
|
}
|
2008-02-26 21:26:43 +01:00
|
|
|
#endif // LTO_MODULE_H
|