Module.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. /// @file
  11. /// Module.h This file contains the declarations for the Module class.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_MODULE_H
  15. #define LLVM_IR_MODULE_H
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/IR/Comdat.h"
  18. #include "llvm/IR/DataLayout.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/GlobalAlias.h"
  21. #include "llvm/IR/GlobalVariable.h"
  22. #include "llvm/IR/Metadata.h"
  23. #include "llvm/Support/CBindingWrapping.h"
  24. #include "llvm/Support/CodeGen.h"
  25. #include "llvm/Support/DataTypes.h"
  26. #include <system_error>
  27. // HLSL Change start
  28. namespace hlsl {
  29. class DxilModule;
  30. class HLModule;
  31. }
  32. // HLSL Change end
  33. namespace llvm {
  34. class FunctionType;
  35. class GVMaterializer;
  36. class LLVMContext;
  37. class RandomNumberGenerator;
  38. class StructType;
  39. template<> struct ilist_traits<Function>
  40. : public SymbolTableListTraits<Function, Module> {
  41. // createSentinel is used to get hold of the node that marks the end of the
  42. // list... (same trick used here as in ilist_traits<Instruction>)
  43. Function *createSentinel() const {
  44. return static_cast<Function*>(&Sentinel);
  45. }
  46. static void destroySentinel(Function*) {}
  47. Function *provideInitialHead() const { return createSentinel(); }
  48. Function *ensureHead(Function*) const { return createSentinel(); }
  49. static void noteHead(Function*, Function*) {}
  50. private:
  51. mutable ilist_node<Function> Sentinel;
  52. };
  53. template<> struct ilist_traits<GlobalVariable>
  54. : public SymbolTableListTraits<GlobalVariable, Module> {
  55. // createSentinel is used to create a node that marks the end of the list.
  56. GlobalVariable *createSentinel() const {
  57. return static_cast<GlobalVariable*>(&Sentinel);
  58. }
  59. static void destroySentinel(GlobalVariable*) {}
  60. GlobalVariable *provideInitialHead() const { return createSentinel(); }
  61. GlobalVariable *ensureHead(GlobalVariable*) const { return createSentinel(); }
  62. static void noteHead(GlobalVariable*, GlobalVariable*) {}
  63. private:
  64. mutable ilist_node<GlobalVariable> Sentinel;
  65. };
  66. template<> struct ilist_traits<GlobalAlias>
  67. : public SymbolTableListTraits<GlobalAlias, Module> {
  68. // createSentinel is used to create a node that marks the end of the list.
  69. GlobalAlias *createSentinel() const {
  70. return static_cast<GlobalAlias*>(&Sentinel);
  71. }
  72. static void destroySentinel(GlobalAlias*) {}
  73. GlobalAlias *provideInitialHead() const { return createSentinel(); }
  74. GlobalAlias *ensureHead(GlobalAlias*) const { return createSentinel(); }
  75. static void noteHead(GlobalAlias*, GlobalAlias*) {}
  76. private:
  77. mutable ilist_node<GlobalAlias> Sentinel;
  78. };
  79. template<> struct ilist_traits<NamedMDNode>
  80. : public ilist_default_traits<NamedMDNode> {
  81. // createSentinel is used to get hold of a node that marks the end of
  82. // the list...
  83. NamedMDNode *createSentinel() const {
  84. return static_cast<NamedMDNode*>(&Sentinel);
  85. }
  86. static void destroySentinel(NamedMDNode*) {}
  87. NamedMDNode *provideInitialHead() const { return createSentinel(); }
  88. NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
  89. static void noteHead(NamedMDNode*, NamedMDNode*) {}
  90. void addNodeToList(NamedMDNode *) {}
  91. void removeNodeFromList(NamedMDNode *) {}
  92. private:
  93. mutable ilist_node<NamedMDNode> Sentinel;
  94. };
  95. /// A Module instance is used to store all the information related to an
  96. /// LLVM module. Modules are the top level container of all other LLVM
  97. /// Intermediate Representation (IR) objects. Each module directly contains a
  98. /// list of globals variables, a list of functions, a list of libraries (or
  99. /// other modules) this module depends on, a symbol table, and various data
  100. /// about the target's characteristics.
  101. ///
  102. /// A module maintains a GlobalValRefMap object that is used to hold all
  103. /// constant references to global variables in the module. When a global
  104. /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
  105. /// @brief The main container class for the LLVM Intermediate Representation.
  106. class Module {
  107. /// @name Types And Enumerations
  108. /// @{
  109. public:
  110. /// The type for the list of global variables.
  111. typedef iplist<GlobalVariable> GlobalListType;
  112. /// The type for the list of functions.
  113. typedef iplist<Function> FunctionListType;
  114. /// The type for the list of aliases.
  115. typedef iplist<GlobalAlias> AliasListType;
  116. /// The type for the list of named metadata.
  117. typedef ilist<NamedMDNode> NamedMDListType;
  118. /// The type of the comdat "symbol" table.
  119. typedef StringMap<Comdat> ComdatSymTabType;
  120. /// The Global Variable iterator.
  121. typedef GlobalListType::iterator global_iterator;
  122. /// The Global Variable constant iterator.
  123. typedef GlobalListType::const_iterator const_global_iterator;
  124. /// The Function iterators.
  125. typedef FunctionListType::iterator iterator;
  126. /// The Function constant iterator
  127. typedef FunctionListType::const_iterator const_iterator;
  128. /// The Function reverse iterator.
  129. typedef FunctionListType::reverse_iterator reverse_iterator;
  130. /// The Function constant reverse iterator.
  131. typedef FunctionListType::const_reverse_iterator const_reverse_iterator;
  132. /// The Global Alias iterators.
  133. typedef AliasListType::iterator alias_iterator;
  134. /// The Global Alias constant iterator
  135. typedef AliasListType::const_iterator const_alias_iterator;
  136. /// The named metadata iterators.
  137. typedef NamedMDListType::iterator named_metadata_iterator;
  138. /// The named metadata constant iterators.
  139. typedef NamedMDListType::const_iterator const_named_metadata_iterator;
  140. /// This enumeration defines the supported behaviors of module flags.
  141. enum ModFlagBehavior {
  142. /// Emits an error if two values disagree, otherwise the resulting value is
  143. /// that of the operands.
  144. Error = 1,
  145. /// Emits a warning if two values disagree. The result value will be the
  146. /// operand for the flag from the first module being linked.
  147. Warning = 2,
  148. /// Adds a requirement that another module flag be present and have a
  149. /// specified value after linking is performed. The value must be a metadata
  150. /// pair, where the first element of the pair is the ID of the module flag
  151. /// to be restricted, and the second element of the pair is the value the
  152. /// module flag should be restricted to. This behavior can be used to
  153. /// restrict the allowable results (via triggering of an error) of linking
  154. /// IDs with the **Override** behavior.
  155. Require = 3,
  156. /// Uses the specified value, regardless of the behavior or value of the
  157. /// other module. If both modules specify **Override**, but the values
  158. /// differ, an error will be emitted.
  159. Override = 4,
  160. /// Appends the two values, which are required to be metadata nodes.
  161. Append = 5,
  162. /// Appends the two values, which are required to be metadata
  163. /// nodes. However, duplicate entries in the second list are dropped
  164. /// during the append operation.
  165. AppendUnique = 6,
  166. // Markers:
  167. ModFlagBehaviorFirstVal = Error,
  168. ModFlagBehaviorLastVal = AppendUnique
  169. };
  170. /// Checks if Metadata represents a valid ModFlagBehavior, and stores the
  171. /// converted result in MFB.
  172. static bool isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB);
  173. struct ModuleFlagEntry {
  174. ModFlagBehavior Behavior;
  175. MDString *Key;
  176. Metadata *Val;
  177. ModuleFlagEntry(ModFlagBehavior B, MDString *K, Metadata *V)
  178. : Behavior(B), Key(K), Val(V) {}
  179. };
  180. /// @}
  181. /// @name Member Variables
  182. /// @{
  183. private:
  184. LLVMContext &Context; ///< The LLVMContext from which types and
  185. ///< constants are allocated.
  186. GlobalListType GlobalList; ///< The Global Variables in the module
  187. FunctionListType FunctionList; ///< The Functions in the module
  188. AliasListType AliasList; ///< The Aliases in the module
  189. NamedMDListType NamedMDList; ///< The named metadata in the module
  190. std::string GlobalScopeAsm; ///< Inline Asm at global scope.
  191. ValueSymbolTable *ValSymTab; ///< Symbol table for values
  192. ComdatSymTabType ComdatSymTab; ///< Symbol table for COMDATs
  193. std::unique_ptr<GVMaterializer>
  194. Materializer; ///< Used to materialize GlobalValues
  195. std::string ModuleID; ///< Human readable identifier for the module
  196. std::string TargetTriple; ///< Platform target triple Module compiled on
  197. ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
  198. void *NamedMDSymTab; ///< NamedMDNode names.
  199. DataLayout DL; ///< DataLayout associated with the module
  200. friend class Constant;
  201. // HLSL Change start
  202. hlsl::HLModule *TheHLModule = nullptr;
  203. hlsl::DxilModule *TheDxilModule = nullptr;
  204. // HLSL Change end
  205. /// @}
  206. /// @name Constructors
  207. /// @{
  208. public:
  209. /// The Module constructor. Note that there is no default constructor. You
  210. /// must provide a name for the module upon construction.
  211. explicit Module(StringRef ModuleID, LLVMContext& C);
  212. /// The module destructor. This will dropAllReferences.
  213. ~Module();
  214. /// @}
  215. /// @name Module Level Accessors
  216. /// @{
  217. /// Get the module identifier which is, essentially, the name of the module.
  218. /// @returns the module identifier as a string
  219. const std::string &getModuleIdentifier() const { return ModuleID; }
  220. /// \brief Get a short "name" for the module.
  221. ///
  222. /// This is useful for debugging or logging. It is essentially a convenience
  223. /// wrapper around getModuleIdentifier().
  224. StringRef getName() const { return ModuleID; }
  225. /// Get the data layout string for the module's target platform. This is
  226. /// equivalent to getDataLayout()->getStringRepresentation().
  227. const std::string &getDataLayoutStr() const {
  228. return DL.getStringRepresentation();
  229. }
  230. /// Get the data layout for the module's target platform.
  231. const DataLayout &getDataLayout() const;
  232. /// Get the target triple which is a string describing the target host.
  233. /// @returns a string containing the target triple.
  234. const std::string &getTargetTriple() const { return TargetTriple; }
  235. /// Get the global data context.
  236. /// @returns LLVMContext - a container for LLVM's global information
  237. LLVMContext &getContext() const { return Context; }
  238. /// Get any module-scope inline assembly blocks.
  239. /// @returns a string containing the module-scope inline assembly blocks.
  240. const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
  241. /// Get a RandomNumberGenerator salted for use with this module. The
  242. /// RNG can be seeded via -rng-seed=<uint64> and is salted with the
  243. /// ModuleID and the provided pass salt. The returned RNG should not
  244. /// be shared across threads or passes.
  245. ///
  246. /// A unique RNG per pass ensures a reproducible random stream even
  247. /// when other randomness consuming passes are added or removed. In
  248. /// addition, the random stream will be reproducible across LLVM
  249. /// versions when the pass does not change.
  250. RandomNumberGenerator *createRNG(const Pass* P) const;
  251. /// @}
  252. /// @name Module Level Mutators
  253. /// @{
  254. /// Set the module identifier.
  255. void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
  256. /// Set the data layout
  257. void setDataLayout(StringRef Desc);
  258. void setDataLayout(const DataLayout &Other);
  259. /// Set the target triple.
  260. void setTargetTriple(StringRef T) { TargetTriple = T; }
  261. /// Set the module-scope inline assembly blocks.
  262. /// A trailing newline is added if the input doesn't have one.
  263. void setModuleInlineAsm(StringRef Asm) {
  264. GlobalScopeAsm = Asm;
  265. if (!GlobalScopeAsm.empty() &&
  266. GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
  267. GlobalScopeAsm += '\n';
  268. }
  269. /// Append to the module-scope inline assembly blocks.
  270. /// A trailing newline is added if the input doesn't have one.
  271. void appendModuleInlineAsm(StringRef Asm) {
  272. GlobalScopeAsm += Asm;
  273. if (!GlobalScopeAsm.empty() &&
  274. GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
  275. GlobalScopeAsm += '\n';
  276. }
  277. /// @}
  278. /// @name Generic Value Accessors
  279. /// @{
  280. /// Return the global value in the module with the specified name, of
  281. /// arbitrary type. This method returns null if a global with the specified
  282. /// name is not found.
  283. GlobalValue *getNamedValue(StringRef Name) const;
  284. /// Return a unique non-zero ID for the specified metadata kind. This ID is
  285. /// uniqued across modules in the current LLVMContext.
  286. unsigned getMDKindID(StringRef Name) const;
  287. /// Populate client supplied SmallVector with the name for custom metadata IDs
  288. /// registered in this LLVMContext.
  289. void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
  290. /// Return the type with the specified name, or null if there is none by that
  291. /// name.
  292. StructType *getTypeByName(StringRef Name) const;
  293. std::vector<StructType *> getIdentifiedStructTypes() const;
  294. /// @}
  295. /// @name Function Accessors
  296. /// @{
  297. /// Look up the specified function in the module symbol table. Four
  298. /// possibilities:
  299. /// 1. If it does not exist, add a prototype for the function and return it.
  300. /// 2. If it exists, and has a local linkage, the existing function is
  301. /// renamed and a new one is inserted.
  302. /// 3. Otherwise, if the existing function has the correct prototype, return
  303. /// the existing function.
  304. /// 4. Finally, the function exists but has the wrong prototype: return the
  305. /// function with a constantexpr cast to the right prototype.
  306. Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
  307. AttributeSet AttributeList);
  308. Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
  309. /// Look up the specified function in the module symbol table. If it does not
  310. /// exist, add a prototype for the function and return it. This function
  311. /// guarantees to return a constant of pointer to the specified function type
  312. /// or a ConstantExpr BitCast of that type if the named function has a
  313. /// different type. This version of the method takes a null terminated list of
  314. /// function arguments, which makes it easier for clients to use.
  315. Constant *getOrInsertFunction(StringRef Name,
  316. AttributeSet AttributeList,
  317. Type *RetTy, ...) LLVM_END_WITH_NULL;
  318. /// Same as above, but without the attributes.
  319. Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
  320. LLVM_END_WITH_NULL;
  321. /// Look up the specified function in the module symbol table. If it does not
  322. /// exist, return null.
  323. Function *getFunction(StringRef Name) const;
  324. /// @}
  325. /// @name Global Variable Accessors
  326. /// @{
  327. /// Look up the specified global variable in the module symbol table. If it
  328. /// does not exist, return null. If AllowInternal is set to true, this
  329. /// function will return types that have InternalLinkage. By default, these
  330. /// types are not returned.
  331. GlobalVariable *getGlobalVariable(StringRef Name) const {
  332. return getGlobalVariable(Name, false);
  333. }
  334. GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const {
  335. return const_cast<Module *>(this)->getGlobalVariable(Name, AllowInternal);
  336. }
  337. GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal = false);
  338. /// Return the global variable in the module with the specified name, of
  339. /// arbitrary type. This method returns null if a global with the specified
  340. /// name is not found.
  341. GlobalVariable *getNamedGlobal(StringRef Name) {
  342. return getGlobalVariable(Name, true);
  343. }
  344. const GlobalVariable *getNamedGlobal(StringRef Name) const {
  345. return const_cast<Module *>(this)->getNamedGlobal(Name);
  346. }
  347. /// Look up the specified global in the module symbol table.
  348. /// 1. If it does not exist, add a declaration of the global and return it.
  349. /// 2. Else, the global exists but has the wrong type: return the function
  350. /// with a constantexpr cast to the right type.
  351. /// 3. Finally, if the existing global is the correct declaration, return
  352. /// the existing global.
  353. Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
  354. /// @}
  355. /// @name Global Alias Accessors
  356. /// @{
  357. /// Return the global alias in the module with the specified name, of
  358. /// arbitrary type. This method returns null if a global with the specified
  359. /// name is not found.
  360. GlobalAlias *getNamedAlias(StringRef Name) const;
  361. /// @}
  362. /// @name Named Metadata Accessors
  363. /// @{
  364. /// Return the first NamedMDNode in the module with the specified name. This
  365. /// method returns null if a NamedMDNode with the specified name is not found.
  366. NamedMDNode *getNamedMetadata(const Twine &Name) const;
  367. /// Return the named MDNode in the module with the specified name. This method
  368. /// returns a new NamedMDNode if a NamedMDNode with the specified name is not
  369. /// found.
  370. NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
  371. /// Remove the given NamedMDNode from this module and delete it.
  372. void eraseNamedMetadata(NamedMDNode *NMD);
  373. /// @}
  374. /// @name Comdat Accessors
  375. /// @{
  376. /// Return the Comdat in the module with the specified name. It is created
  377. /// if it didn't already exist.
  378. Comdat *getOrInsertComdat(StringRef Name);
  379. /// @}
  380. /// @name Module Flags Accessors
  381. /// @{
  382. /// Returns the module flags in the provided vector.
  383. void getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const;
  384. /// Return the corresponding value if Key appears in module flags, otherwise
  385. /// return null.
  386. Metadata *getModuleFlag(StringRef Key) const;
  387. /// Returns the NamedMDNode in the module that represents module-level flags.
  388. /// This method returns null if there are no module-level flags.
  389. NamedMDNode *getModuleFlagsMetadata() const;
  390. /// Returns the NamedMDNode in the module that represents module-level flags.
  391. /// If module-level flags aren't found, it creates the named metadata that
  392. /// contains them.
  393. NamedMDNode *getOrInsertModuleFlagsMetadata();
  394. /// Add a module-level flag to the module-level flags metadata. It will create
  395. /// the module-level flags named metadata if it doesn't already exist.
  396. void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
  397. void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
  398. void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
  399. void addModuleFlag(MDNode *Node);
  400. /// @}
  401. /// @name Materialization
  402. /// @{
  403. /// Sets the GVMaterializer to GVM. This module must not yet have a
  404. /// Materializer. To reset the materializer for a module that already has one,
  405. /// call MaterializeAllPermanently first. Destroying this module will destroy
  406. /// its materializer without materializing any more GlobalValues. Without
  407. /// destroying the Module, there is no way to detach or destroy a materializer
  408. /// without materializing all the GVs it controls, to avoid leaving orphan
  409. /// unmaterialized GVs.
  410. void setMaterializer(GVMaterializer *GVM);
  411. /// Retrieves the GVMaterializer, if any, for this Module.
  412. GVMaterializer *getMaterializer() const { return Materializer.get(); }
  413. /// Returns true if this GV was loaded from this Module's GVMaterializer and
  414. /// the GVMaterializer knows how to dematerialize the GV.
  415. bool isDematerializable(const GlobalValue *GV) const;
  416. /// Make sure the GlobalValue is fully read. If the module is corrupt, this
  417. /// returns true and fills in the optional string with information about the
  418. /// problem. If successful, this returns false.
  419. std::error_code materialize(GlobalValue *GV);
  420. /// If the GlobalValue is read in, and if the GVMaterializer supports it,
  421. /// release the memory for the function, and set it up to be materialized
  422. /// lazily. If !isDematerializable(), this method is a no-op.
  423. void dematerialize(GlobalValue *GV);
  424. /// Make sure all GlobalValues in this Module are fully read.
  425. std::error_code materializeAll();
  426. /// Make sure all GlobalValues in this Module are fully read and clear the
  427. /// Materializer. If the module is corrupt, this DOES NOT clear the old
  428. /// Materializer.
  429. std::error_code materializeAllPermanently();
  430. std::error_code materializeMetadata();
  431. std::error_code materializeSelectNamedMetadata(ArrayRef<StringRef> NamedMetadata); // HLSL Change
  432. /// @}
  433. /// @name Direct access to the globals list, functions list, and symbol table
  434. /// @{
  435. /// Get the Module's list of global variables (constant).
  436. const GlobalListType &getGlobalList() const { return GlobalList; }
  437. /// Get the Module's list of global variables.
  438. GlobalListType &getGlobalList() { return GlobalList; }
  439. static GlobalListType Module::*getSublistAccess(GlobalVariable*) {
  440. return &Module::GlobalList;
  441. }
  442. /// Get the Module's list of functions (constant).
  443. const FunctionListType &getFunctionList() const { return FunctionList; }
  444. /// Get the Module's list of functions.
  445. FunctionListType &getFunctionList() { return FunctionList; }
  446. static FunctionListType Module::*getSublistAccess(Function*) {
  447. return &Module::FunctionList;
  448. }
  449. /// Get the Module's list of aliases (constant).
  450. const AliasListType &getAliasList() const { return AliasList; }
  451. /// Get the Module's list of aliases.
  452. AliasListType &getAliasList() { return AliasList; }
  453. static AliasListType Module::*getSublistAccess(GlobalAlias*) {
  454. return &Module::AliasList;
  455. }
  456. /// Get the Module's list of named metadata (constant).
  457. const NamedMDListType &getNamedMDList() const { return NamedMDList; }
  458. /// Get the Module's list of named metadata.
  459. NamedMDListType &getNamedMDList() { return NamedMDList; }
  460. static NamedMDListType Module::*getSublistAccess(NamedMDNode*) {
  461. return &Module::NamedMDList;
  462. }
  463. /// Get the symbol table of global variable and function identifiers
  464. const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
  465. /// Get the Module's symbol table of global variable and function identifiers.
  466. ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
  467. /// Get the Module's symbol table for COMDATs (constant).
  468. const ComdatSymTabType &getComdatSymbolTable() const { return ComdatSymTab; }
  469. /// Get the Module's symbol table for COMDATs.
  470. ComdatSymTabType &getComdatSymbolTable() { return ComdatSymTab; }
  471. /// @}
  472. /// @name Global Variable Iteration
  473. /// @{
  474. global_iterator global_begin() { return GlobalList.begin(); }
  475. const_global_iterator global_begin() const { return GlobalList.begin(); }
  476. global_iterator global_end () { return GlobalList.end(); }
  477. const_global_iterator global_end () const { return GlobalList.end(); }
  478. bool global_empty() const { return GlobalList.empty(); }
  479. iterator_range<global_iterator> globals() {
  480. return iterator_range<global_iterator>(global_begin(), global_end());
  481. }
  482. iterator_range<const_global_iterator> globals() const {
  483. return iterator_range<const_global_iterator>(global_begin(), global_end());
  484. }
  485. /// @}
  486. /// @name Function Iteration
  487. /// @{
  488. iterator begin() { return FunctionList.begin(); }
  489. const_iterator begin() const { return FunctionList.begin(); }
  490. iterator end () { return FunctionList.end(); }
  491. const_iterator end () const { return FunctionList.end(); }
  492. reverse_iterator rbegin() { return FunctionList.rbegin(); }
  493. const_reverse_iterator rbegin() const{ return FunctionList.rbegin(); }
  494. reverse_iterator rend() { return FunctionList.rend(); }
  495. const_reverse_iterator rend() const { return FunctionList.rend(); }
  496. size_t size() const { return FunctionList.size(); }
  497. bool empty() const { return FunctionList.empty(); }
  498. iterator_range<iterator> functions() {
  499. return iterator_range<iterator>(begin(), end());
  500. }
  501. iterator_range<const_iterator> functions() const {
  502. return iterator_range<const_iterator>(begin(), end());
  503. }
  504. /// @}
  505. /// @name Alias Iteration
  506. /// @{
  507. alias_iterator alias_begin() { return AliasList.begin(); }
  508. const_alias_iterator alias_begin() const { return AliasList.begin(); }
  509. alias_iterator alias_end () { return AliasList.end(); }
  510. const_alias_iterator alias_end () const { return AliasList.end(); }
  511. size_t alias_size () const { return AliasList.size(); }
  512. bool alias_empty() const { return AliasList.empty(); }
  513. iterator_range<alias_iterator> aliases() {
  514. return iterator_range<alias_iterator>(alias_begin(), alias_end());
  515. }
  516. iterator_range<const_alias_iterator> aliases() const {
  517. return iterator_range<const_alias_iterator>(alias_begin(), alias_end());
  518. }
  519. /// @}
  520. /// @name Named Metadata Iteration
  521. /// @{
  522. named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
  523. const_named_metadata_iterator named_metadata_begin() const {
  524. return NamedMDList.begin();
  525. }
  526. named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
  527. const_named_metadata_iterator named_metadata_end() const {
  528. return NamedMDList.end();
  529. }
  530. size_t named_metadata_size() const { return NamedMDList.size(); }
  531. bool named_metadata_empty() const { return NamedMDList.empty(); }
  532. iterator_range<named_metadata_iterator> named_metadata() {
  533. return iterator_range<named_metadata_iterator>(named_metadata_begin(),
  534. named_metadata_end());
  535. }
  536. iterator_range<const_named_metadata_iterator> named_metadata() const {
  537. return iterator_range<const_named_metadata_iterator>(named_metadata_begin(),
  538. named_metadata_end());
  539. }
  540. /// Destroy ConstantArrays in LLVMContext if they are not used.
  541. /// ConstantArrays constructed during linking can cause quadratic memory
  542. /// explosion. Releasing all unused constants can cause a 20% LTO compile-time
  543. /// slowdown for a large application.
  544. ///
  545. /// NOTE: Constants are currently owned by LLVMContext. This can then only
  546. /// be called where all uses of the LLVMContext are understood.
  547. void dropTriviallyDeadConstantArrays();
  548. /// @}
  549. /// @name Utility functions for printing and dumping Module objects
  550. /// @{
  551. /// Print the module to an output stream with an optional
  552. /// AssemblyAnnotationWriter. If \c ShouldPreserveUseListOrder, then include
  553. /// uselistorder directives so that use-lists can be recreated when reading
  554. /// the assembly.
  555. void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW,
  556. bool ShouldPreserveUseListOrder = false) const;
  557. /// Dump the module to stderr (for debugging).
  558. void dump() const;
  559. /// This function causes all the subinstructions to "let go" of all references
  560. /// that they are maintaining. This allows one to 'delete' a whole class at
  561. /// a time, even though there may be circular references... first all
  562. /// references are dropped, and all use counts go to zero. Then everything
  563. /// is delete'd for real. Note that no operations are valid on an object
  564. /// that has "dropped all references", except operator delete.
  565. void dropAllReferences();
  566. /// @}
  567. /// @name Utility functions for querying Debug information.
  568. /// @{
  569. /// \brief Returns the Dwarf Version by checking module flags.
  570. unsigned getDwarfVersion() const;
  571. /// @}
  572. /// @name Utility functions for querying and setting PIC level
  573. /// @{
  574. /// \brief Returns the PIC level (small or large model)
  575. PICLevel::Level getPICLevel() const;
  576. /// \brief Set the PIC level (small or large model)
  577. void setPICLevel(PICLevel::Level PL);
  578. /// @}
  579. // HLSL Change start
  580. typedef void (*RemoveGlobalCallback)(llvm::Module*, llvm::GlobalObject*);
  581. typedef void(*ResetModuleCallback)(llvm::Module*);
  582. RemoveGlobalCallback pfnRemoveGlobal = nullptr;
  583. void CallRemoveGlobalHook(llvm::GlobalObject* G) {
  584. if (pfnRemoveGlobal) (*pfnRemoveGlobal)(this, G);
  585. }
  586. bool HasHLModule() const { return TheHLModule != nullptr; }
  587. void SetHLModule(hlsl::HLModule *pValue) { TheHLModule = pValue; }
  588. hlsl::HLModule &GetHLModule() const { return *TheHLModule; }
  589. hlsl::HLModule &GetOrCreateHLModule(bool skipInit = false);
  590. ResetModuleCallback pfnResetHLModule = nullptr;
  591. void ResetHLModule() { if (pfnResetHLModule) (*pfnResetHLModule)(this); }
  592. bool HasDxilModule() const { return TheDxilModule != nullptr; }
  593. void SetDxilModule(hlsl::DxilModule *pValue) { TheDxilModule = pValue; }
  594. hlsl::DxilModule &GetDxilModule() const { return *TheDxilModule; }
  595. hlsl::DxilModule &GetOrCreateDxilModule(bool skipInit = false);
  596. ResetModuleCallback pfnResetDxilModule = nullptr;
  597. void ResetDxilModule() { if (pfnResetDxilModule) (*pfnResetDxilModule)(this); }
  598. // HLSL Change end
  599. };
  600. /// An raw_ostream inserter for modules.
  601. inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
  602. M.print(O, nullptr);
  603. return O;
  604. }
  605. // Create wrappers for C Binding types (see CBindingWrapping.h).
  606. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef)
  607. /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
  608. * Module.
  609. */
  610. inline Module *unwrap(LLVMModuleProviderRef MP) {
  611. return reinterpret_cast<Module*>(MP);
  612. }
  613. } // End llvm namespace
  614. #endif