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