42 #include <system_error> 48 :
Mod(
std::move(M)), MBRef(MBRef), _target(TM) {
49 SymTab.addModule(
Mod.get());
69 BufferOrErr.
get()->getMemBufferRef());
79 return Result->IsThinLTO;
106 return *ProducerOrErr;
114 if (std::error_code EC = BufferOrErr.
getError()) {
118 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
119 return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
126 return createFromOpenFileSlice(Context, fd, path, size, 0, options);
131 size_t map_size, off_t offset,
135 if (std::error_code EC = BufferOrErr.
getError()) {
139 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.
get());
140 return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
150 return makeLTOModule(Buffer, options, Context,
false);
155 const void *mem,
size_t length,
162 makeLTOModule(Buffer, options, *Context,
true);
164 (*Ret)->OwnedContext = std::move(Context);
173 IRObjectFile::findBitcodeInMemBuffer(Buffer);
197 if (std::error_code EC = MOrErr.
getError())
199 std::unique_ptr<Module> &M = *MOrErr;
201 std::string TripleStr = M->getTargetTriple();
202 if (TripleStr.empty())
215 std::string FeatureStr = Features.
getString();
230 std::unique_ptr<LTOModule>
Ret(
new LTOModule(std::move(M), Buffer, target));
232 Ret->parseMetadata();
234 return std::move(Ret);
238 std::unique_ptr<MemoryBuffer>
240 const char *startPtr = (
const char*)mem;
246 LTOModule::objcClassNameFromExpression(
const Constant *c, std::string &
name) {
247 if (
const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
250 Constant *cn = gvn->getInitializer();
252 if (ca->isCString()) {
253 name = (
".objc_class_name_" + ca->getAsCString()).str();
268 std::string superclassName;
269 if (objcClassNameFromExpression(c->
getOperand(1), superclassName)) {
271 _undefines.insert(std::make_pair(superclassName, NameAndAttributes()));
272 if (IterBool.second) {
273 NameAndAttributes &
info = IterBool.first->second;
274 info.name = IterBool.first->first();
276 info.isFunction =
false;
282 std::string className;
283 if (objcClassNameFromExpression(c->
getOperand(2), className)) {
284 auto Iter = _defines.insert(className).first;
286 NameAndAttributes
info;
287 info.name = Iter->first();
290 info.isFunction =
false;
292 _symbols.push_back(info);
302 std::string targetclassName;
303 if (!objcClassNameFromExpression(c->
getOperand(1), targetclassName))
307 _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
309 if (!IterBool.second)
312 NameAndAttributes &
info = IterBool.first->second;
313 info.name = IterBool.first->first();
315 info.isFunction =
false;
321 std::string targetclassName;
322 if (!objcClassNameFromExpression(clgv->
getInitializer(), targetclassName))
326 _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
328 if (!IterBool.second)
331 NameAndAttributes &
info = IterBool.first->second;
332 info.name = IterBool.first->first();
334 info.isFunction =
false;
342 SymTab.printSymbolName(OS, Sym);
347 addDefinedDataSymbol(Buffer, V);
352 addDefinedSymbol(Name, v,
false);
385 else if (Section.
startswith(
"__OBJC,__category,")) {
390 else if (Section.
startswith(
"__OBJC,__cls_refs,")) {
400 SymTab.printSymbolName(OS, Sym);
405 addDefinedFunctionSymbol(Buffer, F);
410 addDefinedSymbol(Name, F,
true);
454 if (isa<GlobalAlias>(def))
457 auto Iter = _defines.insert(Name).first;
460 NameAndAttributes
info;
464 info.attributes = attr;
465 info.isFunction = isFunction;
469 _symbols.push_back(info);
474 void LTOModule::addAsmGlobalSymbol(
StringRef name,
476 auto IterBool = _defines.insert(name);
479 if (!IterBool.second)
482 NameAndAttributes &
info = _undefines[IterBool.first->first()];
484 if (info.symbol ==
nullptr) {
494 info.name = IterBool.first->first();
497 info.isFunction =
false;
498 info.symbol =
nullptr;
501 _symbols.push_back(info);
506 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
508 addDefinedDataSymbol(info.name, info.symbol);
511 _symbols.back().attributes |= scope;
516 void LTOModule::addAsmGlobalSymbolUndef(
StringRef name) {
517 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
519 _asm_undefines.push_back(IterBool.first->first());
522 if (!IterBool.second)
527 NameAndAttributes &
info = IterBool.first->second;
528 info.name = IterBool.first->first();
529 info.attributes = attr;
530 info.isFunction =
false;
531 info.symbol =
nullptr;
540 SymTab.printSymbolName(OS, Sym);
544 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
547 if (!IterBool.second)
550 NameAndAttributes &
info = IterBool.first->second;
552 info.name = IterBool.first->first();
561 info.isFunction = isFunc;
565 void LTOModule::parseSymbols() {
566 for (
auto Sym : SymTab.symbols()) {
568 uint32_t Flags = SymTab.getSymbolFlags(Sym);
578 SymTab.printSymbolName(OS, Sym);
584 addAsmGlobalSymbolUndef(Name);
594 addPotentialUndefinedSymbol(Sym, F !=
nullptr);
599 addDefinedFunctionSymbol(Sym);
603 if (isa<GlobalVariable>(GV)) {
604 addDefinedDataSymbol(Sym);
608 assert(isa<GlobalAlias>(GV));
609 addDefinedDataSymbol(Sym);
614 e = _undefines.end(); u != e; ++u) {
617 if (_defines.count(u->getKey()))
continue;
618 NameAndAttributes
info = u->getValue();
619 _symbols.push_back(info);
624 void LTOModule::parseMetadata() {
629 getModule().getNamedMetadata(
"llvm.linker.options")) {
630 for (
unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
632 for (
unsigned ii = 0, ie = MDOptions->
getNumOperands(); ii != ie; ++ii) {
640 const Triple TT(_target->getTargetTriple());
641 if (!TT.isOSBinFormatCOFF())
644 for (
const NameAndAttributes &Sym : _symbols) {
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
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.
bool hasLocalLinkage() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
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...
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
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...
std::string getString() const
Returns features as a string.
const FeatureBitset Features
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...
const MDOperand & getOperand(unsigned I) const
bool hasExternalWeakLinkage() const
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.
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).
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
std::error_code make_error_code(BitcodeError E)
Tagged union holding either a T or a Error.
bool hasCommonLinkage() 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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
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)
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
unsigned getAlignment() const
bool hasLinkOnceLinkage() const
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Value * getOperand(unsigned i) const
StringRef getString() const
Expected< std::string > getBitcodeProducerString(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the producer string information...
bool hasProtectedVisibility() const
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.
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::error_code getError() const
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.
C++ class which implements the opaque lto_module_t type.
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Triple - Helper class for working with autoconf configuration names.
bool hasWeakLinkage() const
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Module.h This file contains the declarations for the Module class.
MemoryBufferRef getMemBufferRef() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
The access may modify the value stored in memory.
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 hasHiddenVisibility() const
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
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.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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.
T get() const
Returns the value of the specified pointer type.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Lightweight error class with error context and mandatory checking.
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)
unsigned getNumOperands() const
Return number of MDNode operands.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
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="")