[C++11] More 'nullptr' conversion or in some cases just using a boolean check instead...
[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 namespace llvm {
25
26 class PointerType;
27 class Module;
28
29 class GlobalValue : public Constant {
30   GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
31 public:
32   /// @brief An enumeration for the kinds of linkage for global values.
33   enum LinkageTypes {
34     ExternalLinkage = 0,///< Externally visible function
35     AvailableExternallyLinkage, ///< Available for inspection, not emission.
36     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
37     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
38     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
39     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
40     AppendingLinkage,   ///< Special purpose, only applies to global arrays
41     InternalLinkage,    ///< Rename collisions when linking (static functions).
42     PrivateLinkage,     ///< Like Internal, but omit from symbol table.
43     ExternalWeakLinkage,///< ExternalWeak linkage description.
44     CommonLinkage       ///< Tentative definitions.
45   };
46
47   /// @brief An enumeration for the kinds of visibility of global values.
48   enum VisibilityTypes {
49     DefaultVisibility = 0,  ///< The GV is visible
50     HiddenVisibility,       ///< The GV is hidden
51     ProtectedVisibility     ///< The GV is protected
52   };
53
54   /// @brief Storage classes of global values for PE targets.
55   enum DLLStorageClassTypes {
56     DefaultStorageClass   = 0,
57     DLLImportStorageClass = 1, ///< Function to be imported from DLL
58     DLLExportStorageClass = 2  ///< Function to be accessible from DLL.
59   };
60
61 protected:
62   GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
63               LinkageTypes linkage, const Twine &Name)
64     : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
65       Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0),
66       DllStorageClass(DefaultStorageClass), Parent(nullptr) {
67     setName(Name);
68   }
69
70   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
71   // Linkage and Visibility from turning into negative values.
72   LinkageTypes Linkage : 5;   // The linkage of this global
73   unsigned Visibility : 2;    // The visibility style of this global
74   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
75   unsigned UnnamedAddr : 1;   // This value's address is not significant
76   unsigned DllStorageClass : 2; // DLL storage class
77   Module *Parent;             // The containing module.
78   std::string Section;        // Section to emit this into, empty mean default
79 public:
80   ~GlobalValue() {
81     removeDeadConstantUsers();   // remove any dead constants using this.
82   }
83
84   unsigned getAlignment() const {
85     return (1u << Alignment) >> 1;
86   }
87   void setAlignment(unsigned Align);
88
89   bool hasUnnamedAddr() const { return UnnamedAddr; }
90   void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
91
92   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
93   bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
94   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
95   bool hasProtectedVisibility() const {
96     return Visibility == ProtectedVisibility;
97   }
98   void setVisibility(VisibilityTypes V) { Visibility = V; }
99
100   DLLStorageClassTypes getDLLStorageClass() const {
101     return DLLStorageClassTypes(DllStorageClass);
102   }
103   bool hasDLLImportStorageClass() const {
104     return DllStorageClass == DLLImportStorageClass;
105   }
106   bool hasDLLExportStorageClass() const {
107     return DllStorageClass == DLLExportStorageClass;
108   }
109   void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
110
111   bool hasSection() const { return !Section.empty(); }
112   const std::string &getSection() const { return Section; }
113   void setSection(StringRef S) {
114     assert((getValueID() != Value::GlobalAliasVal || S.empty()) &&
115            "GlobalAlias should not have a section!");
116     Section = S;
117   }
118
119   /// If the usage is empty (except transitively dead constants), then this
120   /// global value can be safely deleted since the destructor will
121   /// delete the dead constants as well.
122   /// @brief Determine if the usage of this global value is empty except
123   /// for transitively dead constants.
124   bool use_empty_except_constants();
125
126   /// getType - Global values are always pointers.
127   inline PointerType *getType() const {
128     return cast<PointerType>(User::getType());
129   }
130
131   static LinkageTypes getLinkOnceLinkage(bool ODR) {
132     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
133   }
134   static LinkageTypes getWeakLinkage(bool ODR) {
135     return ODR ? WeakODRLinkage : WeakAnyLinkage;
136   }
137
138   static bool isExternalLinkage(LinkageTypes Linkage) {
139     return Linkage == ExternalLinkage;
140   }
141   static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
142     return Linkage == AvailableExternallyLinkage;
143   }
144   static bool isLinkOnceLinkage(LinkageTypes Linkage) {
145     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
146   }
147   static bool isWeakLinkage(LinkageTypes Linkage) {
148     return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
149   }
150   static bool isAppendingLinkage(LinkageTypes Linkage) {
151     return Linkage == AppendingLinkage;
152   }
153   static bool isInternalLinkage(LinkageTypes Linkage) {
154     return Linkage == InternalLinkage;
155   }
156   static bool isPrivateLinkage(LinkageTypes Linkage) {
157     return Linkage == PrivateLinkage;
158   }
159   static bool isLocalLinkage(LinkageTypes Linkage) {
160     return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
161   }
162   static bool isExternalWeakLinkage(LinkageTypes Linkage) {
163     return Linkage == ExternalWeakLinkage;
164   }
165   static bool isCommonLinkage(LinkageTypes Linkage) {
166     return Linkage == CommonLinkage;
167   }
168
169   /// isDiscardableIfUnused - Whether the definition of this global may be
170   /// discarded if it is not used in its compilation unit.
171   static bool isDiscardableIfUnused(LinkageTypes Linkage) {
172     return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
173   }
174
175   /// mayBeOverridden - Whether the definition of this global may be replaced
176   /// by something non-equivalent at link time.  For example, if a function has
177   /// weak linkage then the code defining it may be replaced by different code.
178   static bool mayBeOverridden(LinkageTypes Linkage) {
179     return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
180            Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
181   }
182
183   /// isWeakForLinker - Whether the definition of this global may be replaced at
184   /// link time.  NB: Using this method outside of the code generators is almost
185   /// always a mistake: when working at the IR level use mayBeOverridden instead
186   /// as it knows about ODR semantics.
187   static bool isWeakForLinker(LinkageTypes Linkage)  {
188     return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
189            Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
190            Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
191            Linkage == ExternalWeakLinkage;
192   }
193
194   bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
195   bool hasAvailableExternallyLinkage() const {
196     return isAvailableExternallyLinkage(Linkage);
197   }
198   bool hasLinkOnceLinkage() const {
199     return isLinkOnceLinkage(Linkage);
200   }
201   bool hasWeakLinkage() const {
202     return isWeakLinkage(Linkage);
203   }
204   bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
205   bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
206   bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
207   bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
208   bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
209   bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
210
211   void setLinkage(LinkageTypes LT) { Linkage = LT; }
212   LinkageTypes getLinkage() const { return Linkage; }
213
214   bool isDiscardableIfUnused() const {
215     return isDiscardableIfUnused(Linkage);
216   }
217
218   bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
219
220   bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
221
222   /// copyAttributesFrom - copy all additional attributes (those not needed to
223   /// create a GlobalValue) from the GlobalValue Src to this one.
224   virtual void copyAttributesFrom(const GlobalValue *Src);
225
226   /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
227   /// printer to not emit usual symbol prefix before the symbol name is used
228   /// then return linkage name after skipping this special LLVM prefix.
229   static StringRef getRealLinkageName(StringRef Name) {
230     if (!Name.empty() && Name[0] == '\1')
231       return Name.substr(1);
232     return Name;
233   }
234
235 /// @name Materialization
236 /// Materialization is used to construct functions only as they're needed. This
237 /// is useful to reduce memory usage in LLVM or parsing work done by the
238 /// BitcodeReader to load the Module.
239 /// @{
240
241   /// isMaterializable - If this function's Module is being lazily streamed in
242   /// functions from disk or some other source, this method can be used to check
243   /// to see if the function has been read in yet or not.
244   bool isMaterializable() const;
245
246   /// isDematerializable - Returns true if this function was loaded from a
247   /// GVMaterializer that's still attached to its Module and that knows how to
248   /// dematerialize the function.
249   bool isDematerializable() const;
250
251   /// Materialize - make sure this GlobalValue is fully read.  If the module is
252   /// corrupt, this returns true and fills in the optional string with
253   /// information about the problem.  If successful, this returns false.
254   bool Materialize(std::string *ErrInfo = nullptr);
255
256   /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
257   /// supports it, release the memory for the function, and set it up to be
258   /// materialized lazily.  If !isDematerializable(), this method is a noop.
259   void Dematerialize();
260
261 /// @}
262
263   /// Override from Constant class.
264   void destroyConstant() override;
265
266   /// isDeclaration - Return true if the primary definition of this global 
267   /// value is outside of the current translation unit.
268   bool isDeclaration() const;
269
270   /// removeFromParent - This method unlinks 'this' from the containing module,
271   /// but does not delete it.
272   virtual void removeFromParent() = 0;
273
274   /// eraseFromParent - This method unlinks 'this' from the containing module
275   /// and deletes it.
276   virtual void eraseFromParent() = 0;
277
278   /// getParent - Get the module that this global value is contained inside
279   /// of...
280   inline Module *getParent() { return Parent; }
281   inline const Module *getParent() const { return Parent; }
282
283   const DataLayout *getDataLayout() const;
284
285   // Methods for support type inquiry through isa, cast, and dyn_cast:
286   static inline bool classof(const Value *V) {
287     return V->getValueID() == Value::FunctionVal ||
288            V->getValueID() == Value::GlobalVariableVal ||
289            V->getValueID() == Value::GlobalAliasVal;
290   }
291 };
292
293 } // End llvm namespace
294
295 #endif