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