LLVM 20.0.0git
LTOModule.cpp
Go to the documentation of this file.
1//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Link Time Optimization library. This library is
10// intended to be used by linker to optimize code at link time.
11//
12//===----------------------------------------------------------------------===//
13
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Mangler.h"
20#include "llvm/IR/Metadata.h"
21#include "llvm/IR/Module.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCSymbol.h"
30#include "llvm/Object/MachO.h"
34#include "llvm/Support/Path.h"
42#include <system_error>
43using namespace llvm;
44using namespace llvm::object;
45
46LTOModule::LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
48 : Mod(std::move(M)), MBRef(MBRef), _target(TM) {
49 assert(_target && "target machine is null");
50 SymTab.addModule(Mod.get());
51}
52
53LTOModule::~LTOModule() = default;
54
55/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
56/// bitcode.
57bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
59 MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
60 return !errorToBool(BCData.takeError());
61}
62
66 if (!BufferOrErr)
67 return false;
68
70 BufferOrErr.get()->getMemBufferRef());
71 return !errorToBool(BCData.takeError());
72}
73
76 if (!Result) {
77 logAllUnhandledErrors(Result.takeError(), errs());
78 return false;
79 }
80 return Result->IsThinLTO;
81}
82
84 StringRef TriplePrefix) {
87 if (errorToBool(BCOrErr.takeError()))
88 return false;
89 LLVMContext Context;
90 ErrorOr<std::string> TripleOrErr =
92 if (!TripleOrErr)
93 return false;
94 return StringRef(*TripleOrErr).starts_with(TriplePrefix);
95}
96
100 if (errorToBool(BCOrErr.takeError()))
101 return "";
102 LLVMContext Context;
104 Context, getBitcodeProducerString(*BCOrErr));
105 if (!ProducerOrErr)
106 return "";
107 return *ProducerOrErr;
108}
109
112 const TargetOptions &options) {
115 if (std::error_code EC = BufferOrErr.getError()) {
116 Context.emitError(EC.message());
117 return EC;
118 }
119 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
120 return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
121 /* ShouldBeLazy*/ false);
122}
123
126 size_t size, const TargetOptions &options) {
127 return createFromOpenFileSlice(Context, fd, path, size, 0, options);
128}
129
132 size_t map_size, off_t offset,
133 const TargetOptions &options) {
136 map_size, offset);
137 if (std::error_code EC = BufferOrErr.getError()) {
138 Context.emitError(EC.message());
139 return EC;
140 }
141 std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
142 return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
143 /* ShouldBeLazy */ false);
144}
145
147LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,
148 size_t length, const TargetOptions &options,
149 StringRef path) {
150 StringRef Data((const char *)mem, length);
151 MemoryBufferRef Buffer(Data, path);
152 return makeLTOModule(Buffer, options, Context, /* ShouldBeLazy */ false);
153}
154
156LTOModule::createInLocalContext(std::unique_ptr<LLVMContext> Context,
157 const void *mem, size_t length,
158 const TargetOptions &options, StringRef path) {
159 StringRef Data((const char *)mem, length);
160 MemoryBufferRef Buffer(Data, path);
161 // If we own a context, we know this is being used only for symbol extraction,
162 // not linking. Be lazy in that case.
164 makeLTOModule(Buffer, options, *Context, /* ShouldBeLazy */ true);
165 if (Ret)
166 (*Ret)->OwnedContext = std::move(Context);
167 return Ret;
168}
169
172 bool ShouldBeLazy) {
173 // Find the buffer.
176 if (Error E = MBOrErr.takeError()) {
177 std::error_code EC = errorToErrorCode(std::move(E));
178 Context.emitError(EC.message());
179 return EC;
180 }
181
182 if (!ShouldBeLazy) {
183 // Parse the full file.
184 return expectedToErrorOrAndEmitErrors(Context,
185 parseBitcodeFile(*MBOrErr, Context));
186 }
187
188 // Parse lazily.
190 Context,
191 getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));
192}
193
195LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
196 LLVMContext &Context, bool ShouldBeLazy) {
198 parseBitcodeFileImpl(Buffer, Context, ShouldBeLazy);
199 if (std::error_code EC = MOrErr.getError())
200 return EC;
201 std::unique_ptr<Module> &M = *MOrErr;
202
203 std::string TripleStr = M->getTargetTriple();
204 if (TripleStr.empty())
205 TripleStr = sys::getDefaultTargetTriple();
206 llvm::Triple Triple(TripleStr);
207
208 // find machine architecture for this module
209 std::string errMsg;
210 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
211 if (!march)
213
214 // construct LTOModule, hand over ownership of module and target
215 SubtargetFeatures Features;
217 std::string FeatureStr = Features.getString();
218 // Set a default CPU for Darwin triples.
219 std::string CPU;
220 if (Triple.isOSDarwin()) {
222 CPU = "core2";
223 else if (Triple.getArch() == llvm::Triple::x86)
224 CPU = "yonah";
225 else if (Triple.isArm64e())
226 CPU = "apple-a12";
227 else if (Triple.getArch() == llvm::Triple::aarch64 ||
229 CPU = "cyclone";
230 }
231
232 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
233 options, std::nullopt);
234
235 std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));
236 Ret->parseSymbols();
237 Ret->parseMetadata();
238
239 return std::move(Ret);
240}
241
242/// Create a MemoryBuffer from a memory range with an optional name.
243std::unique_ptr<MemoryBuffer>
244LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
245 const char *startPtr = (const char*)mem;
246 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
247}
248
249/// objcClassNameFromExpression - Get string that the data pointer points to.
250bool
251LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
252 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
253 Constant *op = ce->getOperand(0);
254 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
255 Constant *cn = gvn->getInitializer();
256 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
257 if (ca->isCString()) {
258 name = (".objc_class_name_" + ca->getAsCString()).str();
259 return true;
260 }
261 }
262 }
263 }
264 return false;
265}
266
267/// addObjCClass - Parse i386/ppc ObjC class data structure.
268void LTOModule::addObjCClass(const GlobalVariable *clgv) {
269 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
270 if (!c) return;
271
272 // second slot in __OBJC,__class is pointer to superclass name
273 std::string superclassName;
274 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
275 auto IterBool =
276 _undefines.insert(std::make_pair(superclassName, NameAndAttributes()));
277 if (IterBool.second) {
278 NameAndAttributes &info = IterBool.first->second;
279 info.name = IterBool.first->first();
281 info.isFunction = false;
282 info.symbol = clgv;
283 }
284 }
285
286 // third slot in __OBJC,__class is pointer to class name
287 std::string className;
288 if (objcClassNameFromExpression(c->getOperand(2), className)) {
289 auto Iter = _defines.insert(className).first;
290
291 NameAndAttributes info;
292 info.name = Iter->first();
293 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
295 info.isFunction = false;
296 info.symbol = clgv;
297 _symbols.push_back(info);
298 }
299}
300
301/// addObjCCategory - Parse i386/ppc ObjC category data structure.
302void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
303 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
304 if (!c) return;
305
306 // second slot in __OBJC,__category is pointer to target class name
307 std::string targetclassName;
308 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
309 return;
310
311 auto IterBool =
312 _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
313
314 if (!IterBool.second)
315 return;
316
317 NameAndAttributes &info = IterBool.first->second;
318 info.name = IterBool.first->first();
320 info.isFunction = false;
321 info.symbol = clgv;
322}
323
324/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
325void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
326 std::string targetclassName;
327 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
328 return;
329
330 auto IterBool =
331 _undefines.insert(std::make_pair(targetclassName, NameAndAttributes()));
332
333 if (!IterBool.second)
334 return;
335
336 NameAndAttributes &info = IterBool.first->second;
337 info.name = IterBool.first->first();
339 info.isFunction = false;
340 info.symbol = clgv;
341}
342
343void LTOModule::addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym) {
344 SmallString<64> Buffer;
345 {
346 raw_svector_ostream OS(Buffer);
347 SymTab.printSymbolName(OS, Sym);
348 Buffer.c_str();
349 }
350
351 const GlobalValue *V = cast<GlobalValue *>(Sym);
352 addDefinedDataSymbol(Buffer, V);
353}
354
355void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
356 // Add to list of defined symbols.
357 addDefinedSymbol(Name, v, false);
358
359 if (!v->hasSection() /* || !isTargetDarwin */)
360 return;
361
362 // Special case i386/ppc ObjC data structures in magic sections:
363 // The issue is that the old ObjC object format did some strange
364 // contortions to avoid real linker symbols. For instance, the
365 // ObjC class data structure is allocated statically in the executable
366 // that defines that class. That data structures contains a pointer to
367 // its superclass. But instead of just initializing that part of the
368 // struct to the address of its superclass, and letting the static and
369 // dynamic linkers do the rest, the runtime works by having that field
370 // instead point to a C-string that is the name of the superclass.
371 // At runtime the objc initialization updates that pointer and sets
372 // it to point to the actual super class. As far as the linker
373 // knows it is just a pointer to a string. But then someone wanted the
374 // linker to issue errors at build time if the superclass was not found.
375 // So they figured out a way in mach-o object format to use an absolute
376 // symbols (.objc_class_name_Foo = 0) and a floating reference
377 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
378 // a class was missing.
379 // The following synthesizes the implicit .objc_* symbols for the linker
380 // from the ObjC data structures generated by the front end.
381
382 // special case if this data blob is an ObjC class definition
383 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(v)) {
384 StringRef Section = GV->getSection();
385 if (Section.starts_with("__OBJC,__class,")) {
386 addObjCClass(GV);
387 }
388
389 // special case if this data blob is an ObjC category definition
390 else if (Section.starts_with("__OBJC,__category,")) {
391 addObjCCategory(GV);
392 }
393
394 // special case if this data blob is the list of referenced classes
395 else if (Section.starts_with("__OBJC,__cls_refs,")) {
396 addObjCClassRef(GV);
397 }
398 }
399}
400
401void LTOModule::addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym) {
402 SmallString<64> Buffer;
403 {
404 raw_svector_ostream OS(Buffer);
405 SymTab.printSymbolName(OS, Sym);
406 Buffer.c_str();
407 }
408
409 auto *GV = cast<GlobalValue *>(Sym);
410 assert((isa<Function>(GV) ||
411 (isa<GlobalAlias>(GV) &&
412 isa<Function>(cast<GlobalAlias>(GV)->getAliasee()))) &&
413 "Not function or function alias");
414
415 addDefinedFunctionSymbol(Buffer, GV);
416}
417
418void LTOModule::addDefinedFunctionSymbol(StringRef Name, const GlobalValue *F) {
419 // add to list of defined symbols
420 addDefinedSymbol(Name, F, true);
421}
422
423void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
424 bool isFunction) {
425 const GlobalObject *go = dyn_cast<GlobalObject>(def);
426 uint32_t attr = go ? Log2(go->getAlign().valueOrOne()) : 0;
427
428 // set permissions part
429 if (isFunction) {
431 } else {
432 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
433 if (gv && gv->isConstant())
435 else
437 }
438
439 // set definition part
440 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage())
442 else if (def->hasCommonLinkage())
444 else
446
447 // set scope part
448 if (def->hasLocalLinkage())
449 // Ignore visibility if linkage is local.
451 else if (def->hasHiddenVisibility())
453 else if (def->hasProtectedVisibility())
455 else if (def->canBeOmittedFromSymbolTable())
457 else
459
460 if (def->hasComdat())
461 attr |= LTO_SYMBOL_COMDAT;
462
463 if (isa<GlobalAlias>(def))
464 attr |= LTO_SYMBOL_ALIAS;
465
466 auto Iter = _defines.insert(Name).first;
467
468 // fill information structure
469 NameAndAttributes info;
470 StringRef NameRef = Iter->first();
471 info.name = NameRef;
472 assert(NameRef.data()[NameRef.size()] == '\0');
473 info.attributes = attr;
474 info.isFunction = isFunction;
475 info.symbol = def;
476
477 // add to table of symbols
478 _symbols.push_back(info);
479}
480
481/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
482/// defined list.
483void LTOModule::addAsmGlobalSymbol(StringRef name,
484 lto_symbol_attributes scope) {
485 auto IterBool = _defines.insert(name);
486
487 // only add new define if not already defined
488 if (!IterBool.second)
489 return;
490
491 NameAndAttributes &info = _undefines[IterBool.first->first()];
492
493 if (info.symbol == nullptr) {
494 // FIXME: This is trying to take care of module ASM like this:
495 //
496 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
497 //
498 // but is gross and its mother dresses it funny. Have the ASM parser give us
499 // more details for this type of situation so that we're not guessing so
500 // much.
501
502 // fill information structure
503 info.name = IterBool.first->first();
504 info.attributes =
506 info.isFunction = false;
507 info.symbol = nullptr;
508
509 // add to table of symbols
510 _symbols.push_back(info);
511 return;
512 }
513
514 if (info.isFunction)
515 addDefinedFunctionSymbol(info.name, cast<Function>(info.symbol));
516 else
517 addDefinedDataSymbol(info.name, info.symbol);
518
519 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
520 _symbols.back().attributes |= scope;
521}
522
523/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
524/// undefined list.
525void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
526 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
527
528 _asm_undefines.push_back(IterBool.first->first());
529
530 // we already have the symbol
531 if (!IterBool.second)
532 return;
533
536 NameAndAttributes &info = IterBool.first->second;
537 info.name = IterBool.first->first();
538 info.attributes = attr;
539 info.isFunction = false;
540 info.symbol = nullptr;
541}
542
543/// Add a symbol which isn't defined just yet to a list to be resolved later.
544void LTOModule::addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
545 bool isFunc) {
547 {
549 SymTab.printSymbolName(OS, Sym);
550 name.c_str();
551 }
552
553 auto IterBool =
554 _undefines.insert(std::make_pair(name.str(), NameAndAttributes()));
555
556 // we already have the symbol
557 if (!IterBool.second)
558 return;
559
560 NameAndAttributes &info = IterBool.first->second;
561
562 info.name = IterBool.first->first();
563
564 const GlobalValue *decl = dyn_cast_if_present<GlobalValue *>(Sym);
565
566 if (decl->hasExternalWeakLinkage())
568 else
570
571 info.isFunction = isFunc;
572 info.symbol = decl;
573}
574
575void LTOModule::parseSymbols() {
576 for (auto Sym : SymTab.symbols()) {
577 auto *GV = dyn_cast_if_present<GlobalValue *>(Sym);
580 continue;
581
582 bool IsUndefined = Flags & object::BasicSymbolRef::SF_Undefined;
583
584 if (!GV) {
585 SmallString<64> Buffer;
586 {
587 raw_svector_ostream OS(Buffer);
588 SymTab.printSymbolName(OS, Sym);
589 Buffer.c_str();
590 }
591 StringRef Name = Buffer;
592
593 if (IsUndefined)
594 addAsmGlobalSymbolUndef(Name);
595 else if (Flags & object::BasicSymbolRef::SF_Global)
596 addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_DEFAULT);
597 else
598 addAsmGlobalSymbol(Name, LTO_SYMBOL_SCOPE_INTERNAL);
599 continue;
600 }
601
602 auto *F = dyn_cast<Function>(GV);
603 if (IsUndefined) {
604 addPotentialUndefinedSymbol(Sym, F != nullptr);
605 continue;
606 }
607
608 if (F) {
609 addDefinedFunctionSymbol(Sym);
610 continue;
611 }
612
613 if (isa<GlobalVariable>(GV)) {
614 addDefinedDataSymbol(Sym);
615 continue;
616 }
617
618 assert(isa<GlobalAlias>(GV));
619
620 if (isa<Function>(cast<GlobalAlias>(GV)->getAliasee()))
621 addDefinedFunctionSymbol(Sym);
622 else
623 addDefinedDataSymbol(Sym);
624 }
625
626 // make symbols for all undefines
628 e = _undefines.end(); u != e; ++u) {
629 // If this symbol also has a definition, then don't make an undefine because
630 // it is a tentative definition.
631 if (_defines.count(u->getKey())) continue;
632 NameAndAttributes info = u->getValue();
633 _symbols.push_back(info);
634 }
635}
636
637/// parseMetadata - Parse metadata from the module
638void LTOModule::parseMetadata() {
639 raw_string_ostream OS(LinkerOpts);
640
641 // Linker Options
642 if (NamedMDNode *LinkerOptions =
643 getModule().getNamedMetadata("llvm.linker.options")) {
644 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
645 MDNode *MDOptions = LinkerOptions->getOperand(i);
646 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
647 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
648 OS << " " << MDOption->getString();
649 }
650 }
651 }
652
653 // Globals - we only need to do this for COFF.
654 const Triple TT(_target->getTargetTriple());
655 if (!TT.isOSBinFormatCOFF())
656 return;
657 Mangler M;
658 for (const NameAndAttributes &Sym : _symbols) {
659 if (!Sym.symbol)
660 continue;
661 emitLinkerFlagsForGlobalCOFF(OS, Sym.symbol, TT, M);
662 }
663}
664
666 size_t buffer_size, const char *path,
667 std::string &outErr) {
668 StringRef Data((const char *)buffer, buffer_size);
669 MemoryBufferRef BufferRef(Data, path);
670
672 lto::InputFile::create(BufferRef);
673
674 if (ObjOrErr)
675 return ObjOrErr->release();
676
677 outErr = std::string(path) +
678 ": Could not read LTO input file: " + toString(ObjOrErr.takeError());
679 return nullptr;
680}
681
683 return input->getDependentLibraries().size();
684}
685
686const char *LTOModule::getDependentLibrary(lto::InputFile *input, size_t index,
687 size_t *size) {
688 StringRef S = input->getDependentLibraries()[index];
689 *size = S.size();
690 return S.data();
691}
692
694 return MachO::getCPUType(Triple(Mod->getTargetTriple()));
695}
696
698 return MachO::getCPUSubType(Triple(Mod->getTargetTriple()));
699}
700
702 for (auto Sym : SymTab.symbols()) {
703 if (auto *GV = dyn_cast_if_present<GlobalValue *>(Sym)) {
704 StringRef Name = GV->getName();
705 if (Name.consume_front("llvm.global_")) {
706 if (Name == "ctors" || Name == "dtors")
707 return true;
708 }
709 }
710 }
711 return false;
712}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define op(i)
Module.h This file contains the declarations for the Module class.
static ErrorOr< std::unique_ptr< Module > > parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldBeLazy)
Definition: LTOModule.cpp:171
lazy value info
#define F(x, y, z)
Definition: MD5.cpp:55
This file contains the declarations for metadata subclasses.
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:46
raw_pwrite_stream & OS
static bool isFunction(SDValue Op)
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:696
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1108
This is an important base class in LLVM.
Definition: Constant.h:42
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:149
std::error_code getError() const
Definition: ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:79
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:515
bool hasLocalLinkage() const
Definition: GlobalValue.h:528
bool hasHiddenVisibility() const
Definition: GlobalValue.h:250
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:529
bool hasComdat() const
Definition: GlobalValue.h:241
bool hasWeakLinkage() const
Definition: GlobalValue.h:522
bool hasCommonLinkage() const
Definition: GlobalValue.h:532
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition: Globals.cpp:425
bool hasProtectedVisibility() const
Definition: GlobalValue.h:251
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
Metadata node.
Definition: Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1430
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1436
A single uniqued string.
Definition: Metadata.h:720
StringRef getString() const
Definition: Metadata.cpp:616
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
MemoryBufferRef getMemBufferRef() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
void printSymbolName(raw_ostream &OS, Symbol S) const
uint32_t getSymbolFlags(Symbol S) const
ArrayRef< Symbol > symbols() const
A tuple of MDNodes.
Definition: Metadata.h:1731
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
const char * c_str()
Definition: SmallString.h:259
iterator end()
Definition: StringMap.h:220
iterator begin()
Definition: StringMap.h:219
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:276
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
Manages the enabling and disabling of subtarget specific features.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
std::string getString() const
Returns features as a string.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
Target - Wrapper for Target specific information.
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM=std::nullopt, CodeGenOptLevel OL=CodeGenOptLevel::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isArm64e() const
Tests whether the target is the Apple "arm64e" AArch64 subarch.
Definition: Triple.h:1048
@ aarch64_32
Definition: Triple.h:53
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:383
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:568
Value * getOperand(unsigned i) const
Definition: User.h:228
An input file.
Definition: LTO.h:115
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:554
ArrayRef< StringRef > getDependentLibraries() const
Returns dependent library specifiers from the input file.
Definition: LTO.h:174
static Expected< MemoryBufferRef > findBitcodeInMemBuffer(MemoryBufferRef Object)
Finds and returns bitcode in the given memory buffer (which may be either a bitcode file or a native ...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
lto_symbol_attributes
Definition: lto.h:54
@ LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN
Definition: lto.h:71
@ LTO_SYMBOL_PERMISSIONS_CODE
Definition: lto.h:57
@ LTO_SYMBOL_DEFINITION_REGULAR
Definition: lto.h:61
@ LTO_SYMBOL_SCOPE_DEFAULT
Definition: lto.h:70
@ LTO_SYMBOL_SCOPE_INTERNAL
Definition: lto.h:67
@ LTO_SYMBOL_ALIAS
Definition: lto.h:73
@ LTO_SYMBOL_SCOPE_HIDDEN
Definition: lto.h:68
@ LTO_SYMBOL_DEFINITION_TENTATIVE
Definition: lto.h:62
@ LTO_SYMBOL_PERMISSIONS_DATA
Definition: lto.h:58
@ LTO_SYMBOL_SCOPE_PROTECTED
Definition: lto.h:69
@ LTO_SYMBOL_PERMISSIONS_RODATA
Definition: lto.h:59
@ LTO_SYMBOL_COMDAT
Definition: lto.h:72
@ LTO_SYMBOL_DEFINITION_WEAKUNDEF
Definition: lto.h:65
@ LTO_SYMBOL_DEFINITION_UNDEFINED
Definition: lto.h:64
@ LTO_SYMBOL_DEFINITION_WEAK
Definition: lto.h:63
Expected< uint32_t > getCPUSubType(const Triple &T)
Definition: MachO.cpp:95
Expected< uint32_t > getCPUType(const Triple &T)
Definition: MachO.cpp:77
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:990
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition: Error.h:1099
@ Length
Definition: DWP.cpp:480
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, ParserCallbacks Callbacks={})
Read the specified bitcode file, returning the module.
std::error_code make_error_code(BitcodeError E)
Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
Definition: BitcodeReader.h:66
Expected< std::string > getBitcodeProducerString(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the producer string information.
Expected< std::unique_ptr< Module > > getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false, ParserCallbacks Callbacks={})
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
Expected< BitcodeLTOInfo > getBitcodeLTOInfo(MemoryBufferRef Buffer)
Returns LTO information for the specified bitcode file.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Definition: Mangler.cpp:214
const char * toString(DWARFSectionKind Kind)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:117
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:38
static ErrorOr< std::unique_ptr< LTOModule > > createFromFile(LLVMContext &Context, StringRef path, const TargetOptions &options)
Create an LTOModule.
Definition: LTOModule.cpp:111
static bool isBitcodeFile(const void *mem, size_t length)
Returns 'true' if the file or memory contents is LLVM bitcode.
Definition: LTOModule.cpp:57
Expected< uint32_t > getMachOCPUType() const
Definition: LTOModule.cpp:693
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.
Definition: LTOModule.cpp:244
static size_t getDependentLibraryCount(lto::InputFile *input)
Definition: LTOModule.cpp:682
bool hasCtorDtor() const
Returns true if the module has either the @llvm.global_ctors or the @llvm.global_dtors symbol.
Definition: LTOModule.cpp:701
static const char * getDependentLibrary(lto::InputFile *input, size_t index, size_t *size)
Definition: LTOModule.cpp:686
const Module & getModule() const
Definition: LTOModule.h:115
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path, size_t map_size, off_t offset, const TargetOptions &options)
Definition: LTOModule.cpp:131
static std::string getProducerString(MemoryBuffer *Buffer)
Returns a string representing the producer identification stored in the bitcode, or "" if the bitcode...
Definition: LTOModule.cpp:97
static bool isBitcodeForTarget(MemoryBuffer *memBuffer, StringRef triplePrefix)
Returns 'true' if the memory buffer is LLVM bitcode for the specified triple.
Definition: LTOModule.cpp:83
bool isThinLTO()
Returns 'true' if the Module is produced for ThinLTO.
Definition: LTOModule.cpp:74
static ErrorOr< std::unique_ptr< LTOModule > > createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size, const TargetOptions &options)
Definition: LTOModule.cpp:125
static ErrorOr< std::unique_ptr< LTOModule > > createInLocalContext(std::unique_ptr< LLVMContext > Context, const void *mem, size_t length, const TargetOptions &options, StringRef path)
Definition: LTOModule.cpp:156
static ErrorOr< std::unique_ptr< LTOModule > > createFromBuffer(LLVMContext &Context, const void *mem, size_t length, const TargetOptions &options, StringRef path="")
Definition: LTOModule.cpp:147
Expected< uint32_t > getMachOCPUSubType() const
Definition: LTOModule.cpp:697
static lto::InputFile * createInputFile(const void *buffer, size_t buffer_size, const char *path, std::string &out_error)
Definition: LTOModule.cpp:665
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.