48 #include <system_error>
50 using namespace llvm::object;
54 : Mod(std::move(M)), MBRef(MBRef), _target(TM) {
55 SymTab.addModule(Mod.get());
75 BufferOrErr.
get()->getMemBufferRef());
85 return Result->IsThinLTO;
112 return *ProducerOrErr;
120 if (std::error_code EC = BufferOrErr.
getError()) {
124 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
125 return makeLTOModule(Buffer->getMemBufferRef(), options,
Context,
137 size_t map_size, off_t offset,
141 if (std::error_code EC = BufferOrErr.
getError()) {
145 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
146 return makeLTOModule(Buffer->getMemBufferRef(), options,
Context,
156 return makeLTOModule(Buffer, options, Context,
false);
161 const void *mem,
size_t length,
168 makeLTOModule(Buffer, options, *Context,
true);
170 (*Ret)->OwnedContext = std::move(Context);
180 IRObjectFile::findBitcodeInMemBuffer(Buffer);
181 if (std::error_code EC = MBOrErr.
getError()) {
203 if (std::error_code EC = MOrErr.
getError())
205 std::unique_ptr<Module> &M = *MOrErr;
207 std::string TripleStr = M->getTargetTriple();
208 if (TripleStr.empty())
216 return std::unique_ptr<LTOModule>(
nullptr);
220 Features.getDefaultSubtargetFeatures(
Triple);
221 std::string FeatureStr = Features.getString();
236 std::unique_ptr<LTOModule>
Ret(
new LTOModule(std::move(M), Buffer, target));
238 Ret->parseMetadata();
240 return std::move(
Ret);
244 std::unique_ptr<MemoryBuffer>
246 const char *startPtr = (
const char*)mem;
252 LTOModule::objcClassNameFromExpression(
const Constant *c, std::string &
name) {
253 if (
const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
256 Constant *cn = gvn->getInitializer();
258 if (ca->isCString()) {
259 name = (
".objc_class_name_" + ca->getAsCString()).str();
274 std::string superclassName;
275 if (objcClassNameFromExpression(c->
getOperand(1), superclassName)) {
277 _undefines.
insert(std::make_pair(superclassName, NameAndAttributes()));
278 if (IterBool.second) {
279 NameAndAttributes &
info = IterBool.first->second;
280 info.name = IterBool.first->first();
282 info.isFunction =
false;
288 std::string className;
289 if (objcClassNameFromExpression(c->
getOperand(2), className)) {
290 auto Iter = _defines.
insert(className).first;
292 NameAndAttributes
info;
293 info.name = Iter->first();
296 info.isFunction =
false;
298 _symbols.push_back(info);
308 std::string targetclassName;
309 if (!objcClassNameFromExpression(c->
getOperand(1), targetclassName))
313 _undefines.
insert(std::make_pair(targetclassName, NameAndAttributes()));
315 if (!IterBool.second)
318 NameAndAttributes &info = IterBool.first->second;
319 info.name = IterBool.first->first();
321 info.isFunction =
false;
327 std::string targetclassName;
328 if (!objcClassNameFromExpression(clgv->
getInitializer(), targetclassName))
332 _undefines.
insert(std::make_pair(targetclassName, NameAndAttributes()));
334 if (!IterBool.second)
337 NameAndAttributes &info = IterBool.first->second;
338 info.name = IterBool.first->first();
340 info.isFunction =
false;
353 addDefinedDataSymbol(Buffer, V);
358 addDefinedSymbol(Name, v,
false);
385 if (Section.compare(0, 15,
"__OBJC,__class,") == 0) {
392 else if (Section.compare(0, 18,
"__OBJC,__category,") == 0) {
399 else if (Section.compare(0, 18,
"__OBJC,__cls_refs,") == 0) {
415 addDefinedFunctionSymbol(Buffer,
F);
420 addDefinedSymbol(Name, F,
true);
464 if (isa<GlobalAlias>(def))
467 auto Iter = _defines.
insert(Name).first;
470 NameAndAttributes
info;
474 info.attributes = attr;
475 info.isFunction = isFunction;
479 _symbols.push_back(info);
484 void LTOModule::addAsmGlobalSymbol(
StringRef name,
486 auto IterBool = _defines.
insert(name);
489 if (!IterBool.second)
492 NameAndAttributes &info = _undefines[IterBool.first->first()];
494 if (info.symbol ==
nullptr) {
504 info.name = IterBool.first->first();
507 info.isFunction =
false;
508 info.symbol =
nullptr;
511 _symbols.push_back(info);
516 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
518 addDefinedDataSymbol(info.name, info.symbol);
521 _symbols.back().attributes |= scope;
526 void LTOModule::addAsmGlobalSymbolUndef(
StringRef name) {
527 auto IterBool = _undefines.
insert(std::make_pair(name, NameAndAttributes()));
529 _asm_undefines.push_back(IterBool.first->first());
532 if (!IterBool.second)
537 NameAndAttributes &info = IterBool.first->second;
538 info.name = IterBool.first->first();
539 info.attributes = attr;
540 info.isFunction =
false;
541 info.symbol =
nullptr;
554 auto IterBool = _undefines.
insert(std::make_pair(name, NameAndAttributes()));
557 if (!IterBool.second)
560 NameAndAttributes &info = IterBool.first->second;
562 info.name = IterBool.first->first();
571 info.isFunction = isFunc;
575 void LTOModule::parseSymbols() {
576 for (
auto Sym : SymTab.
symbols()) {
594 addAsmGlobalSymbolUndef(Name);
604 addPotentialUndefinedSymbol(Sym,
F !=
nullptr);
609 addDefinedFunctionSymbol(Sym);
613 if (isa<GlobalVariable>(GV)) {
614 addDefinedDataSymbol(Sym);
618 assert(isa<GlobalAlias>(GV));
619 addDefinedDataSymbol(Sym);
624 e = _undefines.
end(); u != e; ++u) {
627 if (_defines.
count(u->getKey()))
continue;
628 NameAndAttributes info = u->getValue();
629 _symbols.push_back(info);
634 void LTOModule::parseMetadata() {
639 getModule().getNamedMetadata(
"llvm.linker.options")) {
640 for (
unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
642 for (
unsigned ii = 0, ie = MDOptions->
getNumOperands(); ii != ie; ++ii) {
650 const Triple TT(_target->getTargetTriple());
651 if (!TT.isOSBinFormatCOFF())
654 for (
const NameAndAttributes &Sym : _symbols) {
MemoryBufferRef getMemBufferRef() const
std::error_code getError() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
Represents either an error or a value T.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Expected< std::unique_ptr< Module > > getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false)
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
bool canBeOmittedFromSymbolTable(const GlobalValue *GV)
True if GV can be left out of the object symbol table.
unsigned getNumOperands() const
Return number of MDNode operands.
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
bool isThinLTO()
Returns 'true' if the Module is produced for ThinLTO.
A raw_ostream that writes to an SmallVector or SmallString.
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path, size_t map_size, off_t offset, const TargetOptions &options)
static std::string getProducerString(MemoryBuffer *Buffer)
Returns a string representing the producer identification stored in the bitcode, or "" if the bitcode...
Error takeError()
Take ownership of the stored error.
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasCommonLinkage() const
Tagged union holding either a T or a Error.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
const Module & getModule() const
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
A constant value that is initialized with an expression using other constant values.
static ErrorOr< std::unique_ptr< Module > > parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldBeLazy)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Expected< std::string > getBitcodeProducerString(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the producer string information...
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
This is an important class for using LLVM in a threaded context.
This is an important base class in LLVM.
bool hasHiddenVisibility() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
Value * getOperand(unsigned i) const
C++ class which implements the opaque lto_module_t type.
bool hasWeakLinkage() const
std::pair< typename base::iterator, bool > insert(StringRef Key)
Triple - Helper class for working with autoconf configuration names.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
bool hasExternalWeakLinkage() const
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
T get() const
Returns the value of the specified pointer type.
Module.h This file contains the declarations for the Module class.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Target - Wrapper for Target specific information.
Manages the enabling and disabling of subtarget specific features.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
uint32_t getSymbolFlags(Symbol S) const
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
static std::vector< std::string > Flags
bool hasLinkOnceLinkage() const
Expected< BitcodeLTOInfo > getBitcodeLTOInfo(MemoryBufferRef Buffer)
Returns LTO information for the specified bitcode file.
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
static std::unique_ptr< MemoryBuffer > makeBuffer(const void *mem, size_t length, StringRef name="")
Create a MemoryBuffer from a memory range with an optional name.
ArrayRef< Symbol > symbols() const
bool hasProtectedVisibility() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool hasLocalLinkage() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
static bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
void printSymbolName(raw_ostream &OS, Symbol S) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getAlignment() const
A raw_ostream that writes to an std::string.
const FeatureBitset Features
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
static ErrorOr< std::unique_ptr< LTOModule > > createInLocalContext(std::unique_ptr< LLVMContext > Context, const void *mem, size_t length, const TargetOptions &options, StringRef path)
This file describes how to lower LLVM code to machine code.
StringRef getSection() const
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")