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