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