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