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