LLVM  9.0.0svn
Module.cpp
Go to the documentation of this file.
1 //===- Module.cpp - Implement the Module class ----------------------------===//
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 Module class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Module.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/Comdat.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GVMaterializer.h"
30 #include "llvm/IR/GlobalAlias.h"
31 #include "llvm/IR/GlobalIFunc.h"
32 #include "llvm/IR/GlobalValue.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Metadata.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/TypeFinder.h"
39 #include "llvm/IR/Value.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CodeGen.h"
44 #include "llvm/Support/Error.h"
46 #include "llvm/Support/Path.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <cstdint>
52 #include <memory>
53 #include <utility>
54 #include <vector>
55 
56 using namespace llvm;
57 
58 //===----------------------------------------------------------------------===//
59 // Methods to implement the globals and functions lists.
60 //
61 
62 // Explicit instantiations of SymbolTableListTraits since some of the methods
63 // are not in the public header file.
68 
69 //===----------------------------------------------------------------------===//
70 // Primitive Module methods.
71 //
72 
74  : Context(C), Materializer(), ModuleID(MID), SourceFileName(MID), DL("") {
75  ValSymTab = new ValueSymbolTable();
76  NamedMDSymTab = new StringMap<NamedMDNode *>();
77  Context.addModule(this);
78 }
79 
81  Context.removeModule(this);
83  GlobalList.clear();
84  FunctionList.clear();
85  AliasList.clear();
86  IFuncList.clear();
87  NamedMDList.clear();
88  delete ValSymTab;
89  delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
90 }
91 
92 std::unique_ptr<RandomNumberGenerator> Module::createRNG(const Pass* P) const {
93  SmallString<32> Salt(P->getPassName());
94 
95  // This RNG is guaranteed to produce the same random stream only
96  // when the Module ID and thus the input filename is the same. This
97  // might be problematic if the input filename extension changes
98  // (e.g. from .c to .bc or .ll).
99  //
100  // We could store this salt in NamedMetadata, but this would make
101  // the parameter non-const. This would unfortunately make this
102  // interface unusable by any Machine passes, since they only have a
103  // const reference to their IR Module. Alternatively we can always
104  // store salt metadata from the Module constructor.
106 
107  return std::unique_ptr<RandomNumberGenerator>(new RandomNumberGenerator(Salt));
108 }
109 
110 /// getNamedValue - Return the first global value in the module with
111 /// the specified name, of arbitrary type. This method returns null
112 /// if a global with the specified name is not found.
114  return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
115 }
116 
117 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
118 /// This ID is uniqued across modules in the current LLVMContext.
120  return Context.getMDKindID(Name);
121 }
122 
123 /// getMDKindNames - Populate client supplied SmallVector with the name for
124 /// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
125 /// so it is filled in as an empty string.
127  return Context.getMDKindNames(Result);
128 }
129 
131  return Context.getOperandBundleTags(Result);
132 }
133 
134 //===----------------------------------------------------------------------===//
135 // Methods for easy access to the functions in the module.
136 //
137 
138 // getOrInsertFunction - Look up the specified function in the module symbol
139 // table. If it does not exist, add a prototype for the function and return
140 // it. This is nice because it allows most passes to get away with not handling
141 // the symbol table directly for this common task.
142 //
145  // See if we have a definition for the specified function already.
146  GlobalValue *F = getNamedValue(Name);
147  if (!F) {
148  // Nope, add it
151  if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
152  New->setAttributes(AttributeList);
153  FunctionList.push_back(New);
154  return {Ty, New}; // Return the new prototype.
155  }
156 
157  // If the function exists but has the wrong type, return a bitcast to the
158  // right type.
159  auto *PTy = PointerType::get(Ty, F->getAddressSpace());
160  if (F->getType() != PTy)
161  return {Ty, ConstantExpr::getBitCast(F, PTy)};
162 
163  // Otherwise, we just found the existing function or a prototype.
164  return {Ty, F};
165 }
166 
168  return getOrInsertFunction(Name, Ty, AttributeList());
169 }
170 
171 // getFunction - Look up the specified function in the module symbol table.
172 // If it does not exist, return null.
173 //
175  return dyn_cast_or_null<Function>(getNamedValue(Name));
176 }
177 
178 //===----------------------------------------------------------------------===//
179 // Methods for easy access to the global variables in the module.
180 //
181 
182 /// getGlobalVariable - Look up the specified global variable in the module
183 /// symbol table. If it does not exist, return null. The type argument
184 /// should be the underlying type of the global, i.e., it should not have
185 /// the top-level PointerType, which represents the address of the global.
186 /// If AllowLocal is set to true, this function will return types that
187 /// have an local. By default, these types are not returned.
188 ///
190  bool AllowLocal) const {
191  if (GlobalVariable *Result =
192  dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
193  if (AllowLocal || !Result->hasLocalLinkage())
194  return Result;
195  return nullptr;
196 }
197 
198 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
199 /// 1. If it does not exist, add a declaration of the global and return it.
200 /// 2. Else, the global exists but has the wrong type: return the function
201 /// with a constantexpr cast to the right type.
202 /// 3. Finally, if the existing global is the correct declaration, return the
203 /// existing global.
205  StringRef Name, Type *Ty,
206  function_ref<GlobalVariable *()> CreateGlobalCallback) {
207  // See if we have a definition for the specified global already.
208  GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
209  if (!GV)
210  GV = CreateGlobalCallback();
211  assert(GV && "The CreateGlobalCallback is expected to create a global");
212 
213  // If the variable exists but has the wrong type, return a bitcast to the
214  // right type.
215  Type *GVTy = GV->getType();
217  if (GVTy != PTy)
218  return ConstantExpr::getBitCast(GV, PTy);
219 
220  // Otherwise, we just found the existing function or a prototype.
221  return GV;
222 }
223 
224 // Overload to construct a global variable using its constructor's defaults.
226  return getOrInsertGlobal(Name, Ty, [&] {
227  return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
228  nullptr, Name);
229  });
230 }
231 
232 //===----------------------------------------------------------------------===//
233 // Methods for easy access to the global variables in the module.
234 //
235 
236 // getNamedAlias - Look up the specified global in the module symbol table.
237 // If it does not exist, return null.
238 //
240  return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
241 }
242 
244  return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
245 }
246 
247 /// getNamedMetadata - Return the first NamedMDNode in the module with the
248 /// specified name. This method returns null if a NamedMDNode with the
249 /// specified name is not found.
251  SmallString<256> NameData;
252  StringRef NameRef = Name.toStringRef(NameData);
253  return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
254 }
255 
256 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
257 /// with the specified name. This method returns a new NamedMDNode if a
258 /// NamedMDNode with the specified name is not found.
260  NamedMDNode *&NMD =
261  (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
262  if (!NMD) {
263  NMD = new NamedMDNode(Name);
264  NMD->setParent(this);
265  NamedMDList.push_back(NMD);
266  }
267  return NMD;
268 }
269 
270 /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
271 /// delete it.
273  static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
274  NamedMDList.erase(NMD->getIterator());
275 }
276 
278  if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
279  uint64_t Val = Behavior->getLimitedValue();
280  if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
281  MFB = static_cast<ModFlagBehavior>(Val);
282  return true;
283  }
284  }
285  return false;
286 }
287 
288 /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
289 void Module::
291  const NamedMDNode *ModFlags = getModuleFlagsMetadata();
292  if (!ModFlags) return;
293 
294  for (const MDNode *Flag : ModFlags->operands()) {
295  ModFlagBehavior MFB;
296  if (Flag->getNumOperands() >= 3 &&
297  isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
298  dyn_cast_or_null<MDString>(Flag->getOperand(1))) {
299  // Check the operands of the MDNode before accessing the operands.
300  // The verifier will actually catch these failures.
301  MDString *Key = cast<MDString>(Flag->getOperand(1));
302  Metadata *Val = Flag->getOperand(2);
303  Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
304  }
305  }
306 }
307 
308 /// Return the corresponding value if Key appears in module flags, otherwise
309 /// return null.
312  getModuleFlagsMetadata(ModuleFlags);
313  for (const ModuleFlagEntry &MFE : ModuleFlags) {
314  if (Key == MFE.Key->getString())
315  return MFE.Val;
316  }
317  return nullptr;
318 }
319 
320 /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
321 /// represents module-level flags. This method returns null if there are no
322 /// module-level flags.
324  return getNamedMetadata("llvm.module.flags");
325 }
326 
327 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
328 /// represents module-level flags. If module-level flags aren't found, it
329 /// creates the named metadata that contains them.
331  return getOrInsertNamedMetadata("llvm.module.flags");
332 }
333 
334 /// addModuleFlag - Add a module-level flag to the module-level flags
335 /// metadata. It will create the module-level flags named metadata if it doesn't
336 /// already exist.
338  Metadata *Val) {
339  Type *Int32Ty = Type::getInt32Ty(Context);
340  Metadata *Ops[3] = {
341  ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
342  MDString::get(Context, Key), Val};
344 }
346  Constant *Val) {
347  addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
348 }
350  uint32_t Val) {
351  Type *Int32Ty = Type::getInt32Ty(Context);
352  addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
353 }
355  assert(Node->getNumOperands() == 3 &&
356  "Invalid number of operands for module flag!");
357  assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
358  isa<MDString>(Node->getOperand(1)) &&
359  "Invalid operand types for module flag!");
361 }
362 
364  DL.reset(Desc);
365 }
366 
367 void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
368 
369 const DataLayout &Module::getDataLayout() const { return DL; }
370 
372  return cast<DICompileUnit>(CUs->getOperand(Idx));
373 }
375  return cast<DICompileUnit>(CUs->getOperand(Idx));
376 }
377 
378 void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
379  while (CUs && (Idx < CUs->getNumOperands()) &&
380  ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
381  ++Idx;
382 }
383 
384 //===----------------------------------------------------------------------===//
385 // Methods to control the materialization of GlobalValues in the Module.
386 //
388  assert(!Materializer &&
389  "Module already has a GVMaterializer. Call materializeAll"
390  " to clear it out before setting another one.");
391  Materializer.reset(GVM);
392 }
393 
395  if (!Materializer)
396  return Error::success();
397 
398  return Materializer->materialize(GV);
399 }
400 
402  if (!Materializer)
403  return Error::success();
404  std::unique_ptr<GVMaterializer> M = std::move(Materializer);
405  return M->materializeModule();
406 }
407 
409  if (!Materializer)
410  return Error::success();
411  return Materializer->materializeMetadata();
412 }
413 
414 //===----------------------------------------------------------------------===//
415 // Other module related stuff.
416 //
417 
418 std::vector<StructType *> Module::getIdentifiedStructTypes() const {
419  // If we have a materializer, it is possible that some unread function
420  // uses a type that is currently not visible to a TypeFinder, so ask
421  // the materializer which types it created.
422  if (Materializer)
423  return Materializer->getIdentifiedStructTypes();
424 
425  std::vector<StructType *> Ret;
426  TypeFinder SrcStructTypes;
427  SrcStructTypes.run(*this, true);
428  Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
429  return Ret;
430 }
431 
432 // dropAllReferences() - This function causes all the subelements to "let go"
433 // of all references that they are maintaining. This allows one to 'delete' a
434 // whole module at a time, even though there may be circular references... first
435 // all references are dropped, and all use counts go to zero. Then everything
436 // is deleted for real. Note that no operations are valid on an object that
437 // has "dropped all references", except operator delete.
438 //
440  for (Function &F : *this)
441  F.dropAllReferences();
442 
443  for (GlobalVariable &GV : globals())
444  GV.dropAllReferences();
445 
446  for (GlobalAlias &GA : aliases())
447  GA.dropAllReferences();
448 
449  for (GlobalIFunc &GIF : ifuncs())
450  GIF.dropAllReferences();
451 }
452 
454  auto *Val =
455  cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters"));
456  if (!Val)
457  return 0;
458  return cast<ConstantInt>(Val->getValue())->getZExtValue();
459 }
460 
461 unsigned Module::getDwarfVersion() const {
462  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
463  if (!Val)
464  return 0;
465  return cast<ConstantInt>(Val->getValue())->getZExtValue();
466 }
467 
468 unsigned Module::getCodeViewFlag() const {
469  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
470  if (!Val)
471  return 0;
472  return cast<ConstantInt>(Val->getValue())->getZExtValue();
473 }
474 
476  unsigned NumInstrs = 0;
477  for (Function &F : FunctionList)
478  NumInstrs += F.getInstructionCount();
479  return NumInstrs;
480 }
481 
483  auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
484  Entry.second.Name = &Entry;
485  return &Entry.second;
486 }
487 
489  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
490 
491  if (!Val)
492  return PICLevel::NotPIC;
493 
494  return static_cast<PICLevel::Level>(
495  cast<ConstantInt>(Val->getValue())->getZExtValue());
496 }
497 
499  addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL);
500 }
501 
503  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
504 
505  if (!Val)
506  return PIELevel::Default;
507 
508  return static_cast<PIELevel::Level>(
509  cast<ConstantInt>(Val->getValue())->getZExtValue());
510 }
511 
513  addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
514 }
515 
517  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));
518 
519  if (!Val)
520  return None;
521 
522  return static_cast<CodeModel::Model>(
523  cast<ConstantInt>(Val->getValue())->getZExtValue());
524 }
525 
527  // Linking object files with different code models is undefined behavior
528  // because the compiler would have to generate additional code (to span
529  // longer jumps) if a larger code model is used with a smaller one.
530  // Therefore we will treat attempts to mix code models as an error.
531  addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
532 }
533 
535  if (Kind == ProfileSummary::PSK_CSInstr)
536  addModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);
537  else
538  addModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
539 }
540 
542  return (IsCS ? getModuleFlag("CSProfileSummary")
543  : getModuleFlag("ProfileSummary"));
544 }
545 
546 void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
547  OwnedMemoryBuffer = std::move(MB);
548 }
549 
551  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT"));
552  return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
553 }
554 
556  addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
557 }
558 
560  SmallVector<unsigned, 3> Entries;
561  Entries.push_back(V.getMajor());
562  if (auto Minor = V.getMinor()) {
563  Entries.push_back(*Minor);
564  if (auto Subminor = V.getSubminor())
565  Entries.push_back(*Subminor);
566  // Ignore the 'build' component as it can't be represented in the object
567  // file.
568  }
570  ConstantDataArray::get(Context, Entries));
571 }
572 
574  auto *CM = dyn_cast_or_null<ConstantAsMetadata>(getModuleFlag("SDK Version"));
575  if (!CM)
576  return {};
577  auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
578  if (!Arr)
579  return {};
580  auto getVersionComponent = [&](unsigned Index) -> Optional<unsigned> {
581  if (Index >= Arr->getNumElements())
582  return None;
583  return (unsigned)Arr->getElementAsInteger(Index);
584  };
585  auto Major = getVersionComponent(0);
586  if (!Major)
587  return {};
588  VersionTuple Result = VersionTuple(*Major);
589  if (auto Minor = getVersionComponent(1)) {
590  Result = VersionTuple(*Major, *Minor);
591  if (auto Subminor = getVersionComponent(2)) {
592  Result = VersionTuple(*Major, *Minor, *Subminor);
593  }
594  }
595  return Result;
596 }
597 
599  const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) {
600  const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
601  GlobalVariable *GV = M.getGlobalVariable(Name);
602  if (!GV || !GV->hasInitializer())
603  return GV;
604 
605  const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
606  for (Value *Op : Init->operands()) {
607  GlobalValue *G = cast<GlobalValue>(Op->stripPointerCastsNoFollowAliases());
608  Set.insert(G);
609  }
610  return GV;
611 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
uint64_t CallInst * C
bool isIntrinsic() const
isIntrinsic - Returns true if the function&#39;s name starts with "llvm.".
Definition: Function.h:198
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
This class provides a symbol table of name/value pairs.
llvm::Error materializeAll()
Make sure all GlobalValues in this Module are fully read and clear the Materializer.
Definition: Module.cpp:401
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
Definition: DataLayout.cpp:179
LLVMContext & Context
iterator erase(iterator where)
Definition: ilist.h:265
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
NamedMDNode * getModuleFlagsMetadata() const
Returns the NamedMDNode in the module that represents module-level flags.
Definition: Module.cpp:323
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:453
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:164
const ValueSymbolTable & getValueSymbolTable() const
Get the symbol table of global variable and function identifiers.
Definition: Module.h:568
void setMaterializer(GVMaterializer *GVM)
Sets the GVMaterializer to GVM.
Definition: Module.cpp:387
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:455
void addOperand(MDNode *M)
Definition: Metadata.cpp:1086
DICompileUnit * operator->() const
Definition: Module.cpp:374
This file contains the declarations for metadata subclasses.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:629
Metadata * getProfileSummary(bool IsCS)
Returns profile summary metadata.
Definition: Module.cpp:541
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:116
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Return the named MDNode in the module with the specified name.
Definition: Module.cpp:259
Externally visible function.
Definition: GlobalValue.h:48
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:363
Optional< CodeModel::Model > getCodeModel() const
Returns the code model (tiny, small, kernel, medium or large model)
Definition: Module.cpp:516
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:390
std::string Name
Metadata node.
Definition: Metadata.h:863
F(f)
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1068
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:534
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:79
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
GlobalAlias * getNamedAlias(StringRef Name) const
Return the global alias in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:239
A tuple of MDNodes.
Definition: Metadata.h:1325
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
getMDKindNames - Populate client supplied SmallVector with the name for custom metadata IDs registere...
void eraseNamedMetadata(NamedMDNode *NMD)
Remove the given NamedMDNode from this module and delete it.
Definition: Module.cpp:272
unsigned getInstructionCount()
Returns the number of non-debug IR instructions in the module.
Definition: Module.cpp:475
This file contains the simple types necessary to represent the attributes associated with functions a...
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
Key
PAL metadata keys.
Class to represent function types.
Definition: DerivedTypes.h:102
void setRtLibUseGOT()
Set that PLT should be avoid for RTLib calls.
Definition: Module.cpp:555
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:250
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:113
void dropAllReferences()
This function causes all the subinstructions to "let go" of all references that they are maintaining...
Definition: Module.cpp:439
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:409
iterator_range< op_iterator > operands()
Definition: Metadata.h:1417
Class to represent pointers.
Definition: DerivedTypes.h:498
unsigned getCodeViewFlag() const
Returns the CodeView Version by checking module flags.
Definition: Module.cpp:468
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:117
void getOperandBundleTags(SmallVectorImpl< StringRef > &Result) const
Populate client supplied SmallVector with the bundle tags registered in this LLVMContext.
Definition: Module.cpp:130
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1782
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1165
#define P(N)
Module(StringRef ModuleID, LLVMContext &C)
The Module constructor.
Definition: Module.cpp:73
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:135
PIELevel::Level getPIELevel() const
Returns the PIE level (small or large model)
Definition: Module.cpp:502
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:310
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void getOperandBundleTags(SmallVectorImpl< StringRef > &Result) const
getOperandBundleTags - Populate client supplied SmallVector with the bundle tags registered in this L...
ModFlagBehavior
This enumeration defines the supported behaviors of module flags.
Definition: Module.h:113
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:67
void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val)
Add a module-level flag to the module-level flags metadata.
Definition: Module.cpp:337
unsigned getAddressSpace() const
Definition: Globals.cpp:110
void setCodeModel(CodeModel::Model CL)
Set the code model (tiny, small, kernel, medium or large)
Definition: Module.cpp:526
op_range operands()
Definition: User.h:237
self_iterator getIterator()
Definition: ilist_node.h:81
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:482
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:210
unsigned getDwarfVersion() const
Returns the Dwarf Version by checking module flags.
Definition: Module.cpp:461
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:70
std::vector< StructType * > getIdentifiedStructTypes() const
Definition: Module.cpp:418
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue *> &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:598
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:31
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
Module.h This file contains the declarations for the Module class.
static bool isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB)
Checks if Metadata represents a valid ModFlagBehavior, and stores the converted result in MFB...
Definition: Module.cpp:277
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
NamedMDNode * getOrInsertModuleFlagsMetadata()
Returns the NamedMDNode in the module that represents module-level flags.
Definition: Module.cpp:330
void dropAllReferences()
dropAllReferences() - This method causes all the subinstructions to "let go" of all references that t...
Definition: Function.cpp:345
unsigned getProgramAddressSpace() const
Definition: DataLayout.h:282
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:631
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
iterator end()
Definition: TypeFinder.h:50
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition: Function.h:226
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:365
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
ConstantArray - Constant Array Declarations.
Definition: Constants.h:413
void push_back(pointer val)
Definition: ilist.h:311
StringRef getName() const
Definition: Metadata.cpp:1097
bool getRtLibUseGOT() const
Returns true if PLT should be avoided for RTLib calls.
Definition: Module.cpp:550
DICompileUnit * operator*() const
Definition: Module.cpp:371
A random number generator.
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
Constant * getOrInsertGlobal(StringRef Name, Type *Ty, function_ref< GlobalVariable *()> CreateGlobalCallback)
Look up the specified global in the module symbol table.
Definition: Module.cpp:204
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:564
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:175
void clear()
Definition: ilist.h:307
void setSDKVersion(const VersionTuple &V)
Attach a build SDK version metadata to this module.
Definition: Module.cpp:559
std::unique_ptr< RandomNumberGenerator > createRNG(const Pass *P) const
Get a RandomNumberGenerator salted for use with this module.
Definition: Module.cpp:92
llvm::Error materialize(GlobalValue *GV)
Make sure the GlobalValue is fully read.
Definition: Module.cpp:394
~Module()
The module destructor. This will dropAllReferences.
Definition: Module.cpp:80
void setPIELevel(PIELevel::Level PL)
Set the PIE level (small or large model)
Definition: Module.cpp:512
iterator_range< ifunc_iterator > ifuncs()
Definition: Module.h:645
void setOwnedMemoryBuffer(std::unique_ptr< MemoryBuffer > MB)
Take ownership of the given memory buffer.
Definition: Module.cpp:546
Value * lookup(StringRef Name) const
This method finds the value with the given Name in the the symbol table.
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
Populate client supplied SmallVector with the name for custom metadata IDs registered in this LLVMCon...
Definition: Module.cpp:126
unsigned getMDKindID(StringRef Name) const
Return a unique non-zero ID for the specified metadata kind.
Definition: Module.cpp:119
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
unsigned getNumberRegisterParameters() const
Returns the Number of Register ParametersDwarf Version by checking module flags.
Definition: Module.cpp:453
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
iterator begin()
Definition: TypeFinder.h:49
Defines the llvm::VersionTuple class, which represents a version in the form major[.minor[.subminor]].
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:702
void setPICLevel(PICLevel::Level PL)
Set the PIC level (small or large model)
Definition: Module.cpp:498
PICLevel::Level getPICLevel() const
Returns the PIC level (small or large model)
Definition: Module.cpp:488
iterator_range< global_iterator > globals()
Definition: Module.h:587
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A single uniqued string.
Definition: Metadata.h:603
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition: VersionTuple.h:77
VersionTuple getSDKVersion() const
Get the build SDK version metadata.
Definition: Module.cpp:573
void setProfileSummary(Metadata *M, ProfileSummary::Kind Kind)
Attach profile summary metadata to this module.
Definition: Module.cpp:534
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1074
GlobalIFunc * getNamedIFunc(StringRef Name) const
Return the global ifunc in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:243
TypeFinder - Walk over a module, identifying all of the types that are used by the module...
Definition: TypeFinder.h:30
Root of the metadata hierarchy.
Definition: Metadata.h:57
llvm::Error materializeMetadata()
Definition: Module.cpp:408
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:273
IntegerType * Int32Ty
iterator_range< alias_iterator > aliases()
Definition: Module.h:627