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