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