Modernize the error handling of the Materialize function.
[oota-llvm.git] / include / llvm / IR / GlobalValue.h
1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- 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 // This file is a common base class of all globally definable objects.  As such,
11 // it is subclassed by GlobalVariable, GlobalAlias and by Function.  This is
12 // used because you can do certain things with these global objects that you
13 // can't do to anything else.  For example, use the address of one as a
14 // constant.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_IR_GLOBALVALUE_H
19 #define LLVM_IR_GLOBALVALUE_H
20
21 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/DerivedTypes.h"
23
24 #include <system_error>
25
26 namespace llvm {
27
28 class Comdat;
29 class PointerType;
30 class Module;
31
32 class GlobalValue : public Constant {
33   GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
34 public:
35   /// @brief An enumeration for the kinds of linkage for global values.
36   enum LinkageTypes {
37     ExternalLinkage = 0,///< Externally visible function
38     AvailableExternallyLinkage, ///< Available for inspection, not emission.
39     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
40     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
41     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
42     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
43     AppendingLinkage,   ///< Special purpose, only applies to global arrays
44     InternalLinkage,    ///< Rename collisions when linking (static functions).
45     PrivateLinkage,     ///< Like Internal, but omit from symbol table.
46     ExternalWeakLinkage,///< ExternalWeak linkage description.
47     CommonLinkage       ///< Tentative definitions.
48   };
49
50   /// @brief An enumeration for the kinds of visibility of global values.
51   enum VisibilityTypes {
52     DefaultVisibility = 0,  ///< The GV is visible
53     HiddenVisibility,       ///< The GV is hidden
54     ProtectedVisibility     ///< The GV is protected
55   };
56
57   /// @brief Storage classes of global values for PE targets.
58   enum DLLStorageClassTypes {
59     DefaultStorageClass   = 0,
60     DLLImportStorageClass = 1, ///< Function to be imported from DLL
61     DLLExportStorageClass = 2  ///< Function to be accessible from DLL.
62   };
63
64 protected:
65   GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
66               LinkageTypes Linkage, const Twine &Name)
67       : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
68         Visibility(DefaultVisibility), UnnamedAddr(0),
69         DllStorageClass(DefaultStorageClass),
70         ThreadLocal(NotThreadLocal), Parent(nullptr) {
71     setName(Name);
72   }
73
74   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
75   // Linkage and Visibility from turning into negative values.
76   LinkageTypes Linkage : 5;   // The linkage of this global
77   unsigned Visibility : 2;    // The visibility style of this global
78   unsigned UnnamedAddr : 1;   // This value's address is not significant
79   unsigned DllStorageClass : 2; // DLL storage class
80
81   unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
82                             // the desired model?
83
84 private:
85   // Give subclasses access to what otherwise would be wasted padding.
86   // (19 + 3 + 2 + 1 + 2 + 5) == 32.
87   unsigned SubClassData : 19;
88 protected:
89   static const unsigned GlobalValueSubClassDataBits = 19;
90   unsigned getGlobalValueSubClassData() const {
91     return SubClassData;
92   }
93   void setGlobalValueSubClassData(unsigned V) {
94     assert(V < (1 << 19) && "It will not fit");
95     SubClassData = V;
96   }
97
98   Module *Parent;             // The containing module.
99 public:
100   enum ThreadLocalMode {
101     NotThreadLocal = 0,
102     GeneralDynamicTLSModel,
103     LocalDynamicTLSModel,
104     InitialExecTLSModel,
105     LocalExecTLSModel
106   };
107
108   ~GlobalValue() {
109     removeDeadConstantUsers();   // remove any dead constants using this.
110   }
111
112   unsigned getAlignment() const;
113
114   bool hasUnnamedAddr() const { return UnnamedAddr; }
115   void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
116
117   bool hasComdat() const { return getComdat() != nullptr; }
118   Comdat *getComdat();
119   const Comdat *getComdat() const {
120     return const_cast<GlobalValue *>(this)->getComdat();
121   }
122
123   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
124   bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
125   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
126   bool hasProtectedVisibility() const {
127     return Visibility == ProtectedVisibility;
128   }
129   void setVisibility(VisibilityTypes V) {
130     assert((!hasLocalLinkage() || V == DefaultVisibility) &&
131            "local linkage requires default visibility");
132     Visibility = V;
133   }
134
135   /// If the value is "Thread Local", its value isn't shared by the threads.
136   bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
137   void setThreadLocal(bool Val) {
138     setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
139   }
140   void setThreadLocalMode(ThreadLocalMode Val) {
141     assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
142     ThreadLocal = Val;
143   }
144   ThreadLocalMode getThreadLocalMode() const {
145     return static_cast<ThreadLocalMode>(ThreadLocal);
146   }
147
148   DLLStorageClassTypes getDLLStorageClass() const {
149     return DLLStorageClassTypes(DllStorageClass);
150   }
151   bool hasDLLImportStorageClass() const {
152     return DllStorageClass == DLLImportStorageClass;
153   }
154   bool hasDLLExportStorageClass() const {
155     return DllStorageClass == DLLExportStorageClass;
156   }
157   void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
158
159   bool hasSection() const { return !StringRef(getSection()).empty(); }
160   // It is unfortunate that we have to use "char *" in here since this is
161   // always non NULL, but:
162   // * The C API expects a null terminated string, so we cannot use StringRef.
163   // * The C API expects us to own it, so we cannot use a std:string.
164   // * For GlobalAliases we can fail to find the section and we have to
165   //   return "", so we cannot use a "const std::string &".
166   const char *getSection() const;
167
168   /// Global values are always pointers.
169   inline PointerType *getType() const {
170     return cast<PointerType>(User::getType());
171   }
172
173   static LinkageTypes getLinkOnceLinkage(bool ODR) {
174     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
175   }
176   static LinkageTypes getWeakLinkage(bool ODR) {
177     return ODR ? WeakODRLinkage : WeakAnyLinkage;
178   }
179
180   static bool isExternalLinkage(LinkageTypes Linkage) {
181     return Linkage == ExternalLinkage;
182   }
183   static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
184     return Linkage == AvailableExternallyLinkage;
185   }
186   static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
187     return Linkage == LinkOnceODRLinkage;
188   }
189   static bool isLinkOnceLinkage(LinkageTypes Linkage) {
190     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
191   }
192   static bool isWeakAnyLinkage(LinkageTypes Linkage) {
193     return Linkage == WeakAnyLinkage;
194   }
195   static bool isWeakODRLinkage(LinkageTypes Linkage) {
196     return Linkage == WeakODRLinkage;
197   }
198   static bool isWeakLinkage(LinkageTypes Linkage) {
199     return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
200   }
201   static bool isAppendingLinkage(LinkageTypes Linkage) {
202     return Linkage == AppendingLinkage;
203   }
204   static bool isInternalLinkage(LinkageTypes Linkage) {
205     return Linkage == InternalLinkage;
206   }
207   static bool isPrivateLinkage(LinkageTypes Linkage) {
208     return Linkage == PrivateLinkage;
209   }
210   static bool isLocalLinkage(LinkageTypes Linkage) {
211     return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
212   }
213   static bool isExternalWeakLinkage(LinkageTypes Linkage) {
214     return Linkage == ExternalWeakLinkage;
215   }
216   static bool isCommonLinkage(LinkageTypes Linkage) {
217     return Linkage == CommonLinkage;
218   }
219
220   /// Whether the definition of this global may be discarded if it is not used
221   /// in its compilation unit.
222   static bool isDiscardableIfUnused(LinkageTypes Linkage) {
223     return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
224   }
225
226   /// Whether the definition of this global may be replaced by something
227   /// non-equivalent at link time. For example, if a function has weak linkage
228   /// then the code defining it may be replaced by different code.
229   static bool mayBeOverridden(LinkageTypes Linkage) {
230     return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
231            Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
232   }
233
234   /// Whether the definition of this global may be replaced at link time.  NB:
235   /// Using this method outside of the code generators is almost always a
236   /// mistake: when working at the IR level use mayBeOverridden instead as it
237   /// knows about ODR semantics.
238   static bool isWeakForLinker(LinkageTypes Linkage)  {
239     return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
240            Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
241            Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
242            Linkage == ExternalWeakLinkage;
243   }
244
245   bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
246   bool hasAvailableExternallyLinkage() const {
247     return isAvailableExternallyLinkage(Linkage);
248   }
249   bool hasLinkOnceLinkage() const {
250     return isLinkOnceLinkage(Linkage);
251   }
252   bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); }
253   bool hasWeakLinkage() const {
254     return isWeakLinkage(Linkage);
255   }
256   bool hasWeakAnyLinkage() const {
257     return isWeakAnyLinkage(Linkage);
258   }
259   bool hasWeakODRLinkage() const {
260     return isWeakODRLinkage(Linkage);
261   }
262   bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
263   bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
264   bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
265   bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
266   bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
267   bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
268
269   void setLinkage(LinkageTypes LT) {
270     if (isLocalLinkage(LT))
271       Visibility = DefaultVisibility;
272     Linkage = LT;
273   }
274   LinkageTypes getLinkage() const { return Linkage; }
275
276   bool isDiscardableIfUnused() const {
277     return isDiscardableIfUnused(Linkage);
278   }
279
280   bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
281
282   bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
283
284   /// Copy all additional attributes (those not needed to create a GlobalValue)
285   /// from the GlobalValue Src to this one.
286   virtual void copyAttributesFrom(const GlobalValue *Src);
287
288   /// If special LLVM prefix that is used to inform the asm printer to not emit
289   /// usual symbol prefix before the symbol name is used then return linkage
290   /// name after skipping this special LLVM prefix.
291   static StringRef getRealLinkageName(StringRef Name) {
292     if (!Name.empty() && Name[0] == '\1')
293       return Name.substr(1);
294     return Name;
295   }
296
297 /// @name Materialization
298 /// Materialization is used to construct functions only as they're needed. This
299 /// is useful to reduce memory usage in LLVM or parsing work done by the
300 /// BitcodeReader to load the Module.
301 /// @{
302
303   /// If this function's Module is being lazily streamed in functions from disk
304   /// or some other source, this method can be used to check to see if the
305   /// function has been read in yet or not.
306   bool isMaterializable() const;
307
308   /// Returns true if this function was loaded from a GVMaterializer that's
309   /// still attached to its Module and that knows how to dematerialize the
310   /// function.
311   bool isDematerializable() const;
312
313   /// Make sure this GlobalValue is fully read. If the module is corrupt, this
314   /// returns true and fills in the optional string with information about the
315   /// problem.  If successful, this returns false.
316   std::error_code materialize();
317
318   /// If this GlobalValue is read in, and if the GVMaterializer supports it,
319   /// release the memory for the function, and set it up to be materialized
320   /// lazily. If !isDematerializable(), this method is a noop.
321   void Dematerialize();
322
323 /// @}
324
325   /// Override from Constant class.
326   void destroyConstant() override;
327
328   /// Return true if the primary definition of this global value is outside of
329   /// the current translation unit.
330   bool isDeclaration() const;
331
332   bool isDeclarationForLinker() const {
333     if (hasAvailableExternallyLinkage())
334       return true;
335
336     return isDeclaration();
337   }
338
339   /// This method unlinks 'this' from the containing module, but does not delete
340   /// it.
341   virtual void removeFromParent() = 0;
342
343   /// This method unlinks 'this' from the containing module and deletes it.
344   virtual void eraseFromParent() = 0;
345
346   /// Get the module that this global value is contained inside of...
347   inline Module *getParent() { return Parent; }
348   inline const Module *getParent() const { return Parent; }
349
350   const DataLayout *getDataLayout() const;
351
352   // Methods for support type inquiry through isa, cast, and dyn_cast:
353   static inline bool classof(const Value *V) {
354     return V->getValueID() == Value::FunctionVal ||
355            V->getValueID() == Value::GlobalVariableVal ||
356            V->getValueID() == Value::GlobalAliasVal;
357   }
358 };
359
360 } // End llvm namespace
361
362 #endif