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