OSDN Git Service

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